From a2a1debb6c8c42a8b0ddf21b93c9824a3f8082b0 Mon Sep 17 00:00:00 2001 From: imkarimkarim Date: Fri, 20 Aug 2021 20:03:38 +0430 Subject: [PATCH] add cheat (i wish they didnt asked this feature) --- src/App/Components/Product/Cheat.css | 36 ++++++ src/App/Components/Product/Cheat.jsx | 118 ++++++++++++++++++ src/App/Pages/Car/Car.jsx | 2 +- src/App/Pages/Product/Product.jsx | 30 ++++- src/App/Pages/Reports/PrintCar.jsx | 2 +- .../Pages/Reports/PrintRemainingProducts.jsx | 3 - src/calculators/calcOneProduct.js | 100 +++++++++------ src/db/carDocs.js | 5 +- src/db/productDocs.js | 102 +++++++++++---- src/ipcMains/ipcProducts.js | 27 +++- src/modules/conflictFinder.js | 3 - src/modules/validator.js | 46 +++++++ src/schemas.js | 10 +- 13 files changed, 409 insertions(+), 75 deletions(-) create mode 100644 src/App/Components/Product/Cheat.css create mode 100644 src/App/Components/Product/Cheat.jsx diff --git a/src/App/Components/Product/Cheat.css b/src/App/Components/Product/Cheat.css new file mode 100644 index 0000000..244733e --- /dev/null +++ b/src/App/Components/Product/Cheat.css @@ -0,0 +1,36 @@ +.cheatEditor > .MuiFormControl-root:nth-child(1), +.cheatEditor > .MuiFormControl-root:nth-child(2), +.cheatEditor > .MuiFormControl-root:nth-child(3), +.cheatEditor > .expenseInput { + width: 170px; +} + +.cheat-wrapper .hint { + margin: 5px 0; +} + +.cheat-wrapper .hint:first-child { + margin-bottom: 20px; +} +.cheat-wrapper .deleteCheats { + margin-top: 20px; +} +.cheat-wrapper { + display: flex; + flex-direction: column; + position: absolute; + right: 0; + margin: 170px; + width: 50%; + z-index: 8; + background: #fff; + border-radius: 5px; + padding: 3%; + box-shadow: 0 0 100px 500px #0000003f; +} + +.cheatEditor { + display: flex; + justify-content: space-between; + width: 100%; +} diff --git a/src/App/Components/Product/Cheat.jsx b/src/App/Components/Product/Cheat.jsx new file mode 100644 index 0000000..9570572 --- /dev/null +++ b/src/App/Components/Product/Cheat.jsx @@ -0,0 +1,118 @@ +import React, { useState, useEffect, useRef, useContext } from "react"; +import DoneIcon from "@material-ui/icons/Done"; +const { ipcRenderer } = require("electron"); +import Button from "@material-ui/core/Button"; +import TextField from "@material-ui/core/TextField"; +import Input from "../Input.jsx"; +import ExpenseInput from "../ExpenseInput.jsx"; +import { NotifContext } from "../../Contexts/NotifContext.jsx"; +import "./Cheat.css"; +import { convertToIntIfIsNumber } from "../../utils.js"; + +// TODO: also up/down with arrow keys + +const Cheat = React.memo(({ productId, setCountI }) => { + const [amount, setAmount] = useState(""); + const [weight, setWeight] = useState(""); + const [price, setPrice] = useState(""); + const [edited, setEdited] = useState(false); + const { pushNotif } = useContext(NotifContext); + const init = useRef(true); + + const cheatProduct = () => { + ipcRenderer.send("cheatProduct", { + productId: productId, + cheat: { + amount: amount, + weight: weight, + price: price, + }, + }); + }; + + const removeCheatProduct = () => { + ipcRenderer.send("removeCheatProduct", productId); + }; + + useEffect(() => { + ipcRenderer.on("cheatProduct", (event, cheatStatus) => { + if (cheatStatus.status !== null) { + if (cheatStatus.status === true) { + pushNotif("success", cheatStatus.message); + setCountI(-1); + } else if (cheatStatus.status === false) { + pushNotif("error", cheatStatus.message); + } + } + }); + + ipcRenderer.on("removeCheatProduct", (event, cheatStatus) => { + if (cheatStatus.status !== null && cheatStatus.status === true) { + pushNotif("success", cheatStatus.message); + setCountI(-1); + } + }); + // clean up + return () => { + ipcRenderer.removeAllListeners("cheatProduct"); + }; + }); + + return productId && !edited ? ( +
+ + مقادیر دلخواه را با دقت وارد کنید | (تغییر بار {productId}) + +
+ { + setAmount(convertToIntIfIsNumber(e.target.value)); + }} + /> + { + setWeight(convertToIntIfIsNumber(e.target.value)); + }} + /> + { + setPrice(convertToIntIfIsNumber(e.target.value)); + }} + /> + +
+ +
+ ) : ( +
+ ); +}); + +export default Cheat; diff --git a/src/App/Pages/Car/Car.jsx b/src/App/Pages/Car/Car.jsx index 109c21b..677f3cd 100644 --- a/src/App/Pages/Car/Car.jsx +++ b/src/App/Pages/Car/Car.jsx @@ -378,7 +378,7 @@ const Car = ({ history }) => { پی‌نوشت :
- + {car.ps}
diff --git a/src/App/Pages/Product/Product.jsx b/src/App/Pages/Product/Product.jsx index e45a8f8..6f483df 100755 --- a/src/App/Pages/Product/Product.jsx +++ b/src/App/Pages/Product/Product.jsx @@ -14,6 +14,7 @@ import Expense from "../../Components/Expense.jsx"; import ShowDate from "../../Components/ShowDate.jsx"; import FormControlLabel from "@material-ui/core/FormControlLabel"; import Switch from "@material-ui/core/Switch"; +import Cheat from "../../Components/Product/Cheat.jsx"; import "./Product.css"; // TODO: add delete button @@ -206,6 +207,7 @@ function SaleSection({ productId, product }) { export default function Product({ history }) { const [product, setProduct] = useState(); + const [countI, setCountI] = useState(0); let { id } = useParams(); const init = useRef(true); @@ -218,26 +220,47 @@ export default function Product({ history }) { ipcRenderer.send("toggleProductFinish", id); }; + const handleKeyBoardEvent = (e) => { + if (e.key === "I" || e.key === "i" || e.key === "ه" || e.key === "ّ") + setCountI(countI + 1); + if (e.key === "Escape") setCountI(0); + }; + useEffect(() => { + if (countI === -1) { + setProduct(); + getOneProduct(id); + } + }, [countI]); + + useEffect(() => { + console.log(product); if (init.current) { getOneProduct(id); init.current = false; } + document.addEventListener("keydown", handleKeyBoardEvent); + ipcRenderer.on("getOneProduct", (event, product) => { setProduct(product); }); // clean up return () => { + document.removeEventListener("keydown", handleKeyBoardEvent); ipcRenderer.removeAllListeners("getOneProduct"); - ipcRenderer.removeAllListeners("toggleProductFinish"); }; }); return product ? ( product.inCar ? (
+ {countI > 4 ? ( + + ) : ( +
+ )}