// 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, { useEffect, useState } from "react"; import CollectionSelection from "@/components/InputSelect/CollectionSelection"; import TagSelection from "@/components/InputSelect/TagSelection"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faPlus } from "@fortawesome/free-solid-svg-icons"; import { ExtendedLink, NewLink } from "@/types/global"; import useLinkStore from "@/store/links"; import { useRouter } from "next/router"; import useCollectionStore from "@/store/collections"; import RequiredBadge from "../RequiredBadge"; type Props = { toggleLinkModal: Function; }; export default function AddLink({ toggleLinkModal }: Props) { const router = useRouter(); const [newLink, setNewLink] = useState({ name: "", url: "", tags: [], collection: { id: undefined, name: "", ownerId: undefined, }, }); const { addLink } = useLinkStore(); const { collections } = useCollectionStore(); useEffect(() => { if (router.query.id) { const currentCollection = collections.find( (e) => e.id == Number(router.query.id) ); setNewLink({ ...newLink, collection: { id: currentCollection?.id, name: currentCollection?.name, ownerId: currentCollection?.ownerId, }, }); } }, []); const setTags = (e: any) => { const tagNames = e.map((e: any) => { return { name: e.label }; }); setNewLink({ ...newLink, tags: tagNames }); }; const setCollection = (e: any) => { if (e?.__isNew__) e.value = null; setNewLink({ ...newLink, collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId }, }); }; const submit = async () => { console.log(newLink); const response = await addLink(newLink as NewLink); if (response) toggleLinkModal(); }; return (

New Link

Name

setNewLink({ ...newLink, name: e.target.value })} type="text" placeholder="e.g. Example Link" className="w-60 rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100" />

URL

setNewLink({ ...newLink, url: e.target.value })} type="text" placeholder="e.g. http://example.com/" className="w-60 rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100" />

Collection

Tags

Add Link
); }