From 27c4188128124a2054efbf7407a6560f623e762a Mon Sep 17 00:00:00 2001 From: dat Date: Mon, 4 Dec 2023 17:47:57 +0700 Subject: [PATCH] Can't fully remove profile traits --- .../ModifyEducationModal.tsx | 6 +-- .../ModifyWorkExpirenceModal.tsx | 6 +-- .../lib/profile/profile-functions/index.ts | 1 + .../profile-store/profile-xhr.store.ts | 10 +++- .../profile-functions/profile.functions.ts | 46 ++++++++++++++++++- 5 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/apps/profiles/src/member-profile/education-and-certifications/ModifyEducationModal/ModifyEducationModal.tsx b/src/apps/profiles/src/member-profile/education-and-certifications/ModifyEducationModal/ModifyEducationModal.tsx index 6f88ba4a9..50863cb1c 100644 --- a/src/apps/profiles/src/member-profile/education-and-certifications/ModifyEducationModal/ModifyEducationModal.tsx +++ b/src/apps/profiles/src/member-profile/education-and-certifications/ModifyEducationModal/ModifyEducationModal.tsx @@ -6,7 +6,7 @@ import classNames from 'classnames' import { BaseModal, Button, IconOutline, InputSelect, InputText } from '~/libs/ui' import { - updateOrCreateMemberTraitsAsync, + updateDeleteOrCreateMemberTraitAsync, UserProfile, UserTrait, UserTraitCategoryNames, @@ -72,13 +72,13 @@ const ModifyEducationModal: FC = (props: ModifyEducat setIsSaving(true) - updateOrCreateMemberTraitsAsync(props.profile.handle, [{ + updateDeleteOrCreateMemberTraitAsync(props.profile.handle, { categoryName: UserTraitCategoryNames.education, traitId: UserTraitIds.education, traits: { data: memberEducation || [], }, - }]) + }, props.education) .then(() => { toast.success('Education updated successfully.', { position: toast.POSITION.BOTTOM_RIGHT }) props.onSave() diff --git a/src/apps/profiles/src/member-profile/work-expirence/ModifyWorkExpirenceModal/ModifyWorkExpirenceModal.tsx b/src/apps/profiles/src/member-profile/work-expirence/ModifyWorkExpirenceModal/ModifyWorkExpirenceModal.tsx index a46afb2f5..85658540e 100644 --- a/src/apps/profiles/src/member-profile/work-expirence/ModifyWorkExpirenceModal/ModifyWorkExpirenceModal.tsx +++ b/src/apps/profiles/src/member-profile/work-expirence/ModifyWorkExpirenceModal/ModifyWorkExpirenceModal.tsx @@ -6,7 +6,7 @@ import classNames from 'classnames' import { BaseModal, Button, IconOutline, InputDatePicker, InputSelect, InputText } from '~/libs/ui' import { - updateOrCreateMemberTraitsAsync, + updateDeleteOrCreateMemberTraitAsync, UserProfile, UserTrait, UserTraitCategoryNames, UserTraitIds, @@ -71,13 +71,13 @@ const ModifyWorkExpirenceModal: FC = (props: Modi setIsSaving(true) - updateOrCreateMemberTraitsAsync(props.profile.handle, [{ + updateDeleteOrCreateMemberTraitAsync(props.profile.handle, { categoryName: UserTraitCategoryNames.work, traitId: UserTraitIds.work, traits: { data: workExpirence || [], }, - }]) + }, props.workExpirence) .then(() => { toast.success('Work Experience updated successfully.', { position: toast.POSITION.BOTTOM_RIGHT }) props.onSave() diff --git a/src/libs/core/lib/profile/profile-functions/index.ts b/src/libs/core/lib/profile/profile-functions/index.ts index 1cdf977a4..fd4a249c6 100644 --- a/src/libs/core/lib/profile/profile-functions/index.ts +++ b/src/libs/core/lib/profile/profile-functions/index.ts @@ -16,6 +16,7 @@ export { updateMemberProfileAsync, updateMemberPhotoAsync, updateOrCreateMemberTraitsAsync, + updateDeleteOrCreateMemberTraitAsync, } from './profile.functions' export * from './profile-store' export * from './rating.functions' diff --git a/src/libs/core/lib/profile/profile-functions/profile-store/profile-xhr.store.ts b/src/libs/core/lib/profile/profile-functions/profile-store/profile-xhr.store.ts index e6560d60c..84966a017 100644 --- a/src/libs/core/lib/profile/profile-functions/profile-store/profile-xhr.store.ts +++ b/src/libs/core/lib/profile/profile-functions/profile-store/profile-xhr.store.ts @@ -1,4 +1,4 @@ -import { xhrGetAsync, xhrPatchAsync, xhrPostAsync, xhrPutAsync } from '../../../xhr' +import { xhrDeleteAsync, xhrGetAsync, xhrPatchAsync, xhrPostAsync, xhrPutAsync } from '../../../xhr' import { CountryLookup } from '../../country-lookup.model' import { EditNameRequest } from '../../edit-name-request.model' import { ModifyTracksRequest } from '../../modify-tracks.request' @@ -95,6 +95,14 @@ export async function createMemberTraits( return xhrPostAsync(`${profileUrl(handle)}/traits`, traits) } +export async function deleteMemberTrait( + handle: string, + traitIds: string, +): Promise { + await xhrDeleteAsync(`${profileUrl(handle)}/traits?traitIds=${traitIds}`) + return [] +} + export async function modifyTracks(handle: string, request: ModifyTracksRequest): Promise { return xhrPutAsync(profileUrl(handle), request) } diff --git a/src/libs/core/lib/profile/profile-functions/profile.functions.ts b/src/libs/core/lib/profile/profile-functions/profile.functions.ts index 368c923bc..1f1e482bd 100644 --- a/src/libs/core/lib/profile/profile-functions/profile.functions.ts +++ b/src/libs/core/lib/profile/profile-functions/profile.functions.ts @@ -9,13 +9,14 @@ import { ModifyUserPropertyResponse } from '../modify-user-role.model' import { UserEmailPreferences } from '../user-email-preference.model' import { UserProfile } from '../user-profile.model' import { UserStats } from '../user-stats.model' -import { UserTraits } from '../user-traits.model' +import { UserTrait, UserTraits } from '../user-traits.model' import { UserVerify } from '../user-verify.model' import { profileFactoryCreate } from './profile-factory' import { getMemberStats, getVerification, profileStoreGet, profileStorePatchName } from './profile-store' import { createMemberTraits, + deleteMemberTrait, getCountryLookup, modifyTracks, updateMemberEmailPreferences, @@ -135,6 +136,13 @@ export async function createMemberTraitsAsync( return createMemberTraits(handle, traits) } +export async function deleteMemberTraitAsync( + handle: string, + traitIds: string, +): Promise { + return deleteMemberTrait(handle, traitIds) +} + export async function modifyTracksAsync(handle: string, tracks: ModifyTracksRequest): Promise { return modifyTracks(handle, tracks) } @@ -159,3 +167,39 @@ export async function updateOrCreateMemberTraitsAsync( return createdTraitsRsp } } + +export async function updateDeleteOrCreateMemberTraitAsync( + handle: string, + trait: UserTraits, + previousTraitsData: Array | undefined, +): Promise { + if (!trait.traits.data.length) { + if (!previousTraitsData) { + return [] // no need to delete trait if trait data is null + } + + try { + await deleteMemberTraitAsync(handle, trait.traitId) + return [] + } catch (error) { + } + } + + if (!previousTraitsData) { + try { + // call request to create trait data if trait data is null + const createdTraitsRsp = await createMemberTraitsAsync(handle, [trait]) + return createdTraitsRsp + } catch (error) { + } + } + + try { + // call request to update trait data if trait data is not null + const updatedTraitsRsp = await updateMemberTraitsAsync(handle, [trait]) + return updatedTraitsRsp + } catch (error) { + const createdTraitsRsp = await createMemberTraitsAsync(handle, [trait]) + return createdTraitsRsp + } +}