Only show checkbox if the user has perms

This commit is contained in:
Isaac Wise 2024-02-10 16:04:30 -06:00
parent 582607e726
commit e1ef638f0e
4 changed files with 25 additions and 16 deletions

View File

@ -16,7 +16,7 @@ import Link from "next/link";
import LinkIcon from "./LinkComponents/LinkIcon"; import LinkIcon from "./LinkComponents/LinkIcon";
import useOnScreen from "@/hooks/useOnScreen"; import useOnScreen from "@/hooks/useOnScreen";
import { generateLinkHref } from "@/lib/client/generateLinkHref"; import { generateLinkHref } from "@/lib/client/generateLinkHref";
import useAccountStore from "@/store/account"; import usePermissions from "@/hooks/usePermissions";
type Props = { type Props = {
link: LinkIncludingShortenedCollectionAndTags; link: LinkIncludingShortenedCollectionAndTags;
@ -43,8 +43,6 @@ export default function LinkCard({
} }
}; };
console.log(selectedLinks)
let shortendURL; let shortendURL;
try { try {
@ -70,6 +68,7 @@ export default function LinkCard({
const ref = useRef<HTMLDivElement>(null); const ref = useRef<HTMLDivElement>(null);
const isVisible = useOnScreen(ref); const isVisible = useOnScreen(ref);
const permissions = usePermissions(collection.id as number);
useEffect(() => { useEffect(() => {
let interval: any; let interval: any;
@ -98,7 +97,7 @@ export default function LinkCard({
ref={ref} ref={ref}
className="border border-solid border-neutral-content bg-base-200 shadow-md hover:shadow-none duration-100 rounded-2xl relative" className="border border-solid border-neutral-content bg-base-200 shadow-md hover:shadow-none duration-100 rounded-2xl relative"
> >
{showCheckbox && {showCheckbox && (permissions === true || permissions?.canCreate || permissions?.canDelete) &&
<input <input
type="checkbox" type="checkbox"
className="checkbox checkbox-primary my-auto ml-3 mt-3 absolute z-20 bg-white dark:bg-base-200" className="checkbox checkbox-primary my-auto ml-3 mt-3 absolute z-20 bg-white dark:bg-base-200"

View File

@ -13,6 +13,7 @@ import LinkIcon from "@/components/LinkViews/LinkComponents/LinkIcon";
import Link from "next/link"; import Link from "next/link";
import { isPWA } from "@/lib/client/utils"; import { isPWA } from "@/lib/client/utils";
import { generateLinkHref } from "@/lib/client/generateLinkHref"; import { generateLinkHref } from "@/lib/client/generateLinkHref";
import usePermissions from "@/hooks/usePermissions";
type Props = { type Props = {
link: LinkIncludingShortenedCollectionAndTags; link: LinkIncludingShortenedCollectionAndTags;
@ -30,8 +31,12 @@ export default function LinkCardCompact({
const { collections } = useCollectionStore(); const { collections } = useCollectionStore();
const { links, setSelectedLinks, selectedLinks } = useLinkStore(); const { links, setSelectedLinks, selectedLinks } = useLinkStore();
const handleCheckboxClick = (checkboxId: number) => { const handleCheckboxClick = (link: LinkIncludingShortenedCollectionAndTags) => {
setSelectedLinks((selectedLinks.includes(checkboxId) ? selectedLinks.filter((id) => id !== checkboxId) : [...selectedLinks, checkboxId])); if (selectedLinks.includes(link)) {
setSelectedLinks(selectedLinks.filter((e) => e !== link));
} else {
setSelectedLinks([...selectedLinks, link]);
}
}; };
let shortendURL; let shortendURL;
@ -57,6 +62,8 @@ export default function LinkCardCompact({
); );
}, [collections, links]); }, [collections, links]);
const permissions = usePermissions(collection.id as number);
const [showInfo, setShowInfo] = useState(false); const [showInfo, setShowInfo] = useState(false);
return ( return (
@ -65,12 +72,12 @@ export default function LinkCardCompact({
className={`border-neutral-content relative items-center flex ${!showInfo && !isPWA() ? "hover:bg-base-300 p-3" : "py-3" className={`border-neutral-content relative items-center flex ${!showInfo && !isPWA() ? "hover:bg-base-300 p-3" : "py-3"
} duration-200 rounded-lg`} } duration-200 rounded-lg`}
> >
{showCheckbox && {showCheckbox && (permissions === true || permissions?.canCreate || permissions?.canDelete) &&
<input <input
type="checkbox" type="checkbox"
className="checkbox checkbox-primary my-auto mr-2" className="checkbox checkbox-primary my-auto mr-2"
checked={selectedLinks.includes(link.id)} checked={selectedLinks.includes(link)}
onChange={() => handleCheckboxClick(link.id)} onChange={() => handleCheckboxClick(link)}
/> />
} }
<Link <Link

View File

@ -17,7 +17,7 @@ export default function BulkDeleteLinksModal({ onClose }: Props) {
toast.dismiss(load); toast.dismiss(load);
response.ok && toast.success(`Deleted ${selectedLinks.length} Link${selectedLinks.length > 1 ? "s" : ""}!`); response.ok && toast.success(`Deleted ${selectedLinks.length} Link${selectedLinks.length > 1 ? "s" : ""}`);
setSelectedLinks([]); setSelectedLinks([]);
onClose(); onClose();
@ -25,7 +25,7 @@ export default function BulkDeleteLinksModal({ onClose }: Props) {
return ( return (
<Modal toggleModal={onClose}> <Modal toggleModal={onClose}>
<p className="text-xl font-thin text-red-500">Delete {selectedLinks.length} Link{selectedLinks.length > 1 ? "s" : ""}!</p> <p className="text-xl font-thin text-red-500">Delete {selectedLinks.length} Link{selectedLinks.length > 1 ? "s" : ""}</p>
<div className="divider mb-3 mt-1"></div> <div className="divider mb-3 mt-1"></div>

View File

@ -42,6 +42,7 @@ export default function Index() {
useState<CollectionIncludingMembersAndLinkCount>(); useState<CollectionIncludingMembersAndLinkCount>();
const permissions = usePermissions(activeCollection?.id as number); const permissions = usePermissions(activeCollection?.id as number);
console.log(permissions)
useLinks({ collectionId: Number(router.query.id), sort: sortBy }); useLinks({ collectionId: Number(router.query.id), sort: sortBy });
@ -316,20 +317,22 @@ export default function Index() {
</span> </span>
)} )}
</div> </div>
{selectedLinks.length > 0 && permissions &&
<div className="flex gap-3"> <div className="flex gap-3">
{selectedLinks.length > 0 && (permissions === true || permissions?.canUpdate) &&
<button className="btn btn-sm btn-accent dark:border-violet-400 text-white w-fit ml-auto"> <button className="btn btn-sm btn-accent dark:border-violet-400 text-white w-fit ml-auto">
Edit Links Edit
</button> </button>
}
{selectedLinks.length > 0 && (permissions === true || permissions?.canDelete) &&
<button onClick={(e) => { <button onClick={(e) => {
(document?.activeElement as HTMLElement)?.blur(); (document?.activeElement as HTMLElement)?.blur();
e.shiftKey ? bulkDeleteLinks() : setBulkDeleteLinksModal(true); e.shiftKey ? bulkDeleteLinks() : setBulkDeleteLinksModal(true);
}} className="btn btn-sm bg-red-400 border-red-400 hover:border-red-500 hover:bg-red-500 text-white w-fit ml-auto"> }} className="btn btn-sm bg-red-400 border-red-400 hover:border-red-500 hover:bg-red-500 text-white w-fit ml-auto">
Delete Delete
</button> </button>
</div>
} }
</div> </div>
</div>
{links.some((e) => e.collectionId === Number(router.query.id)) ? ( {links.some((e) => e.collectionId === Number(router.query.id)) ? (
<LinkComponent <LinkComponent