Update order when new collection is created

This commit is contained in:
Isaac Wise 2024-02-22 02:24:10 -06:00
parent 69ac3eb01f
commit 7fd9f5b806
3 changed files with 26 additions and 7 deletions

View File

@ -3,7 +3,7 @@ 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, { ForwardedRef, useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import { DragDropContext, Draggable, DraggableProvided, Droppable } from "react-beautiful-dnd"; import { DragDropContext, Draggable, DraggableProvided, Droppable } from "react-beautiful-dnd";
type Props = { type Props = {
@ -14,24 +14,23 @@ 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 // 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 [localCollectionOrder, setLocalCollectionOrder] = useState<number[] | []>([]);
const [active, setActive] = useState(""); const [active, setActive] = useState("");
const router = useRouter(); const router = useRouter();
useEffect(() => { useEffect(() => {
setActive(router.asPath); setActive(router.asPath);
setLocalCollectionOrder(account.collectionOrder || []);
if (account.collectionOrder?.length === 0) { if (!account.collectionOrder || account.collectionOrder.length === 0) {
updateAccount({ updateAccount({
...account, ...account,
collectionOrder: collections collectionOrder: collections
.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, account]);
return ( return (
<DragDropContext <DragDropContext

View File

@ -6,6 +6,8 @@ import { HexColorPicker } from "react-colorful";
import { Collection } from "@prisma/client"; import { Collection } from "@prisma/client";
import Modal from "../Modal"; import Modal from "../Modal";
import { CollectionIncludingMembersAndLinkCount } from "@/types/global"; import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
import useAccountStore from "@/store/account";
import { useSession } from "next-auth/react";
type Props = { type Props = {
onClose: Function; onClose: Function;
@ -21,6 +23,8 @@ export default function NewCollectionModal({ onClose, parent }: Props) {
} as Partial<Collection>; } as Partial<Collection>;
const [collection, setCollection] = useState<Partial<Collection>>(initial); const [collection, setCollection] = useState<Partial<Collection>>(initial);
const { setAccount } = useAccountStore();
const { data } = useSession();
useEffect(() => { useEffect(() => {
setCollection(initial); setCollection(initial);
@ -42,7 +46,11 @@ export default function NewCollectionModal({ onClose, parent }: Props) {
if (response.ok) { if (response.ok) {
toast.success("Created!"); toast.success("Created!");
onClose(); if (response.data) {
// If the collection was created successfully, we need to get the new collection order
setAccount(data?.user.id as number);
onClose();
}
} else toast.error(response.data as string); } else toast.error(response.data as string);
setSubmitLoader(false); setSubmitLoader(false);

View File

@ -67,6 +67,18 @@ export default async function postCollection(
}, },
}); });
await prisma.user.update({
where: {
id: userId,
},
data: {
collectionOrder: {
push: newCollection.id,
},
},
});
createFolder({ filePath: `archives/${newCollection.id}` }); createFolder({ filePath: `archives/${newCollection.id}` });
return { response: newCollection, status: 200 }; return { response: newCollection, status: 200 };