🐛 Fix config action mutations

This commit is contained in:
Manuel Ruwe
2022-12-30 16:17:22 +01:00
parent 0964e10b43
commit 0565d444d2
7 changed files with 116 additions and 56 deletions

View File

@@ -0,0 +1,37 @@
import { showNotification } from '@mantine/notifications';
import { IconCheck, IconX } from '@tabler/icons';
import { useMutation } from '@tanstack/react-query';
import { useTranslation } from 'next-i18next';
export const useDeleteConfigMutation = (configName: string) => {
const { t } = useTranslation(['settings/general/config-changer']);
return useMutation({
mutationKey: ['configs/delete', { configName }],
mutationFn: () => fetchDeletion(configName),
onSuccess() {
showNotification({
title: t('buttons.delete.notifications.deleted.title'),
icon: <IconCheck />,
color: 'green',
autoClose: 1500,
radius: 'md',
message: t('buttons.delete.notifications.deleted.message'),
});
// TODO: set config to default config and use fallback config if necessary
},
onError() {
showNotification({
title: t('buttons.delete.notifications.deleteFailed.title'),
icon: <IconX />,
color: 'red',
autoClose: 1500,
radius: 'md',
message: t('buttons.delete.notifications.deleteFailed.message'),
});
},
});
};
const fetchDeletion = async (configName: string) =>
(await fetch(`/api/configs/${configName}`, { method: 'DELETE' })).json();