7
7
Grid2 ,
8
8
TextField ,
9
9
Typography ,
10
+ CircularProgress ,
11
+ useMediaQuery ,
10
12
} from "@mui/material" ;
11
13
import { useEffect , useReducer , useState } from "react" ;
12
14
@@ -17,23 +19,28 @@ import {
17
19
languageList ,
18
20
maxMatchTimeout ,
19
21
minMatchTimeout ,
22
+ QUESTION_DOES_NOT_EXIST_ERROR ,
20
23
USE_MATCH_ERROR_MESSAGE ,
21
24
} from "../../utils/constants" ;
22
25
import reducer , {
23
26
getQuestionCategories ,
27
+ getQuestionList ,
24
28
initialState as initialState ,
25
29
} from "../../reducers/questionReducer" ;
26
30
import CustomChip from "../../components/CustomChip" ;
27
31
import homepageImage from "/homepage_image.svg" ;
28
32
import { useMatch } from "../../contexts/MatchContext" ;
29
33
import Loader from "../../components/Loader" ;
34
+ import { toast } from "react-toastify" ;
30
35
31
36
const Home : React . FC = ( ) => {
32
37
const [ complexities , setComplexities ] = useState < string [ ] > ( [ ] ) ;
33
38
const [ categories , setCategories ] = useState < string [ ] > ( [ ] ) ;
34
39
const [ languages , setLanguages ] = useState < string [ ] > ( [ ] ) ;
35
40
const [ timeout , setTimeout ] = useState < number | undefined > ( 30 ) ;
36
41
42
+ const [ isQueryingQnDB , setIsQueryingQnDB ] = useState < boolean > ( false ) ;
43
+
37
44
const [ state , dispatch ] = useReducer ( reducer , initialState ) ;
38
45
39
46
const match = useMatch ( ) ;
@@ -42,10 +49,23 @@ const Home: React.FC = () => {
42
49
}
43
50
const { findMatch, loading } = match ;
44
51
52
+ const isSmallerThan1100px = useMediaQuery ( '(max-width:1100px)' ) ;
53
+
45
54
useEffect ( ( ) => {
46
55
getQuestionCategories ( dispatch ) ;
47
56
} , [ ] ) ;
48
57
58
+ useEffect ( ( ) => {
59
+ if ( isQueryingQnDB ) {
60
+ if ( state . questions . length > 0 ) {
61
+ findMatch ( complexities , categories , languages , timeout ! ) ;
62
+ } else {
63
+ toast . error ( QUESTION_DOES_NOT_EXIST_ERROR ) ;
64
+ }
65
+ }
66
+ setIsQueryingQnDB ( false ) ;
67
+ } , [ state . questions ] ) ;
68
+
49
69
if ( loading ) {
50
70
return < Loader /> ;
51
71
}
@@ -78,19 +98,21 @@ const Home: React.FC = () => {
78
98
Specify your question preferences and sit back as we find you the best
79
99
match.
80
100
</ Typography >
81
- < Box
82
- component = "img"
83
- src = { homepageImage }
84
- alt = "Interview Practice Buddy"
85
- sx = { {
86
- position : "absolute" ,
87
- top : "35%" ,
88
- left : "10%" ,
89
- width : "128px" ,
90
- height : "auto" ,
91
- objectFit : "contain" ,
92
- } }
93
- />
101
+ { isSmallerThan1100px && (
102
+ < Box
103
+ component = "img"
104
+ src = { homepageImage }
105
+ alt = "Interview Practice Buddy"
106
+ sx = { {
107
+ position : "absolute" ,
108
+ top : "35%" ,
109
+ left : "10%" ,
110
+ width : "128px" ,
111
+ height : "auto" ,
112
+ objectFit : "contain" ,
113
+ } }
114
+ />
115
+ ) }
94
116
< Card
95
117
sx = { {
96
118
padding : 4 ,
@@ -272,11 +294,12 @@ const Home: React.FC = () => {
272
294
categories . length == 0 ||
273
295
languages . length == 0
274
296
}
275
- onClick = { ( ) =>
276
- findMatch ( complexities , categories , languages , timeout ! )
277
- }
297
+ onClick = { ( ) => {
298
+ setIsQueryingQnDB ( true ) ;
299
+ getQuestionList ( 1 , 10 , "" , complexities , categories , dispatch ) ;
300
+ } }
278
301
>
279
- Find my match!
302
+ { isQueryingQnDB ? < CircularProgress /> : " Find my match!" }
280
303
</ Button >
281
304
</ Card >
282
305
</ AppMargin >
0 commit comments