el.xwx.moe/components/ModalContent/EditLinkModal.tsx

150 lines
4.3 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from "react";
import CollectionSelection from "@/components/InputSelect/CollectionSelection";
import TagSelection from "@/components/InputSelect/TagSelection";
import TextInput from "@/components/TextInput";
import unescapeString from "@/lib/client/unescapeString";
import useLinkStore from "@/store/links";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import toast from "react-hot-toast";
import Link from "next/link";
2023-12-01 16:42:45 -06:00
import Modal from "../Modal";
2024-06-09 08:27:16 -05:00
import { useTranslation } from "next-i18next";
type Props = {
onClose: Function;
activeLink: LinkIncludingShortenedCollectionAndTags;
};
2023-12-01 16:42:45 -06:00
export default function EditLinkModal({ onClose, activeLink }: Props) {
2024-06-09 08:27:16 -05:00
const { t } = useTranslation();
const [link, setLink] =
useState<LinkIncludingShortenedCollectionAndTags>(activeLink);
2024-06-09 08:27:16 -05:00
let shortenedURL;
try {
2024-06-09 08:27:16 -05:00
shortenedURL = new URL(link.url || "").host.toLowerCase();
} catch (error) {
console.log(error);
}
const { updateLink } = useLinkStore();
const [submitLoader, setSubmitLoader] = useState(false);
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) => {
2024-06-09 08:27:16 -05:00
const tagNames = e.map((e: any) => ({ name: e.label }));
setLink({ ...link, tags: tagNames });
};
useEffect(() => {
setLink(activeLink);
2023-12-01 16:42:45 -06:00
}, []);
const submit = async () => {
if (!submitLoader) {
setSubmitLoader(true);
2024-06-09 08:27:16 -05:00
const load = toast.loading(t("updating"));
let response = await updateLink(link);
toast.dismiss(load);
if (response.ok) {
2024-06-09 08:27:16 -05:00
toast.success(t("updated"));
2023-12-01 16:42:45 -06:00
onClose();
2024-06-09 08:27:16 -05:00
} else {
toast.error(response.data as string);
}
setSubmitLoader(false);
return response;
}
};
return (
2023-12-01 16:42:45 -06:00
<Modal toggleModal={onClose}>
2024-06-09 08:27:16 -05:00
<p className="text-xl font-thin">{t("edit_link")}</p>
2023-12-05 14:17:36 -06:00
<div className="divider mb-3 mt-1"></div>
{link.url && (
<Link
href={link.url}
className="truncate text-neutral flex gap-2 mb-5 w-fit max-w-full"
title={link.url}
target="_blank"
>
2023-12-29 11:21:22 -06:00
<i className="bi-link-45deg text-xl" />
2024-06-09 08:27:16 -05:00
<p>{shortenedURL}</p>
</Link>
)}
2023-12-01 16:42:45 -06:00
<div className="w-full">
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("name")}</p>
2023-12-01 16:42:45 -06:00
<TextInput
value={link.name}
onChange={(e) => setLink({ ...link, name: e.target.value })}
2024-06-09 08:27:16 -05:00
placeholder={t("placeholder_example_link")}
2023-12-01 16:42:45 -06:00
className="bg-base-200"
/>
</div>
2023-12-01 16:42:45 -06:00
<div className="mt-5">
<div className="grid sm:grid-cols-2 gap-3">
<div>
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("collection")}</p>
2023-12-01 16:42:45 -06:00
{link.collection.name ? (
<CollectionSelection
onChange={setCollection}
defaultValue={
link.collection.id
2024-06-09 08:27:16 -05:00
? { value: link.collection.id, label: link.collection.name }
: { value: null as unknown as number, label: "Unorganized" }
}
2024-02-14 07:10:45 -06:00
creatable={false}
/>
2023-12-01 16:42:45 -06:00
) : null}
</div>
<div>
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("tags")}</p>
2023-12-01 16:42:45 -06:00
<TagSelection
onChange={setTags}
2024-06-09 08:27:16 -05:00
defaultValue={link.tags.map((e) => ({
label: e.name,
value: e.id,
}))}
2023-12-01 16:42:45 -06:00
/>
</div>
2023-12-01 16:42:45 -06:00
<div className="sm:col-span-2">
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("description")}</p>
2023-12-01 16:42:45 -06:00
<textarea
value={unescapeString(link.description) as string}
onChange={(e) =>
setLink({ ...link, description: e.target.value })
}
2024-06-09 08:27:16 -05:00
placeholder={t("link_description_placeholder")}
2023-12-01 16:42:45 -06:00
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>
</div>
2023-12-01 16:42:45 -06:00
<div className="flex justify-end items-center mt-5">
2023-12-07 11:29:45 -06:00
<button
className="btn btn-accent dark:border-violet-400 text-white"
onClick={submit}
>
2024-06-09 08:27:16 -05:00
{t("save_changes")}
2023-12-01 16:42:45 -06:00
</button>
</div>
</Modal>
);
}