refactor(web): rewrite useAdminAppAuthClient with react-query (#1690)
This commit is contained in:
@@ -1,47 +1,58 @@
|
|||||||
import React, { useCallback, useMemo } from 'react';
|
import React, { useCallback, useMemo } from 'react';
|
||||||
import { useParams } from 'react-router-dom';
|
import { useParams } from 'react-router-dom';
|
||||||
import { useMutation } from '@apollo/client';
|
import { useMutation } from '@apollo/client';
|
||||||
|
|
||||||
import { UPDATE_APP_AUTH_CLIENT } from 'graphql/mutations/update-app-auth-client';
|
import { UPDATE_APP_AUTH_CLIENT } from 'graphql/mutations/update-app-auth-client';
|
||||||
import useAppAuthClient from 'hooks/useAppAuthClient.ee';
|
|
||||||
import useFormatMessage from 'hooks/useFormatMessage';
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
import AdminApplicationAuthClientDialog from 'components/AdminApplicationAuthClientDialog';
|
import AdminApplicationAuthClientDialog from 'components/AdminApplicationAuthClientDialog';
|
||||||
|
import useAdminAppAuthClient from 'hooks/useAdminAppAuthClient.ee';
|
||||||
|
|
||||||
export default function AdminApplicationUpdateAuthClient(props) {
|
export default function AdminApplicationUpdateAuthClient(props) {
|
||||||
const { application, onClose } = props;
|
const { application, onClose } = props;
|
||||||
const { auth } = application;
|
const { auth } = application;
|
||||||
const authFields = auth?.fields?.map((field) => ({
|
|
||||||
...field,
|
|
||||||
required: false,
|
|
||||||
}));
|
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const { clientId } = useParams();
|
const { clientId } = useParams();
|
||||||
const { appAuthClient, loading: loadingAuthClient } =
|
|
||||||
useAppAuthClient(clientId);
|
const { data: adminAppAuthClient, isLoading: isAdminAuthClientLoading } =
|
||||||
|
useAdminAppAuthClient(clientId);
|
||||||
|
|
||||||
const [updateAppAuthClient, { loading: loadingUpdateAppAuthClient, error }] =
|
const [updateAppAuthClient, { loading: loadingUpdateAppAuthClient, error }] =
|
||||||
useMutation(UPDATE_APP_AUTH_CLIENT, {
|
useMutation(UPDATE_APP_AUTH_CLIENT, {
|
||||||
refetchQueries: ['GetAppAuthClients'],
|
refetchQueries: ['GetAppAuthClients'],
|
||||||
context: { autoSnackbar: false },
|
context: { autoSnackbar: false },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const authFields = auth?.fields?.map((field) => ({
|
||||||
|
...field,
|
||||||
|
required: false,
|
||||||
|
}));
|
||||||
|
|
||||||
const submitHandler = async (values) => {
|
const submitHandler = async (values) => {
|
||||||
if (!appAuthClient) {
|
if (!adminAppAuthClient) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { name, active, ...formattedAuthDefaults } = values;
|
const { name, active, ...formattedAuthDefaults } = values;
|
||||||
|
|
||||||
await updateAppAuthClient({
|
await updateAppAuthClient({
|
||||||
variables: {
|
variables: {
|
||||||
input: {
|
input: {
|
||||||
id: appAuthClient.id,
|
id: adminAppAuthClient.data.id,
|
||||||
name,
|
name,
|
||||||
active,
|
active,
|
||||||
formattedAuthDefaults,
|
formattedAuthDefaults,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
onClose();
|
onClose();
|
||||||
};
|
};
|
||||||
|
|
||||||
const getAuthFieldsDefaultValues = useCallback(() => {
|
const getAuthFieldsDefaultValues = useCallback(() => {
|
||||||
if (!authFields) {
|
if (!authFields) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultValues = {};
|
const defaultValues = {};
|
||||||
authFields.forEach((field) => {
|
authFields.forEach((field) => {
|
||||||
if (field.value || field.type !== 'string') {
|
if (field.value || field.type !== 'string') {
|
||||||
@@ -52,25 +63,27 @@ export default function AdminApplicationUpdateAuthClient(props) {
|
|||||||
});
|
});
|
||||||
return defaultValues;
|
return defaultValues;
|
||||||
}, [auth?.fields]);
|
}, [auth?.fields]);
|
||||||
|
|
||||||
const defaultValues = useMemo(
|
const defaultValues = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
name: appAuthClient?.name || '',
|
name: adminAppAuthClient?.data?.name || '',
|
||||||
active: appAuthClient?.active || false,
|
active: adminAppAuthClient?.data?.active || false,
|
||||||
...getAuthFieldsDefaultValues(),
|
...getAuthFieldsDefaultValues(),
|
||||||
}),
|
}),
|
||||||
[appAuthClient, getAuthFieldsDefaultValues],
|
[adminAppAuthClient, getAuthFieldsDefaultValues],
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AdminApplicationAuthClientDialog
|
<AdminApplicationAuthClientDialog
|
||||||
onClose={onClose}
|
onClose={onClose}
|
||||||
error={error}
|
error={error}
|
||||||
title={formatMessage('updateAuthClient.title')}
|
title={formatMessage('updateAuthClient.title')}
|
||||||
loading={loadingAuthClient}
|
loading={isAdminAuthClientLoading}
|
||||||
submitHandler={submitHandler}
|
submitHandler={submitHandler}
|
||||||
authFields={authFields}
|
authFields={authFields}
|
||||||
submitting={loadingUpdateAppAuthClient}
|
submitting={loadingUpdateAppAuthClient}
|
||||||
defaultValues={defaultValues}
|
defaultValues={defaultValues}
|
||||||
disabled={!appAuthClient}
|
disabled={!adminAppAuthClient}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
17
packages/web/src/hooks/useAdminAppAuthClient.ee.js
Normal file
17
packages/web/src/hooks/useAdminAppAuthClient.ee.js
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
|
||||||
|
import api from 'helpers/api';
|
||||||
|
|
||||||
|
export default function useAdminAppAuthClient(id) {
|
||||||
|
const query = useQuery({
|
||||||
|
queryKey: ['adminAppAuthClient', id],
|
||||||
|
queryFn: async ({ payload, signal }) => {
|
||||||
|
const { data } = await api.get(`/v1/admin/app-auth-clients/${id}`);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
enabled: !!id,
|
||||||
|
});
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
Reference in New Issue
Block a user