2023-12-07 17:08:22 +08:00
|
|
|
import React from 'react'
|
2023-12-01 11:52:37 +08:00
|
|
|
import {Droppable} from "@hello-pangea/dnd";
|
2023-12-15 13:49:54 +08:00
|
|
|
import {cartStyle, compareArraysWithIds} from "./utils";
|
2023-12-12 18:21:09 +08:00
|
|
|
import {ProductCartItem} from "./ProductCartItem";
|
|
|
|
import {FakePlaceholder} from "./FakePlaceholder";
|
2023-12-13 15:17:14 +08:00
|
|
|
import {hp_to_slots} from "./count_resources";
|
2023-12-11 17:05:35 +08:00
|
|
|
import {useShopStore} from "./shop_store";
|
2023-12-15 13:49:54 +08:00
|
|
|
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
import {useRenderCount} from "@uidotdev/usehooks";
|
2023-11-30 17:26:17 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component that displays a list of <ProductCartItem>
|
|
|
|
*/
|
2023-12-11 17:05:35 +08:00
|
|
|
export function Cart({crate_index}) {
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
const renderCount = useRenderCount();
|
|
|
|
|
2023-12-14 17:45:54 +08:00
|
|
|
const crate = useShopStore((state) => state.crates[crate_index], (a, b) => {
|
2023-12-15 13:49:54 +08:00
|
|
|
return compareArraysWithIds(a.items, b.items) && a.occupiedHP === b.occupiedHP && a.crate_mode === b.crate_mode
|
2023-12-14 16:09:33 +08:00
|
|
|
});
|
|
|
|
const crateParams = useShopStore((state) => state.crateParams);
|
|
|
|
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
console.log("Cart renders: ", renderCount)
|
2023-12-11 17:05:35 +08:00
|
|
|
|
2023-12-12 16:09:29 +08:00
|
|
|
const nbrOccupied = hp_to_slots(crate.occupiedHP);
|
|
|
|
const nbrSlots = hp_to_slots(crateParams(crate.crate_mode).hp);
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
const products = crate.items.map((item, index) => {
|
2023-11-30 17:26:17 +08:00
|
|
|
return (
|
2023-12-07 17:08:22 +08:00
|
|
|
<ProductCartItem
|
2023-12-11 17:05:35 +08:00
|
|
|
card_index={index}
|
|
|
|
crate_index={crate_index}
|
|
|
|
first={index === 0}
|
|
|
|
last={index === crate.items.length - 1 && nbrOccupied >= nbrSlots}
|
|
|
|
key={item.id}/>
|
2023-12-07 17:08:22 +08:00
|
|
|
);
|
|
|
|
});
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-07 17:08:22 +08:00
|
|
|
return (
|
2023-12-11 17:05:35 +08:00
|
|
|
<Droppable droppableId={crate.id} direction="horizontal">
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-07 17:08:22 +08:00
|
|
|
{(provided, snapshot) => (
|
|
|
|
<div
|
|
|
|
ref={provided.innerRef}
|
|
|
|
{...provided.droppableProps}
|
|
|
|
style={cartStyle(
|
|
|
|
provided.droppableProps.style,
|
|
|
|
snapshot,
|
|
|
|
)}
|
|
|
|
className="items-cart-list">
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-07 17:08:22 +08:00
|
|
|
{products}
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-07 17:08:22 +08:00
|
|
|
{provided.placeholder && (
|
|
|
|
<div style={{display: 'none'}}>
|
|
|
|
{provided.placeholder}
|
|
|
|
</div>
|
|
|
|
)}
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-07 17:08:22 +08:00
|
|
|
<FakePlaceholder
|
2023-12-08 16:39:28 +08:00
|
|
|
nToDraw={nbrSlots - nbrOccupied}
|
2023-12-07 17:08:22 +08:00
|
|
|
isDraggingOver={snapshot.isDraggingOver}/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
</Droppable>
|
|
|
|
);
|
2023-11-30 17:26:17 +08:00
|
|
|
}
|