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 Nov 14, 2024
1 parent 347999c commit cec5d6a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 55 deletions.
82 changes: 31 additions & 51 deletions packages/client/src/utils/draftUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
}
Expand All @@ -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[] = []

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 cec5d6a

Please sign in to comment.