import React, { useEffect, useState } from "react"; import CollectionSelection from "@/components/InputSelect/CollectionSelection"; import TagSelection from "@/components/InputSelect/TagSelection"; import { LinkIncludingShortenedCollectionAndTags } from "@/types/global"; import { faPenToSquare } from "@fortawesome/free-regular-svg-icons"; import useLinkStore from "@/store/links"; import { faLink, faPlus } from "@fortawesome/free-solid-svg-icons"; import { useSession } from "next-auth/react"; import useCollectionStore from "@/store/collections"; import { useRouter } from "next/router"; import SubmitButton from "../../SubmitButton"; import { toast } from "react-hot-toast"; import Link from "next/link"; import TextInput from "@/components/TextInput"; import unescapeString from "@/lib/client/unescapeString"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; type Props = | { toggleLinkModal: Function; method: "CREATE"; activeLink?: LinkIncludingShortenedCollectionAndTags; } | { toggleLinkModal: Function; method: "UPDATE"; activeLink: LinkIncludingShortenedCollectionAndTags; }; export default function AddOrEditLink({ toggleLinkModal, method, activeLink, }: Props) { const [submitLoader, setSubmitLoader] = useState(false); const [optionsExpanded, setOptionsExpanded] = useState( method === "UPDATE" ? true : false ); const { data } = useSession(); const [link, setLink] = useState( activeLink || { name: "", url: "", description: "", tags: [], screenshotPath: "", pdfPath: "", readabilityPath: "", textContent: "", collection: { name: "", ownerId: data?.user.id as number, }, } ); const { updateLink, addLink } = useLinkStore(); const router = useRouter(); const { collections } = useCollectionStore(); useEffect(() => { if (method === "CREATE") { if (router.query.id) { const currentCollection = collections.find( (e) => e.id == Number(router.query.id) ); if ( currentCollection && currentCollection.ownerId && router.asPath.startsWith("/collections/") ) setLink({ ...link, collection: { id: currentCollection.id, name: currentCollection.name, ownerId: currentCollection.ownerId, }, }); } else setLink({ ...link, collection: { // id: , name: "Unorganized", ownerId: data?.user.id as number, }, }); } }, []); const setTags = (e: any) => { const tagNames = e.map((e: any) => { return { name: e.label }; }); setLink({ ...link, tags: tagNames }); }; const setCollection = (e: any) => { if (e?.__isNew__) e.value = null; setLink({ ...link, collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId }, }); }; const submit = async () => { setSubmitLoader(true); let response; const load = toast.loading( method === "UPDATE" ? "Applying..." : "Creating..." ); if (method === "UPDATE") response = await updateLink(link); else response = await addLink(link); toast.dismiss(load); if (response.ok) { toast.success(`Link ${method === "UPDATE" ? "Saved!" : "Created!"}`); toggleLinkModal(); } else toast.error(response.data as string); setSubmitLoader(false); return response; }; return (
{method === "UPDATE" ? (
{link.url}
) : null} {method === "CREATE" ? (

Address (URL)

setLink({ ...link, url: e.target.value })} placeholder="e.g. http://example.com/" className="bg-base-200" />

Collection

{link.collection.name ? ( ) : null}
) : undefined} {optionsExpanded ? (
{/*
*/}

Name

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

Collection

{link.collection.name ? ( ) : undefined}
) : undefined}

Tags

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

Description