-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add filter component for threeW activities in country page
- Loading branch information
Showing
4 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/views/CountryOngoingActivitiesThreeWActivities/Filters/i18n.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
148
src/views/CountryOngoingActivitiesThreeWActivities/Filters/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Lint JS
|
||
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 GitHub Actions / Lint JS
|
||
(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; |
5 changes: 5 additions & 0 deletions
5
src/views/CountryOngoingActivitiesThreeWActivities/Filters/styles.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters