1
1
import React from "react" ;
2
2
import QuestionForm from "./QuestionForm" ;
3
- import { Box , Button } from "@mui/material" ;
3
+ import { Box , Button } from "@mui/material" ;
4
4
import Question from "./Question" ;
5
5
import Typography from "@mui/material/Typography" ;
6
- import { addQuestion } from "../../api/questions/data" ;
7
- import { AxiosError } from "axios" ;
8
-
6
+ import { addQuestion } from "../../api/questions/data" ;
7
+ import { AxiosError } from "axios" ;
9
8
10
9
const AddQuestionTab : React . FC = ( ) => {
11
- const [ addQuestions , setAddQuestions ] = React . useState ( false ) ;
12
-
13
- const handleAddQuestionClick = ( ) => {
14
- setAddQuestions ( true ) ;
15
- }
16
- const onSubmit = async ( question : Question ) => {
17
- console . log ( question ) ;
18
- let questionToAdd = new Question ( question ) ;
19
-
20
- try {
21
- const questionAdded = await addQuestion ( questionToAdd ) ;
22
- console . log ( questionAdded ) ;
23
- setAddQuestions ( false ) ;
24
- } catch ( e ) {
25
- if ( e instanceof AxiosError && e . response ) {
26
- console . log ( e . response . data . code ) ;
27
- } else if ( e instanceof Error ) {
28
- console . log ( e . message ) ;
29
- }
30
- }
31
- } ;
32
-
33
- const onCancel = ( ) => {
34
- setAddQuestions ( false ) ;
10
+ const [ addQuestions , setAddQuestions ] = React . useState ( false ) ;
11
+
12
+ const handleAddQuestionClick = ( ) => {
13
+ setAddQuestions ( true ) ;
14
+ } ;
15
+ const onSubmit = async ( question : Question ) => {
16
+ console . log ( question ) ;
17
+ let questionToAdd = new Question ( question ) ;
18
+
19
+ try {
20
+ const questionAdded = await addQuestion ( questionToAdd ) ;
21
+ console . log ( questionAdded ) ;
22
+ setAddQuestions ( false ) ;
23
+ } catch ( e ) {
24
+ if ( e instanceof AxiosError && e . response ) {
25
+ console . log ( e . response . data . code ) ;
26
+ } else if ( e instanceof Error ) {
27
+ console . log ( e . message ) ;
28
+ }
35
29
}
30
+ } ;
36
31
32
+ const onCancel = ( ) => {
33
+ setAddQuestions ( false ) ;
34
+ } ;
37
35
38
- return (
36
+ return (
37
+ < Box component = "span" padding = { 2 } width = "80%" >
38
+ { addQuestions ? (
39
39
< React . Fragment >
40
- < Box component = "span" padding = { 2 } width = '80%' >
41
- { addQuestions ?
42
- < React . Fragment >
43
- < Typography variant = "h5" gutterBottom >
44
- Add Question
45
- </ Typography >
46
- < QuestionForm onSubmit = { onSubmit } onCancel = { onCancel } />
47
- </ React . Fragment >
48
- :
49
- < Button variant = "contained" onClick = { handleAddQuestionClick } >
50
- Add Question
51
- </ Button >
52
- }
53
- </ Box >
40
+ < Typography variant = "h5" gutterBottom >
41
+ Add Question
42
+ </ Typography >
43
+ < QuestionForm onSubmit = { onSubmit } onCancel = { onCancel } />
54
44
</ React . Fragment >
55
- ) ;
56
- }
57
-
58
- export default AddQuestionTab ;
45
+ ) : (
46
+ < Button variant = "contained" onClick = { handleAddQuestionClick } >
47
+ Add Question
48
+ </ Button >
49
+ ) }
50
+ </ Box >
51
+ ) ;
52
+ } ;
53
+
54
+ export default AddQuestionTab ;
0 commit comments