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";
|
|
|
|
|
|
|
|
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-02-10 18:34:25 -06:00
|
|
|
const { updateLinks, selectedLinks, setSelectedLinks } = useLinkStore();
|
|
|
|
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-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-02-10 18:34:25 -06:00
|
|
|
const load = toast.loading("Updating...");
|
2024-02-10 01:38:19 -06:00
|
|
|
|
2024-02-11 01:29:11 -06:00
|
|
|
const response = await updateLinks(
|
|
|
|
selectedLinks,
|
|
|
|
removePreviousTags,
|
|
|
|
updatedValues
|
|
|
|
);
|
2024-02-10 01:38:19 -06:00
|
|
|
|
2024-02-10 18:34:25 -06:00
|
|
|
toast.dismiss(load);
|
2024-02-10 01:38:19 -06:00
|
|
|
|
2024-02-10 18:34:25 -06:00
|
|
|
if (response.ok) {
|
|
|
|
toast.success(`Updated!`);
|
|
|
|
onClose();
|
|
|
|
} else toast.error(response.data as string);
|
2024-02-10 01:38:19 -06:00
|
|
|
|
2024-02-10 18:34:25 -06:00
|
|
|
setSelectedLinks([]);
|
|
|
|
setSubmitLoader(false);
|
|
|
|
onClose();
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
};
|
2024-02-10 01:38:19 -06:00
|
|
|
|
2024-02-10 18:34:25 -06:00
|
|
|
return (
|
|
|
|
<Modal toggleModal={onClose}>
|
|
|
|
<p className="text-xl font-thin">Edit Link</p>
|
|
|
|
<div className="divider mb-3 mt-1"></div>
|
|
|
|
<div className="mt-5">
|
|
|
|
<div className="grid sm:grid-cols-2 gap-3">
|
|
|
|
<div>
|
|
|
|
<p className="mb-2">Collection</p>
|
2024-02-11 01:29:11 -06:00
|
|
|
<CollectionSelection
|
|
|
|
showDefaultValue={false}
|
|
|
|
onChange={setCollection}
|
|
|
|
/>
|
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>
|
|
|
|
<p className="mb-2">Tags</p>
|
|
|
|
<TagSelection onChange={setTags} />
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-02-11 01:21:25 -06:00
|
|
|
<div className="mt-3">
|
|
|
|
<label className="flex items-center gap-2">
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
className="checkbox checkbox-primary"
|
|
|
|
checked={removePreviousTags}
|
|
|
|
onChange={(e) => setRemovePreviousTags(e.target.checked)}
|
|
|
|
/>
|
|
|
|
Remove previous tags
|
|
|
|
</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}
|
|
|
|
>
|
|
|
|
Save Changes
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
);
|
2024-02-10 01:38:19 -06:00
|
|
|
}
|