mirror of
https://gitlab.massbug.com/massbug/judge4c.git
synced 2025-07-04 15:22:25 +00:00
feat(workspaces): implement hook for creating workspaces with success/error notifications
This commit is contained in:
parent
e96f111a39
commit
0084b56df6
35
src/features/workspaces/api/use-create-workspace.ts
Normal file
35
src/features/workspaces/api/use-create-workspace.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { toast } from "sonner";
|
||||||
|
import { client } from "@/lib/rpc";
|
||||||
|
import { InferRequestType, InferResponseType } from "hono";
|
||||||
|
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||||
|
|
||||||
|
type ResponseType = InferResponseType<(typeof client.api.workspaces)["$post"]>;
|
||||||
|
|
||||||
|
type RequestType = InferRequestType<(typeof client.api.workspaces)["$post"]>;
|
||||||
|
|
||||||
|
export const useCreateWorkspace = () => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
const mutation = useMutation<ResponseType, Error, RequestType>({
|
||||||
|
mutationFn: async ({ json }) => {
|
||||||
|
const response = await client.api.workspaces["$post"]({
|
||||||
|
json,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Failed to create workspace");
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json();
|
||||||
|
},
|
||||||
|
onSuccess: () => {
|
||||||
|
toast.success("Workspace created successfully");
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["workspaces"] });
|
||||||
|
},
|
||||||
|
onError: () => {
|
||||||
|
toast.error("Failed to create workspace");
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return mutation;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user