refactor(web): rewrite mutation with PATCH /v1/admin/apps/:appKey/config
This commit is contained in:
@@ -13,6 +13,7 @@ import Form from 'components/Form';
|
|||||||
import { Switch } from './style';
|
import { Switch } from './style';
|
||||||
import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar';
|
import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar';
|
||||||
import useAdminCreateAppConfig from 'hooks/useAdminCreateAppConfig';
|
import useAdminCreateAppConfig from 'hooks/useAdminCreateAppConfig';
|
||||||
|
import useAdminUpdateAppConfig from 'hooks/useAdminUpdateAppConfig';
|
||||||
|
|
||||||
function AdminApplicationSettings(props) {
|
function AdminApplicationSettings(props) {
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
@@ -20,28 +21,18 @@ function AdminApplicationSettings(props) {
|
|||||||
|
|
||||||
const { data: appConfig, isLoading: loading } = useAppConfig(props.appKey);
|
const { data: appConfig, isLoading: loading } = useAppConfig(props.appKey);
|
||||||
|
|
||||||
const {
|
const { mutateAsync: createAppConfig, isPending: isCreateAppConfigPending } =
|
||||||
mutateAsync: createAppConfig,
|
useAdminCreateAppConfig(props.appKey);
|
||||||
isPending: isCreateAppConfigPending,
|
|
||||||
} = useAdminCreateAppConfig(props.appKey);
|
|
||||||
|
|
||||||
const [updateAppConfig, { loading: loadingUpdateAppConfig }] = useMutation(
|
const { mutateAsync: updateAppConfig, isPending: isUpdateAppConfigPending } =
|
||||||
UPDATE_APP_CONFIG,
|
useAdminUpdateAppConfig(props.appKey);
|
||||||
{
|
|
||||||
refetchQueries: ['GetAppConfig'],
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleSubmit = async (values) => {
|
const handleSubmit = async (values) => {
|
||||||
try {
|
try {
|
||||||
if (!appConfig?.data) {
|
if (!appConfig?.data) {
|
||||||
await createAppConfig(values);
|
await createAppConfig(values);
|
||||||
} else {
|
} else {
|
||||||
await updateAppConfig({
|
await updateAppConfig(values);
|
||||||
variables: {
|
|
||||||
input: { id: appConfig.data.id, ...values },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enqueueSnackbar(formatMessage('adminAppsSettings.successfullySaved'), {
|
enqueueSnackbar(formatMessage('adminAppsSettings.successfullySaved'), {
|
||||||
@@ -102,7 +93,7 @@ function AdminApplicationSettings(props) {
|
|||||||
variant="contained"
|
variant="contained"
|
||||||
color="primary"
|
color="primary"
|
||||||
sx={{ boxShadow: 2, mt: 5 }}
|
sx={{ boxShadow: 2, mt: 5 }}
|
||||||
loading={isCreateAppConfigPending || loadingUpdateAppConfig}
|
loading={isCreateAppConfigPending || isUpdateAppConfigPending}
|
||||||
disabled={!isDirty || loading}
|
disabled={!isDirty || loading}
|
||||||
>
|
>
|
||||||
{formatMessage('adminAppsSettings.save')}
|
{formatMessage('adminAppsSettings.save')}
|
||||||
|
24
packages/web/src/hooks/useAdminUpdateAppConfig.js
Normal file
24
packages/web/src/hooks/useAdminUpdateAppConfig.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||||
|
import api from 'helpers/api';
|
||||||
|
|
||||||
|
export default function useAdminUpdateAppConfig(appKey) {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
const query = useMutation({
|
||||||
|
mutationFn: async (payload) => {
|
||||||
|
const { data } = await api.patch(
|
||||||
|
`/v1/admin/apps/${appKey}/config`,
|
||||||
|
payload,
|
||||||
|
);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
onSuccess: () => {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ['apps', appKey, 'config'],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
Reference in New Issue
Block a user