refactor(web): rewrite mutation with PATCH /v1/admin/apps/:appKey/auth-clients/:appAuthClientId

This commit is contained in:
Ali BARIN
2024-08-27 16:46:10 +00:00
parent b2bda8479e
commit 09bc0bba1e
7 changed files with 40 additions and 63 deletions

View File

@@ -2,7 +2,7 @@ import appConfig from './app.js';
const corsOptions = { const corsOptions = {
origin: appConfig.webAppUrl, origin: appConfig.webAppUrl,
methods: 'GET,HEAD,POST,DELETE', methods: 'GET,HEAD,POST,PATCH,DELETE',
credentials: true, credentials: true,
optionsSuccessStatus: 200, optionsSuccessStatus: 200,
}; };

View File

@@ -12,7 +12,6 @@ import executeFlow from './mutations/execute-flow.js';
import generateAuthUrl from './mutations/generate-auth-url.js'; import generateAuthUrl from './mutations/generate-auth-url.js';
import registerUser from './mutations/register-user.ee.js'; import registerUser from './mutations/register-user.ee.js';
import resetConnection from './mutations/reset-connection.js'; import resetConnection from './mutations/reset-connection.js';
import updateAppAuthClient from './mutations/update-app-auth-client.ee.js';
import updateAppConfig from './mutations/update-app-config.ee.js'; import updateAppConfig from './mutations/update-app-config.ee.js';
import updateConfig from './mutations/update-config.ee.js'; import updateConfig from './mutations/update-config.ee.js';
import updateConnection from './mutations/update-connection.js'; import updateConnection from './mutations/update-connection.js';
@@ -45,7 +44,6 @@ const mutationResolvers = {
generateAuthUrl, generateAuthUrl,
registerUser, registerUser,
resetConnection, resetConnection,
updateAppAuthClient,
updateAppConfig, updateAppConfig,
updateConfig, updateConfig,
updateConnection, updateConnection,

View File

@@ -1,17 +0,0 @@
import AppAuthClient from '../../models/app-auth-client.js';
const updateAppAuthClient = async (_parent, params, context) => {
context.currentUser.can('update', 'App');
const { id, ...appAuthClientData } = params.input;
const appAuthClient = await AppAuthClient.query()
.findById(id)
.throwIfNotFound();
await appAuthClient.$query().patch(appAuthClientData);
return appAuthClient;
};
export default updateAppAuthClient;

View File

@@ -17,7 +17,6 @@ type Mutation {
generateAuthUrl(input: GenerateAuthUrlInput): AuthLink generateAuthUrl(input: GenerateAuthUrlInput): AuthLink
registerUser(input: RegisterUserInput): User registerUser(input: RegisterUserInput): User
resetConnection(input: ResetConnectionInput): Connection resetConnection(input: ResetConnectionInput): Connection
updateAppAuthClient(input: UpdateAppAuthClientInput): AppAuthClient
updateAppConfig(input: UpdateAppConfigInput): AppConfig updateAppConfig(input: UpdateAppConfigInput): AppConfig
updateConfig(input: JSONObject): JSONObject updateConfig(input: JSONObject): JSONObject
updateConnection(input: UpdateConnectionInput): Connection updateConnection(input: UpdateConnectionInput): Connection
@@ -555,20 +554,6 @@ input UpdateAppConfigInput {
disabled: Boolean disabled: Boolean
} }
type AppAuthClient {
id: String
appConfigId: String
name: String
active: Boolean
}
input UpdateAppAuthClientInput {
id: String
name: String
formattedAuthDefaults: JSONObject
active: Boolean
}
schema { schema {
query: Query query: Query
mutation: Mutation mutation: Mutation

View File

@@ -1,13 +1,12 @@
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
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 { AppPropType } from 'propTypes/propTypes'; import { AppPropType } from 'propTypes/propTypes';
import { UPDATE_APP_AUTH_CLIENT } from 'graphql/mutations/update-app-auth-client';
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'; import useAdminAppAuthClient from 'hooks/useAdminAppAuthClient.ee';
import useAdminUpdateAppAuthClient from 'hooks/useAdminUpdateAppAuthClient.ee';
import useAppAuth from 'hooks/useAppAuth'; import useAppAuth from 'hooks/useAppAuth';
function AdminApplicationUpdateAuthClient(props) { function AdminApplicationUpdateAuthClient(props) {
@@ -20,11 +19,11 @@ function AdminApplicationUpdateAuthClient(props) {
const { data: auth } = useAppAuth(application.key); const { data: auth } = useAppAuth(application.key);
const [updateAppAuthClient, { loading: loadingUpdateAppAuthClient, error }] = const {
useMutation(UPDATE_APP_AUTH_CLIENT, { mutateAsync: updateAppAuthClient,
refetchQueries: ['GetAppAuthClients'], isPending: isUpdateAppAuthClientPending,
context: { autoSnackbar: false }, error: updateAppAuthClientError,
}); } = useAdminUpdateAppAuthClient(application.key, clientId);
const authFields = auth?.data?.fields?.map((field) => ({ const authFields = auth?.data?.fields?.map((field) => ({
...field, ...field,
@@ -39,14 +38,9 @@ function AdminApplicationUpdateAuthClient(props) {
const { name, active, ...formattedAuthDefaults } = values; const { name, active, ...formattedAuthDefaults } = values;
await updateAppAuthClient({ await updateAppAuthClient({
variables: { name,
input: { active,
id: adminAppAuthClient.data.id, formattedAuthDefaults,
name,
active,
formattedAuthDefaults,
},
},
}); });
onClose(); onClose();
@@ -80,12 +74,12 @@ function AdminApplicationUpdateAuthClient(props) {
return ( return (
<AdminApplicationAuthClientDialog <AdminApplicationAuthClientDialog
onClose={onClose} onClose={onClose}
error={error} error={updateAppAuthClientError}
title={formatMessage('updateAuthClient.title')} title={formatMessage('updateAuthClient.title')}
loading={isAdminAuthClientLoading} loading={isAdminAuthClientLoading}
submitHandler={submitHandler} submitHandler={submitHandler}
authFields={authFields} authFields={authFields}
submitting={loadingUpdateAppAuthClient} submitting={isUpdateAppAuthClientPending}
defaultValues={defaultValues} defaultValues={defaultValues}
disabled={!adminAppAuthClient} disabled={!adminAppAuthClient}
/> />

View File

@@ -1,11 +0,0 @@
import { gql } from '@apollo/client';
export const UPDATE_APP_AUTH_CLIENT = gql`
mutation UpdateAppAuthClient($input: UpdateAppAuthClientInput) {
updateAppAuthClient(input: $input) {
id
appConfigId
name
active
}
}
`;

View File

@@ -0,0 +1,28 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import api from 'helpers/api';
export default function useAdminUpdateAppAuthClient(appKey, id) {
const queryClient = useQueryClient();
const query = useMutation({
mutationFn: async (payload) => {
const { data } = await api.patch(
`/v1/admin/apps/${appKey}/auth-clients/${id}`,
payload,
);
return data;
},
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ['admin', 'apps', appKey, 'authClients', id],
});
queryClient.invalidateQueries({
queryKey: ['admin', 'apps', appKey, 'authClients'],
});
},
});
return query;
}