From ea5cdd545617ca0d3ed36e21ecdc2be9456da480 Mon Sep 17 00:00:00 2001 From: Siyasanga Date: Tue, 29 Oct 2024 16:10:25 +0200 Subject: [PATCH] refactor: use getLocalisedName() @ InProgress.tsx We've found cleaner way to make the rendered name customizable for each country through client copy from country-config https://github.com/opencrvs/opencrvs-core/issues/6830 --- packages/client/src/utils/draftUtils.ts | 82 +++++++------------ .../OfficeHome/inProgress/InProgress.tsx | 3 +- .../client/src/views/RecordAudit/utils.ts | 4 +- 3 files changed, 34 insertions(+), 55 deletions(-) diff --git a/packages/client/src/utils/draftUtils.ts b/packages/client/src/utils/draftUtils.ts index 47dfe4dbe57..481c7c59564 100644 --- a/packages/client/src/utils/draftUtils.ts +++ b/packages/client/src/utils/draftUtils.ts @@ -13,7 +13,6 @@ import { SUBMISSION_STATUS, IPrintableDeclaration } from '@client/declarations' -import { IFormSectionData } from '@client/forms' import { Event, History, RegStatus } from '@client/utils/gateway' import type { GQLBirthEventSearchSet, @@ -24,67 +23,48 @@ import type { import { getEvent } from '@client/views/PrintCertificate/utils' import { includes } from 'lodash' import { EMPTY_STRING } from '@client/utils/constants' - -const getEngName = ( - sectionData: IFormSectionData, - lastNameFirst: boolean -): string => { - if (lastNameFirst) { - return `${sectionData.familyNameEng ?? ''} ${ - sectionData.firstNamesEng ?? '' - }` - } - return [ - sectionData.firstNamesEng, - sectionData.middleNameEng, - sectionData.familyNameEng - ] - .filter(Boolean) - .join(' ') - .trim() -} - -const getOtherName = (sectionData: IFormSectionData): string => { - return [ - sectionData.firstNames, - sectionData.middleName, - sectionData.familyName - ] - .filter(Boolean) - .join(' ') - .trim() -} - -const getFullName = ( - sectionData: IFormSectionData, - language = 'en', - lastNameFirst = false -): string => { - if (!sectionData) { - return EMPTY_STRING - } - if (language === 'en') { - return getEngName(sectionData, lastNameFirst) - } - return getOtherName(sectionData) || getEngName(sectionData, lastNameFirst) -} +import { getLocalisedName } from './data-formatting' +import { IntlShape } from 'react-intl' /* * lastNameFirst needs to be removed in #4464 */ export const getDeclarationFullName = ( draft: IDeclaration, - language?: string, - lastNameFirst?: boolean + intl: IntlShape ) => { switch (draft.event) { case Event.Birth: - return getFullName(draft.data.child, language, lastNameFirst) + return draft.data.child + ? getLocalisedName(intl, { + firstNames: draft.data.child.firstNamesEng as string, + middleName: draft.data.child.middleNameEng as string, + familyName: draft.data.child.middleNameEng as string + }) + : EMPTY_STRING case Event.Death: - return getFullName(draft.data.deceased, language, lastNameFirst) + return draft.data.deceased + ? getLocalisedName(intl, { + firstNames: draft.data.deceased.firstNamesEng as string, + middleName: draft.data.deceased.middleNameEng as string, + familyName: draft.data.deceased.familyNameEng as string + }) + : EMPTY_STRING case Event.Marriage: - const brideName = getFullName(draft.data.bride, language, lastNameFirst) - const groomName = getFullName(draft.data.groom, language, lastNameFirst) + const brideName = draft.data.bride + ? getLocalisedName(intl, { + firstNames: draft.data.bride.firstNamesEng as string, + middleName: draft.data.bride.middleNameEng as string, + familyName: draft.data.bride.familyNameEng as string + }) + : EMPTY_STRING + const groomName = draft.data.groom + ? getLocalisedName(intl, { + firstNames: draft.data.groom.firstNamesEng as string, + middleName: draft.data.groom.middleNameEng as string, + familyName: draft.data.groom.familyNameEng as string + }) + : EMPTY_STRING if (brideName && groomName) { return `${groomName} & ${brideName}` } else { diff --git a/packages/client/src/views/OfficeHome/inProgress/InProgress.tsx b/packages/client/src/views/OfficeHome/inProgress/InProgress.tsx index bccebf39128..cd4d32b7e1d 100644 --- a/packages/client/src/views/OfficeHome/inProgress/InProgress.tsx +++ b/packages/client/src/views/OfficeHome/inProgress/InProgress.tsx @@ -344,7 +344,6 @@ class InProgressComponent extends React.Component< transformDraftContent = () => { const { intl } = this.props - const { locale } = intl if (!this.props.drafts || this.props.drafts.length <= 0) { return [] } @@ -361,7 +360,7 @@ class InProgressComponent extends React.Component< } else if (draft.event && draft.event.toString() === 'marriage') { pageRoute = DRAFT_MARRIAGE_FORM_PAGE } - const name = getDeclarationFullName(draft, locale) + const name = getDeclarationFullName(draft, intl) const lastModificationDate = draft.modifiedOn || draft.savedOn const actions: IAction[] = [] diff --git a/packages/client/src/views/RecordAudit/utils.ts b/packages/client/src/views/RecordAudit/utils.ts index a2f58737fe1..3fcff98f333 100644 --- a/packages/client/src/views/RecordAudit/utils.ts +++ b/packages/client/src/views/RecordAudit/utils.ts @@ -346,8 +346,8 @@ export const getDraftDeclarationData = ( ): IDeclarationData => { return { id: declaration.id, - name: getDeclarationFullName(declaration), - type: declaration.event, + name: getDeclarationFullName(declaration, intl), + type: declaration.event || EMPTY_STRING, registrationNo: declaration.data?.registration?.registrationNumber?.toString() || EMPTY_STRING,