MCPcopy Create free account
hub / github.com/Packstack-Tech/packstack / PackForm

Function PackForm

frontend/src/app/PackForm/PackForm.tsx:26–257  ·  view source on GitHub ↗
({ history, packId, getPack, exportItems, getItems, createPack, updatePack, user })

Source from the content-addressed store, hash-verified

24import { PageTitle, Controls, Box, Grid } from "styles/common";
25
26const PackForm: React.FC<PackFormSpecs.Props> = ({ history, packId, getPack, exportItems, getItems, createPack, updatePack, user }) => {
27 const [loading, setLoading] = React.useState<boolean>(true);
28 const [inventory, setInventory] = React.useState<Item[]>([]);
29 const [packItems, setPackItems] = React.useState<PackItem[]>([]);
30 const [packData, setPackData] = React.useState<Pack | null>(null);
31 const { dispatch } = useSidebar();
32
33 React.useEffect(() => {
34 setLoading(true);
35 async function fetchData(id: number) {
36 await getData(id);
37 }
38 getItems()
39 .then(items => setInventory(items))
40 .catch(() => alertError({ message: 'Unable to retrieve inventory' }));
41
42 if (!packId) {
43 setLoading(false);
44 } else {
45 fetchData(packId);
46 }
47 }, [packId]);
48
49 async function getData(id: number) {
50 try {
51 const pack = await getPack(id);
52 const { items, userId, title } = pack;
53 if (user && user.id !== userId) {
54 history.push(getPackPath(id, title));
55 return;
56 }
57 setPackData(pack);
58 setPackItems(items);
59 setLoading(false);
60 } catch (e) {
61 alertError({ message: 'Unable to retrieve pack information.' })
62 }
63 }
64
65 const addItem = (item: Item) => {
66 const items = Object.assign([], [...packItems, { ...item, packItem: { notes: '', quantity: 1, worn: false } }]);
67 setPackItems(items);
68 };
69
70 const removeItem = (itemId: number) => {
71 const items = packItems.filter(i => i.id !== itemId);
72 setPackItems(items);
73 };
74
75 const updateItem = (itemId: number, field: string, value: string | number | boolean) => {
76 const items: PackItem[] = Object.assign([], packItems);
77 const idx = items.findIndex(item => item.id === itemId);
78 items[idx].packItem[field] = value;
79 setPackItems(items);
80 };
81
82 const SidebarContent = () => (
83 <InventorySidebar items={inventory}

Callers

nothing calls this directly

Calls 8

useSidebarFunction · 0.90
alertErrorFunction · 0.90
alertSuccessFunction · 0.90
getPackPathFunction · 0.90
durationUnitOptionsFunction · 0.90
genderOptionsFunction · 0.90
fetchDataFunction · 0.85
SidebarContentFunction · 0.70

Tested by

no test coverage detected