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

View File

@ -155,15 +155,22 @@ const CollectionListing = () => {
const updatedCollectionOrder = [...user.collectionOrder]; const updatedCollectionOrder = [...user.collectionOrder];
if (source.parentId !== destination.parentId) { if (source.parentId !== destination.parentId) {
await updateCollection.mutateAsync({ await updateCollection.mutateAsync(
...movedCollection, {
parentId: ...movedCollection,
destination.parentId && destination.parentId !== "root" parentId:
? Number(destination.parentId) destination.parentId && destination.parentId !== "root"
: destination.parentId === "root" ? Number(destination.parentId)
? "root" : destination.parentId === "root"
: null, ? "root"
} as any); : null,
},
{
onError: (error) => {
toast.error(error.message);
},
}
);
} }
if ( if (

View File

@ -10,6 +10,7 @@ import { useRouter } from "next/router";
import useLinkStore from "@/store/links"; import useLinkStore from "@/store/links";
import { Sort, ViewMode } from "@/types/global"; import { Sort, ViewMode } from "@/types/global";
import { useBulkDeleteLinks, useLinks } from "@/hooks/store/links"; import { useBulkDeleteLinks, useLinks } from "@/hooks/store/links";
import toast from "react-hot-toast";
type Props = { type Props = {
children: React.ReactNode; children: React.ReactNode;
@ -76,11 +77,20 @@ const LinkListOptions = ({
}; };
const bulkDeleteLinks = async () => { const bulkDeleteLinks = async () => {
const load = toast.loading(t("deleting"));
await deleteLinksById.mutateAsync( await deleteLinksById.mutateAsync(
selectedLinks.map((link) => link.id as number), selectedLinks.map((link) => link.id as number),
{ {
onSuccess: () => { onSettled: (data, error) => {
setSelectedLinks([]); toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
setSelectedLinks([]);
toast.success(t("deleted"));
}
}, },
} }
); );

View File

@ -11,6 +11,7 @@ import { dropdownTriggerer } from "@/lib/client/utils";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { useUser } from "@/hooks/store/user"; import { useUser } from "@/hooks/store/user";
import { useDeleteLink, useUpdateLink } from "@/hooks/store/links"; import { useDeleteLink, useUpdateLink } from "@/hooks/store/links";
import toast from "react-hot-toast";
type Props = { type Props = {
link: LinkIncludingShortenedCollectionAndTags; link: LinkIncludingShortenedCollectionAndTags;
@ -44,12 +45,29 @@ export default function LinkActions({
const deleteLink = useDeleteLink(); const deleteLink = useDeleteLink();
const pinLink = async () => { const pinLink = async () => {
const isAlreadyPinned = link?.pinnedBy && link.pinnedBy[0]; const isAlreadyPinned = link?.pinnedBy && link.pinnedBy[0] ? true : false;
await updateLink.mutateAsync({ const load = toast.loading(t("updating"));
...link,
pinnedBy: isAlreadyPinned ? undefined : [{ id: user.id }], await updateLink.mutateAsync(
}); {
...link,
pinnedBy: isAlreadyPinned ? undefined : [{ id: user.id }],
},
{
onSettled: (data, error) => {
toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
toast.success(
isAlreadyPinned ? t("link_unpinned") : t("link_pinned")
);
}
},
}
);
}; };
return ( return (
@ -136,7 +154,21 @@ export default function LinkActions({
onClick={async (e) => { onClick={async (e) => {
(document?.activeElement as HTMLElement)?.blur(); (document?.activeElement as HTMLElement)?.blur();
e.shiftKey e.shiftKey
? await deleteLink.mutateAsync(link.id as number) ? async () => {
const load = toast.loading(t("deleting"));
await deleteLink.mutateAsync(link.id as number, {
onSettled: (data, error) => {
toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
toast.success(t("deleted"));
}
},
});
}
: setDeleteLinkModal(true); : setDeleteLinkModal(true);
}} }}
> >

View File

@ -4,6 +4,7 @@ import Modal from "../Modal";
import Button from "../ui/Button"; import Button from "../ui/Button";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { useBulkDeleteLinks } from "@/hooks/store/links"; import { useBulkDeleteLinks } from "@/hooks/store/links";
import toast from "react-hot-toast";
type Props = { type Props = {
onClose: Function; onClose: Function;
@ -16,12 +17,21 @@ export default function BulkDeleteLinksModal({ onClose }: Props) {
const deleteLinksById = useBulkDeleteLinks(); const deleteLinksById = useBulkDeleteLinks();
const deleteLink = async () => { const deleteLink = async () => {
const load = toast.loading(t("deleting"));
await deleteLinksById.mutateAsync( await deleteLinksById.mutateAsync(
selectedLinks.map((link) => link.id as number), selectedLinks.map((link) => link.id as number),
{ {
onSuccess: () => { onSettled: (data, error) => {
setSelectedLinks([]); toast.dismiss(load);
onClose();
if (error) {
toast.error(error.message);
} else {
setSelectedLinks([]);
onClose();
toast.success(t("deleted"));
}
}, },
} }
); );

View File

@ -36,6 +36,8 @@ export default function BulkEditLinksModal({ onClose }: Props) {
if (!submitLoader) { if (!submitLoader) {
setSubmitLoader(true); setSubmitLoader(true);
const load = toast.loading(t("updating"));
await updateLinks.mutateAsync( await updateLinks.mutateAsync(
{ {
links: selectedLinks, links: selectedLinks,
@ -43,9 +45,16 @@ export default function BulkEditLinksModal({ onClose }: Props) {
removePreviousTags, removePreviousTags,
}, },
{ {
onSuccess: () => { onSettled: (data, error) => {
setSelectedLinks([]); toast.dismiss(load);
onClose();
if (error) {
toast.error(error.message);
} else {
setSelectedLinks([]);
onClose();
toast.success(t("updated"));
}
}, },
} }
); );

View File

@ -7,6 +7,7 @@ import Modal from "../Modal";
import Button from "../ui/Button"; import Button from "../ui/Button";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { useDeleteCollection } from "@/hooks/store/collections"; import { useDeleteCollection } from "@/hooks/store/collections";
import toast from "react-hot-toast";
type Props = { type Props = {
onClose: Function; onClose: Function;
@ -39,10 +40,19 @@ export default function DeleteCollectionModal({
setSubmitLoader(true); setSubmitLoader(true);
const load = toast.loading(t("deleting_collection"));
deleteCollection.mutateAsync(collection.id as number, { deleteCollection.mutateAsync(collection.id as number, {
onSuccess: () => { onSettled: (data, error) => {
onClose(); toast.dismiss(load);
router.push("/collections");
if (error) {
toast.error(error.message);
} else {
onClose();
router.push("/collections");
toast.success(t("deleted"));
}
}, },
}); });

View File

@ -5,6 +5,7 @@ import { useRouter } from "next/router";
import Button from "../ui/Button"; import Button from "../ui/Button";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { useDeleteLink } from "@/hooks/store/links"; import { useDeleteLink } from "@/hooks/store/links";
import toast from "react-hot-toast";
type Props = { type Props = {
onClose: Function; onClose: Function;
@ -24,13 +25,21 @@ export default function DeleteLinkModal({ onClose, activeLink }: Props) {
}, []); }, []);
const submit = async () => { const submit = async () => {
await deleteLink.mutateAsync(link.id as number, { const load = toast.loading(t("deleting"));
onSuccess: () => {
if (router.pathname.startsWith("/links/[id]")) {
router.push("/dashboard");
}
onClose(); await deleteLink.mutateAsync(link.id as number, {
onSettled: (data, error) => {
toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
if (router.pathname.startsWith("/links/[id]")) {
router.push("/dashboard");
}
onClose();
toast.success(t("deleted"));
}
}, },
}); });
}; };

View File

@ -5,6 +5,7 @@ import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
import Modal from "../Modal"; import Modal from "../Modal";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { useUpdateCollection } from "@/hooks/store/collections"; import { useUpdateCollection } from "@/hooks/store/collections";
import toast from "react-hot-toast";
type Props = { type Props = {
onClose: Function; onClose: Function;
@ -29,9 +30,18 @@ export default function EditCollectionModal({
setSubmitLoader(true); setSubmitLoader(true);
const load = toast.loading(t("updating_collection"));
await updateCollection.mutateAsync(collection, { await updateCollection.mutateAsync(collection, {
onSuccess: () => { onSettled: (data, error) => {
onClose(); toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
onClose();
toast.success(t("updated"));
}
}, },
}); });

View File

@ -36,9 +36,18 @@ export default function EditCollectionSharingModal({
setSubmitLoader(true); setSubmitLoader(true);
const load = toast.loading(t("updating_collection"));
await updateCollection.mutateAsync(collection, { await updateCollection.mutateAsync(collection, {
onSuccess: () => { onSettled: (data, error) => {
onClose(); toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
onClose();
toast.success(t("updated"));
}
}, },
}); });

View File

@ -8,6 +8,7 @@ import Link from "next/link";
import Modal from "../Modal"; import Modal from "../Modal";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { useUpdateLink } from "@/hooks/store/links"; import { useUpdateLink } from "@/hooks/store/links";
import toast from "react-hot-toast";
type Props = { type Props = {
onClose: Function; onClose: Function;
@ -51,9 +52,18 @@ export default function EditLinkModal({ onClose, activeLink }: Props) {
if (!submitLoader) { if (!submitLoader) {
setSubmitLoader(true); setSubmitLoader(true);
const load = toast.loading(t("updating"));
await updateLink.mutateAsync(link, { await updateLink.mutateAsync(link, {
onSuccess: () => { onSettled: (data, error) => {
onClose(); toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
onClose();
toast.success(t("updated"));
}
}, },
}); });

View File

@ -6,6 +6,7 @@ import Modal from "../Modal";
import { CollectionIncludingMembersAndLinkCount } from "@/types/global"; import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { useCreateCollection } from "@/hooks/store/collections"; import { useCreateCollection } from "@/hooks/store/collections";
import toast from "react-hot-toast";
type Props = { type Props = {
onClose: Function; onClose: Function;
@ -37,9 +38,18 @@ export default function NewCollectionModal({ onClose, parent }: Props) {
setSubmitLoader(true); setSubmitLoader(true);
const load = toast.loading(t("creating"));
await createCollection.mutateAsync(collection, { await createCollection.mutateAsync(collection, {
onSuccess: () => { onSettled: (data, error) => {
onClose(); toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
onClose();
toast.success(t("created"));
}
}, },
}); });

View File

@ -10,6 +10,7 @@ import Modal from "../Modal";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { useCollections } from "@/hooks/store/collections"; import { useCollections } from "@/hooks/store/collections";
import { useAddLink } from "@/hooks/store/links"; import { useAddLink } from "@/hooks/store/links";
import toast from "react-hot-toast";
type Props = { type Props = {
onClose: Function; onClose: Function;
@ -88,9 +89,18 @@ export default function NewLinkModal({ onClose }: Props) {
if (!submitLoader) { if (!submitLoader) {
setSubmitLoader(true); setSubmitLoader(true);
const load = toast.loading(t("creating_link"));
await addLink.mutateAsync(link, { await addLink.mutateAsync(link, {
onSuccess: () => { onSettled: (data, error) => {
onClose(); toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
onClose();
toast.success(t("link_created"));
}
}, },
}); });

View File

@ -29,9 +29,17 @@ export default function NewTokenModal({ onClose }: Props) {
if (!submitLoader) { if (!submitLoader) {
setSubmitLoader(true); setSubmitLoader(true);
const load = toast.loading(t("creating_token"));
await addToken.mutateAsync(token, { await addToken.mutateAsync(token, {
onSuccess: (data) => { onSettled: (data, error) => {
setNewToken(data.secretKey); toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
setNewToken(data.secretKey);
}
}, },
}); });

View File

@ -4,6 +4,7 @@ import Button from "../ui/Button";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { AccessToken } from "@prisma/client"; import { AccessToken } from "@prisma/client";
import { useRevokeToken } from "@/hooks/store/tokens"; import { useRevokeToken } from "@/hooks/store/tokens";
import toast from "react-hot-toast";
type Props = { type Props = {
onClose: Function; onClose: Function;
@ -21,9 +22,18 @@ export default function DeleteTokenModal({ onClose, activeToken }: Props) {
}, [activeToken]); }, [activeToken]);
const deleteLink = async () => { const deleteLink = async () => {
const load = toast.loading(t("deleting"));
await revokeToken.mutateAsync(token.id, { await revokeToken.mutateAsync(token.id, {
onSuccess: () => { onSettled: (data, error) => {
onClose(); toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
onClose();
toast.success(t("token_revoked"));
}
}, },
}); });
}; };

View File

@ -116,11 +116,20 @@ export default function UploadFileModal({ onClose }: Props) {
setSubmitLoader(true); setSubmitLoader(true);
const load = toast.loading(t("creating"));
await uploadFile.mutateAsync( await uploadFile.mutateAsync(
{ link, file }, { link, file },
{ {
onSuccess: () => { onSettled: (data, error) => {
onClose(); toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
onClose();
toast.success(t("created_success"));
}
}, },
} }
); );

View File

@ -1,7 +1,5 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { CollectionIncludingMembersAndLinkCount } from "@/types/global"; import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
import { useTranslation } from "next-i18next";
import toast from "react-hot-toast";
const useCollections = () => { const useCollections = () => {
return useQuery({ return useQuery({
@ -15,13 +13,10 @@ const useCollections = () => {
}; };
const useCreateCollection = () => { const useCreateCollection = () => {
const { t } = useTranslation();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: async (body: any) => { mutationFn: async (body: any) => {
const load = toast.loading(t("creating"));
const response = await fetch("/api/v1/collections", { const response = await fetch("/api/v1/collections", {
body: JSON.stringify(body), body: JSON.stringify(body),
headers: { headers: {
@ -30,8 +25,6 @@ const useCreateCollection = () => {
method: "POST", method: "POST",
}); });
toast.dismiss(load);
const data = await response.json(); const data = await response.json();
if (!response.ok) throw new Error(data.response); if (!response.ok) throw new Error(data.response);
@ -39,25 +32,18 @@ const useCreateCollection = () => {
return data.response; return data.response;
}, },
onSuccess: (data) => { onSuccess: (data) => {
toast.success(t("created"));
return queryClient.setQueryData(["collections"], (oldData: any) => { return queryClient.setQueryData(["collections"], (oldData: any) => {
return [...oldData, data]; return [...oldData, data];
}); });
}, },
onError: (error) => {
toast.error(error.message);
},
}); });
}; };
const useUpdateCollection = () => { const useUpdateCollection = () => {
const { t } = useTranslation();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: async (body: any) => { mutationFn: async (body: any) => {
const load = toast.loading(t("updating_collection"));
const response = await fetch(`/api/v1/collections/${body.id}`, { const response = await fetch(`/api/v1/collections/${body.id}`, {
method: "PUT", method: "PUT",
headers: { headers: {
@ -66,8 +52,6 @@ const useUpdateCollection = () => {
body: JSON.stringify(body), body: JSON.stringify(body),
}); });
toast.dismiss(load);
const data = await response.json(); const data = await response.json();
if (!response.ok) throw new Error(data.response); if (!response.ok) throw new Error(data.response);
@ -76,7 +60,6 @@ const useUpdateCollection = () => {
}, },
onSuccess: (data) => { onSuccess: (data) => {
{ {
toast.success(t("updated"));
return queryClient.setQueryData(["collections"], (oldData: any) => { return queryClient.setQueryData(["collections"], (oldData: any) => {
return oldData.map((collection: any) => return oldData.map((collection: any) =>
collection.id === data.id ? data : collection collection.id === data.id ? data : collection
@ -92,20 +75,14 @@ const useUpdateCollection = () => {
// ) // )
// }); // });
// }, // },
onError: (error) => {
toast.error(error.message);
},
}); });
}; };
const useDeleteCollection = () => { const useDeleteCollection = () => {
const { t } = useTranslation();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: async (id: number) => { mutationFn: async (id: number) => {
const load = toast.loading(t("deleting_collection"));
const response = await fetch(`/api/v1/collections/${id}`, { const response = await fetch(`/api/v1/collections/${id}`, {
method: "DELETE", method: "DELETE",
headers: { headers: {
@ -113,8 +90,6 @@ const useDeleteCollection = () => {
}, },
}); });
toast.dismiss(load);
const data = await response.json(); const data = await response.json();
if (!response.ok) throw new Error(data.response); if (!response.ok) throw new Error(data.response);
@ -122,14 +97,10 @@ const useDeleteCollection = () => {
return data.response; return data.response;
}, },
onSuccess: (data) => { onSuccess: (data) => {
toast.success(t("deleted"));
return queryClient.setQueryData(["collections"], (oldData: any) => { return queryClient.setQueryData(["collections"], (oldData: any) => {
return oldData.filter((collection: any) => collection.id !== data.id); return oldData.filter((collection: any) => collection.id !== data.id);
}); });
}, },
onError: (error) => {
toast.error(error.message);
},
}); });
}; };

View File

@ -5,14 +5,12 @@ import {
useQueryClient, useQueryClient,
useMutation, useMutation,
} from "@tanstack/react-query"; } from "@tanstack/react-query";
import { useEffect, useMemo } from "react"; import { useMemo } from "react";
import { import {
ArchivedFormat, ArchivedFormat,
LinkIncludingShortenedCollectionAndTags, LinkIncludingShortenedCollectionAndTags,
LinkRequestQuery, LinkRequestQuery,
} from "@/types/global"; } from "@/types/global";
import toast from "react-hot-toast";
import { useTranslation } from "next-i18next";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
const useLinks = (params: LinkRequestQuery = {}) => { const useLinks = (params: LinkRequestQuery = {}) => {
@ -98,13 +96,10 @@ const buildQueryString = (params: LinkRequestQuery) => {
}; };
const useAddLink = () => { const useAddLink = () => {
const { t } = useTranslation();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: async (link: LinkIncludingShortenedCollectionAndTags) => { mutationFn: async (link: LinkIncludingShortenedCollectionAndTags) => {
const load = toast.loading(t("creating_link"));
const response = await fetch("/api/v1/links", { const response = await fetch("/api/v1/links", {
method: "POST", method: "POST",
headers: { headers: {
@ -113,8 +108,6 @@ const useAddLink = () => {
body: JSON.stringify(link), body: JSON.stringify(link),
}); });
toast.dismiss(load);
const data = await response.json(); const data = await response.json();
if (!response.ok) throw new Error(data.response); if (!response.ok) throw new Error(data.response);
@ -122,8 +115,6 @@ const useAddLink = () => {
return data.response; return data.response;
}, },
onSuccess: (data) => { onSuccess: (data) => {
toast.success(t("link_created"));
queryClient.setQueryData(["dashboardData"], (oldData: any) => { queryClient.setQueryData(["dashboardData"], (oldData: any) => {
if (!oldData) return undefined; if (!oldData) return undefined;
return [data, ...oldData]; return [data, ...oldData];
@ -141,20 +132,14 @@ const useAddLink = () => {
queryClient.invalidateQueries({ queryKey: ["tags"] }); queryClient.invalidateQueries({ queryKey: ["tags"] });
queryClient.invalidateQueries({ queryKey: ["publicLinks"] }); queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
}, },
onError: (error) => {
toast.error(error.message);
},
}); });
}; };
const useUpdateLink = () => { const useUpdateLink = () => {
const { t } = useTranslation();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: async (link: LinkIncludingShortenedCollectionAndTags) => { mutationFn: async (link: LinkIncludingShortenedCollectionAndTags) => {
const load = toast.loading(t("updating"));
const response = await fetch(`/api/v1/links/${link.id}`, { const response = await fetch(`/api/v1/links/${link.id}`, {
method: "PUT", method: "PUT",
headers: { headers: {
@ -163,8 +148,6 @@ const useUpdateLink = () => {
body: JSON.stringify(link), body: JSON.stringify(link),
}); });
toast.dismiss(load);
const data = await response.json(); const data = await response.json();
if (!response.ok) throw new Error(data.response); if (!response.ok) throw new Error(data.response);
@ -172,8 +155,6 @@ const useUpdateLink = () => {
return data.response; return data.response;
}, },
onSuccess: (data) => { onSuccess: (data) => {
toast.success(t("updated"));
queryClient.setQueryData(["dashboardData"], (oldData: any) => { queryClient.setQueryData(["dashboardData"], (oldData: any) => {
if (!oldData) return undefined; if (!oldData) return undefined;
return oldData.map((e: any) => (e.id === data.id ? data : e)); return oldData.map((e: any) => (e.id === data.id ? data : e));
@ -193,26 +174,18 @@ const useUpdateLink = () => {
queryClient.invalidateQueries({ queryKey: ["tags"] }); queryClient.invalidateQueries({ queryKey: ["tags"] });
queryClient.invalidateQueries({ queryKey: ["publicLinks"] }); queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
}, },
onError: (error) => {
toast.error(error.message);
},
}); });
}; };
const useDeleteLink = () => { const useDeleteLink = () => {
const { t } = useTranslation();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: async (id: number) => { mutationFn: async (id: number) => {
const load = toast.loading(t("deleting"));
const response = await fetch(`/api/v1/links/${id}`, { const response = await fetch(`/api/v1/links/${id}`, {
method: "DELETE", method: "DELETE",
}); });
toast.dismiss(load);
const data = await response.json(); const data = await response.json();
if (!response.ok) throw new Error(data.response); if (!response.ok) throw new Error(data.response);
@ -220,8 +193,6 @@ const useDeleteLink = () => {
return data.response; return data.response;
}, },
onSuccess: (data) => { onSuccess: (data) => {
toast.success(t("deleted"));
queryClient.setQueryData(["dashboardData"], (oldData: any) => { queryClient.setQueryData(["dashboardData"], (oldData: any) => {
if (!oldData) return undefined; if (!oldData) return undefined;
return oldData.filter((e: any) => e.id !== data.id); return oldData.filter((e: any) => e.id !== data.id);
@ -241,9 +212,6 @@ const useDeleteLink = () => {
queryClient.invalidateQueries({ queryKey: ["tags"] }); queryClient.invalidateQueries({ queryKey: ["tags"] });
queryClient.invalidateQueries({ queryKey: ["publicLinks"] }); queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
}, },
onError: (error) => {
toast.error(error.message);
},
}); });
}; };
@ -281,13 +249,10 @@ const useGetLink = () => {
}; };
const useBulkDeleteLinks = () => { const useBulkDeleteLinks = () => {
const { t } = useTranslation();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: async (linkIds: number[]) => { mutationFn: async (linkIds: number[]) => {
const load = toast.loading(t("deleting"));
const response = await fetch("/api/v1/links", { const response = await fetch("/api/v1/links", {
method: "DELETE", method: "DELETE",
headers: { headers: {
@ -296,8 +261,6 @@ const useBulkDeleteLinks = () => {
body: JSON.stringify({ linkIds }), body: JSON.stringify({ linkIds }),
}); });
toast.dismiss(load);
const data = await response.json(); const data = await response.json();
if (!response.ok) throw new Error(data.response); if (!response.ok) throw new Error(data.response);
@ -305,8 +268,6 @@ const useBulkDeleteLinks = () => {
return linkIds; return linkIds;
}, },
onSuccess: (data) => { onSuccess: (data) => {
toast.success(t("deleted"));
queryClient.setQueryData(["dashboardData"], (oldData: any) => { queryClient.setQueryData(["dashboardData"], (oldData: any) => {
if (!oldData) return undefined; if (!oldData) return undefined;
return oldData.filter((e: any) => !data.includes(e.id)); return oldData.filter((e: any) => !data.includes(e.id));
@ -326,14 +287,10 @@ const useBulkDeleteLinks = () => {
queryClient.invalidateQueries({ queryKey: ["tags"] }); queryClient.invalidateQueries({ queryKey: ["tags"] });
queryClient.invalidateQueries({ queryKey: ["publicLinks"] }); queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
}, },
onError: (error) => {
toast.error(error.message);
},
}); });
}; };
const useUploadFile = () => { const useUploadFile = () => {
const { t } = useTranslation();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
@ -354,8 +311,6 @@ const useUploadFile = () => {
return { ok: false, data: "Invalid file type." }; return { ok: false, data: "Invalid file type." };
} }
const load = toast.loading(t("creating"));
const response = await fetch("/api/v1/links", { const response = await fetch("/api/v1/links", {
body: JSON.stringify({ body: JSON.stringify({
...link, ...link,
@ -385,13 +340,9 @@ const useUploadFile = () => {
); );
} }
toast.dismiss(load);
return data.response; return data.response;
}, },
onSuccess: (data) => { onSuccess: (data) => {
toast.success(t("created_success"));
queryClient.setQueryData(["dashboardData"], (oldData: any) => { queryClient.setQueryData(["dashboardData"], (oldData: any) => {
if (!oldData) return undefined; if (!oldData) return undefined;
return [data, ...oldData]; return [data, ...oldData];
@ -409,14 +360,10 @@ const useUploadFile = () => {
queryClient.invalidateQueries({ queryKey: ["tags"] }); queryClient.invalidateQueries({ queryKey: ["tags"] });
queryClient.invalidateQueries({ queryKey: ["publicLinks"] }); queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
}, },
onError: (error) => {
toast.error(error.message);
},
}); });
}; };
const useBulkEditLinks = () => { const useBulkEditLinks = () => {
const { t } = useTranslation();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
@ -432,8 +379,6 @@ const useBulkEditLinks = () => {
>; >;
removePreviousTags: boolean; removePreviousTags: boolean;
}) => { }) => {
const load = toast.loading(t("updating"));
const response = await fetch("/api/v1/links", { const response = await fetch("/api/v1/links", {
method: "PUT", method: "PUT",
headers: { headers: {
@ -442,8 +387,6 @@ const useBulkEditLinks = () => {
body: JSON.stringify({ links, newData, removePreviousTags }), body: JSON.stringify({ links, newData, removePreviousTags }),
}); });
toast.dismiss(load);
const data = await response.json(); const data = await response.json();
if (!response.ok) throw new Error(data.response); if (!response.ok) throw new Error(data.response);
@ -451,8 +394,6 @@ const useBulkEditLinks = () => {
return data.response; return data.response;
}, },
onSuccess: (data, { links, newData, removePreviousTags }) => { onSuccess: (data, { links, newData, removePreviousTags }) => {
toast.success(t("updated"));
queryClient.setQueryData(["dashboardData"], (oldData: any) => { queryClient.setQueryData(["dashboardData"], (oldData: any) => {
if (!oldData) return undefined; if (!oldData) return undefined;
return oldData.map((e: any) => return oldData.map((e: any) =>
@ -477,9 +418,6 @@ const useBulkEditLinks = () => {
queryClient.invalidateQueries({ queryKey: ["tags"] }); queryClient.invalidateQueries({ queryKey: ["tags"] });
queryClient.invalidateQueries({ queryKey: ["publicLinks"] }); queryClient.invalidateQueries({ queryKey: ["publicLinks"] });
}, },
onError: (error) => {
toast.error(error.message);
},
}); });
}; };

View File

@ -1,6 +1,4 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import toast from "react-hot-toast";
import { useTranslation } from "next-i18next";
import { TagIncludingLinkCount } from "@/types/global"; import { TagIncludingLinkCount } from "@/types/global";
const useTags = () => { const useTags = () => {
@ -18,12 +16,9 @@ const useTags = () => {
const useUpdateTag = () => { const useUpdateTag = () => {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const { t } = useTranslation();
return useMutation({ return useMutation({
mutationFn: async (tag: TagIncludingLinkCount) => { mutationFn: async (tag: TagIncludingLinkCount) => {
const load = toast.loading(t("applying_changes"));
const response = await fetch(`/api/v1/tags/${tag.id}`, { const response = await fetch(`/api/v1/tags/${tag.id}`, {
body: JSON.stringify(tag), body: JSON.stringify(tag),
headers: { headers: {
@ -35,8 +30,6 @@ const useUpdateTag = () => {
const data = await response.json(); const data = await response.json();
if (!response.ok) throw new Error(data.response); if (!response.ok) throw new Error(data.response);
toast.dismiss(load);
return data.response; return data.response;
}, },
onSuccess: (data) => { onSuccess: (data) => {
@ -45,22 +38,15 @@ const useUpdateTag = () => {
tag.id === data.id ? data : tag tag.id === data.id ? data : tag
) )
); );
toast.success(t("tag_renamed"));
},
onError: (error) => {
toast.error(error.message);
}, },
}); });
}; };
const useRemoveTag = () => { const useRemoveTag = () => {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const { t } = useTranslation();
return useMutation({ return useMutation({
mutationFn: async (tagId: number) => { mutationFn: async (tagId: number) => {
const load = toast.loading(t("applying_changes"));
const response = await fetch(`/api/v1/tags/${tagId}`, { const response = await fetch(`/api/v1/tags/${tagId}`, {
method: "DELETE", method: "DELETE",
}); });
@ -68,18 +54,12 @@ const useRemoveTag = () => {
const data = await response.json(); const data = await response.json();
if (!response.ok) throw new Error(data.response); if (!response.ok) throw new Error(data.response);
toast.dismiss(load);
return data.response; return data.response;
}, },
onSuccess: (data, variables) => { onSuccess: (data, variables) => {
queryClient.setQueryData(["tags"], (oldData: any) => queryClient.setQueryData(["tags"], (oldData: any) =>
oldData.filter((tag: TagIncludingLinkCount) => tag.id !== variables) oldData.filter((tag: TagIncludingLinkCount) => tag.id !== variables)
); );
toast.success(t("tag_deleted"));
},
onError: (error) => {
toast.error(error.message);
}, },
}); });
}; };

View File

@ -1,6 +1,4 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import toast from "react-hot-toast";
import { useTranslation } from "next-i18next";
import { AccessToken } from "@prisma/client"; import { AccessToken } from "@prisma/client";
const useTokens = () => { const useTokens = () => {
@ -19,12 +17,9 @@ const useTokens = () => {
const useAddToken = () => { const useAddToken = () => {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const { t } = useTranslation();
return useMutation({ return useMutation({
mutationFn: async (body: Partial<AccessToken>) => { mutationFn: async (body: Partial<AccessToken>) => {
const load = toast.loading(t("creating_token"));
const response = await fetch("/api/v1/tokens", { const response = await fetch("/api/v1/tokens", {
body: JSON.stringify(body), body: JSON.stringify(body),
method: "POST", method: "POST",
@ -33,8 +28,6 @@ const useAddToken = () => {
const data = await response.json(); const data = await response.json();
if (!response.ok) throw new Error(data.response); if (!response.ok) throw new Error(data.response);
toast.dismiss(load);
return data.response; return data.response;
}, },
onSuccess: (data) => { onSuccess: (data) => {
@ -42,22 +35,15 @@ const useAddToken = () => {
...oldData, ...oldData,
data.token, data.token,
]); ]);
toast.success(t("token_added"));
},
onError: (error) => {
toast.error(error.message);
}, },
}); });
}; };
const useRevokeToken = () => { const useRevokeToken = () => {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const { t } = useTranslation();
return useMutation({ return useMutation({
mutationFn: async (tokenId: number) => { mutationFn: async (tokenId: number) => {
const load = toast.loading(t("deleting"));
const response = await fetch(`/api/v1/tokens/${tokenId}`, { const response = await fetch(`/api/v1/tokens/${tokenId}`, {
method: "DELETE", method: "DELETE",
}); });
@ -65,18 +51,12 @@ const useRevokeToken = () => {
const data = await response.json(); const data = await response.json();
if (!response.ok) throw new Error(data.response); if (!response.ok) throw new Error(data.response);
toast.dismiss(load);
return data.response; return data.response;
}, },
onSuccess: (data, variables) => { onSuccess: (data, variables) => {
queryClient.setQueryData(["tokens"], (oldData: AccessToken[]) => queryClient.setQueryData(["tokens"], (oldData: AccessToken[]) =>
oldData.filter((token: Partial<AccessToken>) => token.id !== variables) oldData.filter((token: Partial<AccessToken>) => token.id !== variables)
); );
toast.success(t("token_revoked"));
},
onError: (error) => {
toast.error(error.message);
}, },
}); });
}; };

View File

@ -1,6 +1,4 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; 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"; import { useSession } from "next-auth/react";
const useUser = () => { const useUser = () => {
@ -24,13 +22,10 @@ const useUser = () => {
}; };
const useUpdateUser = () => { const useUpdateUser = () => {
const { t } = useTranslation();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: async (user: any) => { mutationFn: async (user: any) => {
const load = toast.loading(t("applying_settings"));
const response = await fetch(`/api/v1/users/${user.id}`, { const response = await fetch(`/api/v1/users/${user.id}`, {
method: "PUT", method: "PUT",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
@ -39,14 +34,11 @@ const useUpdateUser = () => {
const data = await response.json(); const data = await response.json();
toast.dismiss(load);
if (!response.ok) throw new Error(data.response); if (!response.ok) throw new Error(data.response);
return data; return data;
}, },
onSuccess: (data) => { onSuccess: (data) => {
toast.success(t("settings_applied"));
queryClient.setQueryData(["user"], data.response); queryClient.setQueryData(["user"], data.response);
}, },
onMutate: async (user) => { onMutate: async (user) => {
@ -55,9 +47,6 @@ const useUpdateUser = () => {
return { ...oldData, ...user }; return { ...oldData, ...user };
}); });
}, },
onError: (error) => {
toast.error(error.message);
},
}); });
}; };

View File

@ -80,6 +80,8 @@ export default function Account() {
const submit = async (password?: string) => { const submit = async (password?: string) => {
setSubmitLoader(true); setSubmitLoader(true);
const load = toast.loading(t("applying_settings"));
await updateUser.mutateAsync( await updateUser.mutateAsync(
{ {
...user, ...user,
@ -92,6 +94,20 @@ export default function Account() {
setEmailChangeVerificationModal(false); setEmailChangeVerificationModal(false);
} }
}, },
onSettled: (data, error) => {
toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
if (data.response.email !== user.email) {
toast.success(t("email_change_request"));
setEmailChangeVerificationModal(false);
}
toast.success(t("settings_applied"));
}
},
} }
); );

View File

@ -24,6 +24,8 @@ export default function Password() {
setSubmitLoader(true); setSubmitLoader(true);
const load = toast.loading(t("applying_settings"));
await updateUser.mutateAsync( await updateUser.mutateAsync(
{ {
...account, ...account,
@ -31,9 +33,17 @@ export default function Password() {
oldPassword, oldPassword,
}, },
{ {
onSuccess: () => { onSettled: (data, error) => {
setNewPassword(""); toast.dismiss(load);
setOldPassword("");
if (error) {
toast.error(error.message);
} else {
setNewPassword("");
setOldPassword("");
toast.success(t("settings_applied"));
}
}, },
} }
); );

View File

@ -74,7 +74,22 @@ export default function Appearance() {
const submit = async () => { const submit = async () => {
setSubmitLoader(true); setSubmitLoader(true);
await updateUser.mutateAsync({ ...user }); const load = toast.loading(t("applying_settings"));
await updateUser.mutateAsync(
{ ...user },
{
onSettled: (data, error) => {
toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
toast.success(t("settings_applied"));
}
},
}
);
setSubmitLoader(false); setSubmitLoader(false);
}; };

View File

@ -11,6 +11,7 @@ import getServerSideProps from "@/lib/client/getServerSideProps";
import LinkListOptions from "@/components/LinkListOptions"; import LinkListOptions from "@/components/LinkListOptions";
import { useRemoveTag, useTags, useUpdateTag } from "@/hooks/store/tags"; import { useRemoveTag, useTags, useUpdateTag } from "@/hooks/store/tags";
import Links from "@/components/LinkViews/Links"; import Links from "@/components/LinkViews/Links";
import toast from "react-hot-toast";
export default function Index() { export default function Index() {
const { t } = useTranslation(); const { t } = useTranslation();
@ -74,11 +75,27 @@ export default function Index() {
setSubmitLoader(true); setSubmitLoader(true);
if (activeTag && newTagName) if (activeTag && newTagName) {
await updateTag.mutateAsync({ const load = toast.loading(t("applying_changes"));
...activeTag,
name: newTagName, await updateTag.mutateAsync(
}); {
...activeTag,
name: newTagName,
},
{
onSettled: (data, error) => {
toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
toast.success(t("tag_renamed"));
}
},
}
);
}
setSubmitLoader(false); setSubmitLoader(false);
setRenameTag(false); setRenameTag(false);
@ -87,12 +104,22 @@ export default function Index() {
const remove = async () => { const remove = async () => {
setSubmitLoader(true); setSubmitLoader(true);
if (activeTag?.id) if (activeTag?.id) {
const load = toast.loading(t("applying_changes"));
await removeTag.mutateAsync(activeTag?.id, { await removeTag.mutateAsync(activeTag?.id, {
onSuccess: () => { onSettled: (data, error) => {
router.push("/links"); toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
router.push("/links");
toast.success(t("tag_deleted"));
}
}, },
}); });
}
setSubmitLoader(false); setSubmitLoader(false);
setRenameTag(false); setRenameTag(false);