2023-11-29 08:41:24 -06:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { Toaster } from "react-hot-toast";
|
|
|
|
import CollectionSelection from "@/components/InputSelect/CollectionSelection";
|
|
|
|
import TagSelection from "@/components/InputSelect/TagSelection";
|
|
|
|
import TextInput from "@/components/TextInput";
|
|
|
|
import unescapeString from "@/lib/client/unescapeString";
|
|
|
|
import useCollectionStore from "@/store/collections";
|
|
|
|
import useLinkStore from "@/store/links";
|
|
|
|
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
|
|
|
|
import { useSession } from "next-auth/react";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import toast from "react-hot-toast";
|
2023-12-01 16:42:45 -06:00
|
|
|
import Modal from "../Modal";
|
2023-11-29 08:41:24 -06:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
onClose: Function;
|
|
|
|
};
|
|
|
|
|
2023-12-01 16:42:45 -06:00
|
|
|
export default function NewLinkModal({ onClose }: Props) {
|
2023-11-29 08:41:24 -06:00
|
|
|
const { data } = useSession();
|
|
|
|
|
|
|
|
const initial = {
|
|
|
|
name: "",
|
|
|
|
url: "",
|
|
|
|
description: "",
|
2023-12-02 14:23:26 -06:00
|
|
|
type: "url",
|
2023-11-29 08:41:24 -06:00
|
|
|
tags: [],
|
|
|
|
screenshotPath: "",
|
|
|
|
pdfPath: "",
|
|
|
|
readabilityPath: "",
|
|
|
|
textContent: "",
|
|
|
|
collection: {
|
|
|
|
name: "",
|
|
|
|
ownerId: data?.user.id as number,
|
|
|
|
},
|
2023-12-02 14:23:26 -06:00
|
|
|
} as LinkIncludingShortenedCollectionAndTags;
|
2023-11-29 08:41:24 -06:00
|
|
|
|
|
|
|
const [link, setLink] =
|
|
|
|
useState<LinkIncludingShortenedCollectionAndTags>(initial);
|
|
|
|
|
|
|
|
const { addLink } = useLinkStore();
|
|
|
|
const [submitLoader, setSubmitLoader] = useState(false);
|
|
|
|
|
|
|
|
const [optionsExpanded, setOptionsExpanded] = useState(false);
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
const { collections } = useCollectionStore();
|
|
|
|
|
|
|
|
const setCollection = (e: any) => {
|
|
|
|
if (e?.__isNew__) e.value = null;
|
|
|
|
|
|
|
|
setLink({
|
|
|
|
...link,
|
|
|
|
collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const setTags = (e: any) => {
|
|
|
|
const tagNames = e.map((e: any) => {
|
|
|
|
return { name: e.label };
|
|
|
|
});
|
|
|
|
|
|
|
|
setLink({ ...link, tags: tagNames });
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (router.query.id) {
|
|
|
|
const currentCollection = collections.find(
|
|
|
|
(e) => e.id == Number(router.query.id)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (
|
|
|
|
currentCollection &&
|
|
|
|
currentCollection.ownerId &&
|
|
|
|
router.asPath.startsWith("/collections/")
|
|
|
|
)
|
|
|
|
setLink({
|
|
|
|
...initial,
|
|
|
|
collection: {
|
|
|
|
id: currentCollection.id,
|
|
|
|
name: currentCollection.name,
|
|
|
|
ownerId: currentCollection.ownerId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else
|
|
|
|
setLink({
|
|
|
|
...initial,
|
|
|
|
collection: {
|
|
|
|
name: "Unorganized",
|
|
|
|
ownerId: data?.user.id as number,
|
|
|
|
},
|
|
|
|
});
|
2023-12-01 16:42:45 -06:00
|
|
|
}, []);
|
2023-11-29 08:41:24 -06:00
|
|
|
|
|
|
|
const submit = async () => {
|
|
|
|
if (!submitLoader) {
|
|
|
|
setSubmitLoader(true);
|
|
|
|
|
|
|
|
let response;
|
|
|
|
|
|
|
|
const load = toast.loading("Creating...");
|
|
|
|
|
|
|
|
response = await addLink(link);
|
|
|
|
|
|
|
|
toast.dismiss(load);
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
toast.success(`Created!`);
|
2023-12-01 16:42:45 -06:00
|
|
|
onClose();
|
2023-11-29 08:41:24 -06:00
|
|
|
} else toast.error(response.data as string);
|
|
|
|
|
|
|
|
setSubmitLoader(false);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-12-01 16:42:45 -06:00
|
|
|
<Modal toggleModal={onClose}>
|
2023-12-03 22:52:32 -06:00
|
|
|
<p className="text-xl font-thin">Create a New Link</p>
|
|
|
|
|
2023-12-05 14:17:36 -06:00
|
|
|
<div className="divider mb-3 mt-1"></div>
|
2023-12-03 22:52:32 -06:00
|
|
|
|
2023-12-01 16:42:45 -06:00
|
|
|
<div className="grid grid-flow-row-dense sm:grid-cols-5 gap-3">
|
|
|
|
<div className="sm:col-span-3 col-span-5">
|
|
|
|
<p className="mb-2">Link</p>
|
|
|
|
<TextInput
|
2023-12-02 14:23:26 -06:00
|
|
|
value={link.url || ""}
|
2023-12-01 16:42:45 -06:00
|
|
|
onChange={(e) => setLink({ ...link, url: e.target.value })}
|
|
|
|
placeholder="e.g. http://example.com/"
|
|
|
|
className="bg-base-200"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="sm:col-span-2 col-span-5">
|
|
|
|
<p className="mb-2">Collection</p>
|
|
|
|
{link.collection.name ? (
|
|
|
|
<CollectionSelection
|
|
|
|
onChange={setCollection}
|
|
|
|
defaultValue={{
|
|
|
|
label: link.collection.name,
|
|
|
|
value: link.collection.id,
|
|
|
|
}}
|
2023-11-29 08:41:24 -06:00
|
|
|
/>
|
2023-12-01 16:42:45 -06:00
|
|
|
) : null}
|
2023-11-29 08:41:24 -06:00
|
|
|
</div>
|
2023-12-01 16:42:45 -06:00
|
|
|
</div>
|
2023-11-29 08:41:24 -06:00
|
|
|
|
2023-12-01 16:42:45 -06:00
|
|
|
{optionsExpanded ? (
|
|
|
|
<div className="mt-5">
|
|
|
|
{/* <hr className="mb-3 border border-neutral-content" /> */}
|
|
|
|
<div className="grid sm:grid-cols-2 gap-3">
|
|
|
|
<div>
|
|
|
|
<p className="mb-2">Name</p>
|
|
|
|
<TextInput
|
|
|
|
value={link.name}
|
|
|
|
onChange={(e) => setLink({ ...link, name: e.target.value })}
|
|
|
|
placeholder="e.g. Example Link"
|
|
|
|
className="bg-base-200"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<p className="mb-2">Tags</p>
|
|
|
|
<TagSelection
|
|
|
|
onChange={setTags}
|
|
|
|
defaultValue={link.tags.map((e) => {
|
|
|
|
return { label: e.name, value: e.id };
|
|
|
|
})}
|
|
|
|
/>
|
2023-11-29 08:41:24 -06:00
|
|
|
</div>
|
|
|
|
|
2023-12-01 16:42:45 -06:00
|
|
|
<div className="sm:col-span-2">
|
|
|
|
<p className="mb-2">Description</p>
|
|
|
|
<textarea
|
|
|
|
value={unescapeString(link.description) as string}
|
|
|
|
onChange={(e) =>
|
|
|
|
setLink({ ...link, description: e.target.value })
|
|
|
|
}
|
|
|
|
placeholder="Will be auto generated if nothing is provided."
|
|
|
|
className="resize-none w-full rounded-md p-2 border-neutral-content bg-base-200 focus:border-sky-300 dark:focus:border-sky-600 border-solid border outline-none duration-100"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-11-29 08:41:24 -06:00
|
|
|
</div>
|
2023-12-01 16:42:45 -06:00
|
|
|
) : undefined}
|
|
|
|
|
|
|
|
<div className="flex justify-between items-center mt-5">
|
|
|
|
<div
|
|
|
|
onClick={() => setOptionsExpanded(!optionsExpanded)}
|
|
|
|
className={`rounded-md cursor-pointer btn btn-sm btn-ghost duration-100 flex items-center px-2 w-fit text-sm`}
|
|
|
|
>
|
|
|
|
<p>{optionsExpanded ? "Hide" : "More"} Options</p>
|
|
|
|
</div>
|
|
|
|
|
2023-12-04 09:24:45 -06:00
|
|
|
<button className="btn btn-accent text-white" onClick={submit}>
|
2023-12-01 16:42:45 -06:00
|
|
|
Create Link
|
|
|
|
</button>
|
2023-11-29 08:41:24 -06:00
|
|
|
</div>
|
2023-12-01 16:42:45 -06:00
|
|
|
</Modal>
|
2023-11-29 08:41:24 -06:00
|
|
|
);
|
|
|
|
}
|