2023-12-12 16:09:29 +08:00
|
|
|
import React from 'react';
|
2023-12-01 11:52:37 +08:00
|
|
|
import {Draggable} from "@hello-pangea/dnd";
|
2023-11-30 17:26:17 +08:00
|
|
|
import {formatMoney, productStyle} from "./utils";
|
2023-12-12 16:09:29 +08:00
|
|
|
import {useShopStore} from "./shop_store";
|
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
|
|
|
|
2024-07-11 17:30:34 +08:00
|
|
|
|
|
|
|
function DatasheetLink({datasheet_file, datasheet_name}) {
|
|
|
|
return datasheet_file && datasheet_name && (<div className="ds">
|
|
|
|
<span className='doc-icon'></span>
|
|
|
|
<a href={datasheet_file} target="_blank" rel="noopener noreferrer">
|
|
|
|
{datasheet_name}
|
|
|
|
</a>
|
|
|
|
</div>)
|
|
|
|
}
|
|
|
|
|
|
|
|
function CardSpecs({specs}) {
|
|
|
|
return specs && specs.length > 0 && (<ul>
|
|
|
|
{specs.map((spec, index) =>
|
|
|
|
<li key={index}>{spec}</li>
|
|
|
|
)}
|
|
|
|
</ul>)
|
|
|
|
}
|
|
|
|
|
|
|
|
function AddButton({onAdd}) {
|
|
|
|
return <button onClick={onAdd}>
|
|
|
|
<img src="/images/shop/icon-add.svg" alt="add"/>
|
|
|
|
</button>
|
|
|
|
}
|
|
|
|
|
2023-11-30 17:26:17 +08:00
|
|
|
/**
|
|
|
|
* Component that renders a product.
|
2024-07-11 17:30:34 +08:00
|
|
|
* Used in the aside (e.g catalog of products)
|
2023-11-30 17:26:17 +08:00
|
|
|
*/
|
2023-12-12 16:09:29 +08:00
|
|
|
export function ProductItem({card_index}) {
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
const renderCount = useRenderCount();
|
|
|
|
|
|
|
|
const getCardDescription = useShopStore((state) => state.getCardDescription);
|
|
|
|
const currency = useShopStore((state) => state.currency);
|
2024-07-11 17:30:34 +08:00
|
|
|
const addCardFromCatalog = useShopStore((state) => state.addCardFromCatalog);
|
2023-12-14 16:09:33 +08:00
|
|
|
const card = getCardDescription(card_index);
|
|
|
|
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
console.log("ProductItem renders: ", renderCount)
|
2023-12-12 16:09:29 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<section className="productItem">
|
|
|
|
<div className="content">
|
2024-02-21 11:56:01 +08:00
|
|
|
<h3 style={{'marginBottom': card.name_codename ? '5px' : '20px'}}>{card.name_number} {card.name}</h3>
|
2023-12-12 16:09:29 +08:00
|
|
|
{card.name_codename ? (
|
|
|
|
<p>{card.name_codename}</p>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
<div className="price">{`${currency} ${formatMoney(card.price)}`}</div>
|
|
|
|
|
2024-07-11 17:30:34 +08:00
|
|
|
<CardSpecs specs={card.specs}/>
|
2023-12-12 16:09:29 +08:00
|
|
|
|
2024-07-11 17:30:34 +08:00
|
|
|
<DatasheetLink datasheet_file={card.datasheet_file} datasheet_name={card.datasheet_name}/>
|
2023-11-30 17:26:17 +08:00
|
|
|
</div>
|
2023-12-12 16:09:29 +08:00
|
|
|
|
|
|
|
<div className="content">
|
2024-07-11 17:30:34 +08:00
|
|
|
<AddButton onAdd={() => addCardFromCatalog(null, card_index, null)} />
|
2023-12-12 16:09:29 +08:00
|
|
|
|
2024-02-07 16:23:17 +08:00
|
|
|
<Draggable draggableId={card.id + card_index} index={card_index}>
|
2023-12-12 16:09:29 +08:00
|
|
|
{(provided, snapshot) => (
|
|
|
|
<React.Fragment>
|
|
|
|
<img
|
|
|
|
ref={provided.innerRef}
|
|
|
|
{...provided.draggableProps}
|
|
|
|
{...provided.dragHandleProps}
|
|
|
|
style={productStyle(
|
|
|
|
provided.draggableProps.style,
|
|
|
|
snapshot,
|
|
|
|
true, // hack: remove weird animation after a drop
|
2023-11-30 17:26:17 +08:00
|
|
|
)}
|
2023-12-12 16:09:29 +08:00
|
|
|
src={card.image}/>
|
|
|
|
|
|
|
|
{/* Allows to simulate a clone */}
|
|
|
|
{snapshot.isDragging && (
|
|
|
|
<img className="simclone" src={card.image}/>
|
|
|
|
)}
|
|
|
|
</React.Fragment>
|
|
|
|
)}
|
|
|
|
</Draggable>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
);
|
2023-11-30 17:26:17 +08:00
|
|
|
}
|