-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
donations/grid: Allow admins to edit billingEmail (#1461)
* donations/grid: Allow admins to edit billingEmail Currently billingEmail is used to link all anonymous donations to a specific user profile. Unfortunately when user donates via bank transaction, and prefers the donation to be anonymous, there is no way to link that donation to user's profile. This commits allows billingEmail to be either set or edited manually, thus linking anonymous donations to user's profile * RendedEditBillingEmailCell.tsx: Rename the default function
- Loading branch information
1 parent
30d7062
commit 62e12ce
Showing
3 changed files
with
152 additions
and
2 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
111 changes: 111 additions & 0 deletions
111
src/components/admin/donations/grid/RenderEditBillingEmailCell.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,111 @@ | ||
import React from 'react' | ||
import { AxiosError, AxiosResponse } from 'axios' | ||
import { useMutation } from '@tanstack/react-query' | ||
import { useTranslation } from 'next-i18next' | ||
import { GridRenderEditCellParams } from '@mui/x-data-grid' | ||
import { TextField, Tooltip, Box } from '@mui/material' | ||
import Autocomplete from '@mui/material/Autocomplete' | ||
import { Save } from '@mui/icons-material' | ||
import { PersonResponse } from 'gql/person' | ||
import { DonationResponse, UserDonationInput } from 'gql/donations' | ||
import { useEditDonation } from 'service/donation' | ||
import { ApiErrors } from 'service/apiErrors' | ||
import { AlertStore } from 'stores/AlertStore' | ||
|
||
interface RenderEditCellProps { | ||
params: GridRenderEditCellParams | ||
personList?: PersonResponse[] | ||
onUpdate(): void | ||
} | ||
const addIconStyles = { | ||
background: '#4ac3ff', | ||
borderRadius: '50%', | ||
cursor: 'pointer', | ||
padding: 0.7, | ||
boxShadow: 3, | ||
} | ||
|
||
export default function RenderEditBillingEmailCell({ | ||
params, | ||
personList, | ||
onUpdate, | ||
}: RenderEditCellProps) { | ||
const { t } = useTranslation() | ||
|
||
const initialPerson = { | ||
firstName: params.row.billingEmail, | ||
lastName: '', | ||
email: params.row.email || params.row.billingEmail || null, | ||
} | ||
const [person, setPerson] = React.useState<PersonResponse | null>({ | ||
...initialPerson, | ||
} as PersonResponse) | ||
const mutationFn = useEditDonation(params.row.id) | ||
|
||
const mutation = useMutation< | ||
AxiosResponse<DonationResponse>, | ||
AxiosError<ApiErrors>, | ||
UserDonationInput | ||
>({ | ||
mutationFn, | ||
onError: () => AlertStore.show(t('donations:alerts.error'), 'error'), | ||
onSuccess: () => { | ||
AlertStore.show(t('donations:alerts.editDonor'), 'success') | ||
onUpdate() | ||
params.api.stopCellEditMode({ id: params.row.id, field: params.field }) | ||
}, | ||
}) | ||
|
||
const onClick = () => { | ||
if (person) { | ||
const donationData: UserDonationInput = params.row | ||
donationData.billingEmail = person.email | ||
mutation.mutate(donationData) | ||
} else { | ||
AlertStore.show(t('donations:alerts.requiredError'), 'error') | ||
} | ||
} | ||
|
||
return ( | ||
<Box | ||
sx={{ display: 'flex', alignItems: 'center', width: '100%', padding: 0.7, overflow: 'auto' }}> | ||
<Autocomplete | ||
id="edit-person-cell" | ||
sx={{ width: 300 }} | ||
value={person} | ||
ListboxProps={{ style: { maxHeight: 300 } }} | ||
onChange={(event, newValue: PersonResponse | null) => { | ||
setPerson(newValue) | ||
}} | ||
options={personList || []} | ||
getOptionLabel={(option: PersonResponse) => `${option.email}`} | ||
renderInput={(params) => <TextField {...params} variant="standard" fullWidth />} | ||
renderOption={(params, option: PersonResponse) => ( | ||
<Box component="li" {...params} key={option.id}> | ||
{`${option.firstName} ${option.lastName} (${option.email ? option.email : ''})`} | ||
</Box> | ||
)} | ||
isOptionEqualToValue={(option, value) => option.email === value.email} | ||
filterOptions={(options, state) => { | ||
const displayOptions = options.filter( | ||
(option) => | ||
option.firstName | ||
.toLowerCase() | ||
.trim() | ||
.includes(state.inputValue.toLowerCase().trim()) || | ||
option.email.toLowerCase().trim().includes(state.inputValue.toLowerCase().trim()), | ||
) | ||
|
||
return displayOptions | ||
}} | ||
clearText={t('donations:cta.clear')} | ||
noOptionsText={t('donations:noOptions')} | ||
openText={t('donations:cta.open')} | ||
closeText={t('donations:cta.close')} | ||
/> | ||
<Tooltip title={t('donations:cta.save')}> | ||
<Save sx={addIconStyles} color="action" fontSize="medium" onClick={onClick} /> | ||
</Tooltip> | ||
</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