2023-12-07 17:08:22 +08:00
|
|
|
import React from 'react'
|
|
|
|
import {Accordion} from "react-bootstrap";
|
2023-12-12 18:21:09 +08:00
|
|
|
import {Crate} from "./Crate";
|
2023-12-11 17:05:35 +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-12-07 17:08:22 +08:00
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
export function CrateList() {
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
const renderCount = useRenderCount();
|
|
|
|
|
|
|
|
const crates = useShopStore((state) => state.crates,
|
|
|
|
(a, b) => a.length === b.length);
|
|
|
|
const active_crate = useShopStore((state) => state.active_crate);
|
|
|
|
const onAddCrate = useShopStore((state) => state.newCrate);
|
|
|
|
const setActiveCrate = useShopStore((state) => state.setActiveCrate);
|
|
|
|
|
2024-01-05 17:22:53 +08:00
|
|
|
const onSelectHandler = (e) => {
|
|
|
|
// if e === null, that means that an accordion item was collapsed rather than expanded. e will be non-null when an item is expanded
|
|
|
|
if (e !== null)
|
|
|
|
setActiveCrate(e);
|
|
|
|
else
|
|
|
|
setActiveCrate("")
|
|
|
|
};
|
|
|
|
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
console.log("CrateList renders: ", renderCount)
|
2023-12-07 17:08:22 +08:00
|
|
|
return (
|
2024-01-05 17:22:53 +08:00
|
|
|
<Accordion id="accordion_crates" flush activeKey={active_crate} onSelect={onSelectHandler}>
|
2023-12-11 17:05:35 +08:00
|
|
|
{crates.map((crate, index) =>
|
2023-12-14 14:17:18 +08:00
|
|
|
<Accordion.Item eventKey={crate.id} key={"accordion"+crate.id} className="accordion_crates_item">
|
2024-01-05 17:22:53 +08:00
|
|
|
<Accordion.Header>{crate.name ? crate.name : <>Crate #{`${index}`}</>} </Accordion.Header>
|
2023-12-07 17:08:22 +08:00
|
|
|
<Accordion.Body>
|
2023-12-11 17:05:35 +08:00
|
|
|
<Crate crate_index={index}/>
|
2023-12-07 17:08:22 +08:00
|
|
|
</Accordion.Body>
|
|
|
|
</Accordion.Item>
|
|
|
|
)}
|
2023-12-13 17:35:17 +08:00
|
|
|
<Accordion.Item eventKey="last" id="accordion_crates_add">
|
2023-12-13 15:17:14 +08:00
|
|
|
<Accordion.Header onClick={onAddCrate}>
|
2023-12-07 17:50:33 +08:00
|
|
|
Add new crate
|
|
|
|
</Accordion.Header>
|
|
|
|
</Accordion.Item>
|
2023-12-07 17:08:22 +08:00
|
|
|
</Accordion>)
|
|
|
|
}
|