refactor(web): rewrite mutation with POST /v1/admin/apps/:appKey/config
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import PropTypes from 'prop-types';
|
||||
import React, { useCallback, useMemo } from 'react';
|
||||
import { useMutation } from '@apollo/client';
|
||||
|
||||
import { AppPropType } from 'propTypes/propTypes';
|
||||
import { CREATE_APP_CONFIG } from 'graphql/mutations/create-app-config';
|
||||
import useAdminCreateAppConfig from 'hooks/useAdminCreateAppConfig';
|
||||
import useAppConfig from 'hooks/useAppConfig.ee';
|
||||
import useFormatMessage from 'hooks/useFormatMessage';
|
||||
import useAdminCreateAppAuthClient from 'hooks/useAdminCreateAppAuthClient.ee';
|
||||
@@ -18,13 +17,11 @@ function AdminApplicationCreateAuthClient(props) {
|
||||
const { data: appConfig, isLoading: isAppConfigLoading } =
|
||||
useAppConfig(appKey);
|
||||
|
||||
const [
|
||||
createAppConfig,
|
||||
{ loading: loadingCreateAppConfig, error: createAppConfigError },
|
||||
] = useMutation(CREATE_APP_CONFIG, {
|
||||
refetchQueries: ['GetAppConfig'],
|
||||
context: { autoSnackbar: false },
|
||||
});
|
||||
const {
|
||||
mutateAsync: createAppConfig,
|
||||
isPending: isCreateAppConfigPending,
|
||||
error: createAppConfigError
|
||||
} = useAdminCreateAppConfig(props.appKey);
|
||||
|
||||
const {
|
||||
mutateAsync: createAppAuthClient,
|
||||
@@ -37,17 +34,12 @@ function AdminApplicationCreateAuthClient(props) {
|
||||
|
||||
if (!appConfigId) {
|
||||
const { data: appConfigData } = await createAppConfig({
|
||||
variables: {
|
||||
input: {
|
||||
key: appKey,
|
||||
allowCustomConnection: false,
|
||||
shared: false,
|
||||
disabled: false,
|
||||
},
|
||||
},
|
||||
allowCustomConnection: true,
|
||||
shared: false,
|
||||
disabled: false,
|
||||
});
|
||||
|
||||
appConfigId = appConfigData.createAppConfig.id;
|
||||
appConfigId = appConfigData.id;
|
||||
}
|
||||
|
||||
const { name, active, ...formattedAuthDefaults } = values;
|
||||
@@ -97,7 +89,7 @@ function AdminApplicationCreateAuthClient(props) {
|
||||
loading={isAppConfigLoading}
|
||||
submitHandler={submitHandler}
|
||||
authFields={auth?.data?.fields}
|
||||
submitting={loadingCreateAppConfig || isCreateAppAuthClientPending}
|
||||
submitting={isCreateAppConfigPending || isCreateAppAuthClientPending}
|
||||
defaultValues={defaultValues}
|
||||
/>
|
||||
);
|
||||
|
@@ -8,11 +8,11 @@ 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';
|
||||
import { Switch } from './style';
|
||||
import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar';
|
||||
import useAdminCreateAppConfig from 'hooks/useAdminCreateAppConfig';
|
||||
|
||||
function AdminApplicationSettings(props) {
|
||||
const formatMessage = useFormatMessage();
|
||||
@@ -20,12 +20,10 @@ function AdminApplicationSettings(props) {
|
||||
|
||||
const { data: appConfig, isLoading: loading } = useAppConfig(props.appKey);
|
||||
|
||||
const [createAppConfig, { loading: loadingCreateAppConfig }] = useMutation(
|
||||
CREATE_APP_CONFIG,
|
||||
{
|
||||
refetchQueries: ['GetAppConfig'],
|
||||
},
|
||||
);
|
||||
const {
|
||||
mutateAsync: createAppConfig,
|
||||
isPending: isCreateAppConfigPending,
|
||||
} = useAdminCreateAppConfig(props.appKey);
|
||||
|
||||
const [updateAppConfig, { loading: loadingUpdateAppConfig }] = useMutation(
|
||||
UPDATE_APP_CONFIG,
|
||||
@@ -37,11 +35,7 @@ function AdminApplicationSettings(props) {
|
||||
const handleSubmit = async (values) => {
|
||||
try {
|
||||
if (!appConfig?.data) {
|
||||
await createAppConfig({
|
||||
variables: {
|
||||
input: { key: props.appKey, ...values },
|
||||
},
|
||||
});
|
||||
await createAppConfig(values);
|
||||
} else {
|
||||
await updateAppConfig({
|
||||
variables: {
|
||||
@@ -108,7 +102,7 @@ function AdminApplicationSettings(props) {
|
||||
variant="contained"
|
||||
color="primary"
|
||||
sx={{ boxShadow: 2, mt: 5 }}
|
||||
loading={loadingCreateAppConfig || loadingUpdateAppConfig}
|
||||
loading={isCreateAppConfigPending || loadingUpdateAppConfig}
|
||||
disabled={!isDirty || loading}
|
||||
>
|
||||
{formatMessage('adminAppsSettings.save')}
|
||||
|
Reference in New Issue
Block a user