Use short-term storage as well
This commit is contained in:
parent
44272540aa
commit
69ac3eb01f
|
@ -3,8 +3,8 @@ import useCollectionStore from "@/store/collections";
|
||||||
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
|
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { ForwardedRef, useEffect, useState } from "react";
|
||||||
import { DragDropContext, Draggable, Droppable } from "react-beautiful-dnd";
|
import { DragDropContext, Draggable, DraggableProvided, Droppable } from "react-beautiful-dnd";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
links: boolean;
|
links: boolean;
|
||||||
|
@ -13,6 +13,8 @@ type Props = {
|
||||||
const CollectionSelection = ({ links }: Props) => {
|
const CollectionSelection = ({ links }: Props) => {
|
||||||
const { collections } = useCollectionStore();
|
const { collections } = useCollectionStore();
|
||||||
const { account, updateAccount } = useAccountStore();
|
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 [active, setActive] = useState("");
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
|
@ -26,6 +28,8 @@ const CollectionSelection = ({ links }: Props) => {
|
||||||
.filter((e) => e.parentId === null) // Filter out collections with non-null parentId
|
.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
|
.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]);
|
}, [router, collections]);
|
||||||
|
|
||||||
|
@ -40,6 +44,9 @@ const CollectionSelection = ({ links }: Props) => {
|
||||||
const [movedCollectionId] = updatedCollectionOrder.splice(result.source.index, 1);
|
const [movedCollectionId] = updatedCollectionOrder.splice(result.source.index, 1);
|
||||||
updatedCollectionOrder.splice(result.destination.index, 0, movedCollectionId);
|
updatedCollectionOrder.splice(result.destination.index, 0, movedCollectionId);
|
||||||
|
|
||||||
|
// Update local state with the new collection order
|
||||||
|
setLocalCollectionOrder(updatedCollectionOrder);
|
||||||
|
|
||||||
// Update account with the new collection order
|
// Update account with the new collection order
|
||||||
updateAccount({
|
updateAccount({
|
||||||
...account,
|
...account,
|
||||||
|
@ -50,7 +57,7 @@ const CollectionSelection = ({ links }: Props) => {
|
||||||
<Droppable droppableId="collections">
|
<Droppable droppableId="collections">
|
||||||
{(provided) => (
|
{(provided) => (
|
||||||
<div ref={provided.innerRef} {...provided.droppableProps}>
|
<div ref={provided.innerRef} {...provided.droppableProps}>
|
||||||
{account.collectionOrder?.map((collectionId, index) => {
|
{localCollectionOrder?.map((collectionId, index) => {
|
||||||
const collection = collections.find((c) => c.id === collectionId);
|
const collection = collections.find((c) => c.id === collectionId);
|
||||||
|
|
||||||
if (collection) {
|
if (collection) {
|
||||||
|
@ -74,8 +81,6 @@ const CollectionSelection = ({ links }: Props) => {
|
||||||
</Draggable>
|
</Draggable>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null; // Collection not found
|
|
||||||
})}
|
})}
|
||||||
{provided.placeholder}
|
{provided.placeholder}
|
||||||
</div>
|
</div>
|
||||||
|
@ -97,7 +102,7 @@ const CollectionItem = ({
|
||||||
collection: CollectionIncludingMembersAndLinkCount;
|
collection: CollectionIncludingMembersAndLinkCount;
|
||||||
active: string;
|
active: string;
|
||||||
collections: CollectionIncludingMembersAndLinkCount[];
|
collections: CollectionIncludingMembersAndLinkCount[];
|
||||||
innerRef?: any;
|
innerRef?: DraggableProvided["innerRef"];
|
||||||
}) => {
|
}) => {
|
||||||
const hasChildren = collections.some((e) => e.parentId === collection.id);
|
const hasChildren = collections.some((e) => e.parentId === collection.id);
|
||||||
|
|
||||||
|
|
Ŝarĝante…
Reference in New Issue