-
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
146 additions
and
2 deletions.
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" | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
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,126 @@ | ||
import { useCallback, useState } from 'react'; | ||
import { _cs, isNotDefined } from '@togglecorp/fujs'; | ||
import { EntriesAsList, type SetValueArg } from '@togglecorp/toggle-form'; | ||
|
||
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; | ||
countryId?: string; | ||
} | ||
|
||
function Filters(props: Props) { | ||
const { | ||
className, | ||
value, | ||
countryId, | ||
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]); | ||
|
||
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} | ||
/> | ||
<DistrictMultiCountrySearchMultiSelectInput | ||
placeholder={strings.emergencyActivityFilterDistrict} | ||
name="districts" | ||
disabled={isNotDefined(countryId) || disabled} | ||
countryIds={[Number(countryId)]} | ||
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