-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Note.tsx
204 lines (194 loc) · 6.46 KB
/
Note.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
import * as React from 'react';
import { useState, FormEvent, ChangeEvent } from 'react';
import {
TextField,
ReferenceField,
DateField,
useResourceContext,
useDelete,
useUpdate,
useNotify,
} from 'react-admin';
import {
Box,
Typography,
Tooltip,
IconButton,
FilledInput,
Button,
} from '@mui/material';
import EditIcon from '@mui/icons-material/Edit';
import TrashIcon from '@mui/icons-material/Delete';
import { Status } from '../misc/Status';
export const Note = ({
showStatus,
note,
}: {
showStatus?: boolean;
note: any;
isLast: boolean;
}) => {
const [isHover, setHover] = useState(false);
const [isEditing, setEditing] = useState(false);
const [noteText, setNoteText] = useState(note.text);
const resource = useResourceContext();
const notify = useNotify();
const [update, { isPending }] = useUpdate();
const [deleteNote] = useDelete(
resource,
{ id: note.id, previousData: note },
{
mutationMode: 'undoable',
onSuccess: () => {
notify('Note deleted', { type: 'info', undoable: true });
},
}
);
const handleDelete = () => {
deleteNote();
};
const handleEnterEditMode = () => {
setEditing(true);
};
const handleCancelEdit = () => {
setEditing(false);
setNoteText(note.text);
setHover(false);
};
const handleTextChange = (event: ChangeEvent<HTMLInputElement>) => {
setNoteText(event.target.value);
};
const handleNoteUpdate = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
update(
resource,
{ id: note.id, data: { text: noteText }, previousData: note },
{
onSuccess: () => {
setEditing(false);
setNoteText(note.text);
setHover(false);
},
}
);
};
return (
<Box
mb={2}
onMouseEnter={() => setHover(true)}
onMouseLeave={() => setHover(false)}
>
<Box mb={1} color="text.secondary">
<ReferenceField
record={note}
resource="contactNotes"
source="sales_id"
reference="sales"
>
<TextField source="first_name" variant="body1" />
</ReferenceField>{' '}
<Typography component="span" variant="body1">
added a note on{' '}
</Typography>
<DateField
source="date"
record={note}
variant="body1"
showTime
locales="en"
options={{
dateStyle: 'full',
timeStyle: 'short',
}}
/>{' '}
{showStatus && <Status status={note.status} />}
</Box>
{isEditing ? (
<form onSubmit={handleNoteUpdate}>
<FilledInput
value={noteText}
onChange={handleTextChange}
fullWidth
multiline
sx={{
paddingTop: '16px',
paddingLeft: '14px',
paddingRight: '60px',
paddingBottom: '14px',
lineHeight: 1.3,
}}
autoFocus
/>
<Box display="flex" justifyContent="flex-end" mt={1}>
<Button
sx={{ mr: 1 }}
onClick={handleCancelEdit}
color="primary"
>
Cancel
</Button>
<Button
type="submit"
color="primary"
variant="contained"
disabled={isPending}
>
Update Note
</Button>
</Box>
</form>
) : (
<Box
sx={{
bgcolor: '#edf3f0',
padding: '0 1em',
borderRadius: '10px',
display: 'flex',
alignItems: 'stretch',
marginBottom: 1,
}}
>
<Box flex={1}>
{note.text
.split('\n')
.map((paragraph: string, index: number) => (
<Box
component="p"
fontFamily="fontFamily"
fontSize="body1.fontSize"
lineHeight={1.3}
marginBottom={2.4}
key={index}
>
{paragraph}
</Box>
))}
</Box>
<Box
sx={{
marginLeft: 2,
display: 'flex',
flexDirection: 'column',
justifyContent: 'space-around',
visibility: isHover ? 'visible' : 'hidden',
}}
>
<Tooltip title="Edit note">
<IconButton
size="small"
onClick={handleEnterEditMode}
>
<EditIcon />
</IconButton>
</Tooltip>
<Tooltip title="Delete note">
<IconButton size="small" onClick={handleDelete}>
<TrashIcon />
</IconButton>
</Tooltip>
</Box>
</Box>
)}
</Box>
);
};