@@ -231,10 +231,10 @@ export const useCreateAndEnqueueMatches = (
231231 maps,
232232 } : EpisodeTournamentRoundEnqueueCreateRequest ) => {
233233 const toastFn = async ( ) : Promise < void > => {
234- await createAndEnqueueMatches ( { episodeId, tournament, id, maps } ) ;
235-
236- // Refetch this tournament round and its matches
237234 try {
235+ await createAndEnqueueMatches ( { episodeId, tournament, id, maps } ) ;
236+
237+ // Refetch this tournament round and its matches
238238 const roundInfo = queryClient . refetchQueries ( {
239239 queryKey : buildKey ( episodeQueryKeys . tournamentRoundInfo , {
240240 episodeId,
@@ -248,31 +248,33 @@ export const useCreateAndEnqueueMatches = (
248248 } ) ;
249249
250250 await Promise . all ( [ roundInfo , tourneyMatches ] ) ;
251- } catch ( e ) {
252- toast . error ( ( e as ResponseError ) . message ) ;
251+ } catch ( e : unknown ) {
252+ const error = e as ResponseError ;
253+ // Parse the response text as JSON, detail propety contains the error message
254+ const errorJson = ( await error . response . json ( ) ) as {
255+ detail ?: string ;
256+ } ;
257+ const errorDetail =
258+ errorJson . detail ?? "An unexpected error occurred." ;
259+ throw new Error ( errorDetail ) ;
253260 }
254261 } ;
255262
256263 await toast . promise ( toastFn ( ) , {
257264 loading : "Creating and enqueuing matches..." ,
258265 success : "Matches created and enqueued!" ,
259- error : " Error creating and enqueuing matches." ,
266+ error : ( error : Error ) => error . message , // Return the error message thrown in toastFn
260267 } ) ;
261268 } ,
262269 } ) ;
263270
264271/**
265272 * For releasing the given tournament round to the bracket service.
266273 */
267- export const useReleaseTournamentRound = ( {
268- episodeId,
269- tournament,
270- id,
271- } : EpisodeTournamentRoundReleaseCreateRequest ) : UseMutationResult <
272- void ,
273- Error ,
274- EpisodeTournamentRoundReleaseCreateRequest
275- > =>
274+ export const useReleaseTournamentRound = (
275+ { episodeId, tournament, id } : EpisodeTournamentRoundReleaseCreateRequest ,
276+ queryClient : QueryClient ,
277+ ) : UseMutationResult < void , Error , EpisodeTournamentRoundReleaseCreateRequest > =>
276278 useMutation ( {
277279 mutationKey : episodeMutationKeys . releaseTournamentRound ( {
278280 episodeId,
@@ -285,26 +287,40 @@ export const useReleaseTournamentRound = ({
285287 id,
286288 } : EpisodeTournamentRoundReleaseCreateRequest ) => {
287289 const toastFn = async ( ) : Promise < void > => {
288- await releaseTournamentRound ( { episodeId, tournament, id } ) ;
290+ try {
291+ await releaseTournamentRound ( { episodeId, tournament, id } ) ;
292+
293+ await queryClient . refetchQueries ( {
294+ queryKey : buildKey ( episodeQueryKeys . tournamentRoundInfo , {
295+ episodeId,
296+ tournament,
297+ id,
298+ } ) ,
299+ } ) ;
300+ } catch ( e : unknown ) {
301+ const error = e as ResponseError ;
302+ // Parse the response text as JSON, detail propety contains the error message
303+ const errorJson = ( await error . response . json ( ) ) as {
304+ detail ?: string ;
305+ } ;
306+ const errorDetail =
307+ errorJson . detail ?? "An unexpected error occurred." ;
308+ throw new Error ( errorDetail ) ;
309+ }
289310 } ;
290311
291312 await toast . promise ( toastFn ( ) , {
292313 loading : "Initiating round release..." ,
293314 success : "Round release initiated!" ,
294- error : " Error releasing tournament round." ,
315+ error : ( error : Error ) => error . message , // Return the error message thrown in toastFn
295316 } ) ;
296317 } ,
297318 } ) ;
298319
299- export const useRequeueTournamentRound = ( {
300- episodeId,
301- tournament,
302- id,
303- } : EpisodeTournamentRoundRequeueCreateRequest ) : UseMutationResult <
304- void ,
305- Error ,
306- EpisodeTournamentRoundRequeueCreateRequest
307- > =>
320+ export const useRequeueTournamentRound = (
321+ { episodeId, tournament, id } : EpisodeTournamentRoundRequeueCreateRequest ,
322+ queryClient : QueryClient ,
323+ ) : UseMutationResult < void , Error , EpisodeTournamentRoundRequeueCreateRequest > =>
308324 useMutation ( {
309325 mutationKey : episodeMutationKeys . requeueTournamentRound ( {
310326 episodeId,
@@ -317,15 +333,39 @@ export const useRequeueTournamentRound = ({
317333 id,
318334 } : EpisodeTournamentRoundRequeueCreateRequest ) => {
319335 const toastFn = async ( ) : Promise < void > => {
320- await requeueTournamentRound ( { episodeId, tournament, id } ) ;
336+ try {
337+ await requeueTournamentRound ( { episodeId, tournament, id } ) ;
338+
339+ // Refetch this tournament round and its matches
340+ const roundInfo = queryClient . refetchQueries ( {
341+ queryKey : buildKey ( episodeQueryKeys . tournamentRoundInfo , {
342+ episodeId,
343+ tournament,
344+ id,
345+ } ) ,
346+ } ) ;
321347
322- // TODO: refetch the round and its matches :)
348+ const tourneyMatches = queryClient . refetchQueries ( {
349+ queryKey : buildKey ( competeQueryKeys . matchBase , { episodeId } ) ,
350+ } ) ;
351+
352+ await Promise . all ( [ roundInfo , tourneyMatches ] ) ;
353+ } catch ( e : unknown ) {
354+ const error = e as ResponseError ;
355+ // Parse the response text as JSON, detail propety contains the error message
356+ const errorJson = ( await error . response . json ( ) ) as {
357+ detail ?: string ;
358+ } ;
359+ const errorDetail =
360+ errorJson . detail ?? "An unexpected error occurred." ;
361+ throw new Error ( errorDetail ) ;
362+ }
323363 } ;
324364
325365 await toast . promise ( toastFn ( ) , {
326366 loading : "Initiating round requeue..." ,
327367 success : "Failed matches requeued!" ,
328- error : " Error requeueing tournament round." ,
368+ error : ( error : Error ) => error . message , // Return the error message thrown in toastFn
329369 } ) ;
330370 } ,
331371 } ) ;
0 commit comments