2023-02-24 11:32:28 -06:00
|
|
|
import React, { useState } from "react";
|
2023-03-28 02:31:50 -05:00
|
|
|
import CollectionSelection from "@/components/InputSelect/CollectionSelection";
|
|
|
|
import TagSelection from "@/components/InputSelect/TagSelection";
|
2023-02-24 22:22:33 -06:00
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
2023-03-28 02:31:50 -05:00
|
|
|
import { ExtendedLink } from "@/types/global";
|
|
|
|
import { faPenToSquare } from "@fortawesome/free-regular-svg-icons";
|
2023-03-22 18:11:54 -05:00
|
|
|
import useLinkStore from "@/store/links";
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
type Props = {
|
|
|
|
toggleLinkModal: Function;
|
|
|
|
link: ExtendedLink;
|
|
|
|
};
|
2023-03-05 15:03:20 -06:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
export default function EditLink({ toggleLinkModal, link }: Props) {
|
|
|
|
const [currentLink, setCurrentLink] = useState<ExtendedLink>(link);
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
const { updateLink } = useLinkStore();
|
2023-03-05 15:03:20 -06:00
|
|
|
|
2023-02-24 11:32:28 -06:00
|
|
|
const setTags = (e: any) => {
|
|
|
|
const tagNames = e.map((e: any) => {
|
2023-03-28 02:31:50 -05:00
|
|
|
return { name: e.label };
|
2023-02-24 11:32:28 -06:00
|
|
|
});
|
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
setCurrentLink({ ...currentLink, tags: tagNames });
|
2023-02-24 11:32:28 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
const setCollection = (e: any) => {
|
2023-03-28 02:31:50 -05:00
|
|
|
if (e?.__isNew__) e.value = null;
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
setCurrentLink({
|
|
|
|
...currentLink,
|
|
|
|
collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId } as any,
|
|
|
|
});
|
2023-02-24 11:32:28 -06:00
|
|
|
};
|
|
|
|
|
2023-03-08 15:31:24 -06:00
|
|
|
const submitLink = async () => {
|
2023-03-28 02:31:50 -05:00
|
|
|
updateLink(currentLink);
|
|
|
|
toggleLinkModal();
|
2023-02-24 11:32:28 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-03-28 02:31:50 -05:00
|
|
|
<div className="flex flex-col gap-3">
|
2023-02-24 11:32:28 -06:00
|
|
|
<p className="font-bold text-sky-300 mb-2 text-center">New Link</p>
|
|
|
|
|
|
|
|
<div className="flex gap-5 items-center justify-between">
|
|
|
|
<p className="text-sm font-bold text-sky-300">Name</p>
|
|
|
|
<input
|
2023-03-28 02:31:50 -05:00
|
|
|
value={currentLink.name}
|
|
|
|
onChange={(e) =>
|
|
|
|
setCurrentLink({ ...currentLink, name: e.target.value })
|
|
|
|
}
|
2023-02-24 11:32:28 -06:00
|
|
|
type="text"
|
|
|
|
placeholder="e.g. Example Link"
|
2023-03-25 09:17:34 -05:00
|
|
|
className="w-60 rounded-md p-3 border-sky-100 border-solid border text-sm outline-none focus:border-sky-500 duration-100"
|
2023-02-24 11:32:28 -06:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex gap-5 items-center justify-between">
|
|
|
|
<p className="text-sm font-bold text-sky-300">URL</p>
|
|
|
|
<input
|
2023-03-28 02:31:50 -05:00
|
|
|
value={currentLink.url}
|
|
|
|
onChange={(e) =>
|
|
|
|
setCurrentLink({ ...currentLink, url: e.target.value })
|
|
|
|
}
|
2023-02-24 11:32:28 -06:00
|
|
|
type="text"
|
|
|
|
placeholder="e.g. http://example.com/"
|
2023-03-25 09:17:34 -05:00
|
|
|
className="w-60 rounded-md p-3 border-sky-100 border-solid border text-sm outline-none focus:border-sky-500 duration-100"
|
2023-02-24 11:32:28 -06:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex gap-5 items-center justify-between">
|
|
|
|
<p className="text-sm font-bold text-sky-300">Tags</p>
|
2023-03-28 02:31:50 -05:00
|
|
|
<TagSelection
|
|
|
|
onChange={setTags}
|
|
|
|
defaultValue={link.tags.map((e) => {
|
|
|
|
return { label: e.name, value: e.id };
|
|
|
|
})}
|
|
|
|
/>
|
2023-02-24 11:32:28 -06:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex gap-5 items-center justify-between">
|
|
|
|
<p className="text-sm font-bold text-sky-300">Collection</p>
|
2023-03-28 02:31:50 -05:00
|
|
|
<CollectionSelection
|
|
|
|
onChange={setCollection}
|
|
|
|
defaultValue={{
|
|
|
|
label: link.collection.name,
|
|
|
|
value: link.collection.id,
|
|
|
|
}}
|
|
|
|
/>
|
2023-02-24 11:32:28 -06:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div
|
2023-03-25 09:17:34 -05:00
|
|
|
className="mx-auto mt-2 bg-sky-500 text-white flex items-center gap-2 py-2 px-5 rounded-md select-none font-bold cursor-pointer duration-100 hover:bg-sky-400"
|
2023-03-08 15:31:24 -06:00
|
|
|
onClick={submitLink}
|
2023-02-24 11:32:28 -06:00
|
|
|
>
|
2023-03-28 02:31:50 -05:00
|
|
|
<FontAwesomeIcon icon={faPenToSquare} className="h-5" />
|
|
|
|
Edit Link
|
2023-02-24 11:32:28 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|