|
1 | 1 | import CheckCircleOutlineIcon from "@mui/icons-material/CheckCircleOutline"; |
2 | | -import { List, ListItem, Typography } from "@mui/material"; |
| 2 | +import { List, ListItem, Typography, TextField, Button } from "@mui/material"; |
3 | 3 | import Box from "@mui/material/Box"; |
4 | 4 | import { useParams } from "react-router-dom"; |
| 5 | +import { useState } from "react"; |
5 | 6 | import { useGetExercises } from "../../../queries/useGetExercises"; |
6 | 7 | import { CircularProgressCenterLoader } from "../../shared/CircularProgressCenterLoader"; |
7 | 8 |
|
8 | 9 | export const CheckGitHubAccount = () => { |
9 | 10 | const { account } = useParams(); |
10 | | - const { data: exercises, isLoading } = useGetExercises(account); |
| 11 | + const [accountInput, setAccountInput] = useState(""); |
| 12 | + const [searchedAccount, setSearchedAccount] = useState<string | undefined>(account); |
| 13 | + const [hasSearched, setHasSearched] = useState(!!account); |
| 14 | + |
| 15 | + const { data: exercises, isLoading } = useGetExercises(searchedAccount); |
11 | 16 | const completedExercises = |
12 | 17 | exercises?.filter((exercise) => exercise.completed)?.length ?? 0; |
| 18 | + |
| 19 | + const handleSearch = () => { |
| 20 | + const trimmedAccount = accountInput.trim(); |
| 21 | + if (trimmedAccount) { |
| 22 | + setSearchedAccount(trimmedAccount); |
| 23 | + setHasSearched(true); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + const handleKeyPress = (e: React.KeyboardEvent) => { |
| 28 | + if (e.key === "Enter") { |
| 29 | + handleSearch(); |
| 30 | + } |
| 31 | + }; |
13 | 32 | return ( |
14 | 33 | <Box |
15 | 34 | sx={{ |
16 | 35 | display: "flex", |
17 | 36 | justifyContent: "center", |
18 | 37 | p: 6, |
19 | 38 | flexDirection: "column", |
| 39 | + maxWidth: "800px", |
| 40 | + margin: "0 auto", |
20 | 41 | }} |
21 | 42 | > |
22 | | - <Typography variant="h5"> |
23 | | - Results for GitHub account: {account} |
24 | | - </Typography>{" "} |
25 | | - {!isLoading && ( |
26 | | - <Typography> |
27 | | - {completedExercises}/{exercises?.length ?? 54} exercises completed |
28 | | - </Typography> |
29 | | - )} |
30 | | - <br /> |
31 | | - <Box sx={{ maxHeight: "calc(100vh - 300px)", overflowY: "scroll" }}> |
32 | | - {isLoading ? ( |
33 | | - <CircularProgressCenterLoader /> |
34 | | - ) : ( |
35 | | - <List> |
36 | | - {exercises?.map((exercise) => ( |
37 | | - <ListItem |
38 | | - sx={{ |
39 | | - my: 0, |
40 | | - py: 0.5, |
41 | | - }} |
42 | | - key={exercise.id} |
43 | | - > |
44 | | - <Typography |
45 | | - sx={{ |
46 | | - color: exercise.completed ? "#34b830" : "#999", |
47 | | - }} |
48 | | - > |
49 | | - {exercise.name} |
50 | | - </Typography> |
51 | | - {exercise.completed && ( |
52 | | - <CheckCircleOutlineIcon |
53 | | - sx={{ fontSize: 18, color: "#34b830", ml: 2 }} |
54 | | - /> |
55 | | - )} |
56 | | - </ListItem> |
57 | | - ))} |
58 | | - </List> |
59 | | - )} |
| 43 | + <Typography variant="h4" sx={{ mb: 4, textAlign: "center" }}> |
| 44 | + Check Student Progress |
| 45 | + </Typography> |
| 46 | + |
| 47 | + <Box sx={{ display: "flex", gap: 2, mb: 4, alignItems: "center" }}> |
| 48 | + <TextField |
| 49 | + label="GitHub Username" |
| 50 | + variant="outlined" |
| 51 | + fullWidth |
| 52 | + value={accountInput} |
| 53 | + onChange={(e) => setAccountInput(e.target.value)} |
| 54 | + onKeyPress={handleKeyPress} |
| 55 | + placeholder="e.g.: username" |
| 56 | + sx={{ |
| 57 | + "& .MuiOutlinedInput-root": { |
| 58 | + color: "#FFF", |
| 59 | + "& fieldset": { |
| 60 | + borderColor: "#666", |
| 61 | + }, |
| 62 | + "&:hover fieldset": { |
| 63 | + borderColor: "#999", |
| 64 | + }, |
| 65 | + "&.Mui-focused fieldset": { |
| 66 | + borderColor: "#dd3d3d", |
| 67 | + }, |
| 68 | + }, |
| 69 | + "& .MuiInputLabel-root": { |
| 70 | + color: "#CCC", |
| 71 | + "&.Mui-focused": { |
| 72 | + color: "#dd3d3d", |
| 73 | + }, |
| 74 | + }, |
| 75 | + }} |
| 76 | + /> |
| 77 | + <Button |
| 78 | + variant="contained" |
| 79 | + onClick={handleSearch} |
| 80 | + disabled={!accountInput.trim()} |
| 81 | + sx={{ |
| 82 | + backgroundColor: "#dd3d3d", |
| 83 | + "&:hover": { |
| 84 | + backgroundColor: "#bb2d2d", |
| 85 | + }, |
| 86 | + "&:disabled": { |
| 87 | + backgroundColor: "#666", |
| 88 | + }, |
| 89 | + minWidth: "120px", |
| 90 | + height: "56px", |
| 91 | + }} |
| 92 | + > |
| 93 | + Search |
| 94 | + </Button> |
60 | 95 | </Box> |
| 96 | + |
| 97 | + {hasSearched && searchedAccount && ( |
| 98 | + <> |
| 99 | + <Typography variant="h5" sx={{ mb: 2 }}> |
| 100 | + Results for: {searchedAccount} |
| 101 | + </Typography> |
| 102 | + {!isLoading && ( |
| 103 | + <Typography sx={{ mb: 3 }}> |
| 104 | + {completedExercises}/{exercises?.length ?? 54} exercises completed |
| 105 | + </Typography> |
| 106 | + )} |
| 107 | + <Box sx={{ maxHeight: "calc(100vh - 400px)", overflowY: "auto" }}> |
| 108 | + {isLoading ? ( |
| 109 | + <CircularProgressCenterLoader /> |
| 110 | + ) : ( |
| 111 | + <List> |
| 112 | + {exercises?.map((exercise) => ( |
| 113 | + <ListItem |
| 114 | + sx={{ |
| 115 | + my: 0, |
| 116 | + py: 0.5, |
| 117 | + }} |
| 118 | + key={exercise.id} |
| 119 | + > |
| 120 | + <Typography |
| 121 | + sx={{ |
| 122 | + color: exercise.completed ? "#34b830" : "#999", |
| 123 | + }} |
| 124 | + > |
| 125 | + {exercise.name} |
| 126 | + </Typography> |
| 127 | + {exercise.completed && ( |
| 128 | + <CheckCircleOutlineIcon |
| 129 | + sx={{ fontSize: 18, color: "#34b830", ml: 2 }} |
| 130 | + /> |
| 131 | + )} |
| 132 | + </ListItem> |
| 133 | + ))} |
| 134 | + </List> |
| 135 | + )} |
| 136 | + </Box> |
| 137 | + </> |
| 138 | + )} |
61 | 139 | </Box> |
62 | 140 | ); |
63 | 141 | }; |
0 commit comments