Skip to content

Commit

Permalink
Abstract function to avoid repetetive code
Browse files Browse the repository at this point in the history
  • Loading branch information
Onitoxan committed Oct 9, 2024
1 parent 7e6068c commit c6cbb74
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 51 deletions.
30 changes: 8 additions & 22 deletions apps/hpc-ftsadmin/src/app/components/organization-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 (
<Formik
enableReinitialize
initialValues={
initialValues ? initialValues : ADD_EDIT_ORGANIZATION_INITIAL_VALUES
}
initialValues={initialValues ?? ADD_EDIT_ORGANIZATION_INITIAL_VALUES}
onSubmit={handleSubmit}
validate={(values) => validateForm(values, FORM_VALIDATION)}
>
Expand All @@ -190,7 +171,12 @@ export const OrganizationForm = ({ initialValues, id, load }: Props) => {
React.SetStateAction<string | undefined>
>
}
error={parseError(error)}
error={parseError(
error,
'organizationUpdateCreate',
lang,
errorValue
)}
/>
<C.TextFieldWrapper
label={t.t(
Expand Down
35 changes: 7 additions & 28 deletions apps/hpc-ftsadmin/src/app/components/tables/keywords-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { Form, Formik } from 'formik';
import { util } from '@unocha/hpc-core';
import { LocalStorageSchema } from '../../utils/local-storage-type';
import { Strings } from '../../../i18n/iface';
import { parseError } from '../../utils/map-functions';

export type KeywordQuery = {
orderBy: string;
Expand Down Expand Up @@ -443,33 +444,6 @@ export default function KeywordTable(props: KeywordTableProps) {
);
};

const parseError = (
error:
| {
code: keyof Strings['components']['organizationUpdateCreate']['errors'];
value: string;
}
| undefined,
lang: LanguageKey
): string | undefined => {
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 (
<AppContext.Consumer>
{({ lang }) => (
Expand All @@ -487,7 +461,12 @@ export default function KeywordTable(props: KeywordTableProps) {
<KeywordTableContext.Provider value={{ setError: setError }}>
<C.ErrorAlert
setError={setError}
error={parseError(error, lang)}
error={parseError(
error?.code,
'keywordTable',
lang,
error?.value
)}
/>
<ChipDiv>
<TopRowContainer>
Expand Down
28 changes: 27 additions & 1 deletion apps/hpc-ftsadmin/src/app/utils/map-functions.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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;
};

0 comments on commit c6cbb74

Please sign in to comment.