From 5df2fe713d693194e9ed26b9d1f7f812f015c327 Mon Sep 17 00:00:00 2001 From: meskio <meskio@sindominio.net> Date: Sun, 31 Jan 2021 13:55:13 +0100 Subject: [PATCH] Use price editor on product prices --- src/EditableCell.js | 22 ++++++++++++++++------ src/ProductList.js | 8 +++----- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/EditableCell.js b/src/EditableCell.js index d54c937..3c084f3 100644 --- a/src/EditableCell.js +++ b/src/EditableCell.js @@ -1,12 +1,18 @@ import React, { useState } from "react"; import { Form } from "react-bootstrap"; +import { printMoney } from "./util"; +import PriceEditor from "./PriceEditor"; function EditableCell(props) { const [data, setData] = useState(props.value); const [editing, setEditing] = useState(false); + let value = props.value; + if (props.price) { + value = printMoney(props.value); + } if (props.ro || !editing) { - return <td onClick={() => setEditing(true)}>{props.value}</td>; + return <td onClick={() => setEditing(true)}>{value}</td>; } const submit = (e) => { @@ -18,11 +24,15 @@ function EditableCell(props) { return ( <td> <Form onSubmit={submit}> - <Form.Control - value={data} - onChange={(e) => setData(e.target.value)} - onBlur={() => setEditing(false)} - /> + {props.price ? ( + <PriceEditor value={data} onChange={(price) => setData(price)} /> + ) : ( + <Form.Control + value={data} + onChange={(e) => setData(e.target.value)} + onBlur={() => setEditing(false)} + /> + )} </Form> </td> ); diff --git a/src/ProductList.js b/src/ProductList.js index e184d6d..ea872c3 100644 --- a/src/ProductList.js +++ b/src/ProductList.js @@ -4,7 +4,7 @@ import Fetcher from "./Fetcher"; import EditableCell from "./EditableCell"; import ProductAdder from "./ProductAdder"; import AuthContext from "./AuthContext"; -import { printMoney, url } from "./util"; +import { url } from "./util"; class ProductList extends React.Component { static contextType = AuthContext; @@ -34,9 +34,6 @@ class ProductList extends React.Component { case "stock": value = parseInt(value); break; - case "price": - value = value * 100; - break; default: break; } @@ -127,8 +124,9 @@ class ProductList extends React.Component { /> <EditableCell onChange={(v) => this.update(row, "price", v)} - value={printMoney(product.price)} + value={product.price} ro={!isAdmin} + price /> <EditableCell onChange={(v) => this.update(row, "stock", v)} -- GitLab