Skip to content

Commit 6263946

Browse files
puranbansamshara
authored andcommitted
Add sorted NS actions and planned interventions in all DREF export
1 parent 3a7f384 commit 6263946

File tree

3 files changed

+283
-69
lines changed

3 files changed

+283
-69
lines changed

src/views/DrefApplicationExport/index.tsx

Lines changed: 92 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
_cs,
55
isDefined,
66
isFalsyString,
7+
isNotDefined,
78
isTruthyString,
89
} from '@togglecorp/fujs';
910

@@ -14,7 +15,6 @@ import Heading from '#components/printable/Heading';
1415
import DescriptionText from '#components/printable/DescriptionText';
1516
import Link from '#components/Link';
1617
import DateOutput from '#components/DateOutput';
17-
import { components } from '#generated/types';
1818
import useTranslation from '#hooks/useTranslation';
1919
import { useRequest } from '#utils/restRequest';
2020
import {
@@ -25,7 +25,7 @@ import {
2525
DREF_TYPE_IMMINENT,
2626
DisasterCategory,
2727
} from '#utils/constants';
28-
28+
import { components } from '#generated/types';
2929
import ifrcLogo from '#assets/icons/ifrc-square.png';
3030

3131
import i18n from './i18n.json';
@@ -52,57 +52,57 @@ const colorMap: Record<DisasterCategory, string> = {
5252
[DISASTER_CATEGORY_RED]: styles.red,
5353
};
5454

55-
const plannedInterventionSortedList = [
56-
'shelter_housing_and_settlements',
57-
'livelihoods_and_basic_needs',
58-
'multi-purpose_cash',
59-
'health',
60-
'water_sanitation_and_hygiene',
61-
'protection_gender_and_inclusion',
62-
'education',
63-
'migration',
64-
'risk_reduction_climate_adaptation_and_recovery_',
65-
'community_engagement_and_accountability',
66-
'environmental_sustainability',
67-
'coordination_and_partnerships',
68-
'secretariat_services',
69-
'national_society_strengthening',
70-
] satisfies (PlannedIntervention['title'])[];
55+
const plannedInterventionSortedList: Record<NonNullable<PlannedIntervention['title']>, number> = {
56+
shelter_housing_and_settlements: 1,
57+
livelihoods_and_basic_needs: 2,
58+
multi_purpose_cash: 3,
59+
health: 4,
60+
water_sanitation_and_hygiene: 5,
61+
protection_gender_and_inclusion: 6,
62+
education: 7,
63+
migration_and_displacement: 8,
64+
risk_reduction_climate_adaptation_and_recovery: 9,
65+
community_engagement_and_accountability: 10,
66+
environmental_sustainability: 11,
67+
coordination_and_partnerships: 12,
68+
secretariat_services: 13,
69+
national_society_strengthening: 14,
70+
};
7171

72-
const identifiedNeedsAndGapsSortedList = [
73-
'shelter_housing_and_settlements',
74-
'livelihoods_and_basic_needs',
75-
'multi_purpose_cash_grants',
76-
'health',
77-
'water_sanitation_and_hygiene',
78-
'protection_gender_and_inclusion',
79-
'education',
80-
'migration',
81-
'risk_reduction_climate_adaptation_and_recovery',
82-
'community_engagement_and _accountability',
83-
'environment_sustainability ',
84-
] satisfies (IdentifiedNeedsAndGaps['title'])[];
72+
const identifiedNeedsAndGapsSortedList: Record<NonNullable<IdentifiedNeedsAndGaps['title']>, number> = {
73+
shelter_housing_and_settlements: 1,
74+
livelihoods_and_basic_needs: 2,
75+
multi_purpose_cash_grants: 3,
76+
health: 4,
77+
water_sanitation_and_hygiene: 5,
78+
protection_gender_and_inclusion: 6,
79+
education: 7,
80+
migration_and_displacement: 8,
81+
risk_reduction_climate_adaptation_and_recovery: 9,
82+
community_engagement_and_accountability: 10,
83+
environment_sustainability: 11,
84+
};
8585

86-
const nsActionsSortedList = [
87-
'shelter_housing_and_settlements',
88-
'livelihoods_and_basic_needs',
89-
'multi-purpose_cash',
90-
'health',
91-
'water_sanitation_and_hygiene',
92-
'protection_gender_and_inclusion',
93-
'education',
94-
'migration',
95-
'risk_reduction_climate_adaptation_and_recovery',
96-
'community_engagement_and _accountability',
97-
'environment_sustainability ',
98-
'coordination',
99-
'national_society_readiness',
100-
'assessment',
101-
'resource_mobilization',
102-
'activation_of_contingency_plans',
103-
'national_society_eoc',
104-
'other',
105-
] satisfies (NsActions['title'])[];
86+
const nsActionsSortedList: Record<NsActions['title'], number> = {
87+
shelter_housing_and_settlements: 1,
88+
livelihoods_and_basic_needs: 2,
89+
multi_purpose_cash: 3,
90+
health: 4,
91+
water_sanitation_and_hygiene: 5,
92+
protection_gender_and_inclusion: 6,
93+
education: 7,
94+
migration_and_displacement: 8,
95+
risk_reduction_climate_adaptation_and_recovery: 9,
96+
community_engagement_and_accountability: 10,
97+
environment_sustainability: 11,
98+
coordination: 12,
99+
national_society_readiness: 13,
100+
assessment: 14,
101+
resource_mobilization: 15,
102+
activation_of_contingency_plans: 16,
103+
national_society_eoc: 17,
104+
other: 18,
105+
};
106106

107107
// eslint-disable-next-line import/prefer-default-export
108108
export function Component() {
@@ -153,28 +153,58 @@ export function Component() {
153153
},
154154
});
155155

156+
const filteredPlannedIntervention = useMemo(
157+
() => drefResponse?.planned_interventions?.map((intervention) => {
158+
if (isNotDefined(intervention.title)) {
159+
return undefined;
160+
}
161+
return { ...intervention, title: intervention.title };
162+
}).filter(isDefined),
163+
[drefResponse?.planned_interventions],
164+
);
165+
166+
const filteredIdentifiedNeedsAndGaps = useMemo(
167+
() => drefResponse?.needs_identified?.map((need) => {
168+
if (isNotDefined(need.title)) {
169+
return undefined;
170+
}
171+
return { ...need, title: need.title };
172+
}).filter(isDefined),
173+
[drefResponse?.needs_identified],
174+
);
175+
176+
const filteredNsActions = useMemo(
177+
() => drefResponse?.national_society_actions?.map((nsAction) => {
178+
if (isNotDefined(nsAction.title)) {
179+
return undefined;
180+
}
181+
return { ...nsAction, title: nsAction.title };
182+
}).filter(isDefined),
183+
[drefResponse?.national_society_actions],
184+
);
185+
156186
const sortedPlannedInterventions = useMemo(
157-
() => drefResponse?.planned_interventions?.sort((a, b) => (
187+
() => filteredPlannedIntervention?.sort(
158188
// eslint-disable-next-line max-len
159-
plannedInterventionSortedList.indexOf(a.title) - plannedInterventionSortedList.indexOf(b.title)
160-
)),
161-
[drefResponse?.planned_interventions],
189+
(a, b) => plannedInterventionSortedList[a.title] - plannedInterventionSortedList[b.title],
190+
),
191+
[filteredPlannedIntervention],
162192
);
163193

164194
const sortedIdentifiedNeedsAndGaps = useMemo(
165-
() => drefResponse?.needs_identified?.sort((a, b) => (
195+
() => filteredIdentifiedNeedsAndGaps?.sort(
166196
// eslint-disable-next-line max-len
167-
identifiedNeedsAndGapsSortedList.indexOf(a.title) - identifiedNeedsAndGapsSortedList.indexOf(b.title)
168-
)),
169-
[drefResponse?.needs_identified],
197+
(a, b) => identifiedNeedsAndGapsSortedList[a.title] - identifiedNeedsAndGapsSortedList[b.title],
198+
),
199+
[filteredIdentifiedNeedsAndGaps],
170200
);
171201

172202
const sortedNsActions = useMemo(
173-
() => drefResponse?.national_society_actions?.sort((a, b) => (
203+
() => filteredNsActions?.sort((a, b) => (
174204
// eslint-disable-next-line max-len
175-
nsActionsSortedList.indexOf(a.title) - nsActionsSortedList.indexOf(b.title)
205+
nsActionsSortedList[a.title] - nsActionsSortedList[b.title]
176206
)),
177-
[drefResponse?.national_society_actions],
207+
[filteredNsActions],
178208
);
179209

180210
const eventDescriptionDefined = isTruthyString(drefResponse?.event_description?.trim());

src/views/DrefFinalReportExport/index.tsx

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Fragment, useState } from 'react';
1+
import { Fragment, useMemo, useState } from 'react';
22
import { useParams } from 'react-router-dom';
33
import {
44
_cs,
55
isDefined,
66
isFalsyString,
7+
isNotDefined,
78
isTruthyString,
89
} from '@togglecorp/fujs';
910

@@ -25,12 +26,16 @@ import {
2526
DREF_TYPE_IMMINENT,
2627
DisasterCategory,
2728
} from '#utils/constants';
29+
import { components } from '#generated/types';
2830

2931
import ifrcLogo from '#assets/icons/ifrc-square.png';
3032

3133
import i18n from './i18n.json';
3234
import styles from './styles.module.css';
3335

36+
type PlannedIntervention = components<'read'>['schemas']['PlannedIntervention'];
37+
type IdentifiedNeedsAndGaps = components<'read'>['schemas']['IdentifiedNeed'];
38+
3439
function BlockTextOutput(props: TextOutputProps & { variant?: never, withoutLabelColon?: never }) {
3540
return (
3641
<TextOutput
@@ -48,6 +53,37 @@ const colorMap: Record<DisasterCategory, string> = {
4853
[DISASTER_CATEGORY_RED]: styles.red,
4954
};
5055

56+
const plannedInterventionSortedList: Record<NonNullable<PlannedIntervention['title']>, number> = {
57+
shelter_housing_and_settlements: 1,
58+
livelihoods_and_basic_needs: 2,
59+
multi_purpose_cash: 3,
60+
health: 4,
61+
water_sanitation_and_hygiene: 5,
62+
protection_gender_and_inclusion: 6,
63+
education: 7,
64+
migration_and_displacement: 8,
65+
risk_reduction_climate_adaptation_and_recovery: 9,
66+
community_engagement_and_accountability: 10,
67+
environmental_sustainability: 11,
68+
coordination_and_partnerships: 12,
69+
secretariat_services: 13,
70+
national_society_strengthening: 14,
71+
};
72+
73+
const identifiedNeedsAndGapsSortedList: Record<NonNullable<IdentifiedNeedsAndGaps['title']>, number> = {
74+
shelter_housing_and_settlements: 1,
75+
livelihoods_and_basic_needs: 2,
76+
multi_purpose_cash_grants: 3,
77+
health: 4,
78+
water_sanitation_and_hygiene: 5,
79+
protection_gender_and_inclusion: 6,
80+
education: 7,
81+
migration_and_displacement: 8,
82+
risk_reduction_climate_adaptation_and_recovery: 9,
83+
community_engagement_and_accountability: 10,
84+
environment_sustainability: 11,
85+
};
86+
5187
// eslint-disable-next-line import/prefer-default-export
5288
export function Component() {
5389
const { finalReportId } = useParams<{ finalReportId: string }>();
@@ -96,6 +132,42 @@ export function Component() {
96132
},
97133
});
98134

135+
const filteredPlannedIntervention = useMemo(
136+
() => drefResponse?.planned_interventions?.map((intervention) => {
137+
if (isNotDefined(intervention.title)) {
138+
return undefined;
139+
}
140+
return { ...intervention, title: intervention.title };
141+
}).filter(isDefined),
142+
[drefResponse?.planned_interventions],
143+
);
144+
145+
const filteredIdentifiedNeedsAndGaps = useMemo(
146+
() => drefResponse?.needs_identified?.map((need) => {
147+
if (isNotDefined(need.title)) {
148+
return undefined;
149+
}
150+
return { ...need, title: need.title };
151+
}).filter(isDefined),
152+
[drefResponse?.needs_identified],
153+
);
154+
155+
const sortedPlannedInterventions = useMemo(
156+
() => filteredPlannedIntervention?.sort(
157+
// eslint-disable-next-line max-len
158+
(a, b) => plannedInterventionSortedList[a.title] - plannedInterventionSortedList[b.title],
159+
),
160+
[filteredPlannedIntervention],
161+
);
162+
163+
const sortedIdentifiedNeedsAndGaps = useMemo(
164+
() => filteredIdentifiedNeedsAndGaps?.sort(
165+
// eslint-disable-next-line max-len
166+
(a, b) => identifiedNeedsAndGapsSortedList[a.title] - identifiedNeedsAndGapsSortedList[b.title],
167+
),
168+
[filteredIdentifiedNeedsAndGaps],
169+
);
170+
99171
const showMainDonorsSection = isTruthyString(drefResponse?.main_donors?.trim());
100172
const eventDescriptionDefined = isTruthyString(drefResponse?.event_description?.trim());
101173
const eventScopeDefined = drefResponse?.type_of_dref !== DREF_TYPE_ASSESSMENT
@@ -490,7 +562,7 @@ export function Component() {
490562
<Heading level={2}>
491563
{strings.needsIdentifiedSectionHeading}
492564
</Heading>
493-
{needsIdentifiedDefined && drefResponse?.needs_identified?.map(
565+
{needsIdentifiedDefined && sortedIdentifiedNeedsAndGaps?.map(
494566
(identifiedNeed) => (
495567
<Fragment key={identifiedNeed.id}>
496568
<Heading className={styles.needsIdentifiedHeading}>
@@ -688,7 +760,7 @@ export function Component() {
688760
<Heading level={2}>
689761
{strings.interventionSectionHeading}
690762
</Heading>
691-
{drefResponse?.planned_interventions?.map(
763+
{sortedPlannedInterventions?.map(
692764
(plannedIntervention) => (
693765
<Fragment key={plannedIntervention.id}>
694766
<Heading className={styles.plannedInterventionHeading}>

0 commit comments

Comments
 (0)