Skip to content

Commit 0c4d98a

Browse files
frozenheliumsamshara
authored andcommitted
Refactor DREF
1 parent 7a85a52 commit 0c4d98a

File tree

17 files changed

+192
-131
lines changed

17 files changed

+192
-131
lines changed

src/App/routes/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,7 @@ const preparednessGlobalCatalogue = customWrapRoute({
19421942
},
19431943
});
19441944

1945+
// FIXME: update name to `preparednessOperationalLearning`
19451946
const preparednessGlobalOperational = customWrapRoute({
19461947
parent: preparednessLayout,
19471948
path: 'operational-learning',

src/components/DateOutput/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface Props {
1414
function DateOutput(props: Props) {
1515
const {
1616
value,
17-
format = 'yyyy-MM-dd',
17+
format,
1818
className,
1919
invalidText,
2020
} = props;

src/index.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
--go-ui-font-family-mono: SFMono-Regular, Menlo, Monaco, Consolas, monospace;
55

66
--base-font-size: 0.875rem;
7-
--font-multiplier: 1.5;
8-
--go-ui-dref-super-ticket-size: 0.688rem;
7+
--go-ui-font-size-export: 0.6875rem;
98

109
--go-ui-font-size-2xs: calc(var(--base-font-size) * 0.625);
1110
--go-ui-font-size-xs: calc(var(--base-font-size) * 0.75);

src/utils/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export const DREF_STATUS_IN_PROGRESS = 0 satisfies DrefStatus;
9292
type TypeOfDrefEnum = components<'read'>['schemas']['TypeOfDrefEnum'];
9393
export const DREF_TYPE_IMMINENT = 0 satisfies TypeOfDrefEnum;
9494
export const DREF_TYPE_ASSESSMENT = 1 satisfies TypeOfDrefEnum;
95+
export const DREF_TYPE_RESPONSE = 2 satisfies TypeOfDrefEnum;
96+
export const DREF_TYPE_LOAN = 3 satisfies TypeOfDrefEnum;
9597

9698
// Subscriptions
9799
type SubscriptionRecordTypeEnum = components<'read'>['schemas']['RtypeEnum'];

src/utils/domain/dref.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { components } from '#generated/types';
22

33
type PlannedIntervention = components<'read'>['schemas']['PlannedIntervention'];
4-
type IdentifiedNeedsAndGaps = components<'read'>['schemas']['IdentifiedNeed'];
4+
type PlannedInterventionTitle = NonNullable<PlannedIntervention['title']>;
5+
type IdentifiedNeeds = components<'read'>['schemas']['IdentifiedNeed'];
6+
type IdentifiedNeedsTitle = NonNullable<IdentifiedNeeds['title']>;
57
type NsActions = components<'read'>['schemas']['NationalSocietyAction'];
68

7-
const plannedInterventionSortedList: Record<NonNullable<PlannedIntervention['title']>, number> = {
9+
export const plannedInterventionOrder: Record<PlannedInterventionTitle, number> = {
810
shelter_housing_and_settlements: 1,
911
livelihoods_and_basic_needs: 2,
1012
multi_purpose_cash: 3,
@@ -21,7 +23,7 @@ const plannedInterventionSortedList: Record<NonNullable<PlannedIntervention['tit
2123
national_society_strengthening: 14,
2224
};
2325

24-
const identifiedNeedsAndGapsSortedList: Record<NonNullable<IdentifiedNeedsAndGaps['title']>, number> = {
26+
export const identifiedNeedsAndGapsOrder: Record<IdentifiedNeedsTitle, number> = {
2527
shelter_housing_and_settlements: 1,
2628
livelihoods_and_basic_needs: 2,
2729
multi_purpose_cash_grants: 3,
@@ -35,7 +37,7 @@ const identifiedNeedsAndGapsSortedList: Record<NonNullable<IdentifiedNeedsAndGap
3537
environment_sustainability: 11,
3638
};
3739

38-
const nsActionsSortedList: Record<NsActions['title'], number> = {
40+
export const nsActionsOrder: Record<NsActions['title'], number> = {
3941
shelter_housing_and_settlements: 1,
4042
livelihoods_and_basic_needs: 2,
4143
multi_purpose_cash: 3,
@@ -55,9 +57,3 @@ const nsActionsSortedList: Record<NsActions['title'], number> = {
5557
national_society_eoc: 17,
5658
other: 18,
5759
};
58-
59-
export {
60-
plannedInterventionSortedList,
61-
identifiedNeedsAndGapsSortedList,
62-
nsActionsSortedList,
63-
};

src/views/AccountMyFormsDref/DrefTableActions/index.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import useBooleanState from '#hooks/useBooleanState';
2424
import useTranslation from '#hooks/useTranslation';
2525
import useAlert from '#hooks/useAlert';
2626
import { useLazyRequest } from '#utils/restRequest';
27-
import { DREF_STATUS_IN_PROGRESS } from '#utils/constants';
27+
import { DREF_STATUS_IN_PROGRESS, DREF_TYPE_IMMINENT, DREF_TYPE_LOAN } from '#utils/constants';
2828

2929
import { exportDrefAllocation } from './drefAllocationExport';
3030
import i18n from './i18n.json';
@@ -79,21 +79,24 @@ function DrefTableActions(props: Props) {
7979
),
8080
onSuccess: (response) => {
8181
const exportData = {
82-
allocationFor: response?.type_of_dref_display === 'Loan' ? 'Emergency Appeal' : 'DREF Operation',
82+
// FIXME: use translations
83+
allocationFor: response?.type_of_dref === DREF_TYPE_LOAN ? 'Emergency Appeal' : 'DREF Operation',
8384
appealManager: response?.ifrc_appeal_manager_name,
8485
projectManager: response?.ifrc_project_manager_name,
8586
affectedCountry: response?.country_details?.name,
8687
name: response?.title,
8788
disasterType: response?.disaster_type_details?.name,
88-
responseType: response?.type_of_dref_display === 'Imminent' ? 'Imminent Crisis' : response?.type_of_onset_display,
89+
// FIXME: use translations
90+
responseType: response?.type_of_dref === DREF_TYPE_IMMINENT ? 'Imminent Crisis' : response?.type_of_onset_display,
8991
noOfPeopleTargeted: response?.num_assisted,
9092
nsRequestDate: response?.ns_request_date,
9193
disasterStartDate: response?.event_date,
9294
implementationPeriod: response?.operation_timeframe,
9395
allocationRequested: response?.amount_requested,
9496
previousAllocation: undefined,
9597
totalDREFAllocation: response?.amount_requested,
96-
toBeAllocatedFrom: response?.type_of_dref_display === 'Imminent' ? 'Anticipatory Pillar' : 'Response Pillar',
98+
// FIXME: use translations
99+
toBeAllocatedFrom: response?.type_of_dref === DREF_TYPE_IMMINENT ? 'Anticipatory Pillar' : 'Response Pillar',
97100
focalPointName: response?.regional_focal_point_name,
98101
};
99102
exportDrefAllocation(exportData);

src/views/DrefApplicationExport/index.tsx

Lines changed: 88 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import {
2626
DisasterCategory,
2727
} from '#utils/constants';
2828
import {
29-
identifiedNeedsAndGapsSortedList,
30-
nsActionsSortedList,
31-
plannedInterventionSortedList,
29+
identifiedNeedsAndGapsOrder,
30+
nsActionsOrder,
31+
plannedInterventionOrder,
3232
} from '#utils/domain/dref';
3333
import ifrcLogo from '#assets/icons/ifrc-square.png';
3434

@@ -101,58 +101,72 @@ export function Component() {
101101
},
102102
});
103103

104-
const filteredPlannedIntervention = useMemo(
105-
() => drefResponse?.planned_interventions?.map((intervention) => {
106-
if (isNotDefined(intervention.title)) {
104+
const plannedInterventions = useMemo(
105+
() => {
106+
if (isNotDefined(drefResponse) || isNotDefined(drefResponse.planned_interventions)) {
107107
return undefined;
108108
}
109-
return { ...intervention, title: intervention.title };
110-
}).filter(isDefined),
111-
[drefResponse?.planned_interventions],
109+
110+
const { planned_interventions } = drefResponse;
111+
112+
return planned_interventions.map(
113+
(intervention) => {
114+
if (isNotDefined(intervention.title)) {
115+
return undefined;
116+
}
117+
return { ...intervention, title: intervention.title };
118+
},
119+
).filter(isDefined).sort(
120+
(a, b) => plannedInterventionOrder[a.title] - plannedInterventionOrder[b.title],
121+
);
122+
},
123+
[drefResponse],
112124
);
113125

114-
const filteredIdentifiedNeedsAndGaps = useMemo(
115-
() => drefResponse?.needs_identified?.map((need) => {
116-
if (isNotDefined(need.title)) {
126+
const needsIdentified = useMemo(
127+
() => {
128+
if (isNotDefined(drefResponse) || isNotDefined(drefResponse.needs_identified)) {
117129
return undefined;
118130
}
119-
return { ...need, title: need.title };
120-
}).filter(isDefined),
121-
[drefResponse?.needs_identified],
131+
132+
const { needs_identified } = drefResponse;
133+
134+
return needs_identified.map(
135+
(need) => {
136+
if (isNotDefined(need.title)) {
137+
return undefined;
138+
}
139+
140+
return {
141+
...need,
142+
title: need.title,
143+
};
144+
},
145+
).filter(isDefined).sort((a, b) => (
146+
identifiedNeedsAndGapsOrder[a.title] - identifiedNeedsAndGapsOrder[b.title]
147+
));
148+
},
149+
[drefResponse],
122150
);
123151

124-
const filteredNsActions = useMemo(
125-
() => drefResponse?.national_society_actions?.map((nsAction) => {
126-
if (isNotDefined(nsAction.title)) {
152+
const nsActions = useMemo(
153+
() => {
154+
if (isNotDefined(drefResponse) || isNotDefined(drefResponse.needs_identified)) {
127155
return undefined;
128156
}
129-
return { ...nsAction, title: nsAction.title };
130-
}).filter(isDefined),
131-
[drefResponse?.national_society_actions],
132-
);
133-
134-
const sortedPlannedInterventions = useMemo(
135-
() => filteredPlannedIntervention?.sort(
136-
// eslint-disable-next-line max-len
137-
(a, b) => plannedInterventionSortedList[a.title] - plannedInterventionSortedList[b.title],
138-
),
139-
[filteredPlannedIntervention],
140-
);
141157

142-
const sortedIdentifiedNeedsAndGaps = useMemo(
143-
() => filteredIdentifiedNeedsAndGaps?.sort(
144-
// eslint-disable-next-line max-len
145-
(a, b) => identifiedNeedsAndGapsSortedList[a.title] - identifiedNeedsAndGapsSortedList[b.title],
146-
),
147-
[filteredIdentifiedNeedsAndGaps],
148-
);
158+
const { national_society_actions } = drefResponse;
149159

150-
const sortedNsActions = useMemo(
151-
() => filteredNsActions?.sort((a, b) => (
152-
// eslint-disable-next-line max-len
153-
nsActionsSortedList[a.title] - nsActionsSortedList[b.title]
154-
)),
155-
[filteredNsActions],
160+
return national_society_actions?.map((nsAction) => {
161+
if (isNotDefined(nsAction.title)) {
162+
return undefined;
163+
}
164+
return { ...nsAction, title: nsAction.title };
165+
}).filter(isDefined).sort((a, b) => (
166+
nsActionsOrder[a.title] - nsActionsOrder[b.title]
167+
));
168+
},
169+
[drefResponse],
156170
);
157171

158172
const eventDescriptionDefined = isTruthyString(drefResponse?.event_description?.trim());
@@ -187,7 +201,7 @@ export function Component() {
187201
const showNsAction = isDefined(drefResponse)
188202
&& isDefined(drefResponse.national_society_actions)
189203
&& drefResponse.national_society_actions.length > 0
190-
&& isDefined(sortedNsActions);
204+
&& isDefined(nsActions);
191205

192206
const icrcActionsDefined = isTruthyString(drefResponse?.icrc?.trim());
193207

@@ -209,11 +223,15 @@ export function Component() {
209223
const needsIdentifiedDefined = isDefined(drefResponse)
210224
&& isDefined(drefResponse.needs_identified)
211225
&& drefResponse.needs_identified.length > 0
212-
&& isDefined(sortedIdentifiedNeedsAndGaps);
226+
&& isDefined(needsIdentified);
227+
228+
const assessmentReportDefined = isDefined(drefResponse)
229+
&& isDefined(drefResponse.assessment_report_details)
230+
&& isDefined(drefResponse.assessment_report_details.file);
213231

214232
const showNeedsIdentifiedSection = isDefined(drefResponse)
215233
&& drefResponse.type_of_dref !== DREF_TYPE_ASSESSMENT
216-
&& (identifiedGapsDefined || needsIdentifiedDefined);
234+
&& (identifiedGapsDefined || needsIdentifiedDefined || assessmentReportDefined);
217235

218236
const operationObjectiveDefined = isTruthyString(drefResponse?.operation_objective?.trim());
219237
const responseStrategyDefined = isTruthyString(drefResponse?.response_strategy?.trim());
@@ -242,7 +260,7 @@ export function Component() {
242260
const plannedInterventionDefined = isDefined(drefResponse)
243261
&& isDefined(drefResponse.planned_interventions)
244262
&& drefResponse.planned_interventions.length > 0
245-
&& isDefined(sortedPlannedInterventions);
263+
&& isDefined(plannedInterventions);
246264

247265
const humanResourceDefined = isTruthyString(drefResponse?.human_resource?.trim());
248266
const surgePersonnelDeployedDefined = isTruthyString(
@@ -409,8 +427,6 @@ export function Component() {
409427
className={styles.metaItem}
410428
label={strings.operationStartDateLabel}
411429
value={drefResponse?.date_of_approval}
412-
valueType="date"
413-
format="dd-MM-yyyy"
414430
strongValue
415431
/>
416432
<TextOutput
@@ -426,14 +442,12 @@ export function Component() {
426442
label={strings.operationEndDateLabel}
427443
value={drefResponse?.end_date}
428444
valueType="date"
429-
format="dd-MM-yyyy"
430445
strongValue
431446
/>
432447
<TextOutput
433448
className={styles.metaItem}
434449
label={strings.drefPublishedLabel}
435450
value={drefResponse?.publishing_date}
436-
format="dd-MM-yyyy"
437451
valueType="date"
438452
strongValue
439453
/>
@@ -490,7 +504,6 @@ export function Component() {
490504
>
491505
<DateOutput
492506
value={drefResponse?.event_date}
493-
format="dd-MM-yyyy"
494507
/>
495508
</Container>
496509
)}
@@ -537,13 +550,15 @@ export function Component() {
537550
</Container>
538551
)}
539552
{drefResponse?.supporting_document_details?.file && (
540-
<Link
541-
href={drefResponse?.supporting_document_details?.file}
542-
external
543-
withUnderline
544-
>
545-
{strings.drefApplicationSupportingDocumentation}
546-
</Link>
553+
<Container>
554+
<Link
555+
href={drefResponse?.supporting_document_details?.file}
556+
external
557+
withUnderline
558+
>
559+
{strings.drefApplicationSupportingDocumentation}
560+
</Link>
561+
</Container>
547562
)}
548563
{sourceInformationDefined && (
549564
<Container
@@ -650,14 +665,13 @@ export function Component() {
650665
>
651666
<DateOutput
652667
value={drefResponse?.ns_respond_date}
653-
format="dd-MM-yyyy"
654668
/>
655669
</Container>
656670
)}
657671
<Container
658672
childrenContainerClassName={styles.nsActionsContent}
659673
>
660-
{sortedNsActions?.map(
674+
{nsActions?.map(
661675
(nsAction) => (
662676
<BlockTextOutput
663677
key={nsAction.id}
@@ -753,7 +767,7 @@ export function Component() {
753767
<Heading level={2}>
754768
{strings.needsIdentifiedSectionHeading}
755769
</Heading>
756-
{needsIdentifiedDefined && sortedIdentifiedNeedsAndGaps?.map(
770+
{needsIdentifiedDefined && needsIdentified?.map(
757771
(identifiedNeed) => (
758772
<Fragment key={identifiedNeed.id}>
759773
<Heading className={styles.needsIdentifiedHeading}>
@@ -777,13 +791,17 @@ export function Component() {
777791
</DescriptionText>
778792
</Container>
779793
)}
780-
<Link
781-
href={drefResponse?.assessment_report_details?.file}
782-
external
783-
withUnderline
784-
>
785-
{strings.drefAssessmentReportLink}
786-
</Link>
794+
{assessmentReportDefined && (
795+
<Container>
796+
<Link
797+
href={drefResponse?.assessment_report_details?.file}
798+
external
799+
withUnderline
800+
>
801+
{strings.drefAssessmentReportLink}
802+
</Link>
803+
</Container>
804+
)}
787805
</>
788806
)}
789807
{showOperationStrategySection && (
@@ -976,7 +994,7 @@ export function Component() {
976994
<Heading level={2}>
977995
{strings.plannedInterventionSectionHeading}
978996
</Heading>
979-
{sortedPlannedInterventions?.map((plannedIntervention) => (
997+
{plannedInterventions?.map((plannedIntervention) => (
980998
<Fragment key={plannedIntervention.id}>
981999
<Heading className={styles.plannedInterventionHeading}>
9821000
<img

0 commit comments

Comments
 (0)