// Copyright (C) 2022-present Daniel31x13 // This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3. // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. // You should have received a copy of the GNU General Public License along with this program. If not, see . import React, { useState } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faClose, faPenToSquare, faTrashCan, } from "@fortawesome/free-solid-svg-icons"; import useCollectionStore from "@/store/collections"; import { ExtendedCollection } from "@/types/global"; import { useSession } from "next-auth/react"; import getPublicUserDataByEmail from "@/lib/client/getPublicUserDataByEmail"; import Modal from "@/components/Modal"; import DeleteCollection from "@/components/Modal/DeleteCollection"; import RequiredBadge from "../RequiredBadge"; type Props = { toggleCollectionModal: Function; collection: ExtendedCollection; }; export default function EditCollection({ toggleCollectionModal, collection, }: Props) { const [activeCollection, setActiveCollection] = useState(collection); const [memberEmail, setMemberEmail] = useState(""); const { updateCollection } = useCollectionStore(); const [deleteCollectionModal, setDeleteCollectionModal] = useState(false); const toggleDeleteCollectionModal = () => { setDeleteCollectionModal(!deleteCollectionModal); }; const session = useSession(); const submit = async () => { console.log(activeCollection); const response = await updateCollection( activeCollection as ExtendedCollection ); if (response) toggleCollectionModal(); }; return (

Edit Collection

Name

setActiveCollection({ ...activeCollection, name: e.target.value }) } type="text" placeholder="e.g. Example Collection" className="w-56 sm:w-96 rounded-md p-3 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100" />

Description

setActiveCollection({ ...activeCollection, description: e.target.value, }) } type="text" placeholder="Collection description" className="w-56 sm:w-96 rounded-md p-3 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100" />

Members

{ setMemberEmail(e.target.value); }} onKeyDown={async (e) => { const checkIfMemberAlreadyExists = activeCollection.members.find( (e) => e.user.email === memberEmail ); const ownerEmail = session.data?.user.email; if ( e.key === "Enter" && // no duplicate members !checkIfMemberAlreadyExists && // member can't be empty memberEmail.trim() !== "" && // member can't be the owner memberEmail.trim() !== ownerEmail ) { // Lookup, get data/err, list ... const user = await getPublicUserDataByEmail(memberEmail.trim()); if (user.email) { const newMember = { collectionId: activeCollection.id, userId: user.id, canCreate: false, canUpdate: false, canDelete: false, user: { name: user.name, email: user.email, }, }; setActiveCollection({ ...activeCollection, members: [...activeCollection.members, newMember], }); setMemberEmail(""); } } }} type="text" placeholder="Email" className="w-56 sm:w-96 rounded-md p-3 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100" />
{activeCollection.members[0] ? (

(All Members will have Read access to this collection.)

) : null} {activeCollection.members.map((e, i) => { return (
{ const updatedMembers = activeCollection.members.filter( (member) => { return member.user.email !== e.user.email; } ); setActiveCollection({ ...activeCollection, members: updatedMembers, }); }} />

{e.user.name}

{e.user.email}

Permissions

(Click to toggle.)

); })}
Edit Collection

OR


{ toggleDeleteCollectionModal(); }} className="w-fit inline-flex rounded-md cursor-pointer bg-red-500 hover:bg-red-400 duration-100 p-2" >
{deleteCollectionModal ? ( ) : null}
); }