v0.1 - макет интерфейса, аутентификация через логин, аккаунт админа, создание партии в 2 этапа, базовые настройки профиля и группы, переключение между группами, статистика
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { api, unwrap } from "../api/client";
|
||||
import { qk } from "../api/queryKeys";
|
||||
|
||||
export function useMyGroups() {
|
||||
return useQuery({
|
||||
queryKey: qk.groups,
|
||||
queryFn: async () => unwrap(await api.GET("/api/groups")),
|
||||
});
|
||||
}
|
||||
|
||||
export function useGroup(groupId: number | null) {
|
||||
return useQuery({
|
||||
queryKey: groupId ? qk.group(groupId) : ["group", "none"],
|
||||
enabled: groupId != null,
|
||||
queryFn: async () =>
|
||||
unwrap(
|
||||
await api.GET("/api/groups/{group_id}", {
|
||||
params: { path: { group_id: groupId as number } },
|
||||
}),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function useGroupMembers(groupId: number | null) {
|
||||
return useQuery({
|
||||
queryKey: groupId ? qk.groupMembers(groupId) : ["group", "none", "members"],
|
||||
enabled: groupId != null,
|
||||
queryFn: async () =>
|
||||
unwrap(
|
||||
await api.GET("/api/groups/{group_id}/members", {
|
||||
params: { path: { group_id: groupId as number } },
|
||||
}),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function useGroupStats(groupId: number | null) {
|
||||
return useQuery({
|
||||
queryKey: groupId ? qk.groupStats(groupId) : ["group", "none", "stats"],
|
||||
enabled: groupId != null,
|
||||
queryFn: async () =>
|
||||
unwrap(
|
||||
await api.GET("/api/groups/{group_id}/stats", {
|
||||
params: { path: { group_id: groupId as number } },
|
||||
}),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function useGroupMatches(groupId: number | null, limit = 20, offset = 0) {
|
||||
return useQuery({
|
||||
queryKey: groupId ? [...qk.groupMatches(groupId), limit, offset] : ["group", "none", "matches"],
|
||||
enabled: groupId != null,
|
||||
queryFn: async () =>
|
||||
unwrap(
|
||||
await api.GET("/api/groups/{group_id}/matches", {
|
||||
params: { path: { group_id: groupId as number }, query: { limit, offset } },
|
||||
}),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateGroup() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (body: { name: string; expansion_ids: number[] }) =>
|
||||
unwrap(await api.POST("/api/groups", { body })),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: qk.groups });
|
||||
qc.invalidateQueries({ queryKey: qk.me });
|
||||
qc.invalidateQueries({ queryKey: qk.home });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useSetGroupExpansions(groupId: number) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (expansion_ids: number[]) =>
|
||||
unwrap(
|
||||
await api.PUT("/api/groups/{group_id}/expansions", {
|
||||
params: { path: { group_id: groupId } },
|
||||
body: { expansion_ids },
|
||||
}),
|
||||
),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: qk.group(groupId) });
|
||||
qc.invalidateQueries({ queryKey: qk.groupFactions(groupId) });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useAddMember(groupId: number) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (nickname: string) =>
|
||||
unwrap(
|
||||
await api.POST("/api/groups/{group_id}/members", {
|
||||
params: { path: { group_id: groupId } },
|
||||
body: { nickname },
|
||||
}),
|
||||
),
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: qk.groupMembers(groupId) }),
|
||||
});
|
||||
}
|
||||
|
||||
export function useRemoveMember(groupId: number) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (userId: number) =>
|
||||
unwrap(
|
||||
await api.DELETE("/api/groups/{group_id}/members/{user_id}", {
|
||||
params: { path: { group_id: groupId, user_id: userId } },
|
||||
}),
|
||||
),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: qk.groupMembers(groupId) });
|
||||
qc.invalidateQueries({ queryKey: qk.me });
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user