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

146 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-04-23 08:26:39 -05:00
// Copyright (C) 2022-present Daniel31x13 <daniel31x13@gmail.com>
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-03-28 02:31:50 -05:00
import React, { useEffect, useState } from "react";
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";
import { faPlus } from "@fortawesome/free-solid-svg-icons";
2023-03-28 02:31:50 -05:00
import { ExtendedLink, NewLink } from "@/types/global";
2023-03-22 18:11:54 -05:00
import useLinkStore from "@/store/links";
2023-03-28 02:31:50 -05:00
import { useRouter } from "next/router";
import useCollectionStore from "@/store/collections";
2023-05-01 15:00:23 -05:00
import RequiredBadge from "../RequiredBadge";
2023-03-28 02:31:50 -05:00
type Props = {
toggleLinkModal: Function;
};
2023-03-05 15:03:20 -06:00
2023-03-28 02:31:50 -05:00
export default function AddLink({ toggleLinkModal }: Props) {
const router = useRouter();
const [newLink, setNewLink] = useState<NewLink>({
name: "",
url: "",
tags: [],
2023-03-28 02:31:50 -05:00
collection: {
id: undefined,
name: "",
ownerId: undefined,
},
});
2023-03-22 18:11:54 -05:00
const { addLink } = useLinkStore();
2023-03-28 02:31:50 -05:00
const { collections } = useCollectionStore();
useEffect(() => {
if (router.query.id) {
const currentCollection = collections.find(
(e) => e.id == Number(router.query.id)
);
setNewLink({
...newLink,
collection: {
id: currentCollection?.id,
name: currentCollection?.name,
ownerId: currentCollection?.ownerId,
},
});
}
}, []);
2023-03-05 15:03:20 -06:00
const setTags = (e: any) => {
const tagNames = e.map((e: any) => {
2023-03-28 02:31:50 -05:00
return { name: e.label };
});
setNewLink({ ...newLink, 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
setNewLink({
...newLink,
collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId },
});
};
const submit = async () => {
2023-03-28 02:31:50 -05:00
console.log(newLink);
2023-04-24 16:30:40 -05:00
const response = await addLink(newLink as NewLink);
if (response) toggleLinkModal();
};
2023-05-16 12:08:28 -05:00
return (
2023-05-16 12:08:28 -05:00
<div className="flex flex-col gap-3 sm:w-[35rem] w-80">
<p className="font-bold text-sky-300 mb-2 text-center">New Link</p>
2023-05-16 12:08:28 -05:00
<div className="grid sm:grid-cols-2 gap-3">
<div>
<p className="text-sm font-bold text-sky-300 mb-2">
Name
<RequiredBadge />
</p>
<input
value={newLink.name}
onChange={(e) => setNewLink({ ...newLink, name: e.target.value })}
type="text"
placeholder="e.g. Example Link"
className="w-full rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"
/>
</div>
<div>
<p className="text-sm font-bold text-sky-300 mb-2">
URL
<RequiredBadge />
</p>
<input
value={newLink.url}
onChange={(e) => setNewLink({ ...newLink, url: e.target.value })}
type="text"
placeholder="e.g. http://example.com/"
className="w-full rounded-md p-2 border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"
/>
</div>
<div>
<p className="text-sm font-bold text-sky-300 mb-2">
Collection
<RequiredBadge />
</p>
<CollectionSelection
defaultValue={
newLink.collection.name && newLink.collection.id
? {
value: newLink.collection.id,
label: newLink.collection.name,
}
: undefined
}
onChange={setCollection}
/>
</div>
<div>
<p className="text-sm font-bold text-sky-300 mb-2">Tags</p>
<TagSelection onChange={setTags} />
</div>
2023-05-01 15:00:23 -05: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"
onClick={submit}
>
2023-02-24 22:22:33 -06:00
<FontAwesomeIcon icon={faPlus} className="h-5" />
Add Link
</div>
</div>
);
}