zhou zhou
12 小时以前 287a666e1b2bb155e86aa88ebace201d1e8a51f6
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
import React from "react";
import {
    CreateBase,
    EditBase,
    SaveButton,
    SimpleForm,
    Toolbar,
    useNotify,
    useRefresh,
    useTranslate,
} from "react-admin";
import {
    Button,
    Dialog,
    DialogActions,
    DialogContent,
    DialogTitle,
} from "@mui/material";
 
const DialogFormToolbar = ({ onClose, translate }) => (
    <Toolbar sx={{ justifyContent: "space-between", px: 0 }}>
        <Button onClick={onClose}>{translate("ai.common.cancel")}</Button>
        <SaveButton />
    </Toolbar>
);
 
const AiConfigDialog = ({
    open,
    mode,
    title,
    resource,
    recordId,
    defaultValues,
    maxWidth = "md",
    onClose,
    children,
}) => {
    const notify = useNotify();
    const refresh = useRefresh();
    const translate = useTranslate();
 
    if (!open) {
        return null;
    }
 
    const handleSuccess = () => {
        notify(translate(mode === "create" ? "ai.common.saveSuccess" : "ai.common.updateSuccess"));
        refresh();
        onClose();
    };
 
    const handleError = (error) => {
        notify(error?.message || translate("ai.common.operationFailed"), { type: "error" });
    };
 
    const formContent = (
        <SimpleForm
            defaultValues={mode === "create" ? defaultValues : undefined}
            toolbar={mode === "show" ? false : <DialogFormToolbar onClose={onClose} translate={translate} />}
            sx={{
                "& .RaSimpleForm-form": {
                    maxWidth: "100%",
                },
            }}
        >
            {children}
        </SimpleForm>
    );
 
    return (
        <Dialog open={open} onClose={onClose} fullWidth maxWidth={maxWidth}>
            <DialogTitle>{title}</DialogTitle>
            <DialogContent dividers>
                {mode === "create" ? (
                    <CreateBase
                        resource={resource}
                        mutationOptions={{ onSuccess: handleSuccess, onError: handleError }}
                    >
                        {formContent}
                    </CreateBase>
                ) : (
                    <EditBase
                        resource={resource}
                        id={recordId}
                        mutationMode="pessimistic"
                        mutationOptions={{ onSuccess: handleSuccess, onError: handleError }}
                    >
                        {formContent}
                    </EditBase>
                )}
            </DialogContent>
            {mode === "show" && (
                <DialogActions>
                    <Button onClick={onClose}>{translate("ai.common.close")}</Button>
                </DialogActions>
            )}
        </Dialog>
    );
};
 
export default AiConfigDialog;