cos tam dziala
This commit is contained in:
31
front/src/hooks/useLogin.ts
Normal file
31
front/src/hooks/useLogin.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
export function useLogin() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (data: {
|
||||
username: string;
|
||||
password: string;
|
||||
}) => {
|
||||
const res = await fetch(`${import.meta.env.VITE_API_URL}/auth/login`, {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error("Invalid credentials");
|
||||
}
|
||||
|
||||
return res.json();
|
||||
},
|
||||
|
||||
onSuccess(data) {
|
||||
queryClient.setQueryData(["me"], data.user);
|
||||
},
|
||||
});
|
||||
}
|
||||
18
front/src/hooks/useLogout.ts
Normal file
18
front/src/hooks/useLogout.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
export function useLogout() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async () => {
|
||||
await fetch(`${import.meta.env.VITE_API_URL}/auth/logout`, {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
});
|
||||
},
|
||||
|
||||
onSuccess() {
|
||||
queryClient.setQueryData(["me"], null);
|
||||
},
|
||||
});
|
||||
}
|
||||
23
front/src/hooks/useMe.ts
Normal file
23
front/src/hooks/useMe.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
|
||||
|
||||
export function useMe() {
|
||||
return useQuery<User>({
|
||||
queryKey: ["me"],
|
||||
queryFn: async () => {
|
||||
const res = await fetch(`${import.meta.env.VITE_API_URL}/auth/me`, {
|
||||
credentials: "include",
|
||||
});
|
||||
|
||||
if (res.status === 401) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error("Failed to get current user");
|
||||
}
|
||||
|
||||
return res.json();
|
||||
},
|
||||
});
|
||||
}
|
||||
18
front/src/hooks/video/useCreateClip.ts
Normal file
18
front/src/hooks/video/useCreateClip.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
|
||||
export function useCreateClip() {
|
||||
return useMutation({
|
||||
mutationFn: async () => {
|
||||
const res = await fetch(`${import.meta.env.VITE_API_URL}/protected/createclip`, {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error("Invalid credentials");
|
||||
}
|
||||
|
||||
return res.json();
|
||||
},
|
||||
});
|
||||
}
|
||||
18
front/src/hooks/video/useRefreshFiles.ts
Normal file
18
front/src/hooks/video/useRefreshFiles.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
|
||||
export function useRefreshFiles() {
|
||||
return useMutation({
|
||||
mutationFn: async () => {
|
||||
const res = await fetch(`${import.meta.env.VITE_API_URL}/protected/refreshfiles`, {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error("Invalid credentials");
|
||||
}
|
||||
|
||||
return res.json();
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user