Added recommedation add/remove toasts (#18113)

fixes https://github.com/TryGhost/Product/issues/3844
This commit is contained in:
Simon Backx 2023-09-13 15:31:29 +02:00 committed by GitHub
parent cb6a8ff6df
commit 908e02c016
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 6 deletions

View File

@ -97,3 +97,7 @@ export const showToast = ({
}
);
};
export const dismissAllToasts = (): void => {
toast.dismiss();
};

View File

@ -47,7 +47,13 @@ export const ConfirmationModalContent: React.FC<ConfirmationModalProps> = ({
onCancel={onCancel}
onOk={async () => {
setTaskState('running');
await onOk?.(modal);
try {
await onOk?.(modal);
} catch (e) {
// eslint-disable-next-line no-console
console.error('Unhandled Promise Rejection. Make sure you catch errors in your onOk handler.', e);
}
setTaskState('');
}}
>

View File

@ -6,8 +6,7 @@ import RecommendationReasonForm from './RecommendationReasonForm';
import useForm from '../../../../hooks/useForm';
import useRouting from '../../../../hooks/useRouting';
import {EditOrAddRecommendation, useAddRecommendation} from '../../../../api/recommendations';
import {showToast} from '../../../../admin-x-ds/global/Toast';
import {toast} from 'react-hot-toast';
import {dismissAllToasts, showToast} from '../../../../admin-x-ds/global/Toast';
interface AddRecommendationModalProps {
recommendation: EditOrAddRecommendation,
@ -26,6 +25,10 @@ const AddRecommendationModalConfirm: React.FC<AddRecommendationModalProps> = ({r
onSave: async () => {
await addRecommendation(formState);
modal.remove();
showToast({
message: 'Successfully added a recommendation',
type: 'success'
});
updateRoute('recommendations');
},
onValidate: () => {
@ -95,7 +98,7 @@ const AddRecommendationModalConfirm: React.FC<AddRecommendationModalProps> = ({r
return;
}
toast.remove();
dismissAllToasts();
if (await handleSave({force: true})) {
// Already handled
} else {

View File

@ -10,6 +10,7 @@ import TableRow from '../../../../admin-x-ds/global/TableRow';
import useRouting from '../../../../hooks/useRouting';
import {PaginationData} from '../../../../hooks/usePagination';
import {Recommendation, useDeleteRecommendation} from '../../../../api/recommendations';
import {showToast} from '../../../../admin-x-ds/global/Toast';
interface RecommendationListProps {
recommendations: Recommendation[],
@ -31,8 +32,19 @@ const RecommendationItem: React.FC<{recommendation: Recommendation}> = ({recomme
</>,
okLabel: 'Remove',
onOk: async (modal) => {
await deleteRecommendation(recommendation);
modal?.remove();
try {
await deleteRecommendation(recommendation);
modal?.remove();
showToast({
message: 'Successfully removed the recommendation',
type: 'success'
});
} catch (_) {
showToast({
message: 'Failed to remove the recommendation. Please try again later.',
type: 'error'
});
}
}
});
}} />