Skip to content

Commit

Permalink
Merge pull request #17 from imkarimkarim/addFeature
Browse files Browse the repository at this point in the history
add cheat (i wish they didnt asked this feature)
  • Loading branch information
imkarimkarim authored Aug 20, 2021
2 parents 42b661d + a2a1deb commit d4acbda
Show file tree
Hide file tree
Showing 13 changed files with 409 additions and 75 deletions.
36 changes: 36 additions & 0 deletions src/App/Components/Product/Cheat.css
Original file line number Diff line number Diff line change
@@ -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%;
}
118 changes: 118 additions & 0 deletions src/App/Components/Product/Cheat.jsx
Original file line number Diff line number Diff line change
@@ -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 ? (
<div className="cheat-wrapper">
<span className="hint">
مقادیر دلخواه را با دقت وارد کنید | (تغییر بار {productId})
</span>
<div className="cheatEditor">
<Input
value={amount}
label="تعداد*"
fun={(e) => {
setAmount(convertToIntIfIsNumber(e.target.value));
}}
/>
<Input
value={weight}
label="وزن*"
fun={(e) => {
setWeight(convertToIntIfIsNumber(e.target.value));
}}
/>
<ExpenseInput
value={price}
label="فی فروش*"
fun={(e) => {
setPrice(convertToIntIfIsNumber(e.target.value));
}}
/>
<Button
className="newFactorAddProductInputButton"
disabled={
!(
amount.toString().length >= 1 &&
weight.toString().length >= 1 &&
price.toString().length >= 1
)
}
onClick={cheatProduct}
variant="outlined"
color="primary"
>
<DoneIcon />
</Button>
</div>
<Button
className="deleteCheats"
onClick={removeCheatProduct}
variant="outlined"
color="secondary"
>
حذف مقادیر دلخواه
</Button>
</div>
) : (
<div></div>
);
});

export default Cheat;
2 changes: 1 addition & 1 deletion src/App/Pages/Car/Car.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ const Car = ({ history }) => {
<span>پی‌نوشت</span>
<span> :</span>
<br />
<span key={index}>
<span>
<span>
{car.ps} <br />
</span>
Expand Down
30 changes: 29 additions & 1 deletion src/App/Pages/Product/Product.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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 ? (
<div>
{countI > 4 ? (
<Cheat setCountI={setCountI} productId={product.customeId} />
) : (
<div></div>
)}
<Nav history={history} />
<div className="product-reports">
<br />
Expand Down Expand Up @@ -270,6 +293,11 @@ export default function Product({ history }) {
</div>
) : (
<div>
{countI > 4 ? (
<Cheat setCountI={setCountI} productId={product.customeId} />
) : (
<div></div>
)}
<Nav history={history} />
<div className="product-reports">
<InfoSection product={product} />
Expand Down
2 changes: 1 addition & 1 deletion src/App/Pages/Reports/PrintCar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export default function PrintCar({ history }) {
<span>پی‌نوشت</span>
<span> :</span>
<br />
<span key={index}>
<span>
<span>
{car.ps} <br />
</span>
Expand Down
3 changes: 0 additions & 3 deletions src/App/Pages/Reports/PrintRemainingProducts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ function RenderProduct({ index, product, productsLength, history }) {
const [goBack, setGoBack] = useState(false);
const report = useRef(false);

console.log(history);
console.log("goBack: ", goBack);

useEffect(() => {
if (productsLength === index + 1 && !report.current && !goBack) {
report.current = true;
Expand Down
Loading

0 comments on commit d4acbda

Please sign in to comment.