2023-05-27 12:49:09 -05:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import CollectionSelection from "@/components/InputSelect/CollectionSelection";
|
|
|
|
import TagSelection from "@/components/InputSelect/TagSelection";
|
2023-06-14 17:34:54 -05:00
|
|
|
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
|
2023-05-27 12:49:09 -05:00
|
|
|
import { faPenToSquare } from "@fortawesome/free-regular-svg-icons";
|
|
|
|
import useLinkStore from "@/store/links";
|
2023-05-31 13:33:01 -05:00
|
|
|
import { faPlus } from "@fortawesome/free-solid-svg-icons";
|
2023-06-20 09:39:03 -05:00
|
|
|
import RequiredBadge from "../../RequiredBadge";
|
2023-05-27 12:49:09 -05:00
|
|
|
import { useSession } from "next-auth/react";
|
2023-10-03 06:04:13 -05:00
|
|
|
import useCollectionStore from "@/store/collections";
|
|
|
|
import { useRouter } from "next/router";
|
2023-06-20 09:39:03 -05:00
|
|
|
import SubmitButton from "../../SubmitButton";
|
2023-06-26 17:33:40 -05:00
|
|
|
import { toast } from "react-hot-toast";
|
2023-07-24 08:39:51 -05:00
|
|
|
import Link from "next/link";
|
2023-08-17 15:05:44 -05:00
|
|
|
import TextInput from "@/components/TextInput";
|
2023-08-21 23:43:34 -05:00
|
|
|
import unescapeString from "@/lib/client/unescapeString";
|
2023-05-27 12:49:09 -05:00
|
|
|
|
|
|
|
type Props =
|
|
|
|
| {
|
|
|
|
toggleLinkModal: Function;
|
|
|
|
method: "CREATE";
|
2023-06-14 17:34:54 -05:00
|
|
|
activeLink?: LinkIncludingShortenedCollectionAndTags;
|
2023-05-27 12:49:09 -05:00
|
|
|
}
|
|
|
|
| {
|
|
|
|
toggleLinkModal: Function;
|
|
|
|
method: "UPDATE";
|
2023-06-14 17:34:54 -05:00
|
|
|
activeLink: LinkIncludingShortenedCollectionAndTags;
|
2023-05-27 12:49:09 -05:00
|
|
|
};
|
|
|
|
|
2023-06-26 18:35:12 -05:00
|
|
|
export default function AddOrEditLink({
|
2023-05-27 12:49:09 -05:00
|
|
|
toggleLinkModal,
|
|
|
|
method,
|
|
|
|
activeLink,
|
|
|
|
}: Props) {
|
2023-06-26 17:33:40 -05:00
|
|
|
const [submitLoader, setSubmitLoader] = useState(false);
|
|
|
|
|
2023-08-21 23:43:34 -05:00
|
|
|
const [optionsExpanded, setOptionsExpanded] = useState(
|
|
|
|
method === "UPDATE" ? true : false
|
|
|
|
);
|
|
|
|
|
2023-05-27 12:49:09 -05:00
|
|
|
const { data } = useSession();
|
|
|
|
|
2023-06-14 17:34:54 -05:00
|
|
|
const [link, setLink] = useState<LinkIncludingShortenedCollectionAndTags>(
|
2023-06-20 09:39:03 -05:00
|
|
|
activeLink || {
|
|
|
|
name: "",
|
|
|
|
url: "",
|
|
|
|
description: "",
|
|
|
|
tags: [],
|
2023-10-26 17:49:46 -05:00
|
|
|
screenshotPath: "",
|
|
|
|
pdfPath: "",
|
2023-06-20 09:39:03 -05:00
|
|
|
collection: {
|
|
|
|
name: "",
|
|
|
|
ownerId: data?.user.id as number,
|
|
|
|
},
|
|
|
|
}
|
2023-05-27 12:49:09 -05:00
|
|
|
);
|
|
|
|
|
2023-05-31 13:33:01 -05:00
|
|
|
const { updateLink, addLink } = useLinkStore();
|
2023-05-27 12:49:09 -05:00
|
|
|
|
2023-10-03 06:04:13 -05:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, []);
|
2023-05-27 12:49:09 -05:00
|
|
|
|
|
|
|
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 () => {
|
2023-06-26 17:33:40 -05:00
|
|
|
setSubmitLoader(true);
|
|
|
|
|
2023-05-27 12:49:09 -05:00
|
|
|
let response;
|
2023-06-26 17:33:40 -05:00
|
|
|
const load = toast.loading(
|
|
|
|
method === "UPDATE" ? "Applying..." : "Creating..."
|
|
|
|
);
|
2023-05-27 12:49:09 -05:00
|
|
|
|
|
|
|
if (method === "UPDATE") response = await updateLink(link);
|
2023-06-26 17:33:40 -05:00
|
|
|
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);
|
2023-05-27 12:49:09 -05:00
|
|
|
|
2023-06-26 17:33:40 -05:00
|
|
|
return response;
|
2023-05-27 12:49:09 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="flex flex-col gap-3 sm:w-[35rem] w-80">
|
|
|
|
{method === "UPDATE" ? (
|
2023-06-20 09:39:03 -05:00
|
|
|
<p
|
2023-08-21 23:43:34 -05:00
|
|
|
className="text-gray-500 dark:text-gray-300 text-center truncate w-full"
|
2023-06-20 09:39:03 -05:00
|
|
|
title={link.url}
|
|
|
|
>
|
2023-10-13 01:14:18 -05:00
|
|
|
Editing:{" "}
|
|
|
|
<Link href={link.url} target="_blank">
|
2023-07-24 08:39:51 -05:00
|
|
|
{link.url}
|
|
|
|
</Link>
|
2023-05-27 12:49:09 -05:00
|
|
|
</p>
|
|
|
|
) : null}
|
|
|
|
|
2023-07-24 09:06:24 -05:00
|
|
|
{method === "CREATE" ? (
|
2023-08-21 23:43:34 -05: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="text-sm text-black dark:text-white mb-2 font-bold">
|
|
|
|
Address (URL)
|
|
|
|
<RequiredBadge />
|
|
|
|
</p>
|
|
|
|
<TextInput
|
|
|
|
value={link.url}
|
|
|
|
onChange={(e) => setLink({ ...link, url: e.target.value })}
|
|
|
|
placeholder="e.g. http://example.com/"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="sm:col-span-2 col-span-5">
|
|
|
|
<p className="text-sm text-black dark:text-white mb-2">
|
|
|
|
Collection
|
|
|
|
</p>
|
2023-10-03 06:04:13 -05:00
|
|
|
{link.collection.name ? (
|
|
|
|
<CollectionSelection
|
|
|
|
onChange={setCollection}
|
|
|
|
// defaultValue={{
|
|
|
|
// label: link.collection.name,
|
|
|
|
// value: link.collection.id,
|
|
|
|
// }}
|
|
|
|
defaultValue={
|
|
|
|
link.collection.id
|
|
|
|
? {
|
|
|
|
value: link.collection.id,
|
|
|
|
label: link.collection.name,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
value: null as unknown as number,
|
|
|
|
label: "Unorganized",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) : null}
|
2023-08-21 23:43:34 -05:00
|
|
|
</div>
|
2023-07-24 09:06:24 -05:00
|
|
|
</div>
|
2023-10-03 06:04:13 -05:00
|
|
|
) : undefined}
|
2023-08-21 23:43:34 -05:00
|
|
|
|
|
|
|
{optionsExpanded ? (
|
2023-05-27 12:49:09 -05:00
|
|
|
<div>
|
2023-08-21 23:43:34 -05:00
|
|
|
<hr className="mb-3 border border-sky-100 dark:border-neutral-700" />
|
|
|
|
<div className="grid sm:grid-cols-2 gap-3">
|
|
|
|
<div className={`${method === "UPDATE" ? "sm:col-span-2" : ""}`}>
|
|
|
|
<p className="text-sm text-black dark:text-white mb-2">Name</p>
|
|
|
|
<TextInput
|
|
|
|
value={link.name}
|
|
|
|
onChange={(e) => setLink({ ...link, name: e.target.value })}
|
|
|
|
placeholder="e.g. Example Link"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{method === "UPDATE" ? (
|
|
|
|
<div>
|
|
|
|
<p className="text-sm text-black dark:text-white mb-2">
|
|
|
|
Collection
|
|
|
|
</p>
|
2023-10-03 06:04:13 -05:00
|
|
|
{link.collection.name ? (
|
|
|
|
<CollectionSelection
|
|
|
|
onChange={setCollection}
|
|
|
|
defaultValue={
|
|
|
|
link.collection.name && link.collection.id
|
|
|
|
? {
|
|
|
|
value: link.collection.id,
|
|
|
|
label: link.collection.name,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
value: null as unknown as number,
|
|
|
|
label: "Unorganized",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) : undefined}
|
2023-08-21 23:43:34 -05:00
|
|
|
</div>
|
|
|
|
) : undefined}
|
2023-05-27 12:49:09 -05:00
|
|
|
|
2023-08-21 23:43:34 -05:00
|
|
|
<div>
|
|
|
|
<p className="text-sm text-black dark:text-white mb-2">Tags</p>
|
|
|
|
<TagSelection
|
|
|
|
onChange={setTags}
|
|
|
|
defaultValue={link.tags.map((e) => {
|
|
|
|
return { label: e.name, value: e.id };
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-07-24 08:39:51 -05:00
|
|
|
|
2023-08-21 23:43:34 -05:00
|
|
|
<div className="sm:col-span-2">
|
|
|
|
<p className="text-sm text-black dark:text-white mb-2">
|
|
|
|
Description
|
|
|
|
</p>
|
|
|
|
<textarea
|
|
|
|
value={unescapeString(link.description) as string}
|
|
|
|
onChange={(e) =>
|
|
|
|
setLink({ ...link, description: e.target.value })
|
|
|
|
}
|
|
|
|
placeholder={
|
|
|
|
method === "CREATE"
|
|
|
|
? "Will be auto generated if nothing is provided."
|
|
|
|
: ""
|
|
|
|
}
|
2023-08-22 17:34:46 -05:00
|
|
|
className="resize-none w-full rounded-md p-2 border-sky-100 bg-gray-50 dark:border-neutral-700 focus:border-sky-300 dark:focus:border-sky-600 border-solid border outline-none duration-100 dark:bg-neutral-950"
|
2023-08-21 23:43:34 -05:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-07-24 08:39:51 -05:00
|
|
|
</div>
|
2023-08-21 23:43:34 -05:00
|
|
|
) : undefined}
|
2023-07-24 08:39:51 -05:00
|
|
|
|
2023-08-21 23:43:34 -05:00
|
|
|
<div className="flex justify-between items-center mt-2">
|
|
|
|
<div
|
|
|
|
onClick={() => setOptionsExpanded(!optionsExpanded)}
|
|
|
|
className={`${
|
|
|
|
method === "UPDATE" ? "hidden" : ""
|
|
|
|
} rounded-md cursor-pointer hover:bg-slate-200 hover:dark:bg-neutral-700 duration-100 py-1 px-2 w-fit text-sm`}
|
|
|
|
>
|
|
|
|
{optionsExpanded ? "Hide" : "More"} Options
|
2023-06-20 09:39:03 -05:00
|
|
|
</div>
|
2023-05-27 12:49:09 -05:00
|
|
|
|
2023-08-21 23:43:34 -05:00
|
|
|
<SubmitButton
|
|
|
|
onClick={submit}
|
|
|
|
label={method === "CREATE" ? "Add" : "Save"}
|
|
|
|
icon={method === "CREATE" ? faPlus : faPenToSquare}
|
|
|
|
loading={submitLoader}
|
|
|
|
className={`${method === "CREATE" ? "" : "mx-auto"}`}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-05-27 12:49:09 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|