Skip to content

Commit

Permalink
refactor: toast hide and show
Browse files Browse the repository at this point in the history
  • Loading branch information
RRanath committed Jun 10, 2024
1 parent b6a4e72 commit ddd4c92
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
3 changes: 0 additions & 3 deletions app/components/Analyst/RFI/RFIAnalystUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ const RfiAnalystUpload = ({ query }) => {
) {
showToast(getToastMessage(), 'success', 100000000);
}
router.push(
`/analyst/application/${router.query.applicationId}/rfi`
);
},
});
}
Expand Down
13 changes: 8 additions & 5 deletions app/components/AppProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ToastContextType>({});
const AppContext = createContext<AppContextType>({});

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;
Expand Down Expand Up @@ -48,13 +51,13 @@ export const AppProvider = ({ children }) => {
);

return (
<ToastContext.Provider value={contextValue}>
<AppContext.Provider value={contextValue}>
{children}
{toast?.visible && (
<Toast type={toast?.type} onClose={hideToast} timeout={toast.timeout}>
{toast.message}
</Toast>
)}
</ToastContext.Provider>
</AppContext.Provider>
);
};
50 changes: 25 additions & 25 deletions app/lib/theme/widgets/FileWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,23 +82,24 @@ const FileWidget: React.FC<FileWidgetProps> = ({
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;
}
});
}
}
}
Expand Down Expand Up @@ -158,15 +160,17 @@ const FileWidget: React.FC<FileWidgetProps> = ({
});
};

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<HTMLInputElement>) => {
Expand Down Expand Up @@ -212,18 +216,14 @@ const FileWidget: React.FC<FileWidgetProps> = ({

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));
}
}

Expand Down

0 comments on commit ddd4c92

Please sign in to comment.