#
vincentlu
5 天以前 c65a357fc8b907af755e1ef6a2c201e31eb71e94
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import React, { useMemo } from "react";
import {
    Edit,
    SimpleForm,
    useTranslate,
    NumberInput,
    SaveButton,
    Toolbar,
    useRecordContext,
    DeleteButton,
} from 'react-admin';
import { Stack, Grid, Box, Typography, Paper, Divider } from '@mui/material';
import { EDIT_MODE } from '@/config/setting';
import EditBaseAside from "../components/EditBaseAside";
import CustomerTopToolBar from "../components/EditTopToolBar";
 
const FormToolbar = () => {
    return (
        <Toolbar sx={{ justifyContent: 'space-between' }}>
            <SaveButton />
            <DeleteButton mutationMode="optimistic" />
        </Toolbar>
    )
}
 
const LaneEdit = () => {
    return (
        <Edit
            redirect="list"
            mutationMode={EDIT_MODE}
            actions={<CustomerTopToolBar />}
            aside={<EditBaseAside />}
        >
            <SimpleForm
                shouldUnregister
                warnWhenUnsavedChanges
                toolbar={<FormToolbar />}
                mode="onTouched"
                defaultValues={{}}
            // validate={(values) => { }}
            >
                <LaneEditContent />
            </SimpleForm>
        </Edit >
    )
}
 
const LaneEditContent = () => {
    const translate = useTranslate();
    const record = useRecordContext();
    const codes = useMemo(() => {
        const source = record?.codes;
        if (!source) return [];
 
        if (Array.isArray(source)) {
            return source.filter(Boolean);
        }
 
        if (typeof source === 'string') {
            try {
                const parsed = JSON.parse(source);
                if (Array.isArray(parsed)) {
                    return parsed.filter(Boolean);
                }
            } catch (error) {
                // fallback to comma split
            }
 
            return source
                .split(',')
                .map((item) => item.replace(/[\[\]"]/g, '').trim())
                .filter(Boolean);
        }
 
        return [];
    }, [record?.codes]);
    const codesCount = codes.length;
 
    if (!record) return null;
 
    return (
        <Box
            sx={{
                width: '100%',
                maxWidth: { xs: '100%', md: 800 },
                mx: { xs: 'auto', md: 0 },
                mr: { md: 'auto' },
                display: 'flex',
                flexDirection: 'column',
                gap: 3,
            }}
        >
            <Paper
                elevation={0}
                sx={{
                    p: 3,
                    borderRadius: 3,
                    border: '1px solid',
                    borderColor: 'divider',
                    background: (theme) =>
                        theme.palette.mode === 'dark'
                            ? 'rgba(255,255,255,0.02)'
                            : 'rgba(15,23,42,0.02)',
                }}
            >
                <Stack spacing={1}>
                    <Typography variant="overline" color="text.secondary" sx={{ letterSpacing: 2 }}>
                        {translate('menu.lane')}
                    </Typography>
                    <Typography variant="h5" sx={{ fontWeight: 600 }}>
                        {record.name || record.uuid}
                    </Typography>
                    <Typography variant="body2" color="text.secondary">
                        {translate('table.field.lane.hashCode')}: {record.hashCode || '-'}
                    </Typography>
                    <Stack direction="row" spacing={2} flexWrap="wrap" pt={2}>
                        {/* <Typography variant="body2" color="text.secondary">
                            ID: {record.id}
                        </Typography> */}
                        <Typography variant="body2" color="text.secondary">
                            {translate('common.field.count')}: {codesCount}
                        </Typography>
                    </Stack>
                    <Stack spacing={1.5} mt={5}>
                        {/* <Typography variant="subtitle2" color="text.secondary">
                            {translate('table.field.lane.codes')}
                        </Typography> */}
                        <Box
                            sx={{
                                display: 'flex',
                                flexWrap: 'wrap',
                                gap: 1,
                                maxHeight: 160,
                                overflowY: 'auto',
                                pt: 1,
                            }}
                        >
                            {codes.length ? (
                                codes.map((code, idx) => (
                                    <Box
                                        key={`${code}-${idx}`}
                                        sx={{
                                            px: 1.5,
                                            py: 0.5,
                                            borderRadius: 2,
                                            border: '1px solid',
                                            borderColor: 'divider',
                                            fontFamily: `'JetBrains Mono','Fira Code','SFMono-Regular','Roboto Mono',monospace`,
                                            fontSize: '.9rem',
                                            backgroundColor: 'background.paper',
                                        }}
                                    >
                                        {code}
                                    </Box>
                                ))
                            ) : (
                                <Typography variant="body2" color="text.secondary">
                                    {translate('ra.navigation.no_results')}
                                </Typography>
                            )}
                        </Box>
                    </Stack>
                </Stack>
            </Paper>
 
            <Paper
                elevation={0}
                sx={{
                    p: { xs: 3, md: 4 },
                    borderRadius: 3,
                    border: '1px solid',
                    borderColor: 'divider',
                    backgroundColor: 'background.paper',
                    boxShadow: (theme) => theme.shadows[2],
                }}
            >
                <Stack spacing={2}>
                    <Box display="flex" justifyContent="space-between" alignItems="center">
                        <Typography variant="h6" sx={{ fontWeight: 600 }}>
                            {translate('common.edit.title.main')}
                        </Typography>
                    </Box>
                    <Divider />
                    <Grid container spacing={3}>
                        <Grid item xs={12} md={6}>
                            <Stack spacing={1}>
                                {/* <Typography variant="subtitle2" color="text.secondary">
                                    {translate('table.field.lane.entryAngle')}
                                </Typography> */}
                                <NumberInput
                                    label={"table.field.lane.entryAngle"}
                                    source="entryAngle"
                                    fullWidth
                                    helperText={translate('page.lane.helper.entryAngle')}
                                />
                            </Stack>
                        </Grid>
                        <Grid item xs={12} md={6}>
                            <Stack spacing={1}>
                                {/* <Typography variant="subtitle2" color="text.secondary">
                                    {translate('table.field.lane.maximum')}
                                </Typography> */}
                                <NumberInput
                                    label={"table.field.lane.maximum"}
                                    source="maximum"
                                    fullWidth
                                    helperText={translate('page.lane.helper.maximum')}
                                />
                            </Stack>
                        </Grid>
                    </Grid>
                </Stack>
            </Paper>
        </Box>
    );
};
 
export default LaneEdit;