v0.1 - макет интерфейса, аутентификация через логин, аккаунт админа, создание партии в 2 этапа, базовые настройки профиля и группы, переключение между группами, статистика
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { api, unwrap } from "../api/client";
|
||||
import { qk } from "../api/queryKeys";
|
||||
import type { FactionRead, MatchCreate, MatchFinish, MatchRead } from "../domain/types";
|
||||
|
||||
export function useMatch(matchId: number | null) {
|
||||
return useQuery({
|
||||
queryKey: matchId ? qk.match(matchId) : ["match", "none"],
|
||||
enabled: matchId != null,
|
||||
queryFn: async () =>
|
||||
unwrap(
|
||||
await api.GET("/api/matches/{match_id}", {
|
||||
params: { path: { match_id: matchId as number } },
|
||||
}),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function useRandomizeFaction() {
|
||||
return useMutation({
|
||||
mutationFn: async (args: { group_id: number; exclude_faction_ids: number[] }): Promise<FactionRead> => {
|
||||
const res = unwrap(await api.POST("/api/matches/randomize-faction", { body: args }));
|
||||
return res.faction;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/** Этап 1: старт партии (игроки + фракции, без мест). */
|
||||
export function useStartMatch() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (body: MatchCreate): Promise<MatchRead> =>
|
||||
unwrap(await api.POST("/api/matches", { body })),
|
||||
onSuccess: (m) => {
|
||||
qc.invalidateQueries({ queryKey: qk.groupMatches(m.group_id) });
|
||||
qc.invalidateQueries({ queryKey: qk.home });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/** Этап 2: завершение партии (места, причина победы). */
|
||||
export function useFinishMatch() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (args: { matchId: number; body: MatchFinish }): Promise<MatchRead> =>
|
||||
unwrap(
|
||||
await api.POST("/api/matches/{match_id}/finish", {
|
||||
params: { path: { match_id: args.matchId } },
|
||||
body: args.body,
|
||||
}),
|
||||
),
|
||||
onSuccess: (m) => {
|
||||
qc.invalidateQueries({ queryKey: qk.match(m.id) });
|
||||
qc.invalidateQueries({ queryKey: qk.groupMatches(m.group_id) });
|
||||
qc.invalidateQueries({ queryKey: qk.groupStats(m.group_id) });
|
||||
qc.invalidateQueries({ queryKey: qk.leaderboard });
|
||||
qc.invalidateQueries({ queryKey: qk.home });
|
||||
qc.invalidateQueries({ queryKey: qk.myStats });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteMatch() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (matchId: number) =>
|
||||
unwrap(
|
||||
await api.DELETE("/api/matches/{match_id}", {
|
||||
params: { path: { match_id: matchId } },
|
||||
}),
|
||||
),
|
||||
onSuccess: () => qc.invalidateQueries(),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user