From 4442ce870528c4073c2e4856665f691cb8a4d336 Mon Sep 17 00:00:00 2001 From: daniel31x13 Date: Mon, 26 Feb 2024 23:59:10 -0500 Subject: [PATCH] fix collectionOrder updating + remove index --- components/CollectionListing.tsx | 22 ++++++++++++-- .../collectionId/deleteCollectionById.ts | 29 +++++++++++++++++++ .../collectionId/updateCollectionById.ts | 11 +++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/components/CollectionListing.tsx b/components/CollectionListing.tsx index 8fc98ed0..f61c640e 100644 --- a/components/CollectionListing.tsx +++ b/components/CollectionListing.tsx @@ -19,9 +19,9 @@ const CollectionSelection = ({ links }: Props) => { const { collections } = useCollectionStore(); const { account, updateAccount } = useAccountStore(); // Use local state to store the collection order so we don't have to wait for a response from the API to update the UI - const [localCollectionOrder, setLocalCollectionOrder] = useState< - number[] | [] - >([]); + const [localCollectionOrder, setLocalCollectionOrder] = useState( + [] + ); const [active, setActive] = useState(""); const router = useRouter(); @@ -37,6 +37,22 @@ const CollectionSelection = ({ links }: Props) => { .map((e) => e.id as number), // Use "as number" to assert that e.id is a number }); } + + // if a collection wasn't in the collectionOrder, add it to the end + collections.forEach((collection) => { + if ( + !account.collectionOrder.includes(collection.id as number) && + (!collection.parentId || collection.ownerId !== account.id) + ) { + updateAccount({ + ...account, + collectionOrder: [ + ...account.collectionOrder, + collection.id as number, + ], + }); + } + }); }, [router, collections, account]); return ( diff --git a/lib/api/controllers/collections/collectionId/deleteCollectionById.ts b/lib/api/controllers/collections/collectionId/deleteCollectionById.ts index 98b1e175..e8fe51ab 100644 --- a/lib/api/controllers/collections/collectionId/deleteCollectionById.ts +++ b/lib/api/controllers/collections/collectionId/deleteCollectionById.ts @@ -31,6 +31,8 @@ export default async function deleteCollection( }, }); + await removeFromOrders(userId, collectionId); + return { response: deletedUsersAndCollectionsRelation, status: 200 }; } else if (collectionIsAccessible?.ownerId !== userId) { return { response: "Collection is not accessible.", status: 401 }; @@ -57,6 +59,8 @@ export default async function deleteCollection( await removeFolder({ filePath: `archives/${collectionId}` }); + await removeFromOrders(userId, collectionId); + return await prisma.collection.delete({ where: { id: collectionId, @@ -98,3 +102,28 @@ async function deleteSubCollections(collectionId: number) { await removeFolder({ filePath: `archives/${subCollection.id}` }); } } + +async function removeFromOrders(userId: number, collectionId: number) { + const userCollectionOrder = await prisma.user.findUnique({ + where: { + id: userId, + }, + select: { + collectionOrder: true, + }, + }); + + if (userCollectionOrder) + await prisma.user.update({ + where: { + id: userId, + }, + data: { + collectionOrder: { + set: userCollectionOrder.collectionOrder.filter( + (e: number) => e !== collectionId + ), + }, + }, + }); +} diff --git a/lib/api/controllers/collections/collectionId/updateCollectionById.ts b/lib/api/controllers/collections/collectionId/updateCollectionById.ts index ca9b7895..d78c7384 100644 --- a/lib/api/controllers/collections/collectionId/updateCollectionById.ts +++ b/lib/api/controllers/collections/collectionId/updateCollectionById.ts @@ -47,6 +47,17 @@ export default async function updateCollection( }, }); + await prisma.user.update({ + where: { + id: userId, + }, + data: { + collectionOrder: { + push: collectionId, + }, + }, + }); + return await prisma.collection.update({ where: { id: collectionId,