changes and improvements

This commit is contained in:
daniel31x13
2024-08-14 15:22:28 -04:00
parent d15d965139
commit 9cc3a7206e
24 changed files with 292 additions and 203 deletions
-29
View File
@@ -1,7 +1,5 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
import { useTranslation } from "next-i18next";
import toast from "react-hot-toast";
const useCollections = () => {
return useQuery({
@@ -15,13 +13,10 @@ const useCollections = () => {
};
const useCreateCollection = () => {
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (body: any) => {
const load = toast.loading(t("creating"));
const response = await fetch("/api/v1/collections", {
body: JSON.stringify(body),
headers: {
@@ -30,8 +25,6 @@ const useCreateCollection = () => {
method: "POST",
});
toast.dismiss(load);
const data = await response.json();
if (!response.ok) throw new Error(data.response);
@@ -39,25 +32,18 @@ const useCreateCollection = () => {
return data.response;
},
onSuccess: (data) => {
toast.success(t("created"));
return queryClient.setQueryData(["collections"], (oldData: any) => {
return [...oldData, data];
});
},
onError: (error) => {
toast.error(error.message);
},
});
};
const useUpdateCollection = () => {
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (body: any) => {
const load = toast.loading(t("updating_collection"));
const response = await fetch(`/api/v1/collections/${body.id}`, {
method: "PUT",
headers: {
@@ -66,8 +52,6 @@ const useUpdateCollection = () => {
body: JSON.stringify(body),
});
toast.dismiss(load);
const data = await response.json();
if (!response.ok) throw new Error(data.response);
@@ -76,7 +60,6 @@ const useUpdateCollection = () => {
},
onSuccess: (data) => {
{
toast.success(t("updated"));
return queryClient.setQueryData(["collections"], (oldData: any) => {
return oldData.map((collection: any) =>
collection.id === data.id ? data : collection
@@ -92,20 +75,14 @@ const useUpdateCollection = () => {
// )
// });
// },
onError: (error) => {
toast.error(error.message);
},
});
};
const useDeleteCollection = () => {
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (id: number) => {
const load = toast.loading(t("deleting_collection"));
const response = await fetch(`/api/v1/collections/${id}`, {
method: "DELETE",
headers: {
@@ -113,8 +90,6 @@ const useDeleteCollection = () => {
},
});
toast.dismiss(load);
const data = await response.json();
if (!response.ok) throw new Error(data.response);
@@ -122,14 +97,10 @@ const useDeleteCollection = () => {
return data.response;
},
onSuccess: (data) => {
toast.success(t("deleted"));
return queryClient.setQueryData(["collections"], (oldData: any) => {
return oldData.filter((collection: any) => collection.id !== data.id);
});
},
onError: (error) => {
toast.error(error.message);
},
});
};
+1 -63
View File
@@ -5,14 +5,12 @@ import {
useQueryClient,
useMutation,
} from "@tanstack/react-query";
import { useEffect, useMemo } from "react";
import { useMemo } from "react";
import {
ArchivedFormat,
LinkIncludingShortenedCollectionAndTags,
LinkRequestQuery,
} from "@/types/global";
import toast from "react-hot-toast";
import { useTranslation } from "next-i18next";
import { useRouter } from "next/router";
const useLinks = (params: LinkRequestQuery = {}) => {
@@ -98,13 +96,10 @@ const buildQueryString = (params: LinkRequestQuery) => {
};
const useAddLink = () => {
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (link: LinkIncludingShortenedCollectionAndTags) => {
const load = toast.loading(t("creating_link"));
const response = await fetch("/api/v1/links", {
method: "POST",
headers: {
@@ -113,8 +108,6 @@ const useAddLink = () => {
body: JSON.stringify(link),
});
toast.dismiss(load);
const data = await response.json();
if (!response.ok) throw new Error(data.response);
@@ -122,8 +115,6 @@ const useAddLink = () => {
return data.response;
},
onSuccess: (data) => {
toast.success(t("link_created"));
queryClient.setQueryData(["dashboardData"], (oldData: any) => {
if (!oldData) return undefined;
return [data, ...oldData];
@@ -141,20 +132,14 @@ const useAddLink = () => {
queryClient.invalidateQueries({ queryKey: ["tags"] });
queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
},
onError: (error) => {
toast.error(error.message);
},
});
};
const useUpdateLink = () => {
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (link: LinkIncludingShortenedCollectionAndTags) => {
const load = toast.loading(t("updating"));
const response = await fetch(`/api/v1/links/${link.id}`, {
method: "PUT",
headers: {
@@ -163,8 +148,6 @@ const useUpdateLink = () => {
body: JSON.stringify(link),
});
toast.dismiss(load);
const data = await response.json();
if (!response.ok) throw new Error(data.response);
@@ -172,8 +155,6 @@ const useUpdateLink = () => {
return data.response;
},
onSuccess: (data) => {
toast.success(t("updated"));
queryClient.setQueryData(["dashboardData"], (oldData: any) => {
if (!oldData) return undefined;
return oldData.map((e: any) => (e.id === data.id ? data : e));
@@ -193,26 +174,18 @@ const useUpdateLink = () => {
queryClient.invalidateQueries({ queryKey: ["tags"] });
queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
},
onError: (error) => {
toast.error(error.message);
},
});
};
const useDeleteLink = () => {
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (id: number) => {
const load = toast.loading(t("deleting"));
const response = await fetch(`/api/v1/links/${id}`, {
method: "DELETE",
});
toast.dismiss(load);
const data = await response.json();
if (!response.ok) throw new Error(data.response);
@@ -220,8 +193,6 @@ const useDeleteLink = () => {
return data.response;
},
onSuccess: (data) => {
toast.success(t("deleted"));
queryClient.setQueryData(["dashboardData"], (oldData: any) => {
if (!oldData) return undefined;
return oldData.filter((e: any) => e.id !== data.id);
@@ -241,9 +212,6 @@ const useDeleteLink = () => {
queryClient.invalidateQueries({ queryKey: ["tags"] });
queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
},
onError: (error) => {
toast.error(error.message);
},
});
};
@@ -281,13 +249,10 @@ const useGetLink = () => {
};
const useBulkDeleteLinks = () => {
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (linkIds: number[]) => {
const load = toast.loading(t("deleting"));
const response = await fetch("/api/v1/links", {
method: "DELETE",
headers: {
@@ -296,8 +261,6 @@ const useBulkDeleteLinks = () => {
body: JSON.stringify({ linkIds }),
});
toast.dismiss(load);
const data = await response.json();
if (!response.ok) throw new Error(data.response);
@@ -305,8 +268,6 @@ const useBulkDeleteLinks = () => {
return linkIds;
},
onSuccess: (data) => {
toast.success(t("deleted"));
queryClient.setQueryData(["dashboardData"], (oldData: any) => {
if (!oldData) return undefined;
return oldData.filter((e: any) => !data.includes(e.id));
@@ -326,14 +287,10 @@ const useBulkDeleteLinks = () => {
queryClient.invalidateQueries({ queryKey: ["tags"] });
queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
},
onError: (error) => {
toast.error(error.message);
},
});
};
const useUploadFile = () => {
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
@@ -354,8 +311,6 @@ const useUploadFile = () => {
return { ok: false, data: "Invalid file type." };
}
const load = toast.loading(t("creating"));
const response = await fetch("/api/v1/links", {
body: JSON.stringify({
...link,
@@ -385,13 +340,9 @@ const useUploadFile = () => {
);
}
toast.dismiss(load);
return data.response;
},
onSuccess: (data) => {
toast.success(t("created_success"));
queryClient.setQueryData(["dashboardData"], (oldData: any) => {
if (!oldData) return undefined;
return [data, ...oldData];
@@ -409,14 +360,10 @@ const useUploadFile = () => {
queryClient.invalidateQueries({ queryKey: ["tags"] });
queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
},
onError: (error) => {
toast.error(error.message);
},
});
};
const useBulkEditLinks = () => {
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
@@ -432,8 +379,6 @@ const useBulkEditLinks = () => {
>;
removePreviousTags: boolean;
}) => {
const load = toast.loading(t("updating"));
const response = await fetch("/api/v1/links", {
method: "PUT",
headers: {
@@ -442,8 +387,6 @@ const useBulkEditLinks = () => {
body: JSON.stringify({ links, newData, removePreviousTags }),
});
toast.dismiss(load);
const data = await response.json();
if (!response.ok) throw new Error(data.response);
@@ -451,8 +394,6 @@ const useBulkEditLinks = () => {
return data.response;
},
onSuccess: (data, { links, newData, removePreviousTags }) => {
toast.success(t("updated"));
queryClient.setQueryData(["dashboardData"], (oldData: any) => {
if (!oldData) return undefined;
return oldData.map((e: any) =>
@@ -477,9 +418,6 @@ const useBulkEditLinks = () => {
queryClient.invalidateQueries({ queryKey: ["tags"] });
queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
},
onError: (error) => {
toast.error(error.message);
},
});
};
-20
View File
@@ -1,6 +1,4 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import toast from "react-hot-toast";
import { useTranslation } from "next-i18next";
import { TagIncludingLinkCount } from "@/types/global";
const useTags = () => {
@@ -18,12 +16,9 @@ const useTags = () => {
const useUpdateTag = () => {
const queryClient = useQueryClient();
const { t } = useTranslation();
return useMutation({
mutationFn: async (tag: TagIncludingLinkCount) => {
const load = toast.loading(t("applying_changes"));
const response = await fetch(`/api/v1/tags/${tag.id}`, {
body: JSON.stringify(tag),
headers: {
@@ -35,8 +30,6 @@ const useUpdateTag = () => {
const data = await response.json();
if (!response.ok) throw new Error(data.response);
toast.dismiss(load);
return data.response;
},
onSuccess: (data) => {
@@ -45,22 +38,15 @@ const useUpdateTag = () => {
tag.id === data.id ? data : tag
)
);
toast.success(t("tag_renamed"));
},
onError: (error) => {
toast.error(error.message);
},
});
};
const useRemoveTag = () => {
const queryClient = useQueryClient();
const { t } = useTranslation();
return useMutation({
mutationFn: async (tagId: number) => {
const load = toast.loading(t("applying_changes"));
const response = await fetch(`/api/v1/tags/${tagId}`, {
method: "DELETE",
});
@@ -68,18 +54,12 @@ const useRemoveTag = () => {
const data = await response.json();
if (!response.ok) throw new Error(data.response);
toast.dismiss(load);
return data.response;
},
onSuccess: (data, variables) => {
queryClient.setQueryData(["tags"], (oldData: any) =>
oldData.filter((tag: TagIncludingLinkCount) => tag.id !== variables)
);
toast.success(t("tag_deleted"));
},
onError: (error) => {
toast.error(error.message);
},
});
};
-20
View File
@@ -1,6 +1,4 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import toast from "react-hot-toast";
import { useTranslation } from "next-i18next";
import { AccessToken } from "@prisma/client";
const useTokens = () => {
@@ -19,12 +17,9 @@ const useTokens = () => {
const useAddToken = () => {
const queryClient = useQueryClient();
const { t } = useTranslation();
return useMutation({
mutationFn: async (body: Partial<AccessToken>) => {
const load = toast.loading(t("creating_token"));
const response = await fetch("/api/v1/tokens", {
body: JSON.stringify(body),
method: "POST",
@@ -33,8 +28,6 @@ const useAddToken = () => {
const data = await response.json();
if (!response.ok) throw new Error(data.response);
toast.dismiss(load);
return data.response;
},
onSuccess: (data) => {
@@ -42,22 +35,15 @@ const useAddToken = () => {
...oldData,
data.token,
]);
toast.success(t("token_added"));
},
onError: (error) => {
toast.error(error.message);
},
});
};
const useRevokeToken = () => {
const queryClient = useQueryClient();
const { t } = useTranslation();
return useMutation({
mutationFn: async (tokenId: number) => {
const load = toast.loading(t("deleting"));
const response = await fetch(`/api/v1/tokens/${tokenId}`, {
method: "DELETE",
});
@@ -65,18 +51,12 @@ const useRevokeToken = () => {
const data = await response.json();
if (!response.ok) throw new Error(data.response);
toast.dismiss(load);
return data.response;
},
onSuccess: (data, variables) => {
queryClient.setQueryData(["tokens"], (oldData: AccessToken[]) =>
oldData.filter((token: Partial<AccessToken>) => token.id !== variables)
);
toast.success(t("token_revoked"));
},
onError: (error) => {
toast.error(error.message);
},
});
};
-11
View File
@@ -1,6 +1,4 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import toast from "react-hot-toast";
import { useTranslation } from "next-i18next";
import { useSession } from "next-auth/react";
const useUser = () => {
@@ -24,13 +22,10 @@ const useUser = () => {
};
const useUpdateUser = () => {
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (user: any) => {
const load = toast.loading(t("applying_settings"));
const response = await fetch(`/api/v1/users/${user.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
@@ -39,14 +34,11 @@ const useUpdateUser = () => {
const data = await response.json();
toast.dismiss(load);
if (!response.ok) throw new Error(data.response);
return data;
},
onSuccess: (data) => {
toast.success(t("settings_applied"));
queryClient.setQueryData(["user"], data.response);
},
onMutate: async (user) => {
@@ -55,9 +47,6 @@ const useUpdateUser = () => {
return { ...oldData, ...user };
});
},
onError: (error) => {
toast.error(error.message);
},
});
};