|
1 | | -import { useState, useContext } from "react"; |
2 | | -import PropTypes from "prop-types"; |
| 1 | +import { useContext, useState } from "react"; |
3 | 2 |
|
4 | 3 | // components |
5 | | -import BackArrow from "./BackArrow"; |
6 | | -import ResetGameButton from "../ResetGame/ResetGameButton"; |
7 | | -import ResetGameModal from "../ResetGame/ResetGameModal"; |
8 | | -import SwitchRaceTrackModal from "../SwitchRace/SwitchRaceTrackModal"; |
9 | | -import Footer from "../Shared/Footer"; |
| 4 | +import { ConfirmationModal, Footer } from "@components"; |
10 | 5 |
|
11 | 6 | // context |
12 | | -import { GlobalStateContext } from "@context/GlobalContext"; |
| 7 | +import { GlobalDispatchContext } from "@context/GlobalContext"; |
| 8 | +import { RESET_GAME, SCREEN_MANAGER, SET_ERROR } from "@context/types"; |
| 9 | + |
| 10 | +// utils |
| 11 | +import { backendAPI, getErrorMessage } from "@utils"; |
| 12 | + |
| 13 | +export const AdminView = () => { |
| 14 | + const dispatch = useContext(GlobalDispatchContext); |
13 | 15 |
|
14 | | -function AdminView({ setShowSettings }) { |
15 | | - const { tracks } = useContext(GlobalStateContext); |
16 | | - const [message, setMessage] = useState(false); |
17 | 16 | const [showResetGameModal, setShowResetGameModal] = useState(false); |
18 | | - const [showTrackModal, setShowTrackModal] = useState(false); |
19 | | - const [selectedTrack, setSelectedTrack] = useState(null); |
20 | 17 |
|
21 | 18 | function handleToggleShowResetGameModal() { |
22 | 19 | setShowResetGameModal(!showResetGameModal); |
23 | 20 | } |
24 | 21 |
|
25 | | - function handleToggleShowTrackModal(track) { |
26 | | - setSelectedTrack(track); |
27 | | - setShowTrackModal(!showTrackModal); |
28 | | - } |
29 | | - |
30 | | - function handleTrackSelect(track) { |
31 | | - setSelectedTrack(track.id); |
32 | | - setShowTrackModal(true); |
33 | | - } |
| 22 | + const handleResetGame = async () => { |
| 23 | + await backendAPI |
| 24 | + .post("/race/reset-game") |
| 25 | + .then(() => { |
| 26 | + dispatch({ type: RESET_GAME }); |
| 27 | + dispatch({ type: SCREEN_MANAGER.SHOW_HOME_SCREEN }); |
| 28 | + }) |
| 29 | + .catch((error) => { |
| 30 | + dispatch({ |
| 31 | + type: SET_ERROR, |
| 32 | + payload: { error: getErrorMessage("resetting", error) }, |
| 33 | + }); |
| 34 | + }) |
| 35 | + .finally(() => { |
| 36 | + handleToggleShowResetGameModal(); |
| 37 | + }); |
| 38 | + }; |
34 | 39 |
|
35 | 40 | return ( |
36 | 41 | <> |
| 42 | + <h2 className="text-white">Settings</h2> |
| 43 | + |
| 44 | + <Footer> |
| 45 | + <button className="btn-primary" onClick={handleToggleShowResetGameModal}> |
| 46 | + Reset Game |
| 47 | + </button> |
| 48 | + </Footer> |
| 49 | + |
37 | 50 | {showResetGameModal && ( |
38 | | - <ResetGameModal handleToggleShowModal={handleToggleShowResetGameModal} setMessage={setMessage} /> |
39 | | - )} |
40 | | - {showTrackModal && selectedTrack && ( |
41 | | - <SwitchRaceTrackModal |
42 | | - track={tracks?.find((track) => track.id === selectedTrack)} |
43 | | - handleToggleShowModal={() => handleToggleShowTrackModal(null)} |
44 | | - setMessage={setMessage} |
| 51 | + <ConfirmationModal |
| 52 | + title="Reset Game" |
| 53 | + message="If you reset the game, the leaderboard will be removed. Are you sure that you would like to continue?" |
| 54 | + handleOnConfirm={handleResetGame} |
| 55 | + handleToggleShowConfirmationModal={handleToggleShowResetGameModal} |
45 | 56 | /> |
46 | 57 | )} |
47 | | - <BackArrow setShowSettings={setShowSettings} /> |
48 | | - <div className="px-4 pb-20"> |
49 | | - <div className="text-center pb-8"> |
50 | | - <h2>Settings</h2> |
51 | | - <p className="pt-4">Select a track to change the current one.</p> |
52 | | - <p>{message}</p> |
53 | | - </div> |
54 | | - {tracks?.map((track) => ( |
55 | | - <div |
56 | | - key={track.id} |
57 | | - className={`mb-2 ${selectedTrack === track.id ? "selected" : ""}`} |
58 | | - onClick={() => handleTrackSelect(track)} |
59 | | - > |
60 | | - <div className="card small"> |
61 | | - <div className="card-image" style={{ height: "auto" }}> |
62 | | - <img src={track?.thumbnail} alt={track.name} /> |
63 | | - </div> |
64 | | - <div className="card-details"> |
65 | | - <h4 className="card-title h4">{track.name}</h4> |
66 | | - </div> |
67 | | - </div> |
68 | | - </div> |
69 | | - ))} |
70 | | - </div> |
71 | | - <Footer> |
72 | | - <ResetGameButton handleToggleShowModal={handleToggleShowResetGameModal} /> |
73 | | - </Footer> |
74 | 58 | </> |
75 | 59 | ); |
76 | | -} |
77 | | - |
78 | | -AdminView.propTypes = { |
79 | | - setShowSettings: PropTypes.func, |
80 | 60 | }; |
81 | 61 |
|
82 | 62 | export default AdminView; |
0 commit comments