Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ import type * as _model_tournaments_mutations_publishTournament from "../_model/
import type * as _model_tournaments_mutations_startTournament from "../_model/tournaments/mutations/startTournament.js";
import type * as _model_tournaments_mutations_startTournamentRound from "../_model/tournaments/mutations/startTournamentRound.js";
import type * as _model_tournaments_mutations_toggleTournamentAlignmentsRevealed from "../_model/tournaments/mutations/toggleTournamentAlignmentsRevealed.js";
import type * as _model_tournaments_mutations_toggleTournamentListsRevealed from "../_model/tournaments/mutations/toggleTournamentListsRevealed.js";
import type * as _model_tournaments_mutations_updateTournament from "../_model/tournaments/mutations/updateTournament.js";
import type * as _model_tournaments_queries_getTournament from "../_model/tournaments/queries/getTournament.js";
import type * as _model_tournaments_queries_getTournamentByTournamentPairing from "../_model/tournaments/queries/getTournamentByTournamentPairing.js";
Expand Down Expand Up @@ -575,6 +576,7 @@ declare const fullApi: ApiFromModules<{
"_model/tournaments/mutations/startTournament": typeof _model_tournaments_mutations_startTournament;
"_model/tournaments/mutations/startTournamentRound": typeof _model_tournaments_mutations_startTournamentRound;
"_model/tournaments/mutations/toggleTournamentAlignmentsRevealed": typeof _model_tournaments_mutations_toggleTournamentAlignmentsRevealed;
"_model/tournaments/mutations/toggleTournamentListsRevealed": typeof _model_tournaments_mutations_toggleTournamentListsRevealed;
"_model/tournaments/mutations/updateTournament": typeof _model_tournaments_mutations_updateTournament;
"_model/tournaments/queries/getTournament": typeof _model_tournaments_queries_getTournament;
"_model/tournaments/queries/getTournamentByTournamentPairing": typeof _model_tournaments_queries_getTournamentByTournamentPairing;
Expand Down
3 changes: 3 additions & 0 deletions convex/_model/tournaments/_helpers/getAvailableActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export enum TournamentActionKey {

ToggleFactionsRevealed = 'toggleFactionsRevealed',

ToggleListsRevealed = 'toggleListsRevealed',

/** Set a published Tournament's status to 'active'. */
Start = 'start',

Expand Down Expand Up @@ -127,6 +129,7 @@ export const getAvailableActions = async (
if (isOrganizer) {
actions.push(TournamentActionKey.ToggleAlignmentsRevealed);
actions.push(TournamentActionKey.ToggleFactionsRevealed);
actions.push(TournamentActionKey.ToggleListsRevealed);
}

if (isOrganizer && doc.status === 'published') { // TODO: Check for at least 2 competitors
Expand Down
4 changes: 4 additions & 0 deletions convex/_model/tournaments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export {
toggleTournamentAlignmentsRevealed,
toggleTournamentAlignmentsRevealedArgs,
} from './mutations/toggleTournamentAlignmentsRevealed';
export {
toggleTournamentListsRevealed,
toggleTournamentListsRevealedArgs,
} from './mutations/toggleTournamentListsRevealed';
export {
updateTournament,
updateTournamentArgs,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
ConvexError,
Infer,
v,
} from 'convex/values';

import { MutationCtx } from '../../../_generated/server';
import { getDocStrict } from '../../common/_helpers/getDocStrict';
import { getErrorMessage } from '../../common/errors';
import { getAvailableActions, TournamentActionKey } from '../_helpers/getAvailableActions';

export const toggleTournamentListsRevealedArgs = v.object({
id: v.id('tournaments'),
});

export const toggleTournamentListsRevealed = async (
ctx: MutationCtx,
args: Infer<typeof toggleTournamentListsRevealedArgs>,
): Promise<boolean> => {

// --- AUTH ----
const tournament = await getDocStrict(ctx, args.id);
const availableActions = await getAvailableActions(ctx, tournament);
if (!availableActions.includes(TournamentActionKey.ToggleListsRevealed)) {
throw new ConvexError(getErrorMessage('USER_DOES_NOT_HAVE_PERMISSION'));
}

// ---- PRIMARY ACTIONS ----
const listsRevealed = !tournament.listsRevealed;
await ctx.db.patch(args.id, {
listsRevealed,
});
return listsRevealed;
};
5 changes: 5 additions & 0 deletions convex/tournaments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export const toggleTournamentAlignmentsRevealed = mutation({
handler: model.toggleTournamentAlignmentsRevealed,
});

export const toggleTournamentListsRevealed = mutation({
args: model.toggleTournamentListsRevealedArgs,
handler: model.toggleTournamentListsRevealed,
});

export const endTournamentRound = mutation({
args: model.endTournamentRoundArgs,
handler: model.endTournamentRound,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const useToggleAlignmentsRevealedAction = (
subject: Tournament,
): ActionDefinition<TournamentActionKey> | null => {
const { mutation } = useToggleTournamentAlignmentsRevealed({
onSuccess: (revealed): void => toast.success(`${subject.displayName} is now ${revealed ? 'active' : 'inactive'}.`),
onSuccess: (revealed): void => toast.success(revealed ? 'Alignments are now visible to all players.' : 'Alignments are now hidden.'),
});
if (subject.availableActions.includes(KEY)) {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Tournament, TournamentActionKey } from '~/api';
import { ActionDefinition } from '~/components/ContextMenu/ContextMenu.types';
import { toast } from '~/components/ToastProvider';
import { useToggleTournamentListsRevealed } from '~/services/tournaments';

const KEY = TournamentActionKey.ToggleListsRevealed;

export const useToggleListsRevealedAction = (
subject: Tournament,
): ActionDefinition<TournamentActionKey> | null => {
const { mutation } = useToggleTournamentListsRevealed({
onSuccess: (revealed): void => toast.success(revealed ? 'Lists are now visible to all players.' : 'Lists are now hidden.'),
});
if (subject.availableActions.includes(KEY)) {
return {
key: KEY,
label: subject.listsRevealed ? 'Hide Lists' : 'Reveal Lists',
handler: () => mutation({ id: subject._id }),
};
}
return null;
};
1 change: 1 addition & 0 deletions src/services/tournaments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const usePublishTournament = createMutationHook(api.tournaments.publishTo
export const useStartTournament = createMutationHook(api.tournaments.startTournament);
export const useStartTournamentRound = createMutationHook(api.tournaments.startTournamentRound);
export const useToggleTournamentAlignmentsRevealed = createMutationHook(api.tournaments.toggleTournamentAlignmentsRevealed);
export const useToggleTournamentListsRevealed = createMutationHook(api.tournaments.toggleTournamentListsRevealed);

// Actions
export const useExportFowV4TournamentMatchData = createActionHook(api.tournaments.exportFowV4TournamentMatchData);