From 26997475fdb46c47927f2f320f6a4286077eb5bb Mon Sep 17 00:00:00 2001 From: Isaac Wise Date: Sat, 10 Feb 2024 01:38:19 -0600 Subject: [PATCH] Fix permission checking --- components/LinkViews/LinkCard.tsx | 2 +- .../ModalContent/BulkDeleteLinksModal.tsx | 2 +- .../ModalContent/BulkEditLinksModal.tsx | 150 ++++++++++++++++++ .../controllers/links/bulk/deleteLinksById.ts | 33 ++-- .../controllers/links/bulk/updateLinksById.ts | 64 ++++++++ pages/api/v1/links/bulk/index.ts | 9 +- store/links.ts | 24 +++ 7 files changed, 268 insertions(+), 16 deletions(-) create mode 100644 components/ModalContent/BulkEditLinksModal.tsx create mode 100644 lib/api/controllers/links/bulk/updateLinksById.ts diff --git a/components/LinkViews/LinkCard.tsx b/components/LinkViews/LinkCard.tsx index 8e7f932..a85ffcc 100644 --- a/components/LinkViews/LinkCard.tsx +++ b/components/LinkViews/LinkCard.tsx @@ -95,7 +95,7 @@ export default function LinkCard({ {showCheckbox && handleCheckboxClick(link.id)} /> diff --git a/components/ModalContent/BulkDeleteLinksModal.tsx b/components/ModalContent/BulkDeleteLinksModal.tsx index 69ba026..5cb71b7 100644 --- a/components/ModalContent/BulkDeleteLinksModal.tsx +++ b/components/ModalContent/BulkDeleteLinksModal.tsx @@ -25,7 +25,7 @@ export default function BulkDeleteLinksModal({ onClose }: Props) { return ( -

Delete {selectedLinks.length}

+

Delete {selectedLinks.length} Link{selectedLinks.length > 1 ? "s" : ""}!

diff --git a/components/ModalContent/BulkEditLinksModal.tsx b/components/ModalContent/BulkEditLinksModal.tsx new file mode 100644 index 0000000..9aff5fe --- /dev/null +++ b/components/ModalContent/BulkEditLinksModal.tsx @@ -0,0 +1,150 @@ +import React, { useEffect, useState } from "react"; +import CollectionSelection from "@/components/InputSelect/CollectionSelection"; +import TagSelection from "@/components/InputSelect/TagSelection"; +import TextInput from "@/components/TextInput"; +import unescapeString from "@/lib/client/unescapeString"; +import useLinkStore from "@/store/links"; +import { LinkIncludingShortenedCollectionAndTags } from "@/types/global"; +import toast from "react-hot-toast"; +import Link from "next/link"; +import Modal from "../Modal"; + +type Props = { + onClose: Function; +}; + +export default function EditLinkModal({ onClose }: Props) { + const { updateLink, updateLinksById } = useLinkStore(); + const [submitLoader, setSubmitLoader] = useState(false); + const [updatedValues, setUpdatedValues] = useState>(); + + const setCollection = (e: any) => { + if (e?.__isNew__) e.value = null; + + setUpdatedValues({ + ...updatedValues, + collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId }, + }); + }; + + const setTags = (e: any) => { + const tagNames = e.map((e: any) => { + return { name: e.label }; + }); + + setUpdatedValues({ ...updatedValues, tags: tagNames }); + }; + + const submit = async () => { + if (!submitLoader) { + setSubmitLoader(true); + + let response; + + const load = toast.loading("Updating..."); + + response = await updateLinksById(user); + + toast.dismiss(load); + + if (response.ok) { + toast.success(`Updated!`); + onClose(); + } else toast.error(response.data as string); + + setSubmitLoader(false); + + return response; + } + }; + + return ( + +

Edit Link

+ +
+ + {link.url ? ( + + +

{shortendURL}

+ + ) : undefined} + +
+

Name

+ setLink({ ...link, name: e.target.value })} + placeholder="e.g. Example Link" + className="bg-base-200" + /> +
+ +
+ {/*
*/} +
+
+

Collection

+ {link.collection.name ? ( + + ) : null} +
+ +
+

Tags

+ { + return { label: e.name, value: e.id }; + })} + /> +
+ +
+

Description

+