-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathAnswerForm.js
128 lines (117 loc) · 3.55 KB
/
AnswerForm.js
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
import { useForm } from 'react-hook-form';
import { Link as RouterLink } from 'react-router-dom';
import { useMutation } from '@apollo/client';
import { POST_ANSWER } from '../graphql/mutations';
import { VIEW_QUESTION } from '../graphql/queries';
import { useAuthContext } from '../context/auth';
import { useStateContext } from '../context/state';
import AuthFormModal from '../components/AuthFormModal';
import * as yup from 'yup';
import { yupResolver } from '@hookform/resolvers/yup';
import { getErrorMsg } from '../utils/helperFuncs';
import { Typography, Button, TextField, Chip, Link } from '@material-ui/core';
import { useQuesPageStyles } from '../styles/muiStyles';
const validationSchema = yup.object({
answerBody: yup.string().min(30, 'Must be at least 30 characters'),
});
const AnswerForm = ({ quesId, tags }) => {
const classes = useQuesPageStyles();
const { user } = useAuthContext();
const { clearEdit, notify } = useStateContext();
const { register, handleSubmit, reset, errors } = useForm({
mode: 'onChange',
resolver: yupResolver(validationSchema),
});
const [addAnswer, { loading }] = useMutation(POST_ANSWER, {
onError: (err) => {
notify(getErrorMsg(err), 'error');
},
});
const postAnswer = ({ answerBody }) => {
addAnswer({
variables: { quesId, body: answerBody },
update: (proxy, { data }) => {
reset();
const dataInCache = proxy.readQuery({
query: VIEW_QUESTION,
variables: { quesId },
});
const updatedData = {
...dataInCache.viewQuestion,
answers: data.postAnswer,
};
proxy.writeQuery({
query: VIEW_QUESTION,
variables: { quesId },
data: { viewQuestion: updatedData },
});
notify('Answer submitted!');
},
});
};
return (
<div className={classes.answerForm}>
{user && (
<Typography variant="h6" color="secondary">
Your Answer
</Typography>
)}
{user && (
<form onSubmit={handleSubmit(postAnswer)}>
<TextField
inputRef={register}
name="answerBody"
required
fullWidth
type="text"
placeholder="Enter atleast 30 characters"
variant="outlined"
size="small"
error={'answerBody' in errors}
helperText={'answerBody' in errors ? errors.answerBody.message : ''}
multiline
rows={5}
/>
<div>
<Button
color="primary"
variant="contained"
style={{ marginTop: '0.8em' }}
type="submit"
disabled={loading}
>
Post Your Answer
</Button>
</div>
</form>
)}
<div className={classes.footerText}>
<span>
Browse other questions tagged{' '}
{tags.map((t) => (
<Chip
key={t}
label={t}
variant="outlined"
color="primary"
size="small"
component={RouterLink}
to={`/tags/${t}`}
className={classes.footerTag}
clickable
/>
))}
or{' '}
{user ? (
<Link component={RouterLink} to="/ask" onClick={() => clearEdit()}>
ask your own question.
</Link>
) : (
<AuthFormModal buttonType="link" />
)}
</span>
</div>
</div>
);
};
export default AnswerForm;