diff --git a/app/components/Analyst/RFI/RFIAnalystUpload.tsx b/app/components/Analyst/RFI/RFIAnalystUpload.tsx index de29413583..832a689593 100644 --- a/app/components/Analyst/RFI/RFIAnalystUpload.tsx +++ b/app/components/Analyst/RFI/RFIAnalystUpload.tsx @@ -163,9 +163,6 @@ const RfiAnalystUpload = ({ query }) => { ) { showToast(getToastMessage(), 'success', 100000000); } - router.push( - `/analyst/application/${router.query.applicationId}/rfi` - ); }, }); } diff --git a/app/components/AppProvider.tsx b/app/components/AppProvider.tsx index bc03c7dd53..91ccd6d17b 100644 --- a/app/components/AppProvider.tsx +++ b/app/components/AppProvider.tsx @@ -8,18 +8,21 @@ import React, { } from 'react'; import Toast, { ToastType } from 'components/Toast'; -type ToastContextType = { +type AppContextType = { showToast?: (message: ReactNode, type?: ToastType, timeout?: number) => void; hideToast?: () => void; }; -const ToastContext = createContext({}); +const AppContext = createContext({}); export const useToast = () => { - return useContext(ToastContext); + return useContext(AppContext); }; export const AppProvider = ({ children }) => { + /** + * handling global toast messages + */ const [toast, setToast] = useState<{ visible: boolean; message?: ReactNode; @@ -48,13 +51,13 @@ export const AppProvider = ({ children }) => { ); return ( - + {children} {toast?.visible && ( {toast.message} )} - + ); }; diff --git a/app/lib/theme/widgets/FileWidget.tsx b/app/lib/theme/widgets/FileWidget.tsx index 342018abf8..9dd1a5bcb7 100644 --- a/app/lib/theme/widgets/FileWidget.tsx +++ b/app/lib/theme/widgets/FileWidget.tsx @@ -14,6 +14,7 @@ import FileComponent from 'lib/theme/components/FileComponent'; import useDisposeOnRouteChange from 'lib/helpers/useDisposeOnRouteChange'; import { DateTime } from 'luxon'; import { useToast } from 'components/AppProvider'; +import { ToastType } from 'components/Toast'; type File = { id: string | number; @@ -81,23 +82,24 @@ const FileWidget: React.FC = ({ if (file) { fileFormData.append('file', file); if (setTemplateData) { - const response = await fetch( + await fetch( `/api/applicant/template?templateNumber=${templateNumber}`, { method: 'POST', body: fileFormData, } - ); - if (response.ok) { - response.json().then((data) => { - setTemplateData({ - templateNumber, - data, + ).then((response) => { + if (response.ok) { + response.json().then((data) => { + setTemplateData({ + templateNumber, + data, + }); }); - }); - } else { - isTemplateValid = false; - } + } else { + isTemplateValid = false; + } + }); } } } @@ -158,15 +160,17 @@ const FileWidget: React.FC = ({ }); }; - const getToastMessage = (files: any[], isSuccess: boolean = true) => { + const showToastMessage = (files, type: ToastType = 'success') => { const fields = templateNumber === 1 ? 'Total Households and Indigenous Households data' : 'Total eligible costs and Total project costs data'; - if (isSuccess) { - return `Template ${templateNumber} validation successful, new values for ${fields} data in the application will update upon 'Save'`; - } - return `Template ${templateNumber} validation failed : ${files.join(', ')} did not validate due to formatting issues. ${fields} in the application will not update.`; + const message = + type === 'success' + ? `Template ${templateNumber} validation successful, new values for ${fields} data in the application will update upon 'Save'` + : `Template ${templateNumber} validation failed: ${files.join(', ')} did not validate due to formatting issues. ${fields} in the application will not update.`; + + showToast(message, type, 100000000); }; const handleChange = async (e: React.ChangeEvent) => { @@ -212,18 +216,14 @@ const FileWidget: React.FC = ({ if (templateValidate) { if (validationErrors.length > 0) { - showToast( - getToastMessage( - validationErrors.map( - (error) => error.fileName || error.input?.attachment?.fileName - ), - false + showToastMessage( + validationErrors.map( + (error) => error.fileName || error.input?.attachment?.fileName ), - 'error', - 100000000 + 'error' ); } else if (validationErrors.length === 0 && uploadErrors.length === 0) { - showToast(getToastMessage(fileDetails), 'success', 100000000); + showToastMessage(fileDetails.map((file) => file.name)); } }