2023-12-12 16:09:29 +08:00
|
|
|
import React from 'react';
|
2023-12-12 18:21:09 +08:00
|
|
|
import {SummaryPopup} from "./options/SummaryPopup";
|
2023-11-30 17:26:17 +08:00
|
|
|
import {formatMoney} from "./utils";
|
2023-12-12 18:21:09 +08:00
|
|
|
import {WarningIndicator} from "./CardWarnings";
|
2023-12-12 16:09:29 +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
|
|
|
|
|
|
|
/**
|
|
|
|
* Components that displays the list of card that are used in the crate.
|
|
|
|
* It is a summary of purchase
|
|
|
|
*/
|
2023-12-12 16:09:29 +08:00
|
|
|
export function OrderSummary() {
|
2023-12-14 16:09:33 +08:00
|
|
|
const renderCount = useRenderCount();
|
2023-12-12 16:09:29 +08:00
|
|
|
|
2023-12-14 16:09:33 +08:00
|
|
|
const currency = useShopStore((state) => state.currency);
|
|
|
|
const crates = useShopStore((state) => state.crates);
|
|
|
|
const total_price = useShopStore((state) => state.totalOrderPrice());
|
|
|
|
const crateParams = useShopStore((state) => state.crateParams);
|
|
|
|
const deleteCard = useShopStore((state) => state.deleteCard);
|
|
|
|
const setHighlight = useShopStore((state) => state.highlightCard);
|
|
|
|
const resetHighlight = useShopStore((state) => state.highlightReset);
|
|
|
|
const highlighted = useShopStore((state) => state.highlighted);
|
|
|
|
const clearAll = useShopStore((state) => state.clearAll);
|
|
|
|
const clearCrate = useShopStore((state) => state.clearCrate);
|
|
|
|
const delCrate = useShopStore((state) => state.delCrate);
|
|
|
|
|
|
|
|
|
|
|
|
console.log("OrderSummary renders: ", renderCount)
|
2023-12-12 16:09:29 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="summary-price">
|
|
|
|
|
|
|
|
<table>
|
|
|
|
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<td colSpan="2" className="summary-remove-all">
|
|
|
|
<span className="item-card-name">Remove all cards</span>
|
|
|
|
|
|
|
|
<button onClick={clearAll}>
|
|
|
|
<img src="/images/shop/icon-remove.svg"/>
|
|
|
|
</button>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
|
|
|
|
{crates.map((crate, _i) => {
|
|
|
|
let crate_type = crateParams(crate.crate_mode);
|
|
|
|
return (
|
|
|
|
<tbody key={"summary_crate_body" + crate.id}>
|
|
|
|
<tr key={"summary_crate_" + crate.id}>
|
|
|
|
<td className="item-card-name">{crate_type.name}</td>
|
|
|
|
<td className="price">
|
2023-12-14 14:17:18 +08:00
|
|
|
<div className="d-inline-flex">
|
2023-12-12 16:09:29 +08:00
|
|
|
{`${currency} ${formatMoney(crate_type.price)}`}
|
|
|
|
|
2023-12-13 15:17:14 +08:00
|
|
|
<button onClick={() => clearCrate(crate.id)}>
|
2023-12-14 14:17:18 +08:00
|
|
|
<img src="/images/shop/icon-clear.svg" alt="empty crate"/>
|
2023-12-12 16:09:29 +08:00
|
|
|
</button>
|
|
|
|
|
2023-12-14 14:17:18 +08:00
|
|
|
<button onClick={() => delCrate(crate.id)}>
|
|
|
|
<img src="/images/shop/icon-remove.svg" alt="remove crate"/>
|
|
|
|
</button>
|
|
|
|
</div>
|
2023-12-12 16:09:29 +08:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
{crate.items.map((item, index) => {
|
|
|
|
const options = item && item.options;
|
|
|
|
const options_data = item && item.options_data;
|
|
|
|
const warnings = item && item.show_warnings;
|
|
|
|
const selected = crate.id === highlighted.crate && index === highlighted.card;
|
|
|
|
|
|
|
|
return (<tr key={"summary_crate_" + crate.id + item.id}
|
|
|
|
className={`hoverable ${selected ? 'selected' : ''}`}
|
|
|
|
onClick={() => setHighlight(crate.id, index)}
|
|
|
|
onMouseEnter={() => setHighlight(crate.id, index)}
|
|
|
|
onMouseLeave={() => resetHighlight()}>
|
|
|
|
<td className="item-card-name">
|
2023-12-14 14:17:18 +08:00
|
|
|
<span style={{
|
|
|
|
'display': 'inline-block',
|
|
|
|
'width': '16px',
|
|
|
|
}}> </span>
|
2023-12-12 16:09:29 +08:00
|
|
|
<div>{`${item.name_number} ${item.name} ${item.name_codename}`}</div>
|
|
|
|
</td>
|
|
|
|
|
2023-11-30 17:26:17 +08:00
|
|
|
<td className="price">
|
2023-12-12 16:09:29 +08:00
|
|
|
<div className="d-inline-flex align-content-center">
|
|
|
|
{`${currency} ${formatMoney(item.price)}`}
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-12 16:09:29 +08:00
|
|
|
<button onClick={() => deleteCard(crate.id, index)}>
|
|
|
|
<img src="/images/shop/icon-remove.svg"/>
|
2023-11-30 17:26:17 +08:00
|
|
|
</button>
|
2023-12-08 16:39:28 +08:00
|
|
|
|
2023-12-12 16:09:29 +08:00
|
|
|
<div style={{'width': '45px', 'height': '20px'}}
|
|
|
|
className="d-inline-flex align-content-center align-self-center justify-content-evenly">
|
|
|
|
{(warnings && warnings.length > 0 ? (
|
|
|
|
<WarningIndicator warnings={warnings}/>
|
|
|
|
) : (
|
|
|
|
<span style={{
|
|
|
|
'display': 'inline-block',
|
|
|
|
'width': '20px',
|
|
|
|
}}> </span>
|
|
|
|
))}
|
|
|
|
{((options && options_data) ? (
|
|
|
|
<SummaryPopup id={item.id + "options"} options={options}
|
|
|
|
data={options_data}/>
|
|
|
|
) : (
|
|
|
|
<span style={{
|
|
|
|
'display': 'inline-block',
|
|
|
|
'width': '20px',
|
|
|
|
}}> </span>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-11-30 17:26:17 +08:00
|
|
|
</td>
|
2023-12-12 16:09:29 +08:00
|
|
|
</tr>);
|
|
|
|
})}
|
|
|
|
</tbody>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
|
|
|
|
<tfoot>
|
|
|
|
<tr>
|
|
|
|
<td className="item-card-name">Price estimate</td>
|
|
|
|
<td className="price">
|
|
|
|
<div>
|
|
|
|
{currency} {formatMoney(total_price)}
|
|
|
|
<button style={{'opacity': '0', 'cursor': 'initial'}}>
|
|
|
|
<img src="/images/shop/icon-remove.svg" alt="icon remove"/>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<span style={{
|
|
|
|
'display': 'inline-block',
|
|
|
|
'width': '30px',
|
|
|
|
}}> </span>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</tfoot>
|
|
|
|
|
|
|
|
</table>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
);
|
2023-11-30 17:26:17 +08:00
|
|
|
}
|