-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathDealCreate.tsx
104 lines (97 loc) · 3.22 KB
/
DealCreate.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
import * as React from 'react';
import {
Create,
SimpleForm,
TextInput,
SelectInput,
NumberInput,
ReferenceInput,
AutocompleteInput,
required,
useRedirect,
useDataProvider,
useGetIdentity,
} from 'react-admin';
import { Dialog } from '@mui/material';
import { stageChoices } from './stages';
import { typeChoices } from './types';
import { Deal } from '../types';
const validateRequired = required();
export const DealCreate = ({ open }: { open: boolean }) => {
const redirect = useRedirect();
const dataProvider = useDataProvider();
const handleClose = () => {
redirect('/deals');
};
const onSuccess = (deal: Deal) => {
redirect('/deals');
// increase the index of all deals in the same stage as the new deal
dataProvider
.getList('deals', {
pagination: { page: 1, perPage: 50 },
sort: { field: 'id', order: 'ASC' },
filter: { stage: deal.stage },
})
.then(({ data: deals }) =>
Promise.all(
deals.map(oldDeal =>
dataProvider.update('deals', {
id: oldDeal.id,
data: { index: oldDeal.index + 1 },
previousData: oldDeal,
})
)
)
);
};
const { identity } = useGetIdentity();
return (
<Dialog open={open} onClose={handleClose}>
<Create<Deal>
resource="deals"
mutationOptions={{ onSuccess }}
sx={{ width: 500, '& .RaCreate-main': { mt: 0 } }}
>
<SimpleForm
defaultValues={{
index: 0,
sales_id: identity && identity?.id,
}}
>
<TextInput
source="name"
label="Deal name"
fullWidth
validate={validateRequired}
/>
<TextInput
source="description"
multiline
rows={3}
fullWidth
/>
<ReferenceInput source="company_id" reference="companies">
<AutocompleteInput
optionText="name"
fullWidth
validate={validateRequired}
/>
</ReferenceInput>
<SelectInput
source="stage"
choices={stageChoices}
fullWidth
validate={validateRequired}
defaultValue="opportunity"
/>
<SelectInput
source="type"
choices={typeChoices}
fullWidth
/>
<NumberInput source="amount" fullWidth defaultValue={0} />
</SimpleForm>
</Create>
</Dialog>
);
};