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-12 18:21:09 +08:00
|
|
|
import {DialogPopup} from "./options/DialogPopup";
|
2023-12-01 17:36:55 +08:00
|
|
|
import {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-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-12 16:09:29 +08:00
|
|
|
export function ProductCartItem({card_index, crate_index, ext_data, first, last}) {
|
2023-12-14 16:09:33 +08:00
|
|
|
const renderCount = useRenderCount();
|
|
|
|
|
|
|
|
|
|
|
|
const card = useShopStore((state) => state.crates[crate_index].items[card_index],
|
|
|
|
(a, b) => a.id === b.id && a.show_warnings === b.show_warnings && a.counted_resources === b.counted_resources );
|
|
|
|
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 onCardUpdate = useShopStore((state) => state.updateOptions);
|
|
|
|
const onCardRemove = useShopStore((state) => state.deleteCard);
|
|
|
|
|
|
|
|
console.log("ProductCartItem renders: ", renderCount)
|
2023-12-11 17:05:35 +08:00
|
|
|
|
|
|
|
let options, options_data;
|
2023-12-12 16:09:29 +08:00
|
|
|
const warnings = card && card.show_warnings;
|
|
|
|
const resources = card && card.counted_resources;
|
2023-12-11 17:05:35 +08:00
|
|
|
|
2023-12-11 17:47:16 +08:00
|
|
|
if (card && card.options) {
|
|
|
|
options = card.options;
|
|
|
|
if (!card.options_data) card.options_data = {};
|
|
|
|
options_data = card.options_data;
|
2023-12-11 17:05:35 +08:00
|
|
|
options_data.ext_data = ext_data;
|
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">
|
|
|
|
{warnings && warnings.length > 0 &&
|
|
|
|
(<CardWarnings warnings={warnings} prefix={card_index}/>)
|
|
|
|
}
|
|
|
|
|
|
|
|
{options && (
|
|
|
|
<DialogPopup
|
|
|
|
options={options}
|
|
|
|
data={options_data}
|
|
|
|
options_class={card.options_class}
|
|
|
|
key={"popover" + card_index}
|
|
|
|
id={"popover" + card_index}
|
|
|
|
big={card.size === "big"}
|
|
|
|
first={first}
|
|
|
|
last={last}
|
|
|
|
target={{
|
|
|
|
construct: ((outvar, value) => {
|
|
|
|
// console.log("construct", outvar, value, options_data);
|
|
|
|
options_data[outvar] = value;
|
|
|
|
}),
|
|
|
|
update: ((outvar, value) => {
|
|
|
|
// console.log("update", outvar, value, options_data);
|
|
|
|
if (outvar in options_data) options_data[outvar] = value;
|
2023-12-14 16:09:33 +08:00
|
|
|
onCardUpdate(crate_id, card_index, {[outvar]: value});
|
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
|
|
|
<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 && (
|
|
|
|
<Resources resources={resources}/>
|
|
|
|
)}
|
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
|
|
|
}
|