-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add donations filters for Admin Panel (#1268)
* add donations filters add useStore hook add filter component refactor donations grid add the store provider update donationList api endpoint add donation store * add translations * update translations * add types for pagination data and filterData refactor makeObservable in donation store * update paginationData args * add small tweak on donationList endpoint function Co-authored-by: Andrey <[email protected]>
- Loading branch information
1 parent
10db695
commit be1580e
Showing
14 changed files
with
291 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
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,6 @@ | ||
import { useContext } from 'react' | ||
import { MobXProviderContext } from 'mobx-react' | ||
|
||
export const useStores = () => { | ||
return useContext(MobXProviderContext) | ||
} |
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
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,71 @@ | ||
import { useTranslation } from 'next-i18next' | ||
import { FormControl, InputLabel, MenuItem, Select, SelectChangeEvent } from '@mui/material' | ||
import { Close } from '@mui/icons-material' | ||
|
||
type Options = { | ||
name: string | ||
label: string | ||
} | ||
|
||
interface DropdownFilterProps { | ||
options: Options | ||
value: string | ||
onChange: (filterName: string, filterValue: string) => void | ||
menuItems: string[] | ||
} | ||
|
||
export default function DropdownFilter(props: DropdownFilterProps) { | ||
const { t } = useTranslation() | ||
const { | ||
options: { name, label }, | ||
value, | ||
onChange, | ||
menuItems, | ||
} = props | ||
|
||
const handleChange = (e: SelectChangeEvent) => { | ||
e.stopPropagation() | ||
const filterName = e.target.name as string | ||
const filterValue = e.target.value as string | ||
onChange(filterName, filterValue) | ||
} | ||
|
||
const handleClear = (event: React.MouseEvent, filterName: string, filterValue: string) => { | ||
event.stopPropagation() | ||
onChange(filterName, filterValue) | ||
} | ||
|
||
const selectElementStyle = { | ||
minWidth: 115, | ||
marginRight: 1, | ||
marginLeft: 1, | ||
} | ||
|
||
const closeIconStyle = { | ||
color: 'grey', | ||
cursor: 'pointer', | ||
} | ||
|
||
return ( | ||
<FormControl style={selectElementStyle}> | ||
<InputLabel size="small">{t(label)}</InputLabel> | ||
<Select | ||
startAdornment={ | ||
value ? <Close style={closeIconStyle} onClick={(e) => handleClear(e, name, '')} /> : null | ||
} | ||
name={name} | ||
value={value} | ||
label={t(label)} | ||
size="small" | ||
onChange={(e) => handleChange(e)}> | ||
{menuItems.map((key) => { | ||
return ( | ||
<MenuItem key={key} value={key}> | ||
{key} | ||
</MenuItem> | ||
) | ||
})} | ||
</Select> | ||
</FormControl> | ||
) | ||
} |
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
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,81 @@ | ||
import { Box, TextField } from '@mui/material' | ||
import Filter from './Filter' | ||
import { useStores } from './../../../common/hooks/useStores' | ||
import { observer } from 'mobx-react' | ||
import { DonationStatus, DonationType } from 'gql/donations.enums' | ||
import { DateTimePicker, enUS, LocalizationProvider } from '@mui/x-date-pickers' | ||
import { useTranslation } from 'react-i18next' | ||
import { bg } from 'date-fns/locale' | ||
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns' | ||
|
||
export default observer(function GridFilters() { | ||
const { donationStore } = useStores() | ||
const { t, i18n } = useTranslation() | ||
const donationStatusOptions = { | ||
name: 'status', | ||
label: 'donations:cta.status', | ||
} | ||
|
||
const donationStatusMenuItems = Object.values(DonationStatus) | ||
|
||
const donationTypeOptions = { | ||
name: 'type', | ||
label: 'donations:cta.type', | ||
} | ||
|
||
const donationTypeMenuItems = Object.values(DonationType) | ||
|
||
const handleChange = ( | ||
filterName: string, | ||
filterValue: string | null | { from: Date; to: Date }, | ||
) => { | ||
donationStore.setDonationFilters(filterName, filterValue) | ||
} | ||
|
||
const handleDatePick = ( | ||
date: Date | null | undefined | 'Invalid Date', | ||
column: 'from' | 'to', | ||
) => { | ||
if ((!!date && date?.toString() !== 'Invalid Date') || date === null) { | ||
donationStore.setDonationFilters('date', { | ||
...donationStore.donationFilters.date, | ||
[column]: date || '', | ||
}) | ||
} | ||
} | ||
|
||
return ( | ||
<Box sx={{ display: 'flex', justifyContent: 'center' }}> | ||
<LocalizationProvider | ||
adapterLocale={i18n.language === 'bg' ? bg : enUS} | ||
dateAdapter={AdapterDateFns}> | ||
<DateTimePicker | ||
label={t('donations:cta.from')} | ||
value={donationStore.donationFilters.date?.from || null} | ||
onChange={(date: Date | null | 'Invalid Date') => handleDatePick(date, 'from')} | ||
renderInput={(params) => <TextField size="small" sx={{ marginRight: 1 }} {...params} />} | ||
maxDate={donationStore.donationFilters.date?.to} | ||
/> | ||
<DateTimePicker | ||
label={t('donations:cta.to')} | ||
value={donationStore.donationFilters.date?.to || null} | ||
onChange={(date: Date | null | 'Invalid Date') => handleDatePick(date, 'to')} | ||
renderInput={(params) => <TextField size="small" sx={{ marginRight: 1 }} {...params} />} | ||
minDate={donationStore.donationFilters.date?.from} | ||
/> | ||
</LocalizationProvider> | ||
<Filter | ||
value={donationStore.donationFilters.status} | ||
options={donationStatusOptions} | ||
onChange={handleChange} | ||
menuItems={donationStatusMenuItems} | ||
/> | ||
<Filter | ||
value={donationStore.donationFilters.type} | ||
options={donationTypeOptions} | ||
onChange={handleChange} | ||
menuItems={donationTypeMenuItems} | ||
/> | ||
</Box> | ||
) | ||
}) |
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 |
---|---|---|
@@ -1 +1,12 @@ | ||
export type UUID = string | ||
|
||
export type PaginationData = { | ||
pageIndex: number | ||
pageSize: number | ||
} | ||
|
||
export type FilterData = { | ||
status: DonationStatus | ||
type: DonationType | ||
date: { from: Date | null; to: Date | null } | ||
} |
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
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
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
Oops, something went wrong.