Skip to content

Commit e48ece1

Browse files
committed
Add question criteria check for matching form
1 parent 6e696fd commit e48ece1

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

frontend/src/pages/Home/index.tsx

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
Grid2,
88
TextField,
99
Typography,
10+
CircularProgress,
11+
useMediaQuery,
1012
} from "@mui/material";
1113
import { useEffect, useReducer, useState } from "react";
1214

@@ -17,23 +19,28 @@ import {
1719
languageList,
1820
maxMatchTimeout,
1921
minMatchTimeout,
22+
QUESTION_DOES_NOT_EXIST_ERROR,
2023
USE_MATCH_ERROR_MESSAGE,
2124
} from "../../utils/constants";
2225
import reducer, {
2326
getQuestionCategories,
27+
getQuestionList,
2428
initialState as initialState,
2529
} from "../../reducers/questionReducer";
2630
import CustomChip from "../../components/CustomChip";
2731
import homepageImage from "/homepage_image.svg";
2832
import { useMatch } from "../../contexts/MatchContext";
2933
import Loader from "../../components/Loader";
34+
import { toast } from "react-toastify";
3035

3136
const Home: React.FC = () => {
3237
const [complexities, setComplexities] = useState<string[]>([]);
3338
const [categories, setCategories] = useState<string[]>([]);
3439
const [languages, setLanguages] = useState<string[]>([]);
3540
const [timeout, setTimeout] = useState<number | undefined>(30);
3641

42+
const [isQueryingQnDB, setIsQueryingQnDB] = useState<boolean>(false);
43+
3744
const [state, dispatch] = useReducer(reducer, initialState);
3845

3946
const match = useMatch();
@@ -42,10 +49,23 @@ const Home: React.FC = () => {
4249
}
4350
const { findMatch, loading } = match;
4451

52+
const isSmallerThan1100px = useMediaQuery('(max-width:1100px)');
53+
4554
useEffect(() => {
4655
getQuestionCategories(dispatch);
4756
}, []);
4857

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+
4969
if (loading) {
5070
return <Loader />;
5171
}
@@ -79,19 +99,21 @@ const Home: React.FC = () => {
7999
Specify your question preferences and sit back as we find you the best
80100
match.
81101
</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+
)}
95117
<Card
96118
sx={{
97119
padding: 4,
@@ -273,11 +295,12 @@ const Home: React.FC = () => {
273295
categories.length == 0 ||
274296
languages.length == 0
275297
}
276-
onClick={() =>
277-
findMatch(complexities, categories, languages, timeout!)
278-
}
298+
onClick={() => {
299+
setIsQueryingQnDB(true);
300+
getQuestionList(1, 10, "", complexities, categories, dispatch);
301+
}}
279302
>
280-
Find my match!
303+
{isQueryingQnDB ? <CircularProgress /> : "Find my match!"}
281304
</Button>
282305
</Card>
283306
</AppMargin>

frontend/src/utils/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ export const MATCH_LOGIN_REQUIRED_MESSAGE =
9191
export const MATCH_OFFER_TIMEOUT_MESSAGE = "Match offer timeout!";
9292
export const MATCH_CONNECTION_ERROR =
9393
"Connection error! Please try again later.";
94+
export const QUESTION_DOES_NOT_EXIST_ERROR =
95+
"There are no questions with the specified complexity and category. Please try another combination.";
9496

9597
/* Image paths */
9698
export const FIND_MATCH_FORM_PATH = "/find_match_form.png";

0 commit comments

Comments
 (0)