refactor: rewrite useAppConfig with RQ
This commit is contained in:
@@ -7,6 +7,7 @@ import Paper from '@mui/material/Paper';
|
||||
import Stack from '@mui/material/Stack';
|
||||
import LoadingButton from '@mui/lab/LoadingButton';
|
||||
import { useMutation } from '@apollo/client';
|
||||
|
||||
import { CREATE_APP_CONFIG } from 'graphql/mutations/create-app-config';
|
||||
import { UPDATE_APP_CONFIG } from 'graphql/mutations/update-app-config';
|
||||
import Form from 'components/Form';
|
||||
@@ -14,24 +15,28 @@ import { Switch } from './style';
|
||||
import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar';
|
||||
|
||||
function AdminApplicationSettings(props) {
|
||||
const { appConfig, loading } = useAppConfig(props.appKey);
|
||||
const formatMessage = useFormatMessage();
|
||||
const enqueueSnackbar = useEnqueueSnackbar();
|
||||
|
||||
const { data: appConfig, isLoading: loading } = useAppConfig(props.appKey);
|
||||
|
||||
const [createAppConfig, { loading: loadingCreateAppConfig }] = useMutation(
|
||||
CREATE_APP_CONFIG,
|
||||
{
|
||||
refetchQueries: ['GetAppConfig'],
|
||||
},
|
||||
);
|
||||
|
||||
const [updateAppConfig, { loading: loadingUpdateAppConfig }] = useMutation(
|
||||
UPDATE_APP_CONFIG,
|
||||
{
|
||||
refetchQueries: ['GetAppConfig'],
|
||||
},
|
||||
);
|
||||
const formatMessage = useFormatMessage();
|
||||
const enqueueSnackbar = useEnqueueSnackbar();
|
||||
|
||||
const handleSubmit = async (values) => {
|
||||
try {
|
||||
if (!appConfig) {
|
||||
if (!appConfig.data) {
|
||||
await createAppConfig({
|
||||
variables: {
|
||||
input: { key: props.appKey, ...values },
|
||||
@@ -40,10 +45,11 @@ function AdminApplicationSettings(props) {
|
||||
} else {
|
||||
await updateAppConfig({
|
||||
variables: {
|
||||
input: { id: appConfig.id, ...values },
|
||||
input: { id: appConfig.data.id, ...values },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
enqueueSnackbar(formatMessage('adminAppsSettings.successfullySaved'), {
|
||||
variant: 'success',
|
||||
SnackbarProps: {
|
||||
@@ -54,13 +60,14 @@ function AdminApplicationSettings(props) {
|
||||
throw new Error('Failed while saving!');
|
||||
}
|
||||
};
|
||||
|
||||
const defaultValues = useMemo(
|
||||
() => ({
|
||||
allowCustomConnection: appConfig?.allowCustomConnection || false,
|
||||
shared: appConfig?.shared || false,
|
||||
disabled: appConfig?.disabled || false,
|
||||
allowCustomConnection: appConfig?.data?.allowCustomConnection || false,
|
||||
shared: appConfig?.data?.shared || false,
|
||||
disabled: appConfig?.data?.disabled || false,
|
||||
}),
|
||||
[appConfig],
|
||||
[appConfig?.data],
|
||||
);
|
||||
return (
|
||||
<Form
|
||||
|
Reference in New Issue
Block a user