@@ -165,18 +165,6 @@ export default function Quiz({ params }: { params: { query: string[] } }) {
165
165
router . push ( "/" ) ;
166
166
}
167
167
168
- // See if the player has a UUID in local storage, if not, create one and store it
169
- let uuid : string | null = localStorage . getItem ( "quizPlayerUUID" ) ;
170
- if ( uuid ) {
171
- // If there is a UUID, retrieve the player's previous leaderboard entry (if any)
172
- const entry : TimeAttackEntry | SurvivalEntry | null = await getPastPlayerLeaderboardEntry ( quizId , uuid , shareableToken || undefined ) ;
173
- if ( entry ) setPreviousLeaderboardEntry ( entry ) ;
174
- } else {
175
- uuid = uuidv4 ( ) ; // Generate a new one
176
- localStorage . setItem ( "quizPlayerUUID" , uuid ) ;
177
- }
178
- setPlayerUUID ( uuid ) ;
179
-
180
168
// See if the player has a stored preference for playing SFX in local storage
181
169
let sfxPref : string | null = localStorage . getItem ( "quizSFX" ) ;
182
170
if ( ! sfxPref ) {
@@ -209,6 +197,30 @@ export default function Quiz({ params }: { params: { query: string[] } }) {
209
197
getPageData ( ) ;
210
198
} , [ ] ) ;
211
199
200
+ // Handles the generation/retrieval of the player's UUID and previous leaderboard entry (for updating)
201
+ // Runs on component mount and whenever a quiz is restarted after a score is submitted.
202
+ useEffect ( ( ) => {
203
+ const setUuidAndGetPreviousEntry = async ( ) => {
204
+
205
+ if ( scoreSubmitted ) return ;
206
+ // When scoreSubmitted changes from true to false, the player submitted a score and clicked "Play Again".
207
+ // This means they will have a new previous entry, and we need to update the state accordingly.
208
+
209
+ // See if the player has a UUID in local storage, if not, create one and store it
210
+ let uuid : string | null = localStorage . getItem ( "quizPlayerUUID" ) ;
211
+ if ( uuid ) {
212
+ // If there is a UUID, retrieve the player's previous leaderboard entry (if any)
213
+ const entry : TimeAttackEntry | SurvivalEntry | null = await getPastPlayerLeaderboardEntry ( quizId , uuid , shareableToken || undefined ) ;
214
+ if ( entry ) setPreviousLeaderboardEntry ( entry ) ;
215
+ } else {
216
+ uuid = uuidv4 ( ) ; // Generate a new one
217
+ localStorage . setItem ( "quizPlayerUUID" , uuid ) ;
218
+ }
219
+ setPlayerUUID ( uuid ) ;
220
+ }
221
+ setUuidAndGetPreviousEntry ( ) ;
222
+ } , [ scoreSubmitted ] ) ;
223
+
212
224
// ----- DATA HELPERS -----
213
225
214
226
const getNextMessage = async ( ) => {
@@ -736,7 +748,7 @@ export default function Quiz({ params }: { params: { query: string[] } }) {
736
748
</ > ) ;
737
749
} else if ( previousLeaderboardEntry && ! noResubmit ) {
738
750
modalContent = ( < >
739
- < div className = "w-full mt-3 text-center text-2xl font-semibold" >
751
+ < div className = "w-full mt-5 text-center text-2xl font-semibold" >
740
752
Update previous entry?
741
753
</ div >
742
754
< div className = "w-full text-center text-lg mt-3 text-zinc-300 font-light" >
@@ -746,7 +758,7 @@ export default function Quiz({ params }: { params: { query: string[] } }) {
746
758
</ span >
747
759
</ div >
748
760
< div className = "grid grid-cols-2 gap-2 mt-6 mb-5 mx-7" >
749
- < button className = "grow btn btn-lg bg-black border border-zinc-900 text-zinc-400 font-light"
761
+ < button className = "grow btn btn-lg bg-black border border-zinc-900 text-zinc-400 font-light whitespace-nowrap "
750
762
onClick = { ( ) => setNoResubmit ( true ) } >
751
763
No Thanks
752
764
</ button >
0 commit comments