Skip to content

Commit

Permalink
refactor: use getLocalisedName() @ InProgress.tsx
Browse files Browse the repository at this point in the history
We've found cleaner way to make the rendered name customizable for each country through client copy from country-config

#6830
  • Loading branch information
Siyasanga committed Jan 14, 2025
1 parent a7e0547 commit 0d26aad
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 59 deletions.
87 changes: 32 additions & 55 deletions packages/client/src/utils/draftUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import {
IPrintableDeclaration,
SUBMISSION_STATUS
} from '@client/declarations'
import { IFormSectionData } from '@client/forms'
import { EMPTY_STRING } from '@client/utils/constants'
import { EventType, History, RegStatus } from '@client/utils/gateway'
import type {
GQLBirthEventSearchSet,
Expand All @@ -24,67 +22,46 @@ import type {
} from '@client/utils/gateway-deprecated-do-not-use'
import { getEvent } from '@client/views/PrintCertificate/utils'
import { includes } from 'lodash'
import { EMPTY_STRING } from '@client/utils/constants'
import { getLocalisedName } from './data-formatting'
import { IntlShape } from 'react-intl'

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)
}

/*
* lastNameFirst needs to be removed in #4464
*/
export const getDeclarationFullName = (
draft: IDeclaration,
language?: string,
lastNameFirst?: boolean
intl: IntlShape
) => {
switch (draft.event) {
case EventType.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 EventType.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 EventType.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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ function InProgressComponent(props: IRegistrarHomeProps) {

const transformDraftContent = () => {
const { intl } = props
const { locale } = intl
if (!props.drafts || props.drafts.length <= 0) {
return []
}
Expand All @@ -341,7 +340,7 @@ function InProgressComponent(props: IRegistrarHomeProps) {
} 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[] = []

Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/views/RecordAudit/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,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,
Expand Down

0 comments on commit 0d26aad

Please sign in to comment.