-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathProductEdit.tsx
313 lines (296 loc) · 10.9 KB
/
ProductEdit.tsx
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import * as React from 'react';
import {
Count,
Datagrid,
DateField,
Edit,
EditButton,
Form,
FormDataConsumer,
Pagination,
ReferenceManyField,
required,
TextField,
TextInput,
useResourceContext,
useRecordContext,
Toolbar,
SaveButton,
useNotify,
useGetIdentity,
WithRecord,
SimpleForm,
ReferenceInput,
AutocompleteInput,
DateInput,
} from 'react-admin';
import {
Box,
Card,
CardContent,
useTheme,
Stack,
Typography,
} from '@mui/material';
import { useGetLock, useLockOnMount } from '@react-admin/ra-realtime';
import { useDefineAppLocation } from '@react-admin/ra-navigation';
import {
AccordionSection,
CreateInDialogButton,
} from '@react-admin/ra-form-layout';
import {
RevisionListWithDetails,
CreateRevisionOnSave,
useApplyChangesBasedOnSearchParam,
useGetRevisions,
} from '@react-admin/ra-history';
import CustomerReferenceField from '../visitors/CustomerReferenceField';
import StarRatingField from '../reviews/StarRatingField';
import Poster from './Poster';
import ProductPreview from './ProductPreview';
import { ProductDiff } from './ProductDiff';
import { AdminName } from '../admins';
import { Product } from '../types';
import { ProductEditDetails } from './ProductEditDetails';
import StarRatingInput from '../reviews/StarRatingInput';
const MarkdownInput = React.lazy(() =>
import('@react-admin/ra-markdown').then(module => ({
default: module.MarkdownInput,
}))
);
const ProductEdit = () => (
<Edit title={<ProductTitle />}>
<ProductEditFormWithPreview>
<Poster />
<TextInput id="image" source="image" validate={req} />
<TextInput source="thumbnail" validate={req} />
<div>
<AccordionSection
label="resources.products.tabs.description"
data-tour-id="description-tab"
fullWidth
>
<MarkdownInput
source="description"
label=""
validate={req}
/>
</AccordionSection>
<AccordionSection
label="resources.products.tabs.details"
fullWidth
>
<ProductEditDetails />
</AccordionSection>
<AccordionSection
label="resources.products.tabs.reviews"
fullWidth
count={
<WithRecord
render={record => (
<Count
resource="reviews"
filter={{ product_id: record.id }}
/>
)}
/>
}
>
<ReferenceManyField
reference="reviews"
target="product_id"
pagination={<Pagination />}
>
<Box
sx={{
display: 'flex',
justifyContent: 'flex-end',
mt: 1,
mr: 2,
}}
>
<WithRecord
render={record => (
<CreateInDialogButton
record={{ product_id: record.id }}
fullWidth
maxWidth="sm"
>
<SimpleForm
defaultValues={{
status: 'pending',
}}
>
<ReferenceInput
source="customer_id"
reference="customers"
>
<AutocompleteInput
optionText={
customerOptionRenderer
}
validate={req}
/>
</ReferenceInput>
<DateInput
source="date"
defaultValue={new Date()}
validate={req}
/>
<StarRatingInput
source="rating"
defaultValue={2}
/>
<TextInput
source="comment"
multiline
resettable
validate={req}
/>
</SimpleForm>
</CreateInDialogButton>
)}
/>
</Box>
<Datagrid
sx={{
width: '100%',
'& .column-comment': {
maxWidth: '20em',
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
},
}}
>
<DateField source="date" />
<CustomerReferenceField />
<StarRatingField
label="resources.reviews.fields.rating"
source="rating"
/>
<TextField source="comment" />
<TextField source="status" />
<EditButton />
</Datagrid>
</ReferenceManyField>
</AccordionSection>
<AccordionSection
label="resources.products.tabs.revisions"
fullWidth
count={<ProductRevisionsCount />}
>
<RevisionListWithDetails
sx={{ py: 1 }}
diff={<ProductDiff />}
renderName={id => <AdminName id={id} />}
allowRevert
/>
</AccordionSection>
</div>
</ProductEditFormWithPreview>
</Edit>
);
const ProductRevisionsCount = () => {
const record = useRecordContext();
const { data: revisions } = useGetRevisions(
'products',
{
recordId: record!.id,
},
{ enabled: !!record?.id }
);
return revisions ? <>{revisions?.length}</> : null;
};
const req = [required()];
const ProductTitle = () => {
const record = useRecordContext<Product>();
return record ? <span>Poster "{record.reference}"</span> : null;
};
const ProductEditFormWithPreview = ({ children, ...props }: any) => {
const resource = useResourceContext();
const record = useRecordContext();
const theme = useTheme();
const notify = useNotify();
useDefineAppLocation('catalog.products.edit', { record });
const { isLoading } = useLockOnMount({
resource,
id: record?.id,
lockMutationOptions: {
onError: () => {
notify('ra-realtime.notification.lock.lockedBySomeoneElse');
},
},
});
return (
<CreateRevisionOnSave record={props.record} resource={props.resource}>
<Form {...props}>
<ApplyChangesBasedOnSearchParam />
<Box
sx={{
display: 'flex',
'& > :first-child': {
flex: 1,
minWidth: '60%',
maxWidth: '70%',
borderWidth: '0 1px 0 0',
borderRadius: `${
theme?.shape?.borderRadius || 0
} 0 0 ${theme?.shape?.borderRadius || 0}`,
},
'& > :last-child': {
width: '25%',
flexShrink: 0,
},
}}
>
<Card>
<CardContent>
<Stack>{children}</Stack>
</CardContent>
<CustomToolbar disabled={isLoading} />
</Card>
<div data-testid="product-edit-preview">
<FormDataConsumer>
{({ formData }) => {
return <ProductPreview record={formData} />;
}}
</FormDataConsumer>
</div>
</Box>
</Form>
</CreateRevisionOnSave>
);
};
const CustomToolbar = ({ disabled }: { disabled?: boolean }) => {
const resource = useResourceContext();
const record = useRecordContext();
const { identity } = useGetIdentity();
const { data: lock } = useGetLock(
resource!,
{ id: record!.id },
{ enabled: !!resource && !!record?.id }
);
const isMeLocker = lock?.identity === identity?.id;
return (
<Toolbar>
<SaveButton disabled={disabled === true || !isMeLocker} />
{!isMeLocker && <LockMessage identity={lock?.identity} />}
</Toolbar>
);
};
const ApplyChangesBasedOnSearchParam = () => {
useApplyChangesBasedOnSearchParam();
return null;
};
const LockMessage = (props: any) => {
const { identity, variant = 'body1' } = props;
return (
<Typography py={0} px={1} variant={variant}>
This record is locked by another user: {identity}.
</Typography>
);
};
const customerOptionRenderer = (choice: any) =>
`${choice.first_name} ${choice.last_name}`;
export default ProductEdit;