Skip to content

Commit

Permalink
Add sorted NS actions and planned interventions in all DREF export
Browse files Browse the repository at this point in the history
  • Loading branch information
puranban committed Dec 11, 2023
1 parent 7197fb6 commit 77033c9
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 72 deletions.
153 changes: 92 additions & 61 deletions src/views/DrefApplicationExport/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
_cs,
isDefined,
isFalsyString,
isNotDefined,
isTruthyString,
} from '@togglecorp/fujs';

Expand All @@ -14,7 +15,6 @@ import Heading from '#components/printable/Heading';
import DescriptionText from '#components/printable/DescriptionText';
import Link from '#components/Link';
import DateOutput from '#components/DateOutput';
import { components } from '#generated/types';
import useTranslation from '#hooks/useTranslation';
import { useRequest } from '#utils/restRequest';
import {
Expand All @@ -25,6 +25,7 @@ import {
DREF_TYPE_IMMINENT,
DisasterCategory,
} from '#utils/constants';
import { components } from '#generated/types';

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

Expand Down Expand Up @@ -52,57 +53,57 @@ const colorMap: Record<DisasterCategory, string> = {
[DISASTER_CATEGORY_RED]: styles.red,
};

const plannedInterventionSortedList = [
'shelter_housing_and_settlements',
'livelihoods_and_basic_needs',
'multi-purpose_cash',
'health',
'water_sanitation_and_hygiene',
'protection_gender_and_inclusion',
'education',
'migration',
'risk_reduction_climate_adaptation_and_recovery_',
'community_engagement_and_accountability',
'environmental_sustainability',
'coordination_and_partnerships',
'secretariat_services',
'national_society_strengthening',
] satisfies (PlannedIntervention['title'])[];
const plannedInterventionSortedList: Record<NonNullable<PlannedIntervention['title']>, number> = {
shelter_housing_and_settlements: 1,
livelihoods_and_basic_needs: 2,
multi_purpose_cash: 3,
health: 4,
water_sanitation_and_hygiene: 5,
protection_gender_and_inclusion: 6,
education: 7,
migration_and_displacement: 8,
risk_reduction_climate_adaptation_and_recovery: 9,
community_engagement_and_accountability: 10,
environmental_sustainability: 11,
coordination_and_partnerships: 12,
secretariat_services: 13,
national_society_strengthening: 14,
};

const identifiedNeedsAndGapsSortedList = [
'shelter_housing_and_settlements',
'livelihoods_and_basic_needs',
'multi_purpose_cash_grants',
'health',
'water_sanitation_and_hygiene',
'protection_gender_and_inclusion',
'education',
'migration',
'risk_reduction_climate_adaptation_and_recovery',
'community_engagement_and _accountability',
'environment_sustainability ',
] satisfies (IdentifiedNeedsAndGaps['title'])[];
const identifiedNeedsAndGapsSortedList: Record<NonNullable<IdentifiedNeedsAndGaps['title']>, number> = {
shelter_housing_and_settlements: 1,
livelihoods_and_basic_needs: 2,
multi_purpose_cash_grants: 3,
health: 4,
water_sanitation_and_hygiene: 5,
protection_gender_and_inclusion: 6,
education: 7,
migration_and_displacement: 8,
risk_reduction_climate_adaptation_and_recovery: 9,
community_engagement_and_accountability: 10,
environment_sustainability: 11,
};

const nsActionsSortedList = [
'shelter_housing_and_settlements',
'livelihoods_and_basic_needs',
'multi-purpose_cash',
'health',
'water_sanitation_and_hygiene',
'protection_gender_and_inclusion',
'education',
'migration',
'risk_reduction_climate_adaptation_and_recovery',
'community_engagement_and _accountability',
'environment_sustainability ',
'coordination',
'national_society_readiness',
'assessment',
'resource_mobilization',
'activation_of_contingency_plans',
'national_society_eoc',
'other',
] satisfies (NsActions['title'])[];
const nsActionsSortedList: Record<NsActions['title'], number> = {
shelter_housing_and_settlements: 1,
livelihoods_and_basic_needs: 2,
multi_purpose_cash: 3,
health: 4,
water_sanitation_and_hygiene: 5,
protection_gender_and_inclusion: 6,
education: 7,
migration_and_displacement: 8,
risk_reduction_climate_adaptation_and_recovery: 9,
community_engagement_and_accountability: 10,
environment_sustainability: 11,
coordination: 12,
national_society_readiness: 13,
assessment: 14,
resource_mobilization: 15,
activation_of_contingency_plans: 16,
national_society_eoc: 17,
other: 18,
};

// eslint-disable-next-line import/prefer-default-export
export function Component() {
Expand Down Expand Up @@ -153,28 +154,58 @@ export function Component() {
},
});

const filteredPlannedIntervention = useMemo(
() => drefResponse?.planned_interventions?.map((d) => {
if (isNotDefined(d.title)) {
return undefined;
}
return { ...d, title: d.title };
}).filter(isDefined),
[drefResponse?.planned_interventions],
);

const filteredIdentifiedNeedsAndGaps = useMemo(
() => drefResponse?.needs_identified?.map((d) => {
if (isNotDefined(d.title)) {
return undefined;
}
return { ...d, title: d.title };
}).filter(isDefined),
[drefResponse?.needs_identified],
);

const filteredNsActions = useMemo(
() => drefResponse?.national_society_actions?.map((d) => {
if (isNotDefined(d.title)) {
return undefined;
}
return { ...d, title: d.title };
}).filter(isDefined),
[drefResponse?.national_society_actions],
);

const sortedPlannedInterventions = useMemo(
() => drefResponse?.planned_interventions?.sort((a, b) => (
() => filteredPlannedIntervention?.sort(
// eslint-disable-next-line max-len
plannedInterventionSortedList.indexOf(a.title) - plannedInterventionSortedList.indexOf(b.title)
)),
[drefResponse?.planned_interventions],
(a, b) => plannedInterventionSortedList[a.title] - plannedInterventionSortedList[b.title],
),
[filteredPlannedIntervention],
);

const sortedIdentifiedNeedsAndGaps = useMemo(
() => drefResponse?.needs_identified?.sort((a, b) => (
() => filteredIdentifiedNeedsAndGaps?.sort(
// eslint-disable-next-line max-len
identifiedNeedsAndGapsSortedList.indexOf(a.title) - identifiedNeedsAndGapsSortedList.indexOf(b.title)
)),
[drefResponse?.needs_identified],
(a, b) => identifiedNeedsAndGapsSortedList[a.title] - identifiedNeedsAndGapsSortedList[b.title],
),
[filteredIdentifiedNeedsAndGaps],
);

const sortedNsActions = useMemo(
() => drefResponse?.national_society_actions?.sort((a, b) => (
() => filteredNsActions?.sort((a, b) => (
// eslint-disable-next-line max-len
nsActionsSortedList.indexOf(a.title) - nsActionsSortedList.indexOf(b.title)
nsActionsSortedList[a.title] - nsActionsSortedList[b.title]
)),
[drefResponse?.national_society_actions],
[filteredNsActions],
);

const eventDescriptionDefined = isTruthyString(drefResponse?.event_description?.trim());
Expand Down
78 changes: 75 additions & 3 deletions src/views/DrefFinalReportExport/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Fragment, useState } from 'react';
import { Fragment, useMemo, useState } from 'react';
import { useParams } from 'react-router-dom';
import {
_cs,
isDefined,
isFalsyString,
isNotDefined,
isTruthyString,
} from '@togglecorp/fujs';

Expand All @@ -25,12 +26,16 @@ import {
DREF_TYPE_IMMINENT,
DisasterCategory,
} from '#utils/constants';
import { components } from '#generated/types';

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

import i18n from './i18n.json';
import styles from './styles.module.css';

type PlannedIntervention = components<'read'>['schemas']['PlannedIntervention'];
type IdentifiedNeedsAndGaps = components<'read'>['schemas']['IdentifiedNeed'];

function BlockTextOutput(props: TextOutputProps & { variant?: never, withoutLabelColon?: never }) {
return (
<TextOutput
Expand All @@ -48,6 +53,37 @@ const colorMap: Record<DisasterCategory, string> = {
[DISASTER_CATEGORY_RED]: styles.red,
};

const plannedInterventionSortedList: Record<NonNullable<PlannedIntervention['title']>, number> = {
shelter_housing_and_settlements: 1,
livelihoods_and_basic_needs: 2,
multi_purpose_cash: 3,
health: 4,
water_sanitation_and_hygiene: 5,
protection_gender_and_inclusion: 6,
education: 7,
migration_and_displacement: 8,
risk_reduction_climate_adaptation_and_recovery: 9,
community_engagement_and_accountability: 10,
environmental_sustainability: 11,
coordination_and_partnerships: 12,
secretariat_services: 13,
national_society_strengthening: 14,
};

const identifiedNeedsAndGapsSortedList: Record<NonNullable<IdentifiedNeedsAndGaps['title']>, number> = {
shelter_housing_and_settlements: 1,
livelihoods_and_basic_needs: 2,
multi_purpose_cash_grants: 3,
health: 4,
water_sanitation_and_hygiene: 5,
protection_gender_and_inclusion: 6,
education: 7,
migration_and_displacement: 8,
risk_reduction_climate_adaptation_and_recovery: 9,
community_engagement_and_accountability: 10,
environment_sustainability: 11,
};

// eslint-disable-next-line import/prefer-default-export
export function Component() {
const { finalReportId } = useParams<{ finalReportId: string }>();
Expand Down Expand Up @@ -96,6 +132,42 @@ export function Component() {
},
});

const filteredPlannedIntervention = useMemo(
() => drefResponse?.planned_interventions?.map((d) => {
if (isNotDefined(d.title)) {
return undefined;
}
return { ...d, title: d.title };
}).filter(isDefined),
[drefResponse?.planned_interventions],
);

const filteredIdentifiedNeedsAndGaps = useMemo(
() => drefResponse?.needs_identified?.map((d) => {
if (isNotDefined(d.title)) {
return undefined;
}
return { ...d, title: d.title };
}).filter(isDefined),
[drefResponse?.needs_identified],
);

const sortedPlannedInterventions = useMemo(
() => filteredPlannedIntervention?.sort(
// eslint-disable-next-line max-len
(a, b) => plannedInterventionSortedList[a.title] - plannedInterventionSortedList[b.title],
),
[filteredPlannedIntervention],
);

const sortedIdentifiedNeedsAndGaps = useMemo(
() => filteredIdentifiedNeedsAndGaps?.sort(
// eslint-disable-next-line max-len
(a, b) => identifiedNeedsAndGapsSortedList[a.title] - identifiedNeedsAndGapsSortedList[b.title],
),
[filteredIdentifiedNeedsAndGaps],
);

const showMainDonorsSection = isTruthyString(drefResponse?.main_donors?.trim());
const eventDescriptionDefined = isTruthyString(drefResponse?.event_description?.trim());
const eventScopeDefined = drefResponse?.type_of_dref !== DREF_TYPE_ASSESSMENT
Expand Down Expand Up @@ -490,7 +562,7 @@ export function Component() {
<Heading level={2}>
{strings.needsIdentifiedSectionHeading}
</Heading>
{needsIdentifiedDefined && drefResponse?.needs_identified?.map(
{needsIdentifiedDefined && sortedIdentifiedNeedsAndGaps?.map(
(identifiedNeed) => (
<Fragment key={identifiedNeed.id}>
<Heading className={styles.needsIdentifiedHeading}>
Expand Down Expand Up @@ -688,7 +760,7 @@ export function Component() {
<Heading level={2}>
{strings.interventionSectionHeading}
</Heading>
{drefResponse?.planned_interventions?.map(
{sortedPlannedInterventions?.map(
(plannedIntervention) => (
<Fragment key={plannedIntervention.id}>
<Heading className={styles.plannedInterventionHeading}>
Expand Down
Loading

0 comments on commit 77033c9

Please sign in to comment.