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: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 aside (e.g backlog of product)
|
|
|
|
*/
|
2023-12-12 16:09:29 +08:00
|
|
|
export function ProductItem({card_index}) {
|
2023-12-14 16:09:33 +08:00
|
|
|
const renderCount = useRenderCount();
|
|
|
|
|
|
|
|
const getCardDescription = useShopStore((state) => state.getCardDescription);
|
|
|
|
const currency = useShopStore((state) => state.currency);
|
|
|
|
const onAddCard = useShopStore((state) => state.addCardFromBacklog);
|
|
|
|
const card = getCardDescription(card_index);
|
|
|
|
|
|
|
|
console.log("ProductItem renders: ", renderCount)
|
2023-12-12 16:09:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
const render_specs = (card.specs && card.specs.length > 0 && (
|
|
|
|
<ul>
|
|
|
|
{card.specs.map((spec, index) =>
|
|
|
|
<li key={index}>{spec}</li>
|
|
|
|
)}
|
|
|
|
</ul>
|
|
|
|
));
|
|
|
|
|
|
|
|
const render_datasheet_link = (card.datasheet_file && card.datasheet_name && (
|
|
|
|
<div className="ds">
|
|
|
|
<span className='doc-icon'></span>
|
|
|
|
<a href={card.datasheet_file} target="_blank" rel="noopener noreferrer">
|
|
|
|
{card.datasheet_name}
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<section className="productItem">
|
|
|
|
|
|
|
|
<div className="content">
|
|
|
|
<h3 style={{'marginBottom': card.name_codename ? '5px' : '20px'}}>{card.name}</h3>
|
|
|
|
{card.name_codename ? (
|
|
|
|
<p>{card.name_codename}</p>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
<div className="price">{`${currency} ${formatMoney(card.price)}`}</div>
|
|
|
|
|
|
|
|
{render_specs}
|
|
|
|
|
|
|
|
{render_datasheet_link}
|
2023-11-30 17:26:17 +08:00
|
|
|
</div>
|
2023-12-12 16:09:29 +08:00
|
|
|
|
|
|
|
<div className="content">
|
|
|
|
|
|
|
|
<button onClick={() => onAddCard(null, card_index, null)}>
|
|
|
|
<img src="/images/shop/icon-add.svg" alt="add"/>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<Draggable draggableId={card.id} index={card_index}>
|
|
|
|
{(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>
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-12 16:09:29 +08:00
|
|
|
</section>
|
|
|
|
);
|
2023-11-30 17:26:17 +08:00
|
|
|
|
|
|
|
}
|