@@ -11,6 +11,9 @@ const useUsers = () => {
|
||||
queryFn: async () => {
|
||||
const response = await fetch("/api/v1/users");
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
window.location.href = "/dashboard";
|
||||
}
|
||||
throw new Error("Failed to fetch users.");
|
||||
}
|
||||
|
||||
@@ -27,6 +30,8 @@ const useAddUser = () => {
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (body: any) => {
|
||||
if (body.password.length < 8) throw new Error(t("password_length_error"));
|
||||
|
||||
const load = toast.loading(t("creating_account"));
|
||||
|
||||
const response = await fetch("/api/v1/users", {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useSession } from "next-auth/react";
|
||||
|
||||
@@ -6,11 +7,11 @@ const useDashboardData = () => {
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["dashboardData"],
|
||||
queryFn: async () => {
|
||||
const response = await fetch("/api/v2/dashboard");
|
||||
queryFn: async (): Promise<LinkIncludingShortenedCollectionAndTags[]> => {
|
||||
const response = await fetch("/api/v1/dashboard");
|
||||
const data = await response.json();
|
||||
|
||||
return data.data;
|
||||
return data.response;
|
||||
},
|
||||
enabled: status === "authenticated",
|
||||
});
|
||||
|
||||
+18
-126
@@ -13,7 +13,6 @@ import {
|
||||
} from "@/types/global";
|
||||
import { useRouter } from "next/router";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { PostLinkSchemaType } from "@/lib/shared/schemaValidation";
|
||||
|
||||
const useLinks = (params: LinkRequestQuery = {}) => {
|
||||
const router = useRouter();
|
||||
@@ -104,15 +103,7 @@ const useAddLink = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (link: PostLinkSchemaType) => {
|
||||
if (link.url || link.type === "url") {
|
||||
try {
|
||||
new URL(link.url || "");
|
||||
} catch (error) {
|
||||
throw new Error("invalid_url_guide");
|
||||
}
|
||||
}
|
||||
|
||||
mutationFn: async (link: LinkIncludingShortenedCollectionAndTags) => {
|
||||
const response = await fetch("/api/v1/links", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -129,11 +120,8 @@ const useAddLink = () => {
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData(["dashboardData"], (oldData: any) => {
|
||||
if (!oldData?.links) return undefined;
|
||||
return {
|
||||
...oldData,
|
||||
links: [data, ...oldData?.links],
|
||||
};
|
||||
if (!oldData) return undefined;
|
||||
return [data, ...oldData];
|
||||
});
|
||||
|
||||
queryClient.setQueriesData({ queryKey: ["links"] }, (oldData: any) => {
|
||||
@@ -172,8 +160,8 @@ const useUpdateLink = () => {
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
// queryClient.setQueryData(["dashboardData"], (oldData: any) => {
|
||||
// if (!oldData?.links) return undefined;
|
||||
// return oldData.links.map((e: any) => (e.id === data.id ? data : e));
|
||||
// if (!oldData) return undefined;
|
||||
// return oldData.map((e: any) => (e.id === data.id ? data : e));
|
||||
// });
|
||||
|
||||
// queryClient.setQueriesData({ queryKey: ["links"] }, (oldData: any) => {
|
||||
@@ -213,11 +201,8 @@ const useDeleteLink = () => {
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData(["dashboardData"], (oldData: any) => {
|
||||
if (!oldData?.links) return undefined;
|
||||
return {
|
||||
...oldData,
|
||||
links: oldData.links.filter((e: any) => e.id !== data.id),
|
||||
};
|
||||
if (!oldData) return undefined;
|
||||
return oldData.filter((e: any) => e.id !== data.id);
|
||||
});
|
||||
|
||||
queryClient.setQueriesData({ queryKey: ["links"] }, (oldData: any) => {
|
||||
@@ -240,21 +225,9 @@ const useDeleteLink = () => {
|
||||
const useGetLink = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({
|
||||
id,
|
||||
isPublicRoute = router.pathname.startsWith("/public") ? true : undefined,
|
||||
}: {
|
||||
id: number;
|
||||
isPublicRoute?: boolean;
|
||||
}) => {
|
||||
const path = isPublicRoute
|
||||
? `/api/v1/public/links/${id}`
|
||||
: `/api/v1/links/${id}`;
|
||||
|
||||
const response = await fetch(path);
|
||||
mutationFn: async (id: number) => {
|
||||
const response = await fetch(`/api/v1/links/${id}`);
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) throw new Error(data.response);
|
||||
@@ -263,11 +236,8 @@ const useGetLink = () => {
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData(["dashboardData"], (oldData: any) => {
|
||||
if (!oldData?.links) return undefined;
|
||||
return {
|
||||
...oldData,
|
||||
links: oldData.links.map((e: any) => (e.id === data.id ? data : e)),
|
||||
};
|
||||
if (!oldData) return undefined;
|
||||
return oldData.map((e: any) => (e.id === data.id ? data : e));
|
||||
});
|
||||
|
||||
queryClient.setQueriesData({ queryKey: ["links"] }, (oldData: any) => {
|
||||
@@ -280,20 +250,7 @@ const useGetLink = () => {
|
||||
};
|
||||
});
|
||||
|
||||
queryClient.setQueriesData(
|
||||
{ queryKey: ["publicLinks"] },
|
||||
(oldData: any) => {
|
||||
if (!oldData) return undefined;
|
||||
return {
|
||||
pages: oldData.pages.map((page: any) =>
|
||||
page.map((item: any) => (item.id === data.id ? data : item))
|
||||
),
|
||||
pageParams: oldData.pageParams,
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
// queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -319,8 +276,8 @@ const useBulkDeleteLinks = () => {
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData(["dashboardData"], (oldData: any) => {
|
||||
if (!oldData?.links) return undefined;
|
||||
return oldData.links.filter((e: any) => !data.includes(e.id));
|
||||
if (!oldData) return undefined;
|
||||
return oldData.filter((e: any) => !data.includes(e.id));
|
||||
});
|
||||
|
||||
queryClient.setQueriesData({ queryKey: ["links"] }, (oldData: any) => {
|
||||
@@ -394,11 +351,8 @@ const useUploadFile = () => {
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData(["dashboardData"], (oldData: any) => {
|
||||
if (!oldData?.links) return undefined;
|
||||
return {
|
||||
...oldData,
|
||||
links: [data, ...oldData?.links],
|
||||
};
|
||||
if (!oldData) return undefined;
|
||||
return [data, ...oldData];
|
||||
});
|
||||
|
||||
queryClient.setQueriesData({ queryKey: ["links"] }, (oldData: any) => {
|
||||
@@ -416,67 +370,6 @@ const useUploadFile = () => {
|
||||
});
|
||||
};
|
||||
|
||||
const useUpdatePreview = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ linkId, file }: { linkId: number; file: File }) => {
|
||||
const formBody = new FormData();
|
||||
|
||||
if (!linkId || !file)
|
||||
throw new Error("Error generating preview: Invalid parameters");
|
||||
|
||||
formBody.append("file", file);
|
||||
|
||||
const res = await fetch(
|
||||
`/api/v1/archives/${linkId}?format=` + ArchivedFormat.jpeg,
|
||||
{
|
||||
body: formBody,
|
||||
method: "PUT",
|
||||
}
|
||||
);
|
||||
|
||||
const data = res.json();
|
||||
|
||||
return data;
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
queryClient.setQueryData(["dashboardData"], (oldData: any) => {
|
||||
if (!oldData?.links) return undefined;
|
||||
return {
|
||||
...oldData,
|
||||
links: oldData.links.map((e: any) =>
|
||||
e.id === data.response.id
|
||||
? {
|
||||
...e,
|
||||
preview: `archives/preview/${e.collectionId}/${e.id}.jpeg`,
|
||||
}
|
||||
: e
|
||||
),
|
||||
};
|
||||
});
|
||||
|
||||
queryClient.setQueriesData({ queryKey: ["links"] }, (oldData: any) => {
|
||||
if (!oldData) return undefined;
|
||||
return {
|
||||
pages: oldData.pages.map((page: any) =>
|
||||
page.map((item: any) =>
|
||||
item.id === data.response.id
|
||||
? {
|
||||
...item,
|
||||
preview: `archives/preview/${item.collectionId}/${item.id}.jpeg`,
|
||||
updatedAt: new Date().toISOString(),
|
||||
}
|
||||
: item
|
||||
)
|
||||
),
|
||||
pageParams: oldData.pageParams,
|
||||
};
|
||||
});
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const useBulkEditLinks = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
@@ -510,8 +403,8 @@ const useBulkEditLinks = () => {
|
||||
onSuccess: (data, { links, newData, removePreviousTags }) => {
|
||||
// TODO: Fix these
|
||||
// queryClient.setQueryData(["dashboardData"], (oldData: any) => {
|
||||
// if (!oldData?.links) return undefined;
|
||||
// return oldData.links.map((e: any) =>
|
||||
// if (!oldData) return undefined;
|
||||
// return oldData.map((e: any) =>
|
||||
// data.find((d: any) => d.id === e.id) ? data : e
|
||||
// );
|
||||
// });
|
||||
@@ -561,5 +454,4 @@ export {
|
||||
useGetLink,
|
||||
useBulkEditLinks,
|
||||
resetInfiniteQueryPagination,
|
||||
useUpdatePreview,
|
||||
};
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
import { Tag } from "@prisma/client";
|
||||
import { useQuery, UseQueryResult } from "@tanstack/react-query";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { useRouter } from "next/router";
|
||||
|
||||
type TagIncludingCount = Tag & { _count: { links: number } };
|
||||
|
||||
const usePublicTags = (): UseQueryResult<TagIncludingCount[]> => {
|
||||
const { status } = useSession();
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["tags"],
|
||||
queryFn: async () => {
|
||||
const response = await fetch(
|
||||
"/api/v1/public/collections/tags" +
|
||||
"?collectionId=" +
|
||||
router.query.id || ""
|
||||
);
|
||||
if (!response.ok) throw new Error("Failed to fetch tags.");
|
||||
|
||||
const data = await response.json();
|
||||
return data.response;
|
||||
},
|
||||
enabled: status === "authenticated",
|
||||
});
|
||||
};
|
||||
|
||||
export { usePublicTags };
|
||||
@@ -27,9 +27,6 @@ const useAddToken = () => {
|
||||
const response = await fetch("/api/v1/tokens", {
|
||||
body: JSON.stringify(body),
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
Reference in New Issue
Block a user