add receipt

This commit is contained in:
Stanislaw-Golebiewski 2020-01-19 23:09:02 +01:00
parent c6e775889e
commit e39f045077
6 changed files with 109 additions and 21 deletions

View File

@ -1,3 +1,3 @@
export default { export default {
api_url: "http://api.localhost.pl" api_url: "http://127.0.0.1:8000"
}; };

View File

@ -10,7 +10,8 @@ function ProductTile(props) {
<div <div
className="product_tile-container" className="product_tile-container"
onClick={() => { onClick={() => {
setClicked(!clicked); // setClicked(!clicked);
props.onClick();
}} }}
> >
<div className="tile-number"> <div className="tile-number">

View File

@ -1,4 +1,5 @@
import ProductTile from "./ProductTile/ProductTile"; import ProductTile from "./ProductTile/ProductTile";
import ModalConfirm from "./ModalConfirm/ModalConfirm"; import ModalConfirm from "./ModalConfirm/ModalConfirm";
import ModalProduct from "./ModalProduct/ModalProduct";
export { ProductTile, ModalConfirm }; export { ProductTile, ModalConfirm, ModalProduct };

View File

@ -26,6 +26,7 @@ function AddMetadata() {
const [shops, setShops] = useState([]); const [shops, setShops] = useState([]);
const [shopsLoading, setShopsLoading] = useState(true); const [shopsLoading, setShopsLoading] = useState(true);
const [globalState, globalActions] = useGlobal(); const [globalState, globalActions] = useGlobal();
const [pickedShop, changePickedShop] = useState("");
const uploadPhoto = event => { const uploadPhoto = event => {
setSelectedFile(URL.createObjectURL(event.target.files[0])); setSelectedFile(URL.createObjectURL(event.target.files[0]));
@ -59,6 +60,11 @@ function AddMetadata() {
const handleDateChange = (event, { name, value }) => { const handleDateChange = (event, { name, value }) => {
setPickedDate(value); setPickedDate(value);
}; };
const handleShopChange = event => {
console.log(event.target.value);
changePickedShop(event.target.value);
};
return ( return (
<div className="site-wrapper"> <div className="site-wrapper">
<Menu icon className="nav-menu"> <Menu icon className="nav-menu">
@ -80,7 +86,12 @@ function AddMetadata() {
value={pickedDate} value={pickedDate}
onChange={handleDateChange} onChange={handleDateChange}
/> />
<Input fluid list="shops" placeholder="Wybierz sklep..." /> <Input
onChange={handleShopChange}
fluid
list="shops"
placeholder="Wybierz sklep..."
/>
<datalist id="shops"> <datalist id="shops">
{shops.map((shop, index) => ( {shops.map((shop, index) => (
<option key={index} value={shop.name} /> <option key={index} value={shop.name} />
@ -107,18 +118,23 @@ function AddMetadata() {
<Icon name="camera" /> <Icon name="camera" />
Dodaj zdjęcie Dodaj zdjęcie
</Button> </Button>
<Button {pickedDate !== "" && pickedShop !== "" && (
fluid <Button
icon fluid
color="olive" icon
onClick={() => { color="olive"
history.push("/home/add_receipt/list", { shop: "Biedronka" }); onClick={() => {
}} history.push("/home/add_receipt/list", {
className="add-product-btn" shop: pickedShop,
> date: pickedDate
<Icon name="arrow right" /> });
Dalej }}
</Button> className="add-product-btn"
>
<Icon name="arrow right" />
Dalej
</Button>
)}
{selectedFile && <Image centered src={selectedFile} size="small" />} {selectedFile && <Image centered src={selectedFile} size="small" />}
</div> </div>
</div> </div>

View File

@ -2,27 +2,63 @@ import React, { useState } from "react";
import { Button, Container, Icon, Menu } from "semantic-ui-react"; import { Button, Container, Icon, Menu } from "semantic-ui-react";
import { useHistory } from "react-router-dom"; import { useHistory } from "react-router-dom";
import axios from "axios";
import { fake_product } from "../../../../utils/gen_fake_data"; import { fake_product } from "../../../../utils/gen_fake_data";
import { ProductTile, ModalConfirm } from "../../components"; import config from "../../../../config";
import useGlobal from "../../../../utils/global_state";
import { ProductTile, ModalConfirm, ModalProduct } from "../../components";
import "./ProductsList.css"; import "./ProductsList.css";
function ProductsList() { function ProductsList() {
const [products, setProducts] = useState([]); const [products, setProducts] = useState([]);
const [confirmModalOpen, setConfirmModalOpen] = useState(false); const [confirmModalOpen, setConfirmModalOpen] = useState(false);
const [productModalOpen, setProductModalOpen] = useState(false);
const [globalState, globalActions] = useGlobal();
// none, edit, add
const [productModalMode, setProductModalMode] = useState("none");
const [pickedProduct, setPickedProduct] = useState(-1);
const history = useHistory(); const history = useHistory();
console.log(history.location.state); const meta_info = history.location.state;
const addItem = item => { const addItem = item => {
setProducts([...products, item]); setProducts([...products, item]);
}; };
const updateItem = (item, index) => {
console.log(`Update item ${index} to ${item.name} ${item.price}`);
products[index] = item;
};
const removeItem = index => { const removeItem = index => {
const new_products = [...products]; const new_products = [...products];
new_products.splice(index, 1); new_products.splice(index, 1);
setProducts([...new_products]); setProducts([...new_products]);
}; };
const openProductModal = (mode, picked_index = -1) => {
setPickedProduct(picked_index);
setProductModalMode(mode);
setProductModalOpen(true);
};
const closeProductModal = () => {
setProductModalMode("none");
setProductModalOpen(false);
};
const handleConfirmProduct = (item, is_in_edit_mode) => {
if (is_in_edit_mode) {
updateItem(item, pickedProduct);
} else {
console.log("add new product!", item);
addItem(item);
}
setProductModalOpen(false);
};
const openConfirmModal = () => { const openConfirmModal = () => {
setConfirmModalOpen(true); setConfirmModalOpen(true);
}; };
@ -31,7 +67,23 @@ function ProductsList() {
}; };
const handleConfirm = () => { const handleConfirm = () => {
history.push("/home"); const new_receipt = {
date: meta_info.date,
shop: { name: meta_info.shop, tags: [] },
products
};
console.log(new_receipt);
axios
.post(`${config.api_url}/api/receipts/`, new_receipt, {
headers: { Authorization: `Token ${globalState.auth_token}` }
})
.then(resp => {
console.log(resp);
history.push("/home");
})
.catch(err => {
console.log(err);
});
}; };
return ( return (
@ -50,6 +102,9 @@ function ProductsList() {
number={index + 1} number={index + 1}
name={product.name} name={product.name}
price={product.price} price={product.price}
onClick={() => {
openProductModal("edit", index);
}}
onRemove={() => { onRemove={() => {
removeItem(index); removeItem(index);
}} }}
@ -65,7 +120,8 @@ function ProductsList() {
icon icon
color="green" color="green"
onClick={() => { onClick={() => {
addItem(fake_product()); // addItem(fake_product());
openProductModal("add", -1);
}} }}
className="add-product-btn" className="add-product-btn"
> >
@ -90,6 +146,19 @@ function ProductsList() {
onClose={closeConfirmModal} onClose={closeConfirmModal}
onConfirm={handleConfirm} onConfirm={handleConfirm}
/> />
<ModalProduct
open={productModalOpen}
onClose={closeProductModal}
onConfirm={handleConfirmProduct}
edit_mode={productModalMode === "edit"}
item={pickedProduct === -1 ? {} : products[pickedProduct]}
onRemove={() => {
setPickedProduct(-1);
closeProductModal();
removeItem(pickedProduct);
}}
/>
</div> </div>
); );
} }
@ -97,6 +166,7 @@ function ProductsList() {
function sum_products_prices(products) { function sum_products_prices(products) {
let sum = 0; let sum = 0;
products.forEach(prod => (sum += prod.price)); products.forEach(prod => (sum += prod.price));
sum.toFixed(2);
return sum; return sum;
} }

View File

@ -41,7 +41,7 @@ function Login() {
fluid fluid
icon="user" icon="user"
iconPosition="left" iconPosition="left"
placeholder="Nawa użytkownika" placeholder="Nazwa użytkownika"
onChange={e => { onChange={e => {
setUsername(e.target.value); setUsername(e.target.value);
}} }}