bug fixes

This commit is contained in:
daniel31x13 2024-03-05 08:50:47 -05:00
parent 281b376eac
commit 8dcd2c67d2

View File

@ -51,24 +51,49 @@ const CollectionListing = () => {
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 ||
!collections.find((i) => i.id === e.parentId)
) // 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
}); });
else { else {
// const collectionsIds = collections.map((c) => c.id); const newCollectionOrder: number[] = [
// const orderIds = [...account.collectionOrder]; ...(account.collectionOrder || []),
// const missingInOrder = collectionsIds.filter( ];
// (id) => !orderIds.includes(id)
// ); // Start with collections that are in both account.collectionOrder and collections
// if (missingInOrder.length > 0) { const existingCollectionIds = collections.map((c) => c.id as number);
// updateAccount({ const filteredCollectionOrder = account.collectionOrder.filter((id) =>
// ...account, existingCollectionIds.includes(id)
// collectionOrder: [...account.collectionOrder, ...missingInOrder], );
// });
// } console.log(existingCollectionIds);
// Add new collections that are not in account.collectionOrder and meet the specific conditions
collections.forEach((collection) => {
if (
!filteredCollectionOrder.includes(collection.id as number) &&
(!collection.parentId || collection.ownerId === account.id)
) {
filteredCollectionOrder.push(collection.id as number);
}
});
// check if the newCollectionOrder is the same as the old one
if (
JSON.stringify(newCollectionOrder) !==
JSON.stringify(account.collectionOrder)
) {
updateAccount({
...account,
collectionOrder: newCollectionOrder,
});
} }
} }
}, [account, collections]); }
}, [collections]);
const onExpand = (movedCollectionId: ItemId) => { const onExpand = (movedCollectionId: ItemId) => {
setTree((currentTree) => setTree((currentTree) =>
@ -109,12 +134,6 @@ const CollectionListing = () => {
(c) => c.id === Number(destination.parentId) (c) => c.id === Number(destination.parentId)
); );
console.log(
"Moved:",
movedCollection,
"Destination:",
destinationCollection
);
if ( if (
(movedCollection?.ownerId !== account.id && (movedCollection?.ownerId !== account.id &&
destination.parentId !== source.parentId) || destination.parentId !== source.parentId) ||
@ -126,7 +145,6 @@ const CollectionListing = () => {
); );
} }
console.log("source:", source, "destination:", destination);
setTree((currentTree) => moveItemOnTree(currentTree!, source, destination)); setTree((currentTree) => moveItemOnTree(currentTree!, source, destination));
const updatedCollectionOrder = [...account.collectionOrder]; const updatedCollectionOrder = [...account.collectionOrder];
@ -148,17 +166,11 @@ const CollectionListing = () => {
destination.parentId === source.parentId && destination.parentId === source.parentId &&
source.parentId === "root" source.parentId === "root"
) { ) {
updatedCollectionOrder.includes(movedCollectionId) &&
updatedCollectionOrder.splice(source.index, 1); updatedCollectionOrder.splice(source.index, 1);
console.log("Order1", updatedCollectionOrder);
updatedCollectionOrder.splice(destination.index, 0, movedCollectionId); updatedCollectionOrder.splice(destination.index, 0, movedCollectionId);
console.log("Order2", updatedCollectionOrder);
console.log("Moved id:", movedCollectionId);
console.log("Order:", updatedCollectionOrder);
await updateAccount({ await updateAccount({
...account, ...account,
collectionOrder: updatedCollectionOrder, collectionOrder: updatedCollectionOrder,
@ -167,15 +179,8 @@ const CollectionListing = () => {
destination.index !== undefined && destination.index !== undefined &&
destination.parentId === "root" destination.parentId === "root"
) { ) {
console.log("Order1", updatedCollectionOrder);
updatedCollectionOrder.splice(destination.index, 0, movedCollectionId); updatedCollectionOrder.splice(destination.index, 0, movedCollectionId);
console.log("Order2", updatedCollectionOrder);
console.log("Moved id:", movedCollectionId);
console.log("Order:", updatedCollectionOrder);
await updateAccount({ await updateAccount({
...account, ...account,
collectionOrder: updatedCollectionOrder, collectionOrder: updatedCollectionOrder,
@ -187,8 +192,6 @@ const CollectionListing = () => {
) { ) {
updatedCollectionOrder.splice(source.index, 1); updatedCollectionOrder.splice(source.index, 1);
console.log("Order", updatedCollectionOrder);
await updateAccount({ await updateAccount({
...account, ...account,
collectionOrder: updatedCollectionOrder, collectionOrder: updatedCollectionOrder,
@ -278,7 +281,7 @@ const Icon = (
); );
} }
// return <span>&bull;</span>; // return <span>&bull;</span>;
return <div className="pl-1"></div>; return <div></div>;
}; };
const buildTreeFromCollections = ( const buildTreeFromCollections = (
@ -327,7 +330,7 @@ const buildTreeFromCollections = (
if (Number(item) === activeCollectionId && collection.data.parentId) { if (Number(item) === activeCollectionId && collection.data.parentId) {
// get all the parents of the active collection recursively until root and set isExpanded to true // get all the parents of the active collection recursively until root and set isExpanded to true
let parentId = collection.data.parentId || null; let parentId = collection.data.parentId || null;
while (parentId) { while (parentId && items[parentId]) {
items[parentId].isExpanded = true; items[parentId].isExpanded = true;
parentId = items[parentId].data.parentId; parentId = items[parentId].data.parentId;
} }
@ -347,7 +350,10 @@ const buildTreeFromCollections = (
items[rootId] = { items[rootId] = {
id: rootId, id: rootId,
children: (collections children: (collections
.filter((c) => c.parentId === null) .filter(
(c) =>
c.parentId === null || !collections.find((i) => i.id === c.parentId)
)
.map((c) => c.id) || "") as unknown as string[], .map((c) => c.id) || "") as unknown as string[],
hasChildren: true, hasChildren: true,
isExpanded: true, isExpanded: true,