Skip to content

Commit 45b33a0

Browse files
committed
feat: preview generated title for field report
1 parent b846509 commit 45b33a0

File tree

6 files changed

+105
-13
lines changed

6 files changed

+105
-13
lines changed

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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 { useRequest } from '#utils/restRequest';
7+
8+
import i18n from './i18n.json';
9+
10+
interface Props {
11+
country: number;
12+
disasterType: number;
13+
event?: number;
14+
isCovidReport?: boolean;
15+
startDate?: string;
16+
title: string;
17+
}
18+
19+
function TitlePreview(props: Props) {
20+
const {
21+
country,
22+
disasterType,
23+
event,
24+
isCovidReport,
25+
startDate,
26+
title,
27+
} = props;
28+
29+
const strings = useTranslation(i18n);
30+
const alert = useAlert();
31+
32+
const variables = useMemo(() => ({
33+
countries: [country],
34+
is_covid_report: isCovidReport,
35+
start_date: startDate,
36+
dtype: disasterType,
37+
event,
38+
title,
39+
}), [country, isCovidReport, startDate, disasterType, event, title]);
40+
41+
const {
42+
response: genereateTitleResponse,
43+
} = useRequest({
44+
url: '/api/v2/field-report/generate-title/',
45+
method: 'POST',
46+
body: variables,
47+
preserveResponse: true,
48+
onFailure: () => {
49+
alert.show(
50+
strings.failedToGenerateTitle,
51+
{
52+
variant: 'danger',
53+
},
54+
);
55+
},
56+
});
57+
58+
return (
59+
<TextOutput
60+
value={genereateTitleResponse?.title}
61+
label={strings.titlePreview}
62+
strongLabel
63+
/>
64+
);
65+
}
66+
67+
export default TitlePreview;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +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-
"originalTitleSecondaryLabel": "Original Title"
36+
"originalTitle": "Original Title",
37+
"generatedTitlePreview": "Title Preview"
3738
}
3839
}
3940

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

Lines changed: 26 additions & 10 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';
@@ -70,6 +74,7 @@ function ContextFields(props: Props) {
7074
} = props;
7175

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

7479
const {
7580
api_field_report_status,
@@ -298,16 +303,27 @@ function ContextFields(props: Props) {
298303
disabled={disabled}
299304
withAsterisk
300305
/>
301-
{isTruthyString(value.summary) && (
302-
<TextInput
303-
label={strings.originalTitleSecondaryLabel}
304-
name="summary"
305-
value={value.summary}
306-
onChange={onValueChange}
307-
error={error?.summary}
308-
disabled
309-
/>
310-
)}
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+
)}
311327
</>
312328
)}
313329
</InputSection>

app/src/views/FieldReportForm/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export const reportSchema: FormSchema = {
275275
country: { required: true },
276276
districts: { defaultValue: [] },
277277
dtype: { required: true },
278-
title: { required: true },
278+
title: { required: true, requiredValidation: requiredStringCondition },
279279
start_date: { required: true },
280280
request_assistance: {},
281281
ns_request_assistance: {},

0 commit comments

Comments
 (0)