User tab working

This commit is contained in:
2025-03-09 19:49:40 -04:00
parent 8b8538a968
commit 65fb3479fd
11 changed files with 536 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
import { Account } from "@/interface/Account";
import { RaidGroupPermissionType } from "@/interface/RaidGroup";
import { api } from "@/util/AxiosUtil";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
@@ -28,6 +29,51 @@ export function useGetAccounts(page: number, pageSize: number, searchTerm?: stri
});
}
export function useGetAccountsByRaidGroup(raidGroupId: string, page: number, pageSize: number, searchTerm?: string){
return useQuery({
queryKey: ["accounts", "raidGroup", raidGroupId, {page, pageSize, searchTerm}],
queryFn: async () => {
const params = new URLSearchParams();
params.append("page", page.toString());
params.append("pageSize", pageSize.toString());
if(searchTerm){
params.append("searchTerm", searchTerm ?? "");
}
const response = await api.get(`/account/raidGroup/${raidGroupId}?${params}`);
if(response.status !== 200){
throw new Error("Failed to get accounts");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
return response.data as Account[];
}
});
}
export function useGetRaidGroupPermissionsForAccount(raidGroupId?: string, accountId?: string){
return useQuery({
queryKey: ["accounts", "raidGroup", raidGroupId, "account", accountId],
queryFn: async () => {
const response = await api.get(`/account/${accountId}/raidGroup/${raidGroupId}/permission`);
if(response.status !== 200){
throw new Error("Failed to get permissions");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
return response.data.permission as RaidGroupPermissionType;
},
enabled: !!raidGroupId && !!accountId
});
}
export function useGetAccountsCount(searchTerm?: string){
const searchParams = new URLSearchParams();
if(searchTerm){
@@ -53,6 +99,33 @@ export function useGetAccountsCount(searchTerm?: string){
});
}
export function useGetAccountsByRaidGroupCount(raidGroupId: string, searchTerm?: string){
const searchParams = new URLSearchParams();
if(searchTerm){
searchParams.append("searchTerm", searchTerm);
}
return useQuery({
queryKey: [ "accounts", "raidGroup", raidGroupId, "count", searchTerm],
queryFn: async () => {
const response = await api.get(`/account/raidGroup/${raidGroupId}/count?${searchParams}`);
if(response.status !== 200){
throw new Error("Failed to get accounts count");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
return response.data.count as number;
}
});
}
export function useForcePasswordReset(accountId: string){
const queryClient = useQueryClient();
@@ -165,6 +238,30 @@ export function useUpdateAccount(){
});
}
export function useUpdateRaidGroupPermissionsForAccount(raidGroupId?: string, accountId?: string){
const queryClient = useQueryClient();
return useMutation({
mutationKey: ["updateRaidGroupPermissionsForAccount", raidGroupId, accountId],
mutationFn: async (permission: RaidGroupPermissionType) => {
const response = await api.put(`/account/${accountId}/raidGroup/${raidGroupId}/permission`, {
permission
});
if(response.status !== 200){
throw new Error("Failed to update permissions");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["accounts"] });
}
});
}
export function useDeleteAccount(accountId: string){
const queryClient = useQueryClient();
@@ -187,3 +284,26 @@ export function useDeleteAccount(accountId: string){
});
}
export function UseRemoveAccountFromRaidGroup(raidGroupId?: string, accountId?: string){
const queryClient = useQueryClient();
return useMutation({
mutationKey: ["removeAccountFromRaidGroup", raidGroupId, accountId],
mutationFn: async () => {
const response = await api.delete(`/account/raidGroup/${raidGroupId}/permissions/${accountId}`);
if(response.status !== 200){
throw new Error("Failed to remove account from raid group");
}
else if(response.data.errors){
throw new Error(response.data.errors.join(", "));
}
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["accounts"] });
}
});
}