#
luxiaotao1123
2024-11-08 7820edd7c7e26f4858bbe7dec1bc30de9e1155a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React, { useState } from 'react';
import {
    useUpdateMany,
    useRefresh,
    useNotify,
    useListContext,
    useUnselectAll,
    Button as RaButton,
    Form,
    ReferenceInput,
    AutocompleteInput,
    NumberInput,
    TextInput,
    useTranslate,
} from 'react-admin';
import { Dialog, DialogActions, DialogContent, DialogTitle, Grid, Stack, Divider } from '@mui/material';
import UpdateIcon from '@mui/icons-material/Update';
import MemoInput from "../components/MemoInput";
import ContentSave from '@mui/icons-material/Save';
import CloseIcon from '@mui/icons-material/Close';
 
const BulkUpdateButton = ({ resource, label = 'ra.action.update', ...rest }) => {
    const [open, setOpen] = useState(false);
 
    const refresh = useRefresh();
    const notify = useNotify();
    const translate = useTranslate();
    const unselectAll = useUnselectAll(resource);
    const { selectedIds } = useListContext();
 
    const handleOpen = () => setOpen(true);
    const handleClose = () => setOpen(false);
 
    const [updateMany, { loading }] = useUpdateMany();
 
    const handleSubmit = (data) => {
        const filteredData = Object.fromEntries(
            Object.entries(data).filter(([key, value]) => value !== null && value !== undefined)
        );
        delete filteredData['memoWrap'];
        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>
                        <Grid container rowSpacing={2} columnSpacing={2}>
                            <Grid item xs={6} display="flex" gap={1}>
                                <ReferenceInput
                                    source="code"
                                    reference="code"
                                >
                                    <AutocompleteInput
                                        label="table.field.loc.code"
                                        optionText="data"
                                        filterToQuery={(val) => ({ data: val })}
                                    />
                                </ReferenceInput>
                            </Grid>
                            <Grid item xs={6} display="flex" gap={1}>
                                <ReferenceInput
                                    source="locSts"
                                    reference="locSts"
                                >
                                    <AutocompleteInput
                                        label="table.field.loc.locSts"
                                        optionText="name"
                                        filterToQuery={(val) => ({ name: val })}
                                    />
                                </ReferenceInput>
                            </Grid>
                            <Grid item xs={6} display="flex" gap={1}>
                                <NumberInput
                                    label="table.field.loc.offset"
                                    source="offset"
                                />
                            </Grid>
                            <Grid item xs={12} display="flex" gap={1}>
                                <Stack direction="column" spacing={1} width={'100%'}>
                                    <MemoInput />
                                </Stack>
                            </Grid>
                        </Grid>
                    </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;