Skip to content

Commit 0d0e5d6

Browse files
authored
Merge pull request #1470 from IFRCGo/fix/field-report-title-server-generation
project: Move field report title generation logic to server
2 parents 89e6ace + d613c14 commit 0d0e5d6

File tree

10 files changed

+169
-250
lines changed

10 files changed

+169
-250
lines changed

.changeset/itchy-dryers-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"go-web-app": patch
3+
---
4+
5+
Move field report / emergency title generation logic from client to server

app/env.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default defineConfig({
1616
return value as ('production' | 'staging' | 'testing' | `alpha-${number}` | 'development' | 'APP_ENVIRONMENT_PLACEHOLDER');
1717
},
1818
APP_API_ENDPOINT: Schema.string({ format: 'url', protocol: true, tld: false }),
19-
APP_ADMIN_URL: Schema.string.optional({ format: 'url', protocol: true }),
19+
APP_ADMIN_URL: Schema.string.optional({ format: 'url', protocol: true, tld: false }),
2020
APP_MAPBOX_ACCESS_TOKEN: Schema.string(),
2121
APP_TINY_API_KEY: Schema.string(),
2222
APP_RISK_API_ENDPOINT: Schema.string({ format: 'url', protocol: true }),
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"namespace": "fieldReportForm",
3+
"strings": {
4+
"titlePreview": "Title Preview",
5+
"failedToGenerateTitle": "Failed To Generate the Field Report Title"
6+
}
7+
}
8+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { useMemo } from 'react';
2+
import { TextOutput } from '@ifrc-go/ui';
3+
import { useTranslation } from '@ifrc-go/ui/hooks';
4+
5+
import useAlert from '#hooks/useAlert';
6+
import useDebouncedValue from '#hooks/useDebouncedValue';
7+
import { useRequest } from '#utils/restRequest';
8+
9+
import i18n from './i18n.json';
10+
11+
interface Props {
12+
country: number;
13+
disasterType: number;
14+
event?: number;
15+
isCovidReport?: boolean;
16+
startDate?: string;
17+
title: string;
18+
}
19+
20+
function TitlePreview(props: Props) {
21+
const {
22+
country,
23+
disasterType,
24+
event,
25+
isCovidReport,
26+
startDate,
27+
title,
28+
} = props;
29+
30+
const strings = useTranslation(i18n);
31+
const alert = useAlert();
32+
33+
const variables = useMemo(() => ({
34+
countries: [country],
35+
is_covid_report: isCovidReport,
36+
start_date: startDate,
37+
dtype: disasterType,
38+
event,
39+
title,
40+
}), [country, isCovidReport, startDate, disasterType, event, title]);
41+
42+
const debouncedVariables = useDebouncedValue(variables);
43+
44+
const {
45+
response: generateTitleResponse,
46+
} = useRequest({
47+
url: '/api/v2/field-report/generate-title/',
48+
method: 'POST',
49+
body: debouncedVariables,
50+
preserveResponse: true,
51+
onFailure: () => {
52+
alert.show(
53+
strings.failedToGenerateTitle,
54+
{
55+
variant: 'danger',
56+
},
57+
);
58+
},
59+
});
60+
61+
return (
62+
<TextOutput
63+
value={generateTitleResponse?.title}
64+
label={strings.titlePreview}
65+
strongLabel
66+
/>
67+
);
68+
}
69+
70+
export default TitlePreview;

app/src/views/FieldReportForm/ContextFields/i18n.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
"fieldReportFormContextTitle": "Context",
3434
"fieldReportFormSearchTitle": "Search for existing emergency",
3535
"fieldReportFormSearchDescription": "Type the name of the country you want to report on in the box above to begin the search.",
36-
"fieldPrefix": "Prefix",
37-
"fieldReportFormSuffix": "Suffix"
36+
"originalTitle": "Original Title",
37+
"generatedTitlePreview": "Title Preview"
3838
}
3939
}
4040

app/src/views/FieldReportForm/ContextFields/index.tsx

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ import {
22
useCallback,
33
useMemo,
44
} from 'react';
5+
import { useParams } from 'react-router-dom';
56
import {
67
BooleanInput,
78
Container,
89
DateInput,
910
InputSection,
1011
RadioInput,
1112
TextInput,
13+
TextOutput,
1214
} from '@ifrc-go/ui';
1315
import { useTranslation } from '@ifrc-go/ui/hooks';
1416
import {
17+
isDefined,
1518
isNotDefined,
1619
isTruthyString,
1720
} from '@togglecorp/fujs';
@@ -36,6 +39,7 @@ import {
3639
} from '#utils/constants';
3740

3841
import { type PartialFormValue } from '../common';
42+
import TitlePreview from './TitlePreview';
3943

4044
import i18n from './i18n.json';
4145
import styles from './styles.module.css';
@@ -54,10 +58,6 @@ interface Props {
5458
setDistrictOptions: React.Dispatch<React.SetStateAction<DistrictItem[] | null | undefined>>;
5559
setEventOptions: React.Dispatch<React.SetStateAction<EventItem[] | null | undefined>>;
5660
disabled?: boolean;
57-
58-
fieldReportId: string | undefined;
59-
titlePrefix: string | undefined;
60-
titleSuffix: string | undefined;
6161
}
6262

6363
function ContextFields(props: Props) {
@@ -71,12 +71,10 @@ function ContextFields(props: Props) {
7171
setDistrictOptions,
7272
setEventOptions,
7373
disabled,
74-
titlePrefix,
75-
titleSuffix,
76-
fieldReportId,
7774
} = props;
7875

7976
const strings = useTranslation(i18n);
77+
const { fieldReportId } = useParams<{ fieldReportId: string }>();
8078

8179
const {
8280
api_field_report_status,
@@ -171,16 +169,7 @@ function ContextFields(props: Props) {
171169
[onValueChange, value.dtype],
172170
);
173171

174-
const prefixVisible = !fieldReportId && isTruthyString(titlePrefix);
175172
const summaryVisible = !value.is_covid_report;
176-
const suffixVisible = !fieldReportId && isTruthyString(titleSuffix);
177-
178-
const preferredColumnNoForSummary = Math.max(
179-
(prefixVisible ? 1 : 0)
180-
+ (summaryVisible ? 1 : 0)
181-
+ (suffixVisible ? 1 : 0),
182-
1,
183-
) as 1 | 2 | 3;
184173

185174
return (
186175
<Container
@@ -299,37 +288,43 @@ function ContextFields(props: Props) {
299288
title={strings.summaryLabel}
300289
description={strings.summaryDescription}
301290
withAsteriskOnTitle
302-
numPreferredColumns={preferredColumnNoForSummary}
291+
numPreferredColumns={1}
303292
>
304-
{prefixVisible && (
305-
<TextInput
306-
label={summaryVisible ? strings.fieldPrefix : strings.titleSecondaryLabel}
307-
name={undefined}
308-
value={titlePrefix}
309-
onChange={() => { }}
310-
/>
311-
)}
312293
{summaryVisible && (
313-
<TextInput
314-
label={strings.titleSecondaryLabel}
315-
placeholder={strings.titleInputPlaceholder}
316-
name="summary"
317-
value={value.summary}
318-
maxLength={256}
319-
onChange={onValueChange}
320-
error={error?.summary}
321-
disabled={disabled}
322-
withAsterisk
323-
/>
324-
)}
325-
{suffixVisible && (
326-
<TextInput
327-
label={strings.fieldReportFormSuffix}
328-
name={undefined}
329-
value={titleSuffix}
330-
onChange={() => { }}
331-
// readOnly
332-
/>
294+
<>
295+
<TextInput
296+
label={strings.titleSecondaryLabel}
297+
placeholder={strings.titleInputPlaceholder}
298+
name="title"
299+
value={value.title}
300+
maxLength={256}
301+
onChange={onValueChange}
302+
error={error?.title}
303+
disabled={disabled}
304+
withAsterisk
305+
/>
306+
{isDefined(value.country)
307+
&& isDefined(value.dtype)
308+
&& isDefined(value.title)
309+
&& isTruthyString(value.title.trim())
310+
? (
311+
<TitlePreview
312+
country={value.country}
313+
disasterType={value.dtype}
314+
event={value.event}
315+
isCovidReport={value.is_covid_report}
316+
startDate={value.start_date}
317+
title={value.title}
318+
/>
319+
) : (
320+
<TextOutput
321+
value={value.summary}
322+
label={isDefined(fieldReportId)
323+
? strings.originalTitle : strings.generatedTitlePreview}
324+
strongLabel
325+
/>
326+
)}
327+
</>
333328
)}
334329
</InputSection>
335330
<InputSection

app/src/views/FieldReportForm/common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ const fieldsInContext = [
152152
'summary',
153153
'request_assistance',
154154
'ns_request_assistance',
155+
'title',
155156
] satisfies (keyof PartialFormValue)[];
156157
const fieldsInSituation = [
157158
'affected_pop_centres',
@@ -274,6 +275,7 @@ export const reportSchema: FormSchema = {
274275
country: { required: true },
275276
districts: { defaultValue: [] },
276277
dtype: { required: true },
278+
title: { required: true, requiredValidation: requiredStringCondition },
277279
start_date: { required: true },
278280
request_assistance: {},
279281
ns_request_assistance: {},

0 commit comments

Comments
 (0)