From bfc7d5d0dd9101668849b4e4f034c71e565f7e9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1dvan=20Akca?= <43352493+ridvanakca@users.noreply.github.com> Date: Mon, 4 Mar 2024 15:09:42 +0300 Subject: [PATCH] refactor(web): rewrite useAdminAppAuthClient with react-query (#1690) --- .../index.jsx | 41 ++++++++++++------- .../web/src/hooks/useAdminAppAuthClient.ee.js | 17 ++++++++ 2 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 packages/web/src/hooks/useAdminAppAuthClient.ee.js diff --git a/packages/web/src/components/AdminApplicationUpdateAuthClient/index.jsx b/packages/web/src/components/AdminApplicationUpdateAuthClient/index.jsx index f35f52c5..f837a709 100644 --- a/packages/web/src/components/AdminApplicationUpdateAuthClient/index.jsx +++ b/packages/web/src/components/AdminApplicationUpdateAuthClient/index.jsx @@ -1,47 +1,58 @@ import React, { useCallback, useMemo } from 'react'; import { useParams } from 'react-router-dom'; import { useMutation } from '@apollo/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 AdminApplicationAuthClientDialog from 'components/AdminApplicationAuthClientDialog'; +import useAdminAppAuthClient from 'hooks/useAdminAppAuthClient.ee'; + export default function AdminApplicationUpdateAuthClient(props) { const { application, onClose } = props; const { auth } = application; - const authFields = auth?.fields?.map((field) => ({ - ...field, - required: false, - })); const formatMessage = useFormatMessage(); const { clientId } = useParams(); - const { appAuthClient, loading: loadingAuthClient } = - useAppAuthClient(clientId); + + const { data: adminAppAuthClient, isLoading: isAdminAuthClientLoading } = + useAdminAppAuthClient(clientId); + const [updateAppAuthClient, { loading: loadingUpdateAppAuthClient, error }] = useMutation(UPDATE_APP_AUTH_CLIENT, { refetchQueries: ['GetAppAuthClients'], context: { autoSnackbar: false }, }); + + const authFields = auth?.fields?.map((field) => ({ + ...field, + required: false, + })); + const submitHandler = async (values) => { - if (!appAuthClient) { + if (!adminAppAuthClient) { return; } + const { name, active, ...formattedAuthDefaults } = values; + await updateAppAuthClient({ variables: { input: { - id: appAuthClient.id, + id: adminAppAuthClient.data.id, name, active, formattedAuthDefaults, }, }, }); + onClose(); }; + const getAuthFieldsDefaultValues = useCallback(() => { if (!authFields) { return {}; } + const defaultValues = {}; authFields.forEach((field) => { if (field.value || field.type !== 'string') { @@ -52,25 +63,27 @@ export default function AdminApplicationUpdateAuthClient(props) { }); return defaultValues; }, [auth?.fields]); + const defaultValues = useMemo( () => ({ - name: appAuthClient?.name || '', - active: appAuthClient?.active || false, + name: adminAppAuthClient?.data?.name || '', + active: adminAppAuthClient?.data?.active || false, ...getAuthFieldsDefaultValues(), }), - [appAuthClient, getAuthFieldsDefaultValues], + [adminAppAuthClient, getAuthFieldsDefaultValues], ); + return ( ); } diff --git a/packages/web/src/hooks/useAdminAppAuthClient.ee.js b/packages/web/src/hooks/useAdminAppAuthClient.ee.js new file mode 100644 index 00000000..29b23e75 --- /dev/null +++ b/packages/web/src/hooks/useAdminAppAuthClient.ee.js @@ -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; +}