2023-12-11 17:05:35 +08:00
|
|
|
import React from 'react'
|
2023-12-01 11:52:37 +08:00
|
|
|
import {Draggable} from "@hello-pangea/dnd";
|
2023-12-15 13:49:54 +08:00
|
|
|
import {compareObjectsEmptiness, productStyle} from "./utils";
|
2023-12-12 18:21:09 +08:00
|
|
|
import {Resources} from "./Resources";
|
|
|
|
import {CardWarnings} from "./CardWarnings";
|
2023-12-11 17:05:35 +08:00
|
|
|
import {useShopStore} from "./shop_store";
|
2023-12-15 13:49:54 +08:00
|
|
|
import {OptionsDialogWrapper} from "./OptionsWrapper";
|
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 renders a product.
|
|
|
|
* Used in the crate
|
|
|
|
*/
|
2023-12-15 13:49:54 +08:00
|
|
|
export function ProductCartItem({card_index, crate_index, first, last}) {
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
const renderCount = useRenderCount();
|
|
|
|
|
|
|
|
|
|
|
|
const card = useShopStore((state) => state.crates[crate_index].items[card_index],
|
2023-12-15 13:49:54 +08:00
|
|
|
(a, b) => a.id === b.id);
|
|
|
|
|
|
|
|
const card_show_warnings = useShopStore(state => state.crates[crate_index].items[card_index].show_warnings, compareObjectsEmptiness);
|
|
|
|
const card_counted_resources = useShopStore(state => state.crates[crate_index].items[card_index].counted_resources, compareObjectsEmptiness);
|
|
|
|
|
2023-12-14 16:09:33 +08:00
|
|
|
const highlighted = useShopStore((state) => state.crates[crate_index].id === state.highlighted.crate && card_index === state.highlighted.card);
|
|
|
|
const crate_id = useShopStore((state) => state.crates[crate_index].id);
|
|
|
|
const setHighlight = useShopStore((state) => state.highlightCard);
|
|
|
|
const removeHighlight = useShopStore((state) => state.highlightReset);
|
|
|
|
const onCardRemove = useShopStore((state) => state.deleteCard);
|
|
|
|
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
console.log("ProductCartItem renders: ", renderCount)
|
2023-12-11 17:05:35 +08:00
|
|
|
|
|
|
|
|
2023-12-15 13:49:54 +08:00
|
|
|
const options = card && card.options && card.options.length > 0;
|
|
|
|
const warnings = card_show_warnings && card_show_warnings.length > 0;
|
|
|
|
const resources = card_counted_resources && card_counted_resources.length > 0;
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
return (
|
|
|
|
<Draggable draggableId={card.id} index={card_index}>
|
|
|
|
|
|
|
|
{(provided, snapshot) => (
|
|
|
|
<div
|
|
|
|
ref={provided.innerRef}
|
|
|
|
{...provided.draggableProps}
|
|
|
|
{...provided.dragHandleProps}
|
|
|
|
style={{
|
|
|
|
...productStyle(
|
|
|
|
provided.draggableProps.style,
|
|
|
|
snapshot,
|
|
|
|
true,
|
|
|
|
!!highlighted,
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
}}
|
2023-12-14 16:09:33 +08:00
|
|
|
onMouseEnter={() => setHighlight(crate_id, card_index)}
|
2023-12-11 17:05:35 +08:00
|
|
|
onMouseLeave={removeHighlight}
|
|
|
|
>
|
|
|
|
|
|
|
|
{/* warning container */}
|
|
|
|
|
|
|
|
<div className="progress-container warning d-flex justify-content-evenly">
|
2023-12-15 13:49:54 +08:00
|
|
|
{warnings &&
|
|
|
|
(<CardWarnings crate_index={crate_index} card_index={card_index} />)
|
2023-12-11 17:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{options && (
|
2023-12-15 13:49:54 +08:00
|
|
|
<OptionsDialogWrapper
|
|
|
|
crate_index={crate_index}
|
|
|
|
card_index={card_index}
|
2023-12-11 17:05:35 +08:00
|
|
|
first={first}
|
|
|
|
last={last}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
<h6>{card.name_number}</h6>
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
<div
|
2023-12-14 16:09:33 +08:00
|
|
|
onMouseEnter={() => setHighlight(crate_id, card_index)}
|
|
|
|
onClick={() => setHighlight(crate_id, card_index)}
|
2023-12-11 17:05:35 +08:00
|
|
|
>
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
<img
|
|
|
|
className='item-cart'
|
2023-12-12 16:09:29 +08:00
|
|
|
src={card.image}/>
|
2023-12-11 17:05:35 +08:00
|
|
|
</div>
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
{/* remove container */}
|
|
|
|
<div
|
|
|
|
style={{'display': highlighted ? 'flex' : 'none'}}
|
|
|
|
className="overlayRemove"
|
2023-12-14 16:09:33 +08:00
|
|
|
onClick={() => onCardRemove(crate_id, card_index)}>
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
<img src="/images/shop/icon-remove.svg" alt="rm"/>
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
<p>Remove</p>
|
|
|
|
</div>
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
{/* progression container */}
|
|
|
|
{resources && (
|
2023-12-15 13:49:54 +08:00
|
|
|
<Resources crate_index={crate_index} card_index={card_index} />
|
2023-12-11 17:05:35 +08:00
|
|
|
)}
|
2023-11-30 17:26:17 +08:00
|
|
|
|
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
</div>
|
|
|
|
)}
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
</Draggable>
|
|
|
|
);
|
2023-11-30 17:26:17 +08:00
|
|
|
}
|