import React, { useState } from 'react';
|
import {
|
useUpdateMany,
|
useRefresh,
|
useNotify,
|
useListContext,
|
useUnselectAll,
|
Button as RaButton,
|
Form,
|
ReferenceInput,
|
AutocompleteInput,
|
NumberInput,
|
TextInput,
|
useTranslate,
|
useResourceContext,
|
} from 'react-admin';
|
import { Dialog, DialogActions, DialogContent, DialogTitle, Grid, Stack, Divider } from '@mui/material';
|
import UpdateIcon from '@mui/icons-material/Update';
|
import ContentSave from '@mui/icons-material/Save';
|
import CloseIcon from '@mui/icons-material/Close';
|
|
const BulkUpdateButton = ({ label = 'ra.action.update', children }) => {
|
const [open, setOpen] = useState(false);
|
|
const refresh = useRefresh();
|
const notify = useNotify();
|
const translate = useTranslate();
|
const resource = useResourceContext();
|
const unselectAll = useUnselectAll(resource);
|
const { selectedIds } = useListContext();
|
|
const handleOpen = () => setOpen(true);
|
const handleClose = () => setOpen(false);
|
|
const [updateMany, { loading }] = useUpdateMany();
|
|
const handleSubmit = (data) => {
|
delete data['memoWrap'];
|
const filteredData = Object.fromEntries(
|
Object.entries(data).filter(([key, value]) => value !== null && value !== undefined)
|
);
|
if (Object.keys(filteredData).length > 0) {
|
updateMany(
|
resource,
|
{
|
ids: selectedIds,
|
data: filteredData,
|
},
|
{
|
onSuccess: () => {
|
setOpen(false);
|
refresh();
|
notify('common.response.success', { type: 'success' });
|
unselectAll();
|
},
|
onError: (error) => {
|
notify(error.message || 'common.response.fail', { type: 'error' });
|
},
|
}
|
);
|
} else {
|
notify('common.response.fail', { type: 'warning' });
|
}
|
};
|
|
return (
|
<>
|
<RaButton onClick={handleOpen} startIcon={<UpdateIcon />} label={label} />
|
<Dialog open={open} onClose={handleClose}>
|
<Form onSubmit={handleSubmit}>
|
<DialogTitle>{translate('ra.action.update')}</DialogTitle>
|
<DialogContent>
|
{children}
|
</DialogContent>
|
<Divider sx={{ mb: 1 }} />
|
<DialogActions sx={{ mb: 1 }}>
|
<RaButton
|
onClick={handleClose}
|
variant="contained"
|
color="inherit"
|
size='large'
|
label='ra.action.cancel'
|
aria-label={false}
|
sx={{ mr: 1 }}
|
>
|
<CloseIcon />
|
</RaButton>
|
<RaButton
|
type="submit"
|
variant="contained"
|
color="primary"
|
size='large'
|
label='ra.action.save'
|
aria-label={false}
|
disabled={loading}
|
sx={{ mr: 1 }}
|
>
|
<ContentSave />
|
</RaButton>
|
</DialogActions>
|
</Form>
|
</Dialog >
|
</>
|
);
|
};
|
|
export default BulkUpdateButton;
|