Checkbox to remove previous tags

This commit is contained in:
Isaac Wise 2024-02-11 01:21:25 -06:00
parent 0c531760e8
commit e1c4a8575b
4 changed files with 35 additions and 5 deletions

View File

@ -13,6 +13,7 @@ type Props = {
export default function BulkEditLinksModal({ onClose }: Props) { export default function BulkEditLinksModal({ onClose }: Props) {
const { updateLinks, selectedLinks, setSelectedLinks } = useLinkStore(); const { updateLinks, selectedLinks, setSelectedLinks } = useLinkStore();
const [submitLoader, setSubmitLoader] = useState(false); const [submitLoader, setSubmitLoader] = useState(false);
const [removePreviousTags, setRemovePreviousTags] = useState(false);
const [updatedValues, setUpdatedValues] = useState< const [updatedValues, setUpdatedValues] = useState<
Pick<LinkIncludingShortenedCollectionAndTags, "tags" | "collectionId"> Pick<LinkIncludingShortenedCollectionAndTags, "tags" | "collectionId">
>({ tags: [] }); >({ tags: [] });
@ -33,7 +34,7 @@ export default function BulkEditLinksModal({ onClose }: Props) {
const load = toast.loading("Updating..."); const load = toast.loading("Updating...");
const response = await updateLinks(selectedLinks, updatedValues); const response = await updateLinks(selectedLinks, removePreviousTags, updatedValues);
toast.dismiss(load); toast.dismiss(load);
@ -65,6 +66,18 @@ export default function BulkEditLinksModal({ onClose }: Props) {
<TagSelection onChange={setTags} /> <TagSelection onChange={setTags} />
</div> </div>
</div> </div>
<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>
</div> </div>
<div className="flex justify-end items-center mt-5"> <div className="flex justify-end items-center mt-5">

View File

@ -4,6 +4,7 @@ import updateLinkById from "../linkId/updateLinkById";
export default async function updateLinks( export default async function updateLinks(
userId: number, userId: number,
links: LinkIncludingShortenedCollectionAndTags[], links: LinkIncludingShortenedCollectionAndTags[],
removePreviousTags: boolean,
newData: Pick< newData: Pick<
LinkIncludingShortenedCollectionAndTags, LinkIncludingShortenedCollectionAndTags,
"tags" | "collectionId" "tags" | "collectionId"
@ -14,9 +15,16 @@ export default async function updateLinks(
// Have to use a loop here rather than updateMany, see the following: // Have to use a loop here rather than updateMany, see the following:
// https://github.com/prisma/prisma/issues/3143 // https://github.com/prisma/prisma/issues/3143
for (const link of links) { for (const link of links) {
let updatedTags = [...link.tags, ...(newData.tags ?? [])];
if (removePreviousTags) {
// If removePreviousTags is true, replace the existing tags with new tags
updatedTags = [...(newData.tags ?? [])];
}
const updatedData: LinkIncludingShortenedCollectionAndTags = { const updatedData: LinkIncludingShortenedCollectionAndTags = {
...link, ...link,
tags: [...link.tags, ...(newData.tags ?? [])], tags: updatedTags,
collection: { collection: {
...link.collection, ...link.collection,
id: newData.collectionId ?? link.collection.id, id: newData.collectionId ?? link.collection.id,

View File

@ -45,6 +45,7 @@ export default async function links(req: NextApiRequest, res: NextApiResponse) {
const updated = await updateLinks( const updated = await updateLinks(
user.id, user.id,
req.body.links, req.body.links,
req.body.removePreviousTags,
req.body.newData req.body.newData
); );
return res.status(updated.status).json({ return res.status(updated.status).json({

View File

@ -25,6 +25,7 @@ type LinkStore = {
) => Promise<ResponseObject>; ) => Promise<ResponseObject>;
updateLinks: ( updateLinks: (
links: LinkIncludingShortenedCollectionAndTags[], links: LinkIncludingShortenedCollectionAndTags[],
removePreviousTags: boolean,
newData: Pick< newData: Pick<
LinkIncludingShortenedCollectionAndTags, LinkIncludingShortenedCollectionAndTags,
"tags" | "collectionId" "tags" | "collectionId"
@ -134,9 +135,9 @@ const useLinkStore = create<LinkStore>()((set) => ({
return { ok: response.ok, data: data.response }; return { ok: response.ok, data: data.response };
}, },
updateLinks: async (links, newData) => { updateLinks: async (links, removePreviousTags, newData) => {
const response = await fetch("/api/v1/links", { const response = await fetch("/api/v1/links", {
body: JSON.stringify({ links, newData }), body: JSON.stringify({ links, removePreviousTags, newData }),
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
@ -148,7 +149,14 @@ const useLinkStore = create<LinkStore>()((set) => ({
if (response.ok) { if (response.ok) {
set((state) => ({ set((state) => ({
links: state.links.map((e) => links: state.links.map((e) =>
links.some((link) => link.id === e.id) ? { ...e, tags: [...e.tags, ...(newData.tags ?? [])] } : e links.some((link) => link.id === e.id)
? {
...e,
tags: removePreviousTags
? [...(newData.tags ?? [])]
: [...e.tags, ...(newData.tags ?? [])],
}
: e
), ),
})); }));
useTagStore.getState().setTags(); useTagStore.getState().setTags();