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

181 lines
5.6 KiB
TypeScript
Raw Normal View History

2023-11-29 08:41:24 -06:00
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 { useRouter } from "next/router";
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";
2024-07-30 13:57:09 -05:00
import { useCollections } from "@/hooks/store/collections";
import { useAddLink } from "@/hooks/store/links";
2024-08-14 14:22:28 -05:00
import toast from "react-hot-toast";
2024-09-14 15:00:19 -05:00
import { PostLinkSchemaType } from "@/lib/shared/schemaValidation";
2023-11-29 08:41:24 -06:00
type Props = {
onClose: Function;
};
2023-12-01 16:42:45 -06:00
export default function NewLinkModal({ onClose }: Props) {
2024-06-09 08:27:16 -05:00
const { t } = useTranslation();
2023-11-29 08:41:24 -06:00
const initial = {
name: "",
url: "",
description: "",
2023-12-02 14:23:26 -06:00
type: "url",
2023-11-29 08:41:24 -06:00
tags: [],
collection: {
2024-09-14 15:00:19 -05:00
id: undefined,
2023-11-29 08:41:24 -06:00
name: "",
},
2024-09-14 15:00:19 -05:00
} as PostLinkSchemaType;
2023-11-29 08:41:24 -06:00
2024-09-14 15:00:19 -05:00
const [link, setLink] = useState<PostLinkSchemaType>(initial);
const addLink = useAddLink();
2023-11-29 08:41:24 -06:00
const [submitLoader, setSubmitLoader] = useState(false);
const [optionsExpanded, setOptionsExpanded] = useState(false);
const router = useRouter();
const { data: collections = [] } = useCollections();
2023-11-29 08:41:24 -06:00
const setCollection = (e: any) => {
2024-09-14 15:00:19 -05:00
if (e?.__isNew__) e.value = undefined;
2023-11-29 08:41:24 -06:00
setLink({
...link,
2024-09-14 15:00:19 -05:00
collection: { id: e?.value, name: e?.label },
2023-11-29 08:41:24 -06:00
});
};
const setTags = (e: any) => {
2024-06-09 08:27:16 -05:00
const tagNames = e.map((e: any) => ({ name: e.label }));
2023-11-29 08:41:24 -06:00
setLink({ ...link, tags: tagNames });
};
useEffect(() => {
2024-08-18 01:55:59 -05:00
if (router.pathname.startsWith("/collections/") && router.query.id) {
2023-11-29 08:41:24 -06:00
const currentCollection = collections.find(
(e) => e.id == Number(router.query.id)
);
2024-09-14 15:00:19 -05:00
if (currentCollection && currentCollection.ownerId)
2023-11-29 08:41:24 -06:00
setLink({
...initial,
collection: {
id: currentCollection.id,
name: currentCollection.name,
},
});
} else
setLink({
...initial,
2024-09-14 15:00:19 -05:00
collection: { name: "Unorganized" },
2023-11-29 08:41:24 -06:00
});
2023-12-01 16:42:45 -06:00
}, []);
2023-11-29 08:41:24 -06:00
const submit = async () => {
if (!submitLoader) {
setSubmitLoader(true);
2024-06-09 08:27:16 -05:00
const load = toast.loading(t("creating_link"));
2024-08-14 14:22:28 -05:00
await addLink.mutateAsync(link, {
2024-08-14 14:22:28 -05:00
onSettled: (data, error) => {
toast.dismiss(load);
if (error) {
2024-09-14 15:00:19 -05:00
toast.error(t(error.message));
2024-08-14 14:22:28 -05:00
} else {
onClose();
toast.success(t("link_created"));
}
},
});
2023-11-29 08:41:24 -06:00
setSubmitLoader(false);
}
};
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("create_new_link")}</p>
2023-12-05 14:17:36 -06:00
<div className="divider mb-3 mt-1"></div>
2023-12-01 16:42:45 -06:00
<div className="grid grid-flow-row-dense sm:grid-cols-5 gap-3">
<div className="sm:col-span-3 col-span-5">
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("link")}</p>
2023-12-01 16:42:45 -06:00
<TextInput
2023-12-02 14:23:26 -06:00
value={link.url || ""}
2023-12-01 16:42:45 -06:00
onChange={(e) => setLink({ ...link, url: e.target.value })}
2024-06-09 08:27:16 -05:00
placeholder={t("link_url_placeholder")}
2023-12-01 16:42:45 -06:00
className="bg-base-200"
/>
</div>
<div className="sm:col-span-2 col-span-5">
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("collection")}</p>
2024-09-14 15:00:19 -05:00
{link.collection?.name && (
2023-12-01 16:42:45 -06:00
<CollectionSelection
onChange={setCollection}
defaultValue={{
2024-09-14 15:00:19 -05:00
value: link.collection?.id,
label: link.collection?.name || "Unorganized",
2023-12-01 16:42:45 -06:00
}}
2023-11-29 08:41:24 -06:00
/>
2024-07-27 16:17:38 -05:00
)}
2023-11-29 08:41:24 -06:00
</div>
2023-12-01 16:42:45 -06:00
</div>
<div className={"mt-2"}>
{optionsExpanded && (
<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("name")}</p>
<TextInput
value={link.name}
onChange={(e) => setLink({ ...link, name: e.target.value })}
2024-06-09 08:27:16 -05:00
placeholder={t("link_name_placeholder")}
className="bg-base-200"
/>
</div>
<div>
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("tags")}</p>
<TagSelection
onChange={setTags}
2024-09-14 15:00:19 -05:00
defaultValue={link.tags?.map((e) => ({
2024-06-09 08:27:16 -05:00
label: e.name,
value: e.id,
}))}
/>
</div>
<div className="sm:col-span-2">
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("description")}</p>
<textarea
2024-09-14 15:00:19 -05:00
value={unescapeString(link.description || "") || ""}
onChange={(e) =>
setLink({ ...link, description: e.target.value })
}
2024-06-09 08:27:16 -05:00
placeholder={t("link_description_placeholder")}
2024-08-26 20:04:52 -05:00
className="resize-none w-full h-32 rounded-md p-2 border-neutral-content bg-base-200 focus:border-primary border-solid border outline-none duration-100"
/>
</div>
</div>
</div>
)}
</div>
2023-12-17 22:32:33 -06:00
<div className="flex justify-between items-center mt-5">
<div
onClick={() => setOptionsExpanded(!optionsExpanded)}
className={`rounded-md cursor-pointer btn btn-sm btn-ghost duration-100 flex items-center px-2 w-fit text-sm`}
>
2024-06-09 08:27:16 -05:00
<p>{optionsExpanded ? t("hide_options") : t("more_options")}</p>
<i className={`bi-chevron-${optionsExpanded ? "up" : "down"}`}></i>
2023-12-17 22:32:33 -06:00
</div>
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("create_link")}
2023-12-01 16:42:45 -06:00
</button>
2023-11-29 08:41:24 -06:00
</div>
2023-12-01 16:42:45 -06:00
</Modal>
2023-11-29 08:41:24 -06:00
);
}