generated from metaversecloud-com/sdk-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSwitchTrackScreen.jsx
More file actions
141 lines (124 loc) · 4.19 KB
/
SwitchTrackScreen.jsx
File metadata and controls
141 lines (124 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { useState, useContext, useEffect } from "react";
// components
import { BackButton, ConfirmationModal, Footer } from "@components";
// context
import { GlobalDispatchContext, GlobalStateContext } from "@context/GlobalContext";
import { SET_ERROR, SCREEN_MANAGER, SET_SCENE_DATA } from "@context/types";
// utils
import { backendAPI, getErrorMessage } from "@utils";
export const SwitchTrackScreen = () => {
const dispatch = useContext(GlobalDispatchContext);
const { tracks, lastRaceStartedDate, isAdmin } = useContext(GlobalStateContext);
const [selectedTrack, setSelectedTrack] = useState(null);
const [areAllButtonsDisabled, setAreAllButtonsDisabled] = useState(false);
const [showRaceWarning, setShowRaceWarning] = useState(false);
const [showConfirmationModal, setShowConfirmationModal] = useState(false);
useEffect(() => {
if (lastRaceStartedDate) {
const now = Date.now();
const diffMinutes = (now - lastRaceStartedDate) / (1000 * 60);
const isRecentRace = diffMinutes < 5;
if (isRecentRace) {
if (isAdmin) {
setAreAllButtonsDisabled(false);
setShowRaceWarning(true);
} else {
setAreAllButtonsDisabled(true);
setShowRaceWarning(false);
}
} else {
setAreAllButtonsDisabled(false);
setShowRaceWarning(false);
}
} else {
setAreAllButtonsDisabled(false);
setShowRaceWarning(false);
}
}, [lastRaceStartedDate, isAdmin]);
const updateTrack = async () => {
setAreAllButtonsDisabled(true);
await backendAPI
.post("/race/switch-track", { selectedTrack })
.then((response) => {
const { leaderboard, numberOfCheckpoints } = response.data.sceneData;
dispatch({
type: SET_SCENE_DATA,
payload: {
leaderboard,
numberOfCheckpoints,
tracks,
},
});
})
.catch((error) => {
dispatch({
type: SET_ERROR,
payload: { error: getErrorMessage("resetting", error) },
});
setAreAllButtonsDisabled(false);
});
};
const handleUpdateTrackClick = () => {
if (showRaceWarning) {
setShowConfirmationModal(true);
} else {
updateTrack();
}
};
return (
<>
<BackButton onClick={() => dispatch({ type: SCREEN_MANAGER.SHOW_HOME_SCREEN })} />
<div className="text-center pb-8">
<h2>Choose New Track</h2>
<p className="pt-4">Updates will reflect for everyone</p>
</div>
<div className="grid grid-cols-2 gap-6">
{tracks?.map((track) => {
return (
<button key={track.id} className="mb-2" onClick={() => setSelectedTrack(track)}>
<div className="tooltip">
<span className="tooltip-content">{track.name}</span>
<img
className={`track object-cover ${selectedTrack && selectedTrack.id === track.id ? "selected" : ""}`}
src={track?.thumbnail}
alt={track.name}
/>
</div>
</button>
);
})}
</div>
<Footer>
{areAllButtonsDisabled && !isAdmin ? (
<div className="tooltip">
<span className="tooltip-content">A race was recently started. Please try again in a few minutes.</span>
<button
className="btn-primary"
disabled={areAllButtonsDisabled || !selectedTrack}
onClick={handleUpdateTrackClick}
>
Update Track
</button>
</div>
) : (
<button
className="btn-primary"
disabled={areAllButtonsDisabled || !selectedTrack}
onClick={handleUpdateTrackClick}
>
Update Track
</button>
)}
</Footer>
{showConfirmationModal && (
<ConfirmationModal
title="Switch Track"
message="A race may currently be in progress. Are you sure you want to switch tracks?"
handleOnConfirm={updateTrack}
handleToggleShowConfirmationModal={() => setShowConfirmationModal(false)}
/>
)}
</>
);
};
export default SwitchTrackScreen;