-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathNewMessageForm.tsx
132 lines (125 loc) · 3.25 KB
/
NewMessageForm.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
import { useRef } from "react";
import {
Form,
required,
SelectInput,
TextInput,
useCreate,
useGetIdentity,
useListContext,
useRecordContext,
useUpdate,
} from "react-admin";
import { useLockOnCall, useGetLockLive } from "@react-admin/ra-realtime";
import { useFormContext } from "react-hook-form";
import {
Avatar,
Button,
ListItem,
ListItemAvatar,
ListItemText,
} from "@mui/material";
import { v4 as uuidv4 } from "uuid";
export const NewMessageForm = () => {
const { data: lock } = useGetLockLive("tickets");
const [doLock] = useLockOnCall({ resource: "tickets" });
const { refetch } = useListContext();
const record = useRecordContext();
const [create, { isLoading: isCreating }] = useCreate();
const [update] = useUpdate();
const resetForm = useRef<any>();
const { identity } = useGetIdentity();
const isFormDisabled = lock && lock.identity !== identity?.id;
const handleSubmit = (values: any) => {
const { status, ...message } = values;
const timestamp: string = new Date().toISOString();
create(
"messages",
{
data: {
id: uuidv4(),
...message,
ticket_id: record.id,
author: "agent",
email: identity?.email,
agent_id: identity?.id,
timestamp,
},
},
{
onSuccess: () => {
update("tickets", {
id: record.id,
data: { status, updated_at: timestamp },
});
resetForm.current && resetForm.current();
refetch();
},
}
);
};
return (
<ListItem alignItems="flex-start" sx={{ backgroundColor: "#ffffef" }}>
<ListItemAvatar>
<Avatar src={identity?.avatar} />
</ListItemAvatar>
<ListItemText
sx={{
width: "100%",
flexDirection: "column-reverse",
display: "flex",
gap: 0.5,
"& .MuiListItemText-primary": {
whiteSpace: "pre-line",
},
"& .MuiListItemText-secondary": {},
}}
secondary={`${identity?.fullName} <${identity?.email}>`}
>
<Form
onSubmit={handleSubmit}
record={{
status: "pending",
}}
>
<SetFormContext resetForm={resetForm} />
<TextInput
source="message"
multiline
fullWidth
minRows={3}
helperText={false}
validate={required()}
onFocus={() => {
doLock();
}}
disabled={isFormDisabled}
/>
<SelectInput
source="status"
choices={[
{ id: "open", name: "Open" },
{ id: "pending", name: "Pending" },
{ id: "closed", name: "Closed" },
]}
validate={required()}
disabled={isFormDisabled}
/>
<Button
type="submit"
variant="contained"
sx={{ m: 2 }}
disabled={isCreating || isFormDisabled}
>
Submit
</Button>
</Form>
</ListItemText>
</ListItem>
);
};
const SetFormContext = ({ resetForm }: any) => {
const form = useFormContext();
resetForm.current = form.reset;
return null;
};