forked from amand33p/bug-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteForm.tsx
More file actions
104 lines (96 loc) · 2.6 KB
/
Copy pathNoteForm.tsx
File metadata and controls
104 lines (96 loc) · 2.6 KB
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 { useForm } from 'react-hook-form';
import { useSelector, useDispatch } from 'react-redux';
import {
createNote,
editNote,
clearSubmitBugError,
selectBugsState,
} from '../../redux/slices/bugsSlice';
import ErrorBox from '../../components/ErrorBox';
import * as yup from 'yup';
import { yupResolver } from '@hookform/resolvers/yup';
import { TextField, Button, InputAdornment } from '@material-ui/core';
import { useFormStyles } from '../../styles/muiStyles';
import CommentIcon from '@material-ui/icons/Comment';
const validationSchema = yup.object({
body: yup.string().required('Required'),
});
interface NoteFormProps {
closeDialog?: () => void;
projectId: string;
bugId: string;
isEditMode: boolean;
currentBody?: string;
noteId?: number;
}
const NoteForm: React.FC<NoteFormProps> = ({
closeDialog,
isEditMode,
projectId,
bugId,
currentBody,
noteId,
}) => {
const classes = useFormStyles();
const dispatch = useDispatch();
const { submitError, submitLoading } = useSelector(selectBugsState);
const { register, handleSubmit, errors } = useForm({
mode: 'onChange',
resolver: yupResolver(validationSchema),
defaultValues: {
body: currentBody || '',
},
});
const handleCreateNote = ({ body }: { body: string }) => {
dispatch(createNote(projectId, bugId, body, closeDialog));
};
const handleUpdateNote = ({ body }: { body: string }) => {
dispatch(editNote(projectId, bugId, noteId as number, body, closeDialog));
};
return (
<form
onSubmit={handleSubmit(isEditMode ? handleUpdateNote : handleCreateNote)}
>
<TextField
multiline
rows={1}
rowsMax={4}
inputRef={register}
name="body"
placeholder="Type a note..."
required
fullWidth
type="text"
label="Note"
variant="outlined"
error={'body' in errors}
helperText={'body' in errors ? errors.body?.message : ''}
InputProps={{
startAdornment: (
<InputAdornment position="start">
<CommentIcon color="primary" />
</InputAdornment>
),
}}
/>
<Button
size="large"
color="primary"
variant="contained"
fullWidth
className={classes.submitBtn}
type="submit"
disabled={submitLoading}
>
{isEditMode ? 'Update Note' : 'Submit Note'}
</Button>
{submitError && (
<ErrorBox
errorMsg={submitError}
clearErrorMsg={() => dispatch(clearSubmitBugError())}
/>
)}
</form>
);
};
export default NoteForm;