// 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, faPlus } from "@fortawesome/free-solid-svg-icons"; import useCollectionStore from "@/store/collections"; import { NewCollection } from "@/types/global"; import { useSession } from "next-auth/react"; type Props = { toggleCollectionModal: Function; }; export default function AddCollection({ toggleCollectionModal }: Props) { const [newCollection, setNewCollection] = useState({ name: "", description: "", members: [], }); const [memberEmail, setMemberEmail] = useState(""); const { addCollection } = useCollectionStore(); const session = useSession(); const submit = async () => { console.log(newCollection); const response = await addCollection(newCollection as NewCollection); if (response) toggleCollectionModal(); }; const getUserByEmail = async (email: string) => { const response = await fetch(`/api/routes/users?email=${email}`); const data = await response.json(); return data.response; }; return (

New Collection

Name

setNewCollection({ ...newCollection, 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 text-sm outline-none focus:border-sky-500 duration-100" />

Description

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

Members

{ setMemberEmail(e.target.value); }} onKeyDown={async (e) => { const checkIfMemberAlreadyExists = newCollection.members.find( (e) => e.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 getUserByEmail(memberEmail.trim()); if (user.email) { const newMember = { name: user.name, email: user.email, canCreate: false, canUpdate: false, canDelete: false, }; setNewCollection({ ...newCollection, members: [...newCollection.members, newMember], }); setMemberEmail(""); } } }} type="text" placeholder="Email (Optional)" className="w-56 sm:w-96 rounded-md p-3 border-sky-100 border-solid border text-sm outline-none focus:border-sky-500 duration-100" />
{newCollection.members[0] ? (

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

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

{e.name}

{e.email}

Permissions

(Click to toggle.)

); })}
Add Collection
); }