Modals and API calls working for admin tab
This commit is contained in:
164
src/hooks/AccountHooks.ts
Normal file
164
src/hooks/AccountHooks.ts
Normal file
@@ -0,0 +1,164 @@
|
||||
import { Account } from "@/interface/Account";
|
||||
import { api } from "@/util/AxiosUtil";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
|
||||
export function useGetAccounts(page: number, pageSize: number, searchTerm?: string){
|
||||
return useQuery({
|
||||
queryKey: ["accounts", {page, pageSize, searchTerm}],
|
||||
queryFn: async () => {
|
||||
const params = new URLSearchParams();
|
||||
params.append("page", page.toString());
|
||||
params.append("pageSize", pageSize.toString());
|
||||
if(searchTerm){
|
||||
params.append("search", searchTerm ?? "");
|
||||
}
|
||||
|
||||
const response = await api.get(`/account?${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 useForcePasswordReset(accountId: string){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["forcePasswordReset", accountId],
|
||||
mutationFn: async () => {
|
||||
const response = await api.put(`/account/${accountId}/forcePasswordReset`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to force password reset");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["accounts"] });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useResetPassword(accountId: string){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["resetPassword", accountId],
|
||||
mutationFn: async (password: string) => {
|
||||
const response = await api.put(`/account/${accountId}/resetPassword`, {
|
||||
password
|
||||
});
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to reset password");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["accounts"] });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useRevokeRefreshToken(accountId: string){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["revokeRefreshToken", accountId],
|
||||
mutationFn: async () => {
|
||||
const response = await api.put(`/account/${accountId}/revokeRefreshToken`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to revoke refresh token");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["accounts"] });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateAccount(){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["createAccount"],
|
||||
mutationFn: async (account: Account) => {
|
||||
const response = await api.post("/account", account);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to create account");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["accounts"] });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateAccount(){
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["updateAccount"],
|
||||
mutationFn: async (account: Account) => {
|
||||
const response = await api.put(`/account/${account.accountId}`, account);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to update account");
|
||||
}
|
||||
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();
|
||||
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["deleteAccount", accountId],
|
||||
mutationFn: async () => {
|
||||
const response = await api.delete(`/account/${accountId}`);
|
||||
|
||||
if(response.status !== 200){
|
||||
throw new Error("Failed to delete account");
|
||||
}
|
||||
else if(response.data.errors){
|
||||
throw new Error(response.data.errors.join(", "));
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["accounts"] });
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user