Skip to content

Commit

Permalink
fix: move field report title generation logic to server
Browse files Browse the repository at this point in the history
  • Loading branch information
samshara committed Nov 14, 2024
1 parent 8f7a9bb commit c7d2eb5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 255 deletions.
43 changes: 5 additions & 38 deletions app/src/views/FieldReportForm/ContextFields/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import {
TextInput,
} from '@ifrc-go/ui';
import { useTranslation } from '@ifrc-go/ui/hooks';
import {
isNotDefined,
isTruthyString,
} from '@togglecorp/fujs';
import { isNotDefined } from '@togglecorp/fujs';
import {
type EntriesAsList,
type Error,
Expand Down Expand Up @@ -54,10 +51,6 @@ interface Props {
setDistrictOptions: React.Dispatch<React.SetStateAction<DistrictItem[] | null | undefined>>;
setEventOptions: React.Dispatch<React.SetStateAction<EventItem[] | null | undefined>>;
disabled?: boolean;

fieldReportId: string | undefined;
titlePrefix: string | undefined;
titleSuffix: string | undefined;
}

function ContextFields(props: Props) {
Expand All @@ -71,9 +64,6 @@ function ContextFields(props: Props) {
setDistrictOptions,
setEventOptions,
disabled,
titlePrefix,
titleSuffix,
fieldReportId,
} = props;

const strings = useTranslation(i18n);
Expand Down Expand Up @@ -171,14 +161,10 @@ function ContextFields(props: Props) {
[onValueChange, value.dtype],
);

const prefixVisible = !fieldReportId && isTruthyString(titlePrefix);
const summaryVisible = !value.is_covid_report;
const suffixVisible = !fieldReportId && isTruthyString(titleSuffix);

const preferredColumnNoForSummary = Math.max(
(prefixVisible ? 1 : 0)
+ (summaryVisible ? 1 : 0)
+ (suffixVisible ? 1 : 0),
summaryVisible ? 1 : 0,
1,
) as 1 | 2 | 3;

Expand Down Expand Up @@ -301,38 +287,19 @@ function ContextFields(props: Props) {
withAsteriskOnTitle
numPreferredColumns={preferredColumnNoForSummary}
>
{prefixVisible && (
<TextInput
label={summaryVisible ? strings.fieldPrefix : strings.titleSecondaryLabel}
name={undefined}
value={titlePrefix}
// eslint-disable-next-line @typescript-eslint/no-empty-function
onChange={() => { }}
/>
)}
{summaryVisible && (
<TextInput
label={strings.titleSecondaryLabel}
placeholder={strings.titleInputPlaceholder}
name="summary"
value={value.summary}
name="title"
value={value.title}
maxLength={256}
onChange={onValueChange}
error={error?.summary}
error={error?.title}
disabled={disabled}
withAsterisk
/>
)}
{suffixVisible && (
<TextInput
label={strings.fieldReportFormSuffix}
name={undefined}
value={titleSuffix}
// eslint-disable-next-line @typescript-eslint/no-empty-function
onChange={() => { }}
// readOnly
/>
)}
</InputSection>
<InputSection
title={strings.assistanceLabel}
Expand Down
36 changes: 19 additions & 17 deletions app/src/views/FieldReportForm/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ const fieldsInContext = [
'summary',
'request_assistance',
'ns_request_assistance',
'title',
] satisfies (keyof PartialFormValue)[];
const fieldsInSituation = [
'affected_pop_centres',
Expand Down Expand Up @@ -274,6 +275,7 @@ export const reportSchema: FormSchema = {
country: { required: true },
districts: { defaultValue: [] },
dtype: { required: true },
title: { required: true },
start_date: { required: true },
request_assistance: {},
ns_request_assistance: {},
Expand Down Expand Up @@ -309,23 +311,23 @@ export const reportSchema: FormSchema = {
});

// CONTEXT
baseSchema = addCondition(
baseSchema,
value,
['status', 'is_covid_report', 'dtype'],
['summary'],
(val): Pick<FormSchemaFields, 'summary'> => {
const reportType = getReportType(val?.status, val?.is_covid_report, val?.dtype);
if (reportType === 'COVID') {
return {
summary: { forceValue: nullValue },
};
}
return {
summary: { required: true, requiredValidation: requiredStringCondition },
};
},
);
// baseSchema = addCondition(
// baseSchema,
// value,
// ['status', 'is_covid_report', 'dtype'],
// ['summary'],
// (val): Pick<FormSchemaFields, 'summary'> => {
// const reportType = getReportType(val?.status, val?.is_covid_report, val?.dtype);
// if (reportType === 'COVID') {
// return {
// summary: { forceValue: nullValue },
// };
// }
// return {
// summary: { required: true, requiredValidation: requiredStringCondition },
// };
// },
// );

// SITUATION / RISK ANALYSIS

Expand Down
Loading

0 comments on commit c7d2eb5

Please sign in to comment.