Pages Categories
This commit is contained in:
parent
ba01132cd4
commit
a7072d7db2
|
@ -1,217 +1,231 @@
|
|||
import React, {useEffect, useState} from "react";
|
||||
import {Button, Form, Input, message, Modal, Select, Space, Table, Tag} from "antd";
|
||||
import {observer} from "mobx-react-lite";
|
||||
import {ExclamationCircleOutlined} from "@ant-design/icons";
|
||||
import {useHistory} from "react-router-dom";
|
||||
import {capitalize} from "lodash";
|
||||
import {useStore} from "../utils/useStore";
|
||||
import {LINKS} from "../routes/app";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import {
|
||||
Button,
|
||||
Form,
|
||||
Input,
|
||||
message,
|
||||
Modal,
|
||||
Select,
|
||||
Space,
|
||||
Table,
|
||||
Tag,
|
||||
} from "antd";
|
||||
import { observer } from "mobx-react-lite";
|
||||
import { ExclamationCircleOutlined } from "@ant-design/icons";
|
||||
import { useHistory } from "react-router-dom";
|
||||
import { capitalize } from "lodash";
|
||||
import { useStore } from "../utils/useStore";
|
||||
import { LINKS } from "../routes/app";
|
||||
|
||||
export const CategoryComponent = observer((props) => {
|
||||
const store = useStore();
|
||||
const [form] = Form.useForm();
|
||||
const {Option} = Select;
|
||||
const history = useHistory();
|
||||
const [idData, setIdData] = useState('');
|
||||
const [confirmLoading, setConfirmLoading] = useState(false);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const store = useStore();
|
||||
const [form] = Form.useForm();
|
||||
const { Option } = Select;
|
||||
const history = useHistory();
|
||||
const [idData, setIdData] = useState("");
|
||||
const [confirmLoading, setConfirmLoading] = useState(false);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const init = async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
//store.product.pageCategories=StrToLower(props.category)
|
||||
await store.product.getDataSubCategories();
|
||||
setIsLoading(false);
|
||||
} catch (e) {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
init();
|
||||
}, []);
|
||||
|
||||
const handleEditButton = (data) => {
|
||||
console.log(data, "isi data")
|
||||
form.setFieldsValue({
|
||||
name: data.name,
|
||||
});
|
||||
store.category.visibleModalCategory = true;
|
||||
setIdData(data.id);
|
||||
}
|
||||
|
||||
const columns = [
|
||||
{
|
||||
title: "Category Name",
|
||||
dataIndex: "name",
|
||||
key: "name",
|
||||
},
|
||||
// {
|
||||
// title: "Gangguan",
|
||||
// dataIndex: "status",
|
||||
// key: "status",
|
||||
// render: (text, record) => (
|
||||
// <Tag
|
||||
// color={record?.status === "ACTIVE" ? "blue" : "#E3E8EE"}
|
||||
// style={{color: "#4F566B"}}
|
||||
// >
|
||||
// {capitalize(record?.status)}
|
||||
// </Tag>
|
||||
// ),
|
||||
// },
|
||||
// {
|
||||
// title: "Tersedia",
|
||||
// dataIndex: "tersedia",
|
||||
// key: "tersedia",
|
||||
// render: (text, record) => (
|
||||
// <Tag
|
||||
// color={record?.status === "ACTIVE" ? "blue" : "#E3E8EE"}
|
||||
// style={{color: "#4F566B"}}
|
||||
// >
|
||||
// {record?.status === "ACTIVE" ? " Ya" : "Tidak"}
|
||||
// </Tag>
|
||||
// ),
|
||||
// },
|
||||
{
|
||||
title: "Action",
|
||||
key: "action",
|
||||
render: (text, record) => (
|
||||
<Space size="middle">
|
||||
<Button
|
||||
onClick={() => handleEditButton(record)}
|
||||
>Edit</Button>
|
||||
<Button
|
||||
onClick={() => handleDelete(record.id)}
|
||||
>
|
||||
Delete
|
||||
</Button>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
const deleteData = async (id) => {
|
||||
try {
|
||||
console.log(id);
|
||||
await store.category.delete(id);
|
||||
message.success("Data Berhasil Dihapus");
|
||||
history.push(LINKS.PRODUCT);
|
||||
} catch (err) {
|
||||
console.log("error", err);
|
||||
message.error("Gagal menghapus");
|
||||
}
|
||||
useEffect(() => {
|
||||
const init = async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
//store.product.pageCategories=StrToLower(props.category)
|
||||
await store.product.getDataSubCategories();
|
||||
setIsLoading(false);
|
||||
} catch (e) {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
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");
|
||||
},
|
||||
});
|
||||
};
|
||||
init();
|
||||
}, []);
|
||||
|
||||
const handleCancel = () => {
|
||||
setIdData('')
|
||||
store.category.visibleModalCategory = false;
|
||||
const handleEditButton = (data) => {
|
||||
console.log(data, "isi data");
|
||||
form.setFieldsValue({
|
||||
name: data.name,
|
||||
code: data.code,
|
||||
});
|
||||
store.category.visibleModalCategory = true;
|
||||
setIdData(data.id);
|
||||
};
|
||||
|
||||
const columns = [
|
||||
{
|
||||
title: "Kode",
|
||||
dataIndex: "code",
|
||||
key: "code",
|
||||
},
|
||||
{
|
||||
title: "Category Name",
|
||||
dataIndex: "name",
|
||||
key: "name",
|
||||
},
|
||||
// {
|
||||
// title: "Gangguan",
|
||||
// dataIndex: "status",
|
||||
// key: "status",
|
||||
// render: (text, record) => (
|
||||
// <Tag
|
||||
// color={record?.status === "ACTIVE" ? "blue" : "#E3E8EE"}
|
||||
// style={{color: "#4F566B"}}
|
||||
// >
|
||||
// {capitalize(record?.status)}
|
||||
// </Tag>
|
||||
// ),
|
||||
// },
|
||||
// {
|
||||
// title: "Tersedia",
|
||||
// dataIndex: "tersedia",
|
||||
// key: "tersedia",
|
||||
// render: (text, record) => (
|
||||
// <Tag
|
||||
// color={record?.status === "ACTIVE" ? "blue" : "#E3E8EE"}
|
||||
// style={{color: "#4F566B"}}
|
||||
// >
|
||||
// {record?.status === "ACTIVE" ? " Ya" : "Tidak"}
|
||||
// </Tag>
|
||||
// ),
|
||||
// },
|
||||
{
|
||||
title: "Action",
|
||||
key: "action",
|
||||
render: (text, record) => (
|
||||
<Space size="middle">
|
||||
<Button onClick={() => handleEditButton(record)}>Edit</Button>
|
||||
<Button onClick={() => handleDelete(record.id)}>Delete</Button>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
const deleteData = async (id) => {
|
||||
try {
|
||||
console.log(id);
|
||||
await store.category.delete(id);
|
||||
message.success("Data Berhasil Dihapus");
|
||||
history.push(LINKS.PRODUCT);
|
||||
} catch (err) {
|
||||
console.log("error", err);
|
||||
message.error("Gagal menghapus");
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async (data) => {
|
||||
console.log(data, "isi data2")
|
||||
if (idData !== '') {
|
||||
setConfirmLoading(true);
|
||||
try {
|
||||
await store.category.update(idData, data)
|
||||
message.success("Success Update Data Category")
|
||||
} catch (e) {
|
||||
message.error("Failed Update Data Category")
|
||||
}
|
||||
setConfirmLoading(false);
|
||||
store.category.visibleModalCategory = false;
|
||||
setIdData('');
|
||||
form.resetFields();
|
||||
} else {
|
||||
setConfirmLoading(true);
|
||||
try {
|
||||
await store.category.create(data)
|
||||
message.success("Success Add New Category")
|
||||
} catch (e) {
|
||||
console.log(e, "apa errornya")
|
||||
message.error("Failed Add Category")
|
||||
}
|
||||
setConfirmLoading(false);
|
||||
store.category.visibleModalCategory = false;
|
||||
setIdData('');
|
||||
form.resetFields();
|
||||
}
|
||||
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.category.visibleModalCategory = false;
|
||||
};
|
||||
|
||||
const handleSubmit = async (data) => {
|
||||
console.log(data, "isi data2");
|
||||
if (idData !== "") {
|
||||
setConfirmLoading(true);
|
||||
try {
|
||||
await store.category.update(idData, data);
|
||||
message.success("Success Update Data Category");
|
||||
} catch (e) {
|
||||
message.error("Failed Update Data Category");
|
||||
}
|
||||
setConfirmLoading(false);
|
||||
store.category.visibleModalCategory = false;
|
||||
setIdData("");
|
||||
form.resetFields();
|
||||
} else {
|
||||
setConfirmLoading(true);
|
||||
try {
|
||||
await store.category.create(data);
|
||||
message.success("Success Add New Category");
|
||||
} catch (e) {
|
||||
console.log(e, "apa errornya");
|
||||
message.error("Failed Add Category");
|
||||
}
|
||||
setConfirmLoading(false);
|
||||
store.category.visibleModalCategory = false;
|
||||
setIdData("");
|
||||
form.resetFields();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Table
|
||||
style={{textAlign: "center"}}
|
||||
columns={columns}
|
||||
dataSource={store.category.data}
|
||||
bordered
|
||||
pagination={{
|
||||
pageSize: store.product.pageSize,
|
||||
total: store.product.total_data,
|
||||
current: store.product.page + 1,
|
||||
showSizeChanger: true,
|
||||
simple: false
|
||||
}}
|
||||
onChange={async (page) => {
|
||||
let pageNumber = page.current;
|
||||
store.product.pageSize = page.pageSize;
|
||||
store.product.page = pageNumber - 1;
|
||||
// store.membership.isLoading = true;
|
||||
await store.product.getData();
|
||||
// store.membership.isLoading = false;
|
||||
}}
|
||||
/>
|
||||
return (
|
||||
<div>
|
||||
<Table
|
||||
style={{ textAlign: "center" }}
|
||||
columns={columns}
|
||||
dataSource={store.category.data}
|
||||
bordered
|
||||
pagination={{
|
||||
pageSize: store.product.pageSize,
|
||||
total: store.product.total_data,
|
||||
current: store.product.page + 1,
|
||||
showSizeChanger: true,
|
||||
simple: false,
|
||||
}}
|
||||
onChange={async (page) => {
|
||||
let pageNumber = page.current;
|
||||
store.product.pageSize = page.pageSize;
|
||||
store.product.page = pageNumber - 1;
|
||||
// store.membership.isLoading = true;
|
||||
await store.product.getData();
|
||||
// store.membership.isLoading = false;
|
||||
}}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
visible={store.category.visibleModalCategory}
|
||||
title={idData ? "Edit Category" : "Create a new Category"}
|
||||
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 category!"}]}
|
||||
>
|
||||
<Input/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
<Modal
|
||||
visible={store.category.visibleModalCategory}
|
||||
title={idData ? "Edit Category" : "Create a new Category"}
|
||||
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="code"
|
||||
label="Code"
|
||||
rules={[{ required: true, message: "Please input name code!" }]}
|
||||
>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="name"
|
||||
label="Name"
|
||||
rules={[{ required: true, message: "Please input name category!" }]}
|
||||
>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user