From 69ac3eb01f3e6800a90d5fd7d95c7aa5ec7fb7de Mon Sep 17 00:00:00 2001 From: Isaac Wise Date: Thu, 22 Feb 2024 02:04:01 -0600 Subject: [PATCH] Use short-term storage as well --- components/CollectionListing.tsx | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/components/CollectionListing.tsx b/components/CollectionListing.tsx index 7564165..0d14f60 100644 --- a/components/CollectionListing.tsx +++ b/components/CollectionListing.tsx @@ -3,8 +3,8 @@ import useCollectionStore from "@/store/collections"; import { CollectionIncludingMembersAndLinkCount } from "@/types/global"; import Link from "next/link"; import { useRouter } from "next/router"; -import React, { useEffect, useState } from "react"; -import { DragDropContext, Draggable, Droppable } from "react-beautiful-dnd"; +import React, { ForwardedRef, useEffect, useState } from "react"; +import { DragDropContext, Draggable, DraggableProvided, Droppable } from "react-beautiful-dnd"; type Props = { links: boolean; @@ -13,6 +13,8 @@ type Props = { 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(account.collectionOrder || []); const [active, setActive] = useState(""); const router = useRouter(); @@ -26,6 +28,8 @@ const CollectionSelection = ({ links }: Props) => { .filter((e) => e.parentId === null) // Filter out collections with non-null parentId .map((e) => e.id as number), // Use "as number" to assert that e.id is a number }); + + setLocalCollectionOrder(collections.filter((e) => e.parentId === null).map((e) => e.id as number)); } }, [router, collections]); @@ -40,6 +44,9 @@ const CollectionSelection = ({ links }: Props) => { const [movedCollectionId] = updatedCollectionOrder.splice(result.source.index, 1); updatedCollectionOrder.splice(result.destination.index, 0, movedCollectionId); + // Update local state with the new collection order + setLocalCollectionOrder(updatedCollectionOrder); + // Update account with the new collection order updateAccount({ ...account, @@ -50,7 +57,7 @@ const CollectionSelection = ({ links }: Props) => { {(provided) => (
- {account.collectionOrder?.map((collectionId, index) => { + {localCollectionOrder?.map((collectionId, index) => { const collection = collections.find((c) => c.id === collectionId); if (collection) { @@ -74,8 +81,6 @@ const CollectionSelection = ({ links }: Props) => { ); } - - return null; // Collection not found })} {provided.placeholder}
@@ -97,7 +102,7 @@ const CollectionItem = ({ collection: CollectionIncludingMembersAndLinkCount; active: string; collections: CollectionIncludingMembersAndLinkCount[]; - innerRef?: any; + innerRef?: DraggableProvided["innerRef"]; }) => { const hasChildren = collections.some((e) => e.parentId === collection.id);