import React, { useState, useEffect } from 'react'; import { Table, Thead, Tbody, Tr, Th, Td, TableContainer, Box, Button, Flex, Heading, } from '@chakra-ui/react'; import { BiSearchAlt2, BiPlus } from 'react-icons/bi'; import { Manage as i18n } from '../../global/i18n'; import Loading from '../../global/components/Loading'; import store from '../../redux/store'; import InlineLabelInput from '../../global/components/FormControl/InlineLabelInput'; import DynamicModal from './components/DynamicModal'; import New from './components/modal/New'; import Edit from './components/modal/Edit'; import search from '../../global/search'; import IconTextDuo from '../../global/components/IconTextDuo'; const init = async (setProductsPt, setLoadedPt, setAllProductsPt) => { const resp = await fetch('/api/v1/products'); const data = await resp.json(); if (resp.ok) { setProductsPt(data); setAllProductsPt(data); setLoadedPt(true); } else { store.dispatch({ type: 'SET_ALERT', data: { header: i18n.downloadFailedAlert.header, content: JSON.stringify(data.error) } }); } }; export default () => { // New Product Modal management const [newModal, setNewModal] = useState(false); const [editItem, setEditItem] = useState(null); const [hasLoaded, setLoaded] = useState(false); const [allProducts, setAllProducts] = useState([]); const [products, setProducts] = useState([]); useEffect(() => { if (hasLoaded) return () => {}; init(setProducts, setLoaded, setAllProducts); return () => {}; }, [hasLoaded, setLoaded, setProducts, setAllProducts]); if (!hasLoaded) { return (); } return (
OpenCommerce Store Product Management setProducts(search(evt.target.value, allProducts))} propKeyPressEvt={() => {}} propSubmitEvt={() => {}} type="text" labelBgColor="green.400" labelFgColor="white" labelIcon={()} labelText="Search.." error={null} />
{products.map((x) => ( setEditItem(x)} cursor="pointer" _hover={{ backgroundColor: 'var(--chakra-colors-green-500)' }} key={x.name} > ))}
{i18n.labels.name} {i18n.labels.price} {i18n.labels.qty} {i18n.labels.rating}
{x.name} {`$${x.price}`} {x.quantity} {x.overallRating || 'No Reviews'}
setNewModal(false)} size="3xl" > { init(setProducts, setLoaded, setAllProducts).then(() => setNewModal(false)); }} /> setEditItem(null)} size="3xl" > { init(setProducts, setLoaded, setAllProducts).then(() => setEditItem(null)); }} />
); };