232 lines
6.1 KiB
JavaScript
232 lines
6.1 KiB
JavaScript
import React, { useEffect, useState } from "react";
|
|
import { Button, Form, Input, message, Modal, Space, Table, Tag } from "antd";
|
|
import { observer } from "mobx-react-lite";
|
|
import { ExclamationCircleOutlined } from "@ant-design/icons";
|
|
import { useHistory } from "react-router-dom";
|
|
import { useStore } from "../utils/useStore";
|
|
import { LINKS } from "../routes/app";
|
|
import { TopupsaldoModal } from "./TopupsaldoModal";
|
|
|
|
export const SupplierComponent = observer((props) => {
|
|
const store = useStore();
|
|
const [form] = Form.useForm();
|
|
const history = useHistory();
|
|
const [idData, setIdData] = useState("");
|
|
const [code, setCode] = useState("");
|
|
|
|
useEffect(() => {
|
|
const init = async () => {
|
|
await store.supplier.getData();
|
|
};
|
|
init();
|
|
}, []);
|
|
|
|
const handleEditButton = (data) => {
|
|
console.log(data, "isi data");
|
|
form.setFieldsValue({
|
|
name: data.name,
|
|
code: data.code,
|
|
status: data.status,
|
|
});
|
|
store.supplier.visibleModalSupplier = true;
|
|
setIdData(data.id);
|
|
};
|
|
|
|
const handleTopup = (data) => {
|
|
console.log(data, "isi data");
|
|
form.setFieldsValue({
|
|
code: data.code,
|
|
});
|
|
store.supplier.visibleModalTransaction = true;
|
|
setCode(data.code);
|
|
};
|
|
const changeStatus = async (id, isActive) => {
|
|
const status = isActive ? "inactive" : "active";
|
|
const status2 = isActive ? "Inactivating" : "Activating";
|
|
try {
|
|
const response = await store.supplier.changeStatus(id, status);
|
|
|
|
response?.body?.statusCode === 201
|
|
? message.success(`Success ${status2} Supplier`)
|
|
: message.error(`Failed ${status2} Supplier`);
|
|
} catch (err) {
|
|
console.log("error", err);
|
|
message.error(`Failed ${status2} Supplier`);
|
|
}
|
|
};
|
|
|
|
const columns = [
|
|
{
|
|
title: "Name",
|
|
dataIndex: "name",
|
|
key: "name",
|
|
},
|
|
{
|
|
title: "Kode",
|
|
dataIndex: "code",
|
|
key: "code",
|
|
},
|
|
,
|
|
{
|
|
title: "Saldo",
|
|
dataIndex: "saldo",
|
|
key: "saldo",
|
|
},
|
|
{
|
|
title: "Status",
|
|
dataIndex: "status",
|
|
key: "status",
|
|
render: (text, record) => (
|
|
<Tag
|
|
color={record?.status === true ? "processing" : "#E3E8EE"}
|
|
style={{ color: "#4F566B"}}
|
|
>
|
|
{record?.status === true ? " ACTIVE" : "INACTIVE"}
|
|
</Tag>
|
|
),
|
|
},
|
|
{
|
|
title: "Action",
|
|
key: "action",
|
|
render: (text, record) => (
|
|
<Space size="middle">
|
|
<Button
|
|
type={record?.status === true ? "danger" : "primary"}
|
|
onClick={() => changeStatus(record?.id, record?.status)}
|
|
>
|
|
{record?.status === true ? "INACTIVE" : "ACTIVE"}
|
|
</Button>
|
|
<Button onClick={() => handleTopup(record)}> Top Up Saldo</Button>
|
|
<Button onClick={() => handleEditButton(record)}>Edit</Button>
|
|
</Space>
|
|
),
|
|
},
|
|
];
|
|
|
|
const deleteData = async (id) => {
|
|
try {
|
|
console.log(id);
|
|
await store.supplier.delete(id);
|
|
message.success("Data Berhasil Dihapus");
|
|
history.push(LINKS.PRODUCT);
|
|
} catch (err) {
|
|
console.log("error", err);
|
|
message.error("Gagal menghapus");
|
|
}
|
|
};
|
|
|
|
const handleDelete = (id) => {
|
|
Modal.confirm({
|
|
title: "Are you sure delete this record?",
|
|
icon: <ExclamationCircleOutlined />,
|
|
okText: "Yes",
|
|
okType: "primary",
|
|
cancelText: "Cancel",
|
|
onOk() {
|
|
return deleteData(id);
|
|
},
|
|
onCancel() {
|
|
console.log("Cancel");
|
|
},
|
|
});
|
|
};
|
|
|
|
const handleCancel = () => {
|
|
setIdData("");
|
|
store.supplier.visibleModalSupplier = false;
|
|
};
|
|
|
|
const handleSubmit = async (data) => {
|
|
console.log(data, "isi data2");
|
|
if (idData !== "") {
|
|
try {
|
|
await store.supplier.update(idData, data);
|
|
message.success("Success Update Data Member");
|
|
await store.supplier.getData();
|
|
} catch (e) {
|
|
message.error("Failed Update Data Member");
|
|
}
|
|
|
|
store.supplier.visibleModalSupplier = false;
|
|
setIdData("");
|
|
form.resetFields();
|
|
} else {
|
|
try {
|
|
await store.supplier.create(data);
|
|
message.success("Success Add New Member");
|
|
} catch (e) {
|
|
console.log(e, "apa errornya");
|
|
message.error("Failed Add Member");
|
|
}
|
|
|
|
store.supplier.visibleModalSupplier = false;
|
|
setIdData("");
|
|
form.resetFields();
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div>
|
|
<Table
|
|
style={{ textAlign: "center" }}
|
|
columns={columns}
|
|
dataSource={store.supplier.data}
|
|
bordered
|
|
pagination={{
|
|
pageSize: store.supplier.pageSize,
|
|
total: store.supplier.total_data,
|
|
current: store.supplier.page + 1,
|
|
showSizeChanger: true,
|
|
simple: false,
|
|
}}
|
|
onChange={async (page) => {
|
|
let pageNumber = page.current;
|
|
store.supplier.pageSize = page.pageSize;
|
|
store.supplier.page = pageNumber - 1;
|
|
await store.supplier.getData();
|
|
}}
|
|
/>
|
|
<Modal
|
|
visible={store.supplier.visibleModalSupplier}
|
|
title={idData ? "Edit Supplier" : "Create a new Supplier"}
|
|
okText={idData ? "Edit" : "Create"}
|
|
cancelText="Cancel"
|
|
onCancel={() => {
|
|
form.resetFields();
|
|
handleCancel();
|
|
}}
|
|
onOk={() => {
|
|
form
|
|
.validateFields()
|
|
.then((values) => {
|
|
console.log(values, "isi form");
|
|
handleSubmit(values);
|
|
form.resetFields();
|
|
})
|
|
.catch((info) => {
|
|
console.error("Validate Failed:", info);
|
|
});
|
|
}}
|
|
>
|
|
<Form form={form} layout="vertical">
|
|
<Form.Item
|
|
name="name"
|
|
label="Name"
|
|
rules={[{ required: true, message: "Please input name!" }]}
|
|
>
|
|
<Input />
|
|
</Form.Item>
|
|
<Form.Item
|
|
name="code"
|
|
label="Kode"
|
|
rules={[{ required: true, message: "Please input kode!" }]}
|
|
>
|
|
<Input />
|
|
</Form.Item>
|
|
</Form>
|
|
</Modal>
|
|
<TopupsaldoModal code={code} />
|
|
</div>
|
|
);
|
|
});
|