From c6cbb74a67792c80234cfef2c74e3b5184f9102a Mon Sep 17 00:00:00 2001 From: Onitoxan Date: Wed, 9 Oct 2024 11:50:45 +0200 Subject: [PATCH] Abstract function to avoid repetetive code --- .../src/app/components/organization-form.tsx | 30 +++++----------- .../app/components/tables/keywords-table.tsx | 35 ++++--------------- .../src/app/utils/map-functions.ts | 28 ++++++++++++++- 3 files changed, 42 insertions(+), 51 deletions(-) diff --git a/apps/hpc-ftsadmin/src/app/components/organization-form.tsx b/apps/hpc-ftsadmin/src/app/components/organization-form.tsx index df77e2dd8..8c3d5cfbc 100644 --- a/apps/hpc-ftsadmin/src/app/components/organization-form.tsx +++ b/apps/hpc-ftsadmin/src/app/components/organization-form.tsx @@ -19,7 +19,7 @@ import { fnLocations, fnOrganizations, } from '../utils/fn-promises'; -import { valueToInteger } from '../utils/map-functions'; +import { parseError, valueToInteger } from '../utils/map-functions'; interface Props { id?: number; load?: () => void; @@ -156,29 +156,10 @@ export const OrganizationForm = ({ initialValues, id, load }: Props) => { }); } }; - const parseError = ( - error: - | keyof Strings['components']['organizationUpdateCreate']['errors'] - | undefined - ): string | undefined => { - if (!error) { - return undefined; - } - const traducedError = t.t( - lang, - (s) => s.components.organizationUpdateCreate.errors[error] - ); - if (error === 'duplicate') { - return traducedError.replace('{organizationName}', errorValue); - } - return traducedError; - }; return ( validateForm(values, FORM_VALIDATION)} > @@ -190,7 +171,12 @@ export const OrganizationForm = ({ initialValues, id, load }: Props) => { React.SetStateAction > } - error={parseError(error)} + error={parseError( + error, + 'organizationUpdateCreate', + lang, + errorValue + )} /> { - if (!error) { - return undefined; - } - const errorCode = error.code; - - if (!errorCode) { - return undefined; - } - const traducedError = t.t( - lang, - (s) => s.components.keywordTable.errors[errorCode] - ); - if (error.code === 'duplicate') { - return traducedError.replace('{keywordName}', error.value); - } - return traducedError; - }; - return ( {({ lang }) => ( @@ -487,7 +461,12 @@ export default function KeywordTable(props: KeywordTableProps) { diff --git a/apps/hpc-ftsadmin/src/app/utils/map-functions.ts b/apps/hpc-ftsadmin/src/app/utils/map-functions.ts index 067488e20..85c264bdf 100644 --- a/apps/hpc-ftsadmin/src/app/utils/map-functions.ts +++ b/apps/hpc-ftsadmin/src/app/utils/map-functions.ts @@ -1,6 +1,7 @@ import { organizations } from '@unocha/hpc-data'; -import { LanguageKey } from '../../i18n'; +import { LanguageKey, t } from '../../i18n'; import dayjs from 'dayjs'; +import { Strings } from '../../i18n/iface'; export const valueToInteger = (value: string | number) => { return typeof value === 'number' ? value : parseInt(value); @@ -19,3 +20,28 @@ export const parseUpdatedCreatedBy = ( return `${participantName} (${dayjs(date).locale(lang).format('D/M/YYYY')})`; }; + +export const parseError = ( + error: 'unknown' | 'duplicate' | undefined, + component: 'organizationUpdateCreate' | 'keywordTable', + lang: LanguageKey, + errorValue?: string +) => { + if (!error) { + return undefined; + } + const translatedError = t.t( + lang, + (s) => s.components[component].errors[error] + ); + if (error === 'duplicate' && errorValue) { + return translatedError.replace( + `${ + component === 'keywordTable' ? '{keywordName}' : '{organizationName}' + }`, + errorValue + ); + } + + return translatedError; +};