el.xwx.moe/components/Modal/EditLink.tsx

93 lines
2.9 KiB
TypeScript
Raw Normal View History

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-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-03-28 02:31:50 -05:00
const { updateLink } = useLinkStore();
2023-03-05 15:03:20 -06:00
2023-03-28 10:11:34 -05:00
const shortendURL = new URL(link.url).host.toLowerCase();
const setTags = (e: any) => {
const tagNames = e.map((e: any) => {
2023-03-28 02:31:50 -05:00
return { name: e.label };
});
2023-03-28 02:31:50 -05:00
setCurrentLink({ ...currentLink, tags: tagNames });
};
const setCollection = (e: any) => {
2023-03-28 02:31:50 -05:00
if (e?.__isNew__) e.value = null;
2023-03-28 02:31:50 -05:00
setCurrentLink({
...currentLink,
collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId } as any,
});
};
const submitLink = async () => {
2023-03-28 02:31:50 -05:00
updateLink(currentLink);
toggleLinkModal();
};
return (
2023-04-07 20:10:55 -05:00
<div className="flex flex-col gap-3 sm:w-96 w-80">
2023-03-28 10:11:34 -05:00
<p className="font-bold text-sky-300 mb-2 text-center">Edit Link</p>
2023-03-28 21:45:25 -05:00
<p className="text-sky-700">
2023-04-07 20:10:55 -05:00
<b>{shortendURL}</b> | {link.title}
2023-03-28 21:45:25 -05:00
</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 })
}
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"
/>
</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 };
})}
/>
</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,
}}
/>
</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"
onClick={submitLink}
>
2023-03-28 02:31:50 -05:00
<FontAwesomeIcon icon={faPenToSquare} className="h-5" />
Edit Link
</div>
</div>
);
}