feat: adjust buy product on click

This commit is contained in:
caturbgs 2021-12-17 17:50:02 +07:00
parent 5069f63374
commit c73f642b93
3 changed files with 113 additions and 178 deletions

View File

@ -1,66 +0,0 @@
import React from "react";
import {Form, Input, message, Modal} from "antd";
import {observer} from "mobx-react-lite";
import {useStore} from "../utils/useStore";
export const BuyProductModal = observer(({initialData}) => {
const store = useStore();
const [form] = Form.useForm();
const handleCancelBuyProduct = () => {
form.resetFields();
store.transaction.visibleModalTransaction = false;
};
const handleSubmitBuyProduct = async (data) => {
try {
const response = await store.transaction.buyProduct({productCode: data});
if (response.status === 200) {
message.success("Success Buy Product");
} else {
message.error("Failed Buy Product");
}
} catch (e) {
console.log(e, "apa errornya");
message.error("Failed Buy Product");
}
store.transaction.visibleModalTransaction = false;
form.resetFields();
};
return (
<div>
<Modal
visible={store.transaction.visibleModalTransaction}
title={`Buy Product ${initialData?.name}`}
okText="Buy"
cancelText="Cancel"
onCancel={handleCancelBuyProduct}
onOk={() => {
form
.validateFields()
.then((values) => {
handleSubmitBuyProduct(values);
})
.catch((info) => {
console.error("Validate Failed:", info);
});
}}
>
<Form
form={form}
layout="vertical"
initialValues={{supplier: store.supplier.code}}
>
<Form.Item
name="code"
label="Code"
rules={[{required: true, message: "Please input Code!"}]}
>
<Input/>
</Form.Item>
</Form>
</Modal>
</div>
);
});

View File

@ -1,5 +1,6 @@
export const appConfig = { export const appConfig = {
apiUrl: 'https://ppob-backend.k3s.bangun-kreatif.com/v1' apiUrl: 'https://ppob-backend.k3s.bangun-kreatif.com/v1'
// apiUrl: 'http://localhost:3222/v1'
}; };
//export default appConfig; //export default appConfig;

View File

@ -1,20 +1,17 @@
import React, { useEffect, useState } from "react"; import React, {useEffect, useState} from "react";
import { useStore } from "../../utils/useStore"; import {useStore} from "../../utils/useStore";
import { Button, Card, Col, Input, Row, Select, message } from "antd"; import {Button, Card, Col, Input, message, Modal, Row, Select} from "antd";
import { observer } from "mobx-react-lite"; import {observer} from "mobx-react-lite";
import { BuyProductModal } from "../../component/BuyProductModal"; import {MoneyCollectOutlined} from "@ant-design/icons";
const { Search } = Input; const {Search} = Input;
const { Option } = Select; const {Option} = Select;
export const Product = observer(() => { export const Product = observer(() => {
const store = useStore(); const store = useStore();
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [productData, setProductData] = useState([]); const [productData, setProductData] = useState([]);
const [data, setData] = useState({});
const [kode, setKode] = useState({});
const [cardIndex, setCardIndex] = useState({});
useEffect(() => { useEffect(() => {
const init = async () => { const init = async () => {
@ -46,17 +43,20 @@ export const Product = observer(() => {
}, [store.transaction.dataSubCategories]); }, [store.transaction.dataSubCategories]);
const handleChangeSubcategory = async (item) => { const handleChangeSubcategory = async (item) => {
console.log("item", item);
store.transaction.filterSubCategory = item; store.transaction.filterSubCategory = item;
await store.transaction.getData(); await store.transaction.getData();
}; };
const handleBuyProduct = async (kode) => { const handleBuyProduct = async (data) => {
console.log(kode);
try { try {
await store.product.buyProduct({ productCode: kode }); const response = await store.transaction.buyProduct({productCode: data});
if (response.status === 200) {
message.success("Success Buy Product"); message.success("Success Buy Product");
} else {
message.error("Failed Buy Product");
}
} catch (e) { } catch (e) {
console.log(e, "apa errornya");
message.error("Failed Buy Product"); message.error("Failed Buy Product");
} }
}; };
@ -64,9 +64,7 @@ export const Product = observer(() => {
return ( return (
<div> <div>
<Row> <Row>
<span style={{ fontWeight: "bold", marginBottom: "10px" }}> <span style={{fontWeight: "bold", marginBottom: "10px"}}>Sub Category</span>
Sub Category
</span>
</Row> </Row>
<Row> <Row>
<Col span={24}> <Col span={24}>
@ -78,7 +76,7 @@ export const Product = observer(() => {
handleChangeSubcategory(val); handleChangeSubcategory(val);
} }
}} }}
style={{ marginBottom: "10px", width: "100%" }} style={{marginBottom: "10px", width: "100%"}}
> >
{store.transaction.dataSubCategories.map((item, index) => ( {store.transaction.dataSubCategories.map((item, index) => (
<Option key={item.id} value={item.id}> <Option key={item.id} value={item.id}>
@ -88,17 +86,17 @@ export const Product = observer(() => {
</Select> </Select>
</Col> </Col>
</Row> </Row>
<Row justify={"center"} align={"center"} style={{ marginBottom: "1rem" }}> <Row justify={"center"} align={"center"} style={{marginBottom: "1rem"}}>
<Col <Col
span={12} span={12}
style={{ fontWeight: "bold", display: "flex", alignItems: "center" }} style={{fontWeight: "bold", display: "flex", alignItems: "center"}}
> >
Produk & Nominal Produk & Nominal
</Col> </Col>
<Col span={12} style={{ textAlign: "right" }}> <Col span={12} style={{textAlign: "right"}}>
<Search <Search
placeholder="input search text" placeholder="input search text"
style={{ width: 200, marginRight: 10 }} style={{width: 200, marginRight: 10}}
/> />
</Col> </Col>
</Row> </Row>
@ -108,19 +106,25 @@ export const Product = observer(() => {
<Col key={index} xs={24} md={16} lg={8}> <Col key={index} xs={24} md={16} lg={8}>
<Card <Card
onClick={() => { onClick={() => {
setKode(item.code); Modal.confirm({
setCardIndex(index); title: `Are you sure buy ${item.name}?`,
}} icon: <MoneyCollectOutlined/>,
hoverable okText: "Confirm",
style={{ cancelText: "Cancel",
cursor: "pointer", okType: "primary",
borderColor: cardIndex === index ? "#2D9CDB" : "", onOk() {
marginLeft: "10px", handleBuyProduct(item.code)
},
onCancel() {
console.log("Cancel");
},
});
}} }}
style={{cursor: "pointer"}}
> >
<span style={{ color: "black" }}>{item.name}</span> <span style={{color: "black"}}>{item.name}</span>
<br /> <br/>
<span style={{ color: "grey", fontSize: 10 }}> <span style={{color: "grey", fontSize: 10}}>
{new Intl.NumberFormat("id-ID", { {new Intl.NumberFormat("id-ID", {
style: "currency", style: "currency",
currency: "IDR", currency: "IDR",
@ -133,15 +137,11 @@ export const Product = observer(() => {
)} )}
{productData.length !== 0 && ( {productData.length !== 0 && (
<Col style={{ textAlign: "right" }}> <Col style={{ textAlign: "right" }}>
<Button <Button style={{backgroundColor: "#2D9CDB", color: "white"}}>
style={{ backgroundColor: "#2D9CDB", color: "white" }}
onClick={() => handleBuyProduct(kode)}
>
Beli Sekarang Beli Sekarang
</Button> </Button>
</Col> </Col>
)} )}
<BuyProductModal initialData={data} />
</div> </div>
); );
}); });