diff --git a/src/components/organisms/Profile.tsx b/src/components/organisms/Profile.tsx index 221fd63f..dd7694a0 100644 --- a/src/components/organisms/Profile.tsx +++ b/src/components/organisms/Profile.tsx @@ -14,6 +14,7 @@ import { INSERT_USER_TOPIC_MUTATION, UPSERT_USER_AGENCY_MUTATION, UPSERT_USER_CERTIFICATION_MUTATION, + UPDATE_USER_CERTIFICATION_MUTATION, } from '../../graphql/mutations/userInfos' import { GET_USER_AGENCY_AND_ALL_AGENCIES_QUERY } from '../../graphql/queries/userInfos' import Custom404 from '../../pages/404' @@ -75,6 +76,9 @@ const Profile = ({ const [upsertCertificationMutation] = useMutation( UPSERT_USER_CERTIFICATION_MUTATION ) + const [updateCertificationMutation] = useMutation( + UPDATE_USER_CERTIFICATION_MUTATION + ) const [deleteCertificationMutation] = useMutation( DELETE_USER_CERTIFICATION_MUTATION ) @@ -137,33 +141,64 @@ const Profile = ({ } const updateCertification = (userCert: UserCertification) => { - upsertCertificationMutation({ - variables: { - email: userEmail, - certId: userCert.Certification.id, - obtained: userCert.obtained, - from: userCert.from, - to: userCert.to, - url: userCert.url, - }, - }) - .then(() => { - displayNotification( - t('myProfile.updateUserCertSuccess'), - 'green', - 5000 - ) - setCertModalOpened(false) - setSelectedUserCert(null) - refetch() + if (selectedUserCert) { + updateCertificationMutation({ + variables: { + email: userEmail, + certId: userCert.Certification.id, + oldFrom: selectedUserCert.from, + newFrom: userCert.from, + obtained: userCert.obtained, + to: userCert.to, + url: userCert.url, + }, }) - .catch(() => { - displayNotification( - `${t('myProfile.updateUserCertError')}`, - 'red', - 5000 - ) + .then(() => { + displayNotification( + t('myProfile.updateUserCertSuccess'), + 'green', + 5000 + ) + setCertModalOpened(false) + setSelectedUserCert(null) + refetch() + }) + .catch(() => { + displayNotification( + `${t('myProfile.updateUserCertError')}`, + 'red', + 5000 + ) + }) + } else { + upsertCertificationMutation({ + variables: { + email: userEmail, + certId: userCert.Certification.id, + obtained: userCert.obtained, + from: userCert.from, + to: userCert.to, + url: userCert.url, + }, }) + .then(() => { + displayNotification( + t('myProfile.updateUserCertSuccess'), + 'green', + 5000 + ) + setCertModalOpened(false) + setSelectedUserCert(null) + refetch() + }) + .catch(() => { + displayNotification( + `${t('myProfile.updateUserCertError')}`, + 'red', + 5000 + ) + }) + } } const deleteCertification = (userCert: UserCertification) => { diff --git a/src/graphql/mutations/userInfos.ts b/src/graphql/mutations/userInfos.ts index 705ab13e..e040bb1c 100644 --- a/src/graphql/mutations/userInfos.ts +++ b/src/graphql/mutations/userInfos.ts @@ -83,6 +83,29 @@ export const UPSERT_USER_CERTIFICATION_MUTATION = gql` } ` +export const UPDATE_USER_CERTIFICATION_MUTATION = gql` + mutation updateUserCertification( + $email: String! + $certId: Int! + $oldFrom: date! + $newFrom: date! + $obtained: Boolean! + $to: date + $url: String + ) { + update_UserCertification( + where: { + userEmail: { _eq: $email } + certId: { _eq: $certId } + from: { _eq: $oldFrom } + } + _set: { from: $newFrom, obtained: $obtained, to: $to, url: $url } + ) { + affected_rows + } + } +` + export const DELETE_USER_CERTIFICATION_MUTATION = gql` mutation deleteUserCertificationMutation( $email: String!