1
1
import { useState } from "react" ;
2
2
import { useNavigate } from "react-router-dom" ;
3
- import {
4
- Autocomplete ,
5
- Button ,
6
- IconButton ,
7
- Stack ,
8
- TextField ,
9
- } from "@mui/material" ;
3
+ import { Autocomplete , Button , IconButton , Stack , TextField } from "@mui/material" ;
10
4
import ArrowBackIcon from "@mui/icons-material/ArrowBack" ;
11
5
import axios from "axios" ;
12
6
import { ToastContainer , toast } from "react-toastify" ;
@@ -25,38 +19,22 @@ const NewQuestion = () => {
25
19
26
20
const [ title , setTitle ] = useState < string > ( "" ) ;
27
21
const [ markdownText , setMarkdownText ] = useState < string > ( "" ) ;
28
- const [ selectedComplexity , setselectedComplexity ] = useState < string | null > (
29
- null
30
- ) ;
22
+ const [ selectedComplexity , setselectedComplexity ] = useState < string | null > ( null ) ;
31
23
const [ selectedCategories , setSelectedCategories ] = useState < string [ ] > ( [ ] ) ;
32
24
const [ uploadedImagesUrl , setUploadedImagesUrl ] = useState < string [ ] > ( [ ] ) ;
33
25
const [ isPreviewQuestion , setIsPreviewQuestion ] = useState < boolean > ( false ) ;
34
26
35
27
const handleBack = ( ) => {
36
- if (
37
- title ||
38
- markdownText ||
39
- selectedComplexity ||
40
- selectedCategories . length > 0
41
- ) {
42
- if (
43
- ! confirm (
44
- "Are you sure you want to leave this page? All process will be lost."
45
- )
46
- ) {
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." ) ) {
47
30
return ;
48
31
}
49
32
}
50
33
navigate ( "/questions" ) ;
51
34
} ;
52
35
53
36
const handleSubmit = async ( ) => {
54
- if (
55
- ! title ||
56
- ! markdownText ||
57
- ! selectedComplexity ||
58
- selectedCategories . length === 0
59
- ) {
37
+ if ( ! title || ! markdownText || ! selectedComplexity || selectedCategories . length === 0 ) {
60
38
toast . error ( "Please fill in all fields" ) ;
61
39
return ;
62
40
}
@@ -80,8 +58,7 @@ const NewQuestion = () => {
80
58
navigate ( "/questions" ) ;
81
59
} catch ( error ) {
82
60
if ( axios . isAxiosError ( error ) ) {
83
- const message =
84
- error . response ?. data . message || "Failed to create question" ;
61
+ const message = error . response ?. data . message || "Failed to create question" ;
85
62
toast . error ( message ) ;
86
63
} else {
87
64
toast . error ( "Failed to create question" ) ;
@@ -122,12 +99,11 @@ const NewQuestion = () => {
122
99
onChange = { ( e , newcomplexitySelected ) => {
123
100
setselectedComplexity ( newcomplexitySelected ) ;
124
101
} }
125
- renderInput = { ( params ) => (
126
- < TextField { ...params } label = "Complexity" />
127
- ) }
102
+ renderInput = { ( params ) => < TextField { ...params } label = "Complexity" /> }
128
103
/>
129
104
130
105
< QuestionCategoryAutoComplete
106
+ selectedCategories = { selectedCategories }
131
107
setSelectedCategories = { setSelectedCategories }
132
108
/>
133
109
@@ -136,10 +112,7 @@ const NewQuestion = () => {
136
112
setUploadedImagesUrl = { setUploadedImagesUrl }
137
113
/>
138
114
139
- < QuestionMarkdown
140
- markdownText = { markdownText }
141
- setMarkdownText = { setMarkdownText }
142
- />
115
+ < QuestionMarkdown markdownText = { markdownText } setMarkdownText = { setMarkdownText } />
143
116
</ >
144
117
) }
145
118
@@ -152,10 +125,7 @@ const NewQuestion = () => {
152
125
color = "secondary"
153
126
fullWidth
154
127
disabled = {
155
- ! title &&
156
- ! markdownText &&
157
- ! selectedComplexity &&
158
- selectedCategories . length === 0
128
+ ! title && ! markdownText && ! selectedComplexity && selectedCategories . length === 0
159
129
}
160
130
onClick = { ( ) => setIsPreviewQuestion ( ( prev ) => ! prev ) }
161
131
>
0 commit comments