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

114 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-02-10 16:23:59 -06:00
import React, { useState } from "react";
2024-02-10 01:38:19 -06:00
import CollectionSelection from "@/components/InputSelect/CollectionSelection";
import TagSelection from "@/components/InputSelect/TagSelection";
import useLinkStore from "@/store/links";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import toast from "react-hot-toast";
import Modal from "../Modal";
2024-06-09 08:27:16 -05:00
import { useTranslation } from "next-i18next";
2024-08-13 02:01:02 -05:00
import { useBulkEditLinks } from "@/hooks/store/links";
2024-02-10 01:38:19 -06:00
type Props = {
2024-02-10 18:34:25 -06:00
onClose: Function;
2024-02-10 01:38:19 -06:00
};
2024-02-10 16:23:59 -06:00
export default function BulkEditLinksModal({ onClose }: Props) {
2024-06-09 08:27:16 -05:00
const { t } = useTranslation();
2024-08-13 02:01:02 -05:00
const { selectedLinks, setSelectedLinks } = useLinkStore();
2024-02-10 18:34:25 -06:00
const [submitLoader, setSubmitLoader] = useState(false);
2024-02-11 01:21:25 -06:00
const [removePreviousTags, setRemovePreviousTags] = useState(false);
2024-02-10 18:34:25 -06:00
const [updatedValues, setUpdatedValues] = useState<
Pick<LinkIncludingShortenedCollectionAndTags, "tags" | "collectionId">
>({ tags: [] });
2024-02-10 01:38:19 -06:00
2024-08-13 02:01:02 -05:00
const updateLinks = useBulkEditLinks();
2024-02-10 18:34:25 -06:00
const setCollection = (e: any) => {
const collectionId = e?.value || null;
setUpdatedValues((prevValues) => ({ ...prevValues, collectionId }));
};
2024-02-10 01:38:19 -06:00
2024-02-10 18:34:25 -06:00
const setTags = (e: any) => {
const tags = e.map((tag: any) => ({ name: tag.label }));
setUpdatedValues((prevValues) => ({ ...prevValues, tags }));
};
2024-02-10 01:38:19 -06:00
2024-02-10 18:34:25 -06:00
const submit = async () => {
if (!submitLoader) {
setSubmitLoader(true);
2024-02-10 01:38:19 -06:00
2024-08-14 14:22:28 -05:00
const load = toast.loading(t("updating"));
2024-08-13 02:01:02 -05:00
await updateLinks.mutateAsync(
{
links: selectedLinks,
newData: updatedValues,
removePreviousTags,
},
{
2024-08-14 14:22:28 -05:00
onSettled: (data, error) => {
toast.dismiss(load);
if (error) {
toast.error(error.message);
} else {
setSelectedLinks([]);
onClose();
toast.success(t("updated"));
}
2024-08-13 02:01:02 -05:00
},
}
2024-02-11 01:29:11 -06:00
);
2024-02-10 01:38:19 -06:00
2024-02-10 18:34:25 -06:00
setSubmitLoader(false);
}
};
2024-02-10 01:38:19 -06:00
2024-02-10 18:34:25 -06:00
return (
<Modal toggleModal={onClose}>
2024-02-13 04:54:18 -06:00
<p className="text-xl font-thin">
2024-06-09 08:27:16 -05:00
{selectedLinks.length === 1
? t("edit_link")
: t("edit_links", { count: selectedLinks.length })}
2024-02-13 04:54:18 -06:00
</p>
2024-02-10 18:34:25 -06:00
<div className="divider mb-3 mt-1"></div>
<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("move_to_collection")}</p>
2024-02-11 01:29:11 -06:00
<CollectionSelection
showDefaultValue={false}
onChange={setCollection}
2024-02-14 07:10:45 -06:00
creatable={false}
2024-02-11 01:29:11 -06:00
/>
2024-02-10 18:34:25 -06:00
</div>
2024-02-10 01:38:19 -06:00
2024-02-10 18:34:25 -06:00
<div>
2024-06-09 08:27:16 -05:00
<p className="mb-2">{t("add_tags")}</p>
2024-02-10 18:34:25 -06:00
<TagSelection onChange={setTags} />
</div>
</div>
2024-02-14 07:10:45 -06:00
<div className="sm:ml-auto w-1/2 p-3">
2024-02-13 04:54:18 -06:00
<label className="flex items-center gap-2 ">
2024-02-11 01:21:25 -06:00
<input
type="checkbox"
className="checkbox checkbox-primary"
checked={removePreviousTags}
onChange={(e) => setRemovePreviousTags(e.target.checked)}
/>
2024-06-09 08:27:16 -05:00
{t("remove_previous_tags")}
2024-02-11 01:21:25 -06:00
</label>
</div>
2024-02-10 18:34:25 -06:00
</div>
2024-02-10 01:38:19 -06:00
2024-02-10 18:34:25 -06:00
<div className="flex justify-end items-center mt-5">
<button
className="btn btn-accent dark:border-violet-400 text-white"
onClick={submit}
>
2024-06-09 08:27:16 -05:00
{t("save_changes")}
2024-02-10 18:34:25 -06:00
</button>
</div>
</Modal>
);
2024-02-10 01:38:19 -06:00
}