mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-10 23:45:48 +01:00
✨ Add check for deletion of default config
This commit is contained in:
@@ -43,6 +43,10 @@
|
|||||||
"deleteFailed": {
|
"deleteFailed": {
|
||||||
"title": "Config delete failed",
|
"title": "Config delete failed",
|
||||||
"message": "Config delete failed"
|
"message": "Config delete failed"
|
||||||
|
},
|
||||||
|
"deleteFailedDefaultConfig": {
|
||||||
|
"title": "Default config can't be deleted",
|
||||||
|
"message": "Configuration was not deleted from the file system"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import { ActionIcon, Center, createStyles, Flex, Text, useMantineTheme } from '@mantine/core';
|
import { ActionIcon, Center, createStyles, Flex, Text, useMantineTheme } from '@mantine/core';
|
||||||
import { useDisclosure } from '@mantine/hooks';
|
import { useDisclosure } from '@mantine/hooks';
|
||||||
import { IconCopy, IconDownload, IconTrash } from '@tabler/icons';
|
import { showNotification } from '@mantine/notifications';
|
||||||
|
import { IconCheck, IconCopy, IconDownload, IconTrash } from '@tabler/icons';
|
||||||
import fileDownload from 'js-file-download';
|
import fileDownload from 'js-file-download';
|
||||||
import { useTranslation } from 'next-i18next';
|
import { useTranslation } from 'next-i18next';
|
||||||
import { useConfigContext } from '../../../../config/provider';
|
import { useConfigContext } from '../../../../config/provider';
|
||||||
|
import { useConfigStore } from '../../../../config/store';
|
||||||
import { useDeleteConfigMutation } from '../../../../tools/config/mutations/useDeleteConfigMutation';
|
import { useDeleteConfigMutation } from '../../../../tools/config/mutations/useDeleteConfigMutation';
|
||||||
import Tip from '../../../layout/Tip';
|
import Tip from '../../../layout/Tip';
|
||||||
import { CreateConfigCopyModal } from './CreateCopyModal';
|
import { CreateConfigCopyModal } from './CreateCopyModal';
|
||||||
@@ -12,6 +14,7 @@ export default function ConfigActions() {
|
|||||||
const { t } = useTranslation(['settings/general/config-changer', 'settings/common']);
|
const { t } = useTranslation(['settings/general/config-changer', 'settings/common']);
|
||||||
const [createCopyModalOpened, createCopyModal] = useDisclosure(false);
|
const [createCopyModalOpened, createCopyModal] = useDisclosure(false);
|
||||||
const { config } = useConfigContext();
|
const { config } = useConfigContext();
|
||||||
|
const { removeConfig } = useConfigStore();
|
||||||
const { mutateAsync } = useDeleteConfigMutation(config?.configProperties.name ?? 'default');
|
const { mutateAsync } = useDeleteConfigMutation(config?.configProperties.name ?? 'default');
|
||||||
|
|
||||||
if (!config) return null;
|
if (!config) return null;
|
||||||
@@ -22,7 +25,26 @@ export default function ConfigActions() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleDeletion = async () => {
|
const handleDeletion = async () => {
|
||||||
await mutateAsync();
|
const response = await mutateAsync();
|
||||||
|
|
||||||
|
if (response.message) {
|
||||||
|
showNotification({
|
||||||
|
title: t('buttons.delete.notifications.deleteFailedDefaultConfig.title'),
|
||||||
|
message: t('buttons.delete.notifications.deleteFailedDefaultConfig.message'),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
showNotification({
|
||||||
|
title: t('buttons.delete.notifications.deleted.title'),
|
||||||
|
icon: <IconCheck />,
|
||||||
|
color: 'green',
|
||||||
|
autoClose: 1500,
|
||||||
|
radius: 'md',
|
||||||
|
message: t('buttons.delete.notifications.deleted.message'),
|
||||||
|
});
|
||||||
|
|
||||||
|
removeConfig(config?.configProperties.name ?? 'default');
|
||||||
};
|
};
|
||||||
|
|
||||||
const { classes } = useStyles();
|
const { classes } = useStyles();
|
||||||
|
|||||||
@@ -27,6 +27,12 @@ export const useConfigStore = create<UseConfigStoreType>((set, get) => ({
|
|||||||
}
|
}
|
||||||
axios.put(`/api/configs/${name}`, { ...config });
|
axios.put(`/api/configs/${name}`, { ...config });
|
||||||
},
|
},
|
||||||
|
removeConfig: (name: string) => {
|
||||||
|
set((old) => ({
|
||||||
|
...old,
|
||||||
|
configs: old.configs.filter((x) => x.value.configProperties.name !== name),
|
||||||
|
}));
|
||||||
|
},
|
||||||
updateConfig: async (
|
updateConfig: async (
|
||||||
name,
|
name,
|
||||||
updateCallback: (previous: ConfigType) => ConfigType,
|
updateCallback: (previous: ConfigType) => ConfigType,
|
||||||
@@ -72,6 +78,7 @@ interface UseConfigStoreType {
|
|||||||
config: ConfigType,
|
config: ConfigType,
|
||||||
shouldSaveConfigToFileSystem: boolean
|
shouldSaveConfigToFileSystem: boolean
|
||||||
) => Promise<void>;
|
) => Promise<void>;
|
||||||
|
removeConfig: (name: string) => void;
|
||||||
updateConfig: (
|
updateConfig: (
|
||||||
name: string,
|
name: string,
|
||||||
updateCallback: (previous: ConfigType) => ConfigType,
|
updateCallback: (previous: ConfigType) => ConfigType,
|
||||||
|
|||||||
@@ -136,6 +136,13 @@ function Delete(req: NextApiRequest, res: NextApiResponse<any>) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (slug.toLowerCase() === 'default') {
|
||||||
|
Consola.error('Rejected config deletion because default configuration can\'t be deleted');
|
||||||
|
return res.status(403).json({
|
||||||
|
message: 'Default config can\'t be deleted',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Loop over all the files in the /data/configs directory
|
// Loop over all the files in the /data/configs directory
|
||||||
// Get all the configs in the /data/configs folder
|
// Get all the configs in the /data/configs folder
|
||||||
// All the files that end in ".json"
|
// All the files that end in ".json"
|
||||||
|
|||||||
@@ -9,17 +9,6 @@ export const useDeleteConfigMutation = (configName: string) => {
|
|||||||
return useMutation({
|
return useMutation({
|
||||||
mutationKey: ['configs/delete', { configName }],
|
mutationKey: ['configs/delete', { configName }],
|
||||||
mutationFn: () => fetchDeletion(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() {
|
onError() {
|
||||||
showNotification({
|
showNotification({
|
||||||
title: t('buttons.delete.notifications.deleteFailed.title'),
|
title: t('buttons.delete.notifications.deleteFailed.title'),
|
||||||
|
|||||||
Reference in New Issue
Block a user