From c64ca9d9b7a50748a24c6f0e8ff2e3c157c16892 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Thu, 6 Apr 2023 13:34:43 +0000 Subject: [PATCH] fix(ProfileSettings): don't submit password unless changed --- .../web/src/graphql/mutations/update-user.ts | 3 +- .../src/graphql/queries/get-current-user.ts | 2 -- .../web/src/pages/ProfileSettings/index.tsx | 28 ++++++++++++++++--- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/web/src/graphql/mutations/update-user.ts b/packages/web/src/graphql/mutations/update-user.ts index e445a4c9..74324829 100644 --- a/packages/web/src/graphql/mutations/update-user.ts +++ b/packages/web/src/graphql/mutations/update-user.ts @@ -4,9 +4,8 @@ export const UPDATE_USER = gql` mutation UpdateUser($input: UpdateUserInput) { updateUser(input: $input) { id - email fullName - updatedAt + email } } `; diff --git a/packages/web/src/graphql/queries/get-current-user.ts b/packages/web/src/graphql/queries/get-current-user.ts index 4d3151c3..9a17de1f 100644 --- a/packages/web/src/graphql/queries/get-current-user.ts +++ b/packages/web/src/graphql/queries/get-current-user.ts @@ -6,8 +6,6 @@ export const GET_CURRENT_USER = gql` id fullName email - createdAt - updatedAt } } `; diff --git a/packages/web/src/pages/ProfileSettings/index.tsx b/packages/web/src/pages/ProfileSettings/index.tsx index fe08070f..4cda68e6 100644 --- a/packages/web/src/pages/ProfileSettings/index.tsx +++ b/packages/web/src/pages/ProfileSettings/index.tsx @@ -19,6 +19,12 @@ import { UPDATE_USER } from 'graphql/mutations/update-user'; import useFormatMessage from 'hooks/useFormatMessage'; import useCurrentUser from 'hooks/useCurrentUser'; +type TMutationInput = { + fullName: string; + email: string; + password?: string; +} + const validationSchema = yup .object({ fullName: yup.string().required(), @@ -43,13 +49,27 @@ function ProfileSettings() { const formatMessage = useFormatMessage(); const [updateUser] = useMutation(UPDATE_USER); - const handleProfileSettingsUpdate = async (data: any) => { - const { fullName , password, email } = data; + const handleProfileSettingsUpdate = async (data: any) => { + const { fullName, password, email } = data; + + const mutationInput: TMutationInput = { + fullName, + email, + } + + if (password) { + mutationInput.password = password; + } + await updateUser({ variables: { - input: { + input: mutationInput, + }, + optimisticResponse: { + updateUser: { + __typename: 'User', + id: currentUser.id, fullName, - password, email, }, },