1
- import { useState } from "react" ;
1
+ import { useState , useReducer } from "react" ;
2
2
import { useNavigate } from "react-router-dom" ;
3
- import { Autocomplete , Button , IconButton , Stack , TextField } from "@mui/material" ;
3
+ import {
4
+ Autocomplete ,
5
+ Button ,
6
+ IconButton ,
7
+ Stack ,
8
+ TextField ,
9
+ } from "@mui/material" ;
4
10
import ArrowBackIcon from "@mui/icons-material/ArrowBack" ;
5
- import axios from "axios" ;
11
+ import reducer , {
12
+ createQuestion ,
13
+ initialState ,
14
+ } from "../../reducers/questionReducer" ;
6
15
import { ToastContainer , toast } from "react-toastify" ;
7
16
import "react-toastify/dist/ReactToastify.css" ;
8
17
9
- import { questionClient } from "../../utils/api" ;
10
18
import { complexityList } from "../../utils/constants" ;
11
19
import AppMargin from "../../components/AppMargin" ;
12
20
import QuestionMarkdown from "../../components/QuestionMarkdown" ;
@@ -17,52 +25,60 @@ import QuestionDetail from "../../components/QuestionDetail";
17
25
const NewQuestion = ( ) => {
18
26
const navigate = useNavigate ( ) ;
19
27
28
+ const [ state , dispatch ] = useReducer ( reducer , initialState ) ;
29
+
20
30
const [ title , setTitle ] = useState < string > ( "" ) ;
21
31
const [ markdownText , setMarkdownText ] = useState < string > ( "" ) ;
22
- const [ selectedComplexity , setselectedComplexity ] = useState < string | null > ( null ) ;
32
+ const [ selectedComplexity , setselectedComplexity ] = useState < string | null > (
33
+ null
34
+ ) ;
23
35
const [ selectedCategories , setSelectedCategories ] = useState < string [ ] > ( [ ] ) ;
24
36
const [ uploadedImagesUrl , setUploadedImagesUrl ] = useState < string [ ] > ( [ ] ) ;
25
37
const [ isPreviewQuestion , setIsPreviewQuestion ] = useState < boolean > ( false ) ;
26
38
27
39
const handleBack = ( ) => {
28
- if ( title || markdownText || selectedComplexity || selectedCategories . length > 0 ) {
29
- if ( ! confirm ( "Are you sure you want to leave this page? All process will be lost." ) ) {
40
+ if (
41
+ title ||
42
+ markdownText ||
43
+ selectedComplexity ||
44
+ selectedCategories . length > 0
45
+ ) {
46
+ if (
47
+ ! confirm (
48
+ "Are you sure you want to leave this page? All process will be lost."
49
+ )
50
+ ) {
30
51
return ;
31
52
}
32
53
}
33
54
navigate ( "/questions" ) ;
34
55
} ;
35
56
36
57
const handleSubmit = async ( ) => {
37
- if ( ! title || ! markdownText || ! selectedComplexity || selectedCategories . length === 0 ) {
58
+ if (
59
+ ! title ||
60
+ ! markdownText ||
61
+ ! selectedComplexity ||
62
+ selectedCategories . length === 0
63
+ ) {
38
64
toast . error ( "Please fill in all fields" ) ;
39
65
return ;
40
66
}
41
67
42
- try {
43
- await questionClient . post (
44
- "/" ,
45
- {
46
- title,
47
- description : markdownText ,
48
- complexity : selectedComplexity ,
49
- category : selectedCategories ,
50
- } ,
51
- {
52
- withCredentials : false ,
53
- headers : {
54
- "Content-Type" : "application/json" ,
55
- } ,
56
- }
57
- ) ;
68
+ const result = await createQuestion (
69
+ {
70
+ title,
71
+ description : markdownText ,
72
+ complexity : selectedComplexity ,
73
+ categories : selectedCategories ,
74
+ } ,
75
+ dispatch
76
+ ) ;
77
+
78
+ if ( result ) {
58
79
navigate ( "/questions" ) ;
59
- } catch ( error ) {
60
- if ( axios . isAxiosError ( error ) ) {
61
- const message = error . response ?. data . message || "Failed to create question" ;
62
- toast . error ( message ) ;
63
- } else {
64
- toast . error ( "Failed to create question" ) ;
65
- }
80
+ } else {
81
+ toast . error ( state . selectedQuestionError || "Failed to create question" ) ;
66
82
}
67
83
} ;
68
84
@@ -99,7 +115,9 @@ const NewQuestion = () => {
99
115
onChange = { ( e , newcomplexitySelected ) => {
100
116
setselectedComplexity ( newcomplexitySelected ) ;
101
117
} }
102
- renderInput = { ( params ) => < TextField { ...params } label = "Complexity" /> }
118
+ renderInput = { ( params ) => (
119
+ < TextField { ...params } label = "Complexity" />
120
+ ) }
103
121
/>
104
122
105
123
< QuestionCategoryAutoComplete
@@ -112,7 +130,10 @@ const NewQuestion = () => {
112
130
setUploadedImagesUrl = { setUploadedImagesUrl }
113
131
/>
114
132
115
- < QuestionMarkdown markdownText = { markdownText } setMarkdownText = { setMarkdownText } />
133
+ < QuestionMarkdown
134
+ markdownText = { markdownText }
135
+ setMarkdownText = { setMarkdownText }
136
+ />
116
137
</ >
117
138
) }
118
139
@@ -125,7 +146,10 @@ const NewQuestion = () => {
125
146
color = "secondary"
126
147
fullWidth
127
148
disabled = {
128
- ! title && ! markdownText && ! selectedComplexity && selectedCategories . length === 0
149
+ ! title &&
150
+ ! markdownText &&
151
+ ! selectedComplexity &&
152
+ selectedCategories . length === 0
129
153
}
130
154
onClick = { ( ) => setIsPreviewQuestion ( ( prev ) => ! prev ) }
131
155
>
0 commit comments