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
}
@@ -79,19 +99,21 @@ const Home: React.FC = () => {
79
99
Specify your question preferences and sit back as we find you the best
80
100
match.
81
101
</ Typography >
82
- < Box
83
- component = "img"
84
- src = { homepageImage }
85
- alt = "Interview Practice Buddy"
86
- sx = { {
87
- position : "absolute" ,
88
- top : "35%" ,
89
- left : "10%" ,
90
- width : "128px" ,
91
- height : "auto" ,
92
- objectFit : "contain" ,
93
- } }
94
- />
102
+ { isSmallerThan1100px && (
103
+ < Box
104
+ component = "img"
105
+ src = { homepageImage }
106
+ alt = "Interview Practice Buddy"
107
+ sx = { {
108
+ position : "absolute" ,
109
+ top : "35%" ,
110
+ left : "10%" ,
111
+ width : "128px" ,
112
+ height : "auto" ,
113
+ objectFit : "contain" ,
114
+ } }
115
+ />
116
+ ) }
95
117
< Card
96
118
sx = { {
97
119
padding : 4 ,
@@ -273,11 +295,12 @@ const Home: React.FC = () => {
273
295
categories . length == 0 ||
274
296
languages . length == 0
275
297
}
276
- onClick = { ( ) =>
277
- findMatch ( complexities , categories , languages , timeout ! )
278
- }
298
+ onClick = { ( ) => {
299
+ setIsQueryingQnDB ( true ) ;
300
+ getQuestionList ( 1 , 10 , "" , complexities , categories , dispatch ) ;
301
+ } }
279
302
>
280
- Find my match!
303
+ { isQueryingQnDB ? < CircularProgress /> : " Find my match!" }
281
304
</ Button >
282
305
</ Card >
283
306
</ AppMargin >
0 commit comments