Skip to content

Commit

Permalink
Add filter component for threeW activities in country page
Browse files Browse the repository at this point in the history
  • Loading branch information
puranban committed Dec 5, 2023
1 parent 0ab2cdb commit 43b4791
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"namespace": "emergencyActivities",
"strings": {
"emergencyActivityFilterReportingNs": "National Society",
"emergencyActivityFilterEru": "ERU",
"emergencyActivityFilterSector": "Sector",
"emergencyActivityFilterStatus": "Status",
"emergencyActivityFilterCountry": "Country",
"emergencyActivityFilterDistrict": "Region/Province"
}
}
148 changes: 148 additions & 0 deletions src/views/CountryOngoingActivitiesThreeWActivities/Filters/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { useCallback, useState } from 'react';
import { _cs, isDefined, isNotDefined } from '@togglecorp/fujs';
import { EntriesAsList, type SetValueArg } from '@togglecorp/toggle-form';

import CountryMultiSelectInput from '#components/domain/CountryMultiSelectInput';

Check warning on line 5 in src/views/CountryOngoingActivitiesThreeWActivities/Filters/index.tsx

View workflow job for this annotation

GitHub Actions / Lint JS

'CountryMultiSelectInput' is defined but never used

Check warning on line 5 in src/views/CountryOngoingActivitiesThreeWActivities/Filters/index.tsx

View workflow job for this annotation

GitHub Actions / Build

'CountryMultiSelectInput' is defined but never used
import DistrictMultiCountrySearchMultiSelectInput, { type DistrictItem } from '#components/domain/DistrictMultiCountrySearchMultiSelectInput';
import MultiSelectInput from '#components/MultiSelectInput';
import NationalSocietyMultiSelectInput from '#components/domain/NationalSocietyMultiSelectInput';
import type { GoApiResponse } from '#utils/restRequest';
import useGlobalEnums from '#hooks/domain/useGlobalEnums';
import useTranslation from '#hooks/useTranslation';
import { numericIdSelector, stringTitleSelector, stringValueSelector } from '#utils/selectors';
import { useRequest } from '#utils/restRequest';

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

type DeploymentsEmergencyProjectStatus = NonNullable<GoApiResponse<'/api/v2/global-enums/'>['deployments_emergency_project_status']>[number];
export type FilterValue = Partial<{
reporting_ns: number[];
deployed_eru: number[];
sector: number[];
status: string[];
country: number[];
districts: number[];
}>

function emergencyProjectStatusSelector(option: DeploymentsEmergencyProjectStatus) {
return option.key;
}

interface Props {
className?: string;
value: FilterValue;
onChange: (value: SetValueArg<FilterValue>) => void;
disabled?: boolean;
}

function Filters(props: Props) {
const {
className,
value,
onChange,
disabled,
} = props;

const [districtOptions, setDistrictOptions] = useState<DistrictItem[] | null | undefined>();
const {
response: emergencyProjectOptions,
pending: emergencyProjectOptionsPending,
} = useRequest({
url: '/api/v2/emergency-project/options/',
preserveResponse: true,
});
const {
deployments_emergency_project_status: emergencyProjectStatusOptions,
} = useGlobalEnums();

const strings = useTranslation(i18n);

const handleInputChange = useCallback((...args: EntriesAsList<FilterValue>) => {
const [val, key] = args;
if (onChange) {
onChange((oldFilterValue) => {
const newFilterValue = {
...oldFilterValue,
[key]: val,
};

return newFilterValue;
});
}
}, [onChange]);

const handleCountryInputChange = useCallback(

Check warning on line 75 in src/views/CountryOngoingActivitiesThreeWActivities/Filters/index.tsx

View workflow job for this annotation

GitHub Actions / Lint JS

'handleCountryInputChange' is assigned a value but never used

Check warning on line 75 in src/views/CountryOngoingActivitiesThreeWActivities/Filters/index.tsx

View workflow job for this annotation

GitHub Actions / Build

'handleCountryInputChange' is assigned a value but never used
(newValue: number[]) => {
onChange((oldFilterValue) => {
const newFilterValue = {
...oldFilterValue,
country: newValue,
districts: [],
};

return newFilterValue;
});
},
[onChange],
);

return (
<div className={_cs(styles.filters, className)}>
<NationalSocietyMultiSelectInput
name="reporting_ns"
placeholder={strings.emergencyActivityFilterReportingNs}
value={value.reporting_ns}
onChange={handleInputChange}
disabled={disabled}
/>
<NationalSocietyMultiSelectInput
name="deployed_eru"
placeholder={strings.emergencyActivityFilterEru}
value={value.deployed_eru}
onChange={handleInputChange}
disabled={disabled}
/>
<MultiSelectInput
name="sector"
placeholder={strings.emergencyActivityFilterSector}
options={emergencyProjectOptions?.sectors}
value={value.sector}
keySelector={numericIdSelector}
labelSelector={stringTitleSelector}
onChange={handleInputChange}
disabled={disabled || emergencyProjectOptionsPending}
/>
<MultiSelectInput
name="status"
placeholder={strings.emergencyActivityFilterStatus}
options={emergencyProjectStatusOptions}
value={value.status}
keySelector={emergencyProjectStatusSelector}
labelSelector={stringValueSelector}
onChange={handleInputChange}
disabled={disabled}
/>
{/* <CountryMultiSelectInput
name="country"
placeholder={strings.emergencyActivityFilterCountry}
value={value.country}
onChange={handleCountryInputChange}
disabled={disabled}
/> */}
<DistrictMultiCountrySearchMultiSelectInput
placeholder={strings.emergencyActivityFilterDistrict}
name="districts"
disabled={isNotDefined(value.country) || disabled
|| (isDefined(value.country) && value.country.length < 1)}
countryIds={value?.country}
onChange={handleInputChange}
options={districtOptions}
onOptionsChange={setDistrictOptions}
value={value.districts}
/>
</div>
);
}

export default Filters;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.filters {
display: grid;
grid-gap: var(--go-ui-spacing-md);
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import {
stringTitleSelector,
} from '#utils/selectors';

import Filters, { type FilterValue } from '#views/EmergencyActivities/Filters';
import useEmergencyProjectStats, {
getPeopleReached,
} from '#views/EmergencyActivities/useEmergencyProjectStats';
Expand All @@ -52,6 +51,7 @@ import ActivityActions, {
} from '#views/EmergencyActivities/ActivityActions';

import ResponseActivitiesMap from './ResponseActivitiesMap';
import Filters, { type FilterValue } from './Filters';
import i18n from './i18n.json';
import styles from './styles.module.css';

Expand Down

0 comments on commit 43b4791

Please sign in to comment.