From da0533ac365661a5ff17c1f672c4b3f3d9821f8f Mon Sep 17 00:00:00 2001 From: Isaac Wise Date: Sat, 10 Feb 2024 02:29:15 -0600 Subject: [PATCH] change routes and add todos --- components/ModalContent/BulkEditLinksModal.tsx | 1 + lib/api/controllers/links/bulk/updateLinksById.ts | 1 + pages/api/v1/links/index.ts | 12 ++++++++++++ store/links.ts | 4 ++-- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/components/ModalContent/BulkEditLinksModal.tsx b/components/ModalContent/BulkEditLinksModal.tsx index 9aff5fe..5896f6b 100644 --- a/components/ModalContent/BulkEditLinksModal.tsx +++ b/components/ModalContent/BulkEditLinksModal.tsx @@ -13,6 +13,7 @@ type Props = { onClose: Function; }; +// TODO: Make this work export default function EditLinkModal({ onClose }: Props) { const { updateLink, updateLinksById } = useLinkStore(); const [submitLoader, setSubmitLoader] = useState(false); diff --git a/lib/api/controllers/links/bulk/updateLinksById.ts b/lib/api/controllers/links/bulk/updateLinksById.ts index bdc2e2d..76ca346 100644 --- a/lib/api/controllers/links/bulk/updateLinksById.ts +++ b/lib/api/controllers/links/bulk/updateLinksById.ts @@ -3,6 +3,7 @@ import { prisma } from "@/lib/api/db"; import getPermission from "@/lib/api/getPermission"; import { UsersAndCollections } from "@prisma/client"; +// Need to fix this export default async function updateLinksById(userId: number, linkIds: number[], data: LinkIncludingShortenedCollectionAndTags) { if (!linkIds || linkIds.length === 0) { return { response: "Please choose valid links.", status: 401 }; diff --git a/pages/api/v1/links/index.ts b/pages/api/v1/links/index.ts index 58a352a..8e58bac 100644 --- a/pages/api/v1/links/index.ts +++ b/pages/api/v1/links/index.ts @@ -3,6 +3,8 @@ import getLinks from "@/lib/api/controllers/links/getLinks"; import postLink from "@/lib/api/controllers/links/postLink"; import { LinkRequestQuery } from "@/types/global"; import verifyUser from "@/lib/api/verifyUser"; +import deleteLinksById from "@/lib/api/controllers/links/bulk/deleteLinksById"; +import updateLinksById from "@/lib/api/controllers/links/bulk/updateLinksById"; export default async function links(req: NextApiRequest, res: NextApiResponse) { const user = await verifyUser({ req, res }); @@ -39,5 +41,15 @@ export default async function links(req: NextApiRequest, res: NextApiResponse) { return res.status(newlink.status).json({ response: newlink.response, }); + } else if (req.method === "PUT") { + const updated = await updateLinksById(user.id, req.body.linkIds, req.body.data); + return res.status(updated.status).json({ + response: updated.response, + }); + } else if (req.method === "DELETE") { + const deleted = await deleteLinksById(user.id, req.body.linkIds); + return res.status(deleted.status).json({ + response: deleted.response, + }); } } diff --git a/store/links.ts b/store/links.ts index 89c4f69..00fda9d 100644 --- a/store/links.ts +++ b/store/links.ts @@ -129,7 +129,7 @@ const useLinkStore = create()((set) => ({ return { ok: response.ok, data: data.response }; }, updateLinksById: async (linkIds, data) => { - const response = await fetch("/api/v1/links/bulk", { + const response = await fetch("/api/v1/links", { body: JSON.stringify({ linkIds, data }), headers: { "Content-Type": "application/json", @@ -172,7 +172,7 @@ const useLinkStore = create()((set) => ({ return { ok: response.ok, data: data.response }; }, deleteLinksById: async (linkIds: number[]) => { - const response = await fetch("/api/v1/links/bulk", { + const response = await fetch("/api/v1/links", { body: JSON.stringify({ linkIds }), headers: { "Content-Type": "application/json",