Skip to content

Commit 0fb16ec

Browse files
committed
Refactor code
1 parent f80dc69 commit 0fb16ec

File tree

10 files changed

+106
-121
lines changed

10 files changed

+106
-121
lines changed

frontend/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { BrowserRouter, Routes, Route } from "react-router-dom";
22
import Layout from "./components/Layout";
33
import NewQuestion from "./pages/NewQuestion";
44
import QuestionDetail from "./pages/QuestionDetail";
5-
import PageNotFound from "./pages/Error";
5+
import PageNotFound from "./pages/PageNotFound";
66

77
function App() {
88
return (

frontend/src/components/AppMargin/index.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import { Box } from "@mui/material";
2-
import { FunctionComponent, ReactNode } from "react";
32

4-
type AppMarginProps = { classname?: string; children: ReactNode };
3+
type AppMarginProps = { classname?: string; children: React.ReactNode };
54

6-
const AppMargin: FunctionComponent<AppMarginProps> = (
7-
props: AppMarginProps
8-
) => {
5+
const AppMargin: React.FC<AppMarginProps> = (props) => {
96
const { classname, children } = props;
107
return (
118
<Box

frontend/src/components/Layout/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { Box } from "@mui/material";
2-
import { FunctionComponent } from "react";
32
import { Outlet } from "react-router-dom";
43
import Navbar from "../Navbar";
54

6-
const Layout: FunctionComponent = () => {
5+
const Layout: React.FC = () => {
76
return (
87
<Box
98
sx={{

frontend/src/components/Navbar/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { AppBar, Box, Link, Toolbar, Typography } from "@mui/material";
22
import { grey } from "@mui/material/colors";
3-
import { FunctionComponent } from "react";
43
import AppMargin from "../AppMargin";
54
import { useNavigate } from "react-router-dom";
65

76
type NavbarItem = { label: string; link: string };
87

98
type NavbarProps = { navbarItems?: Array<NavbarItem> };
109

11-
const Navbar: FunctionComponent<NavbarProps> = (props: NavbarProps) => {
10+
const Navbar: React.FC<NavbarProps> = (props) => {
1211
const { navbarItems = [{ label: "Questions", link: "/questions" }] } = props;
1312
const navigate = useNavigate();
1413

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Box, Typography } from "@mui/material";
2+
3+
type NotFoundProps = { title: string; subtitle: string };
4+
5+
const NotFound: React.FC<NotFoundProps> = (props) => {
6+
const { title, subtitle } = props;
7+
return (
8+
<Box>
9+
<Typography
10+
component={"h1"}
11+
variant="h3"
12+
textAlign={"center"}
13+
sx={(theme) => ({ marginBottom: theme.spacing(4) })}
14+
>
15+
{title}
16+
</Typography>
17+
<Typography textAlign={"center"}>{subtitle}</Typography>
18+
</Box>
19+
);
20+
};
21+
22+
export default NotFound;
File renamed without changes.

frontend/src/pages/Error/index.tsx renamed to frontend/src/pages/PageNotFound/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { FunctionComponent } from "react";
21
import AppMargin from "../../components/AppMargin";
32
import { Box, Typography } from "@mui/material";
43
import classes from "./index.module.css";
54

6-
const PageNotFound: FunctionComponent = () => {
5+
const PageNotFound: React.FC = () => {
76
return (
87
<AppMargin classname={`${classes.fullheight} ${classes.center}`}>
98
<Box>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.fullheight {
2+
flex: 1;
3+
}
4+
5+
.center {
6+
display: flex;
7+
flex-direction: column;
8+
align-items: center;
9+
justify-content: center;
10+
}

frontend/src/pages/QuestionDetail/index.tsx

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,105 @@ import {
77
Typography,
88
useTheme,
99
} from "@mui/material";
10-
import { FunctionComponent, useEffect, useReducer } from "react";
10+
import { useEffect, useState } from "react";
1111
import { useParams } from "react-router-dom";
1212
import Markdown from "markdown-to-jsx";
1313
import AppMargin from "../../components/AppMargin";
14-
import reducer, {
15-
getQuestionById,
16-
initialState,
17-
} from "../../reducers/questionReducer";
1814
import { grey } from "@mui/material/colors";
15+
import classes from "./index.module.css";
16+
import NotFound from "../../components/NotFound";
1917

20-
const QuestionDetail: FunctionComponent = () => {
18+
type Question = {
19+
title: string;
20+
description: string;
21+
complexity: string;
22+
categories: Array<string>;
23+
};
24+
25+
const QuestionDetail: React.FC = () => {
2126
const { questionId } = useParams<{ questionId: string }>();
22-
const [state, dispatch] = useReducer(reducer, initialState);
27+
const [question, setQuestion] = useState<Question | null>(null);
28+
const [isLoading, setIsLoading] = useState(true);
2329
const theme = useTheme();
2430

2531
useEffect(() => {
2632
if (!questionId) {
33+
setIsLoading(false);
2734
return;
2835
}
29-
getQuestionById(questionId, dispatch);
30-
// eslint-disable-next-line react-hooks/exhaustive-deps
36+
37+
// TODO: fetch question
38+
const md =
39+
"# Sample header 1\n" +
40+
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br /><br />" +
41+
"**Example ordered list:**\n" +
42+
"1. Item 1\n" +
43+
"2. `Item 2`\n\n" +
44+
"*Example unordered list:*\n" +
45+
"- Item 1\n" +
46+
"- Item 2";
47+
setQuestion({
48+
title: "Test Question",
49+
description: md,
50+
complexity: "Medium",
51+
categories: ["Category 1", "Category 2"],
52+
});
53+
setIsLoading(false);
3154
}, []);
3255

33-
if (!state.selectedQuestion) {
34-
return;
56+
if (!question) {
57+
if (isLoading) {
58+
return (
59+
<AppMargin classname={`${classes.fullheight} ${classes.center}`}>
60+
<NotFound
61+
title="Question not found..."
62+
subtitle="Unfortunately, we can't find what you're looking for 😥"
63+
/>
64+
</AppMargin>
65+
);
66+
} else {
67+
return;
68+
}
3569
}
3670

3771
return (
3872
<AppMargin>
39-
<Box sx={{ marginTop: theme.spacing(4), marginBottom: theme.spacing(4) }}>
73+
<Box
74+
sx={(theme) => ({
75+
marginTop: theme.spacing(4),
76+
marginBottom: theme.spacing(4),
77+
})}
78+
>
4079
<Box
41-
sx={{ marginTop: theme.spacing(4), marginBottom: theme.spacing(4) }}
80+
sx={(theme) => ({
81+
marginTop: theme.spacing(4),
82+
marginBottom: theme.spacing(4),
83+
})}
4284
>
4385
<Typography component={"h1"} variant="h3">
44-
{state.selectedQuestion.title}
86+
{question.title}
4587
</Typography>
46-
<Stack direction={"row"} sx={{ marginTop: theme.spacing(2) }}>
88+
<Stack
89+
direction={"row"}
90+
sx={(theme) => ({ marginTop: theme.spacing(2) })}
91+
>
4792
<Chip
48-
key={state.selectedQuestion.complexity}
49-
label={state.selectedQuestion.complexity}
93+
key={question.complexity}
94+
label={question.complexity}
5095
color="primary"
51-
sx={{
96+
sx={(theme) => ({
5297
marginLeft: theme.spacing(1),
5398
marginRight: theme.spacing(1),
54-
}}
99+
})}
55100
/>
56-
{state.selectedQuestion.categories.map((cat) => (
101+
{question.categories.map((cat) => (
57102
<Chip
58103
key={cat}
59104
label={cat}
60-
sx={{
105+
sx={(theme) => ({
61106
marginLeft: theme.spacing(1),
62107
marginRight: theme.spacing(1),
63-
}}
108+
})}
64109
/>
65110
))}
66111
</Stack>
@@ -119,7 +164,7 @@ const QuestionDetail: FunctionComponent = () => {
119164
},
120165
}}
121166
>
122-
{state.selectedQuestion.description}
167+
{question.description}
123168
</Markdown>
124169
</Box>
125170
</AppMargin>

frontend/src/reducers/questionReducer.ts

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)