// simulate getting products from DataBase const products = [ { name: "Apples", country: "Italy", cost: 30, instock: 10 }, { name: "Oranges", country: "Spain", cost: 40, instock: 3 }, { name: "Beans", country: "USA", cost: 20, instock: 5 }, { name: "Cabbage", country: "USA", cost: 10, instock: 8 }, ]; const useDataApi = (initialUrl, initialData, setItems) => { const { useState, useEffect, useReducer } = React; const [url, setUrl] = useState(initialUrl); const [state, dispatch] = useReducer(dataFetchReducer, { isLoading: false, isError: false, data: initialData, }); // console.log(`useDataApi called`); useEffect(() => { console.log("useEffect Called"); let didCancel = false; const fetchData = async () => { dispatch({ type: "FETCH_INIT" }); try { const result = await axios(url); console.log("FETCH FROM URl"); const tempData = result.data.data.map((el, idx) => el.attributes); console.log(state.data.data); setItems([...state.data.data,...tempData]); if (!didCancel) { dispatch({ type: "FETCH_SUCCESS", payload: tempData }); } } catch (error) { if (!didCancel) { dispatch({ type: "FETCH_FAILURE" }); } } }; fetchData(); return () => { didCancel = true; }; }, [url]); return [state, setUrl]; }; const dataFetchReducer = (state, action) => { switch (action.type) { case "FETCH_INIT": return { ...state, isLoading: true, isError: false, }; case "FETCH_SUCCESS": return { ...state, isLoading: false, isError: false, data: action.payload, }; case "FETCH_FAILURE": return { ...state, isLoading: false, isError: true, }; default: throw new Error(); } }; const Products = () => { const { useState, useEffect } = React; const [items, setItems] = React.useState(products); const [cart, setCart] = React.useState([]); const [total, setTotal] = React.useState(0); const { Card, Accordion, Button, Container, Row, Col, Image, Input } = ReactBootstrap; // Fetch Data const [query, setQuery] = useState("http://localhost:1337/api/products"); const [{ data, isLoading, isError }, doFetch] = useDataApi( "http://localhost:1337/api/products", { data: [], }, setItems ); console.log(`Rendering Products ${JSON.stringify(data)}`); // Fetch Data const addToCart = (e) => { let item = items.filter((item) => item.name == e.target.name); let updatedStock; const newProds = items.map((item) => { if (item.name === e.target.name && item.instock > 0) { updatedStock = item.instock - 1; item.instock -= 1; } return item; }); setItems([...newProds]); if (updatedStock >= 0) { setCart([...cart, ...item]); } console.log(`add to Cart ${JSON.stringify(item)}`); // doFetch(query); }; const deleteCartItem = (index) => { const newCart = cart.filter((item, i) => index !== i); console.log(`newCart ${JSON.stringify(newCart)}`); const foundItem = cart.filter((el, idx) => idx === index); const newProds = items.map((el, idx) => { if (el.name === foundItem[0].name) { el.instock += 1; } return el; }); setCart(newCart); setItems(newProds); }; let list = items.map((item, index) => { let n = index + 236; let url = "https://picsum.photos/id/" + n + "/50/50"; return (
Picture | Name | Stock Available | Unit Price | Action |
---|
Your Cart is Empty, please select any item on Product List
) : (Country | Name | Unit Price | Action |
---|