feat: add upload excel product functions

This commit is contained in:
caturbgs 2021-12-23 13:50:26 +07:00
parent edc6d3e317
commit d591615e66
2 changed files with 145 additions and 65 deletions

View File

@ -1,22 +1,19 @@
import React, { useContext, useEffect } from "react"; import React, {useContext, useEffect, useState} from "react";
import { Button, Card, Col, Input, message, Row, Upload } from "antd"; import {Button, Card, Col, Input, message, Row, Upload} from "antd";
import { import {FilterOutlined, LoadingOutlined, UploadOutlined,} from "@ant-design/icons";
FilterOutlined, import {BreadcumbComponent} from "../../component/BreadcumbComponent";
PlusSquareOutlined, import {useStore} from "../../utils/useStore";
UploadOutlined, import {observer} from "mobx-react-lite";
} from "@ant-design/icons"; import {ProductComponent} from "../../component/ProductComponent";
import { BreadcumbComponent } from "../../component/BreadcumbComponent"; import {LINKS} from "../../routes/app";
import { useStore } from "../../utils/useStore"; import {ModalLoaderContext} from "../../utils/modal";
import { observer } from "mobx-react-lite";
import { ProductComponent } from "../../component/ProductComponent";
import { LINKS } from "../../routes/app";
import { ModalLoaderContext } from "../../utils/modal";
const { Search } = Input; const {Search} = Input;
export const Product = observer(() => { export const Product = observer(() => {
const store = useStore(); const store = useStore();
const modalLoader = useContext(ModalLoaderContext); const modalLoader = useContext(ModalLoaderContext);
const [loading, setLoading] = useState(false);
useEffect(() => { useEffect(() => {
const init = async () => { const init = async () => {
@ -49,24 +46,85 @@ export const Product = observer(() => {
}, },
{ {
route: LINKS.PRODUCT, route: LINKS.PRODUCT,
name: <span style={{ fontWeight: "bold" }}>Product</span>, name: <span style={{fontWeight: "bold"}}>Product</span>,
}, },
]; ];
const beforeUpload = (file) => {
let isLt2M;
let allowedFile = [
"text/csv", "application/csv",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.ms-excel.sheet.binary.macroEnabled.12",
"application/vnd.ms-excel",
"application/vnd.ms-excel.sheet.macroEnabled.12"
];
let isValid = allowedFile.includes(file.type);
if (!isValid) {
message.error("You can only upload Excel file!");
}
isLt2M = file.size / 1024 / 1024 < 10;
if (!isLt2M) {
message.error("File must smaller than 10MB!");
}
return isValid && isLt2M ? true : Upload.LIST_IGNORE;
};
const uploadHandler = async (args) => {
const file = args.file;
const responseUpload = await store.product.uploadExcel(file);
if (responseUpload.status === 201) {
message.success("Success upload excel!");
} else {
message.error("Failed upload excel!");
setLoading(false);
}
const responseUploadProduct = await handleUploadProduct(responseUpload);
setLoading(false);
};
const handleChange = (info) => {
if (info.file.status === 'uploading') {
setLoading(true);
} else {
setLoading(false)
}
};
const handleUploadProduct = async (data) => {
const response = await store.product.uploadProduct({fileName: data.body.filename});
if (response.status === 201) {
message.success("Success Create Product by Excel!");
} else {
message.error("Failed Create Product by Excel!");
setLoading(false);
}
return response;
}
const loadingState = (
<div>
{loading ? <LoadingOutlined/> : null}
</div>
);
return ( return (
<div className={["ppob-container"].join(" ")}> <div className={["ppob-container"].join(" ")}>
<BreadcumbComponent data={routeData} /> <BreadcumbComponent data={routeData}/>
<Card> <Card>
{store.authentication.userData.role !== "Admin" && ( {store.authentication.userData.role !== "Admin" && (
<div> <div>
<Row style={{ marginBottom: 20 }}> <Row style={{marginBottom: 20}}>
<Col span={12}> <Col span={12}>
<Button <Button
onClick={() => { onClick={() => {
store.product.visibleModalFilterProduct = true; store.product.visibleModalFilterProduct = true;
}} }}
> >
<FilterOutlined /> <FilterOutlined/>
Filter Filter
</Button> </Button>
</Col> </Col>
@ -94,23 +152,27 @@ export const Product = observer(() => {
textAlign: "right", textAlign: "right",
}} }}
> >
<Upload> <Upload
showUploadList={false}
onChange={handleChange}
beforeUpload={(file) => beforeUpload(file)}
customRequest={(args) => uploadHandler(args)}
onRemove={(file) => {
setLoading(false);
}}
>
<Button <Button
disabled={store.product.uploadBtnProduct} disabled={loading}
style={{ style={{
marginRight: store.ui.mediaQuery.isMobile ? 0 : 10, marginRight: store.ui.mediaQuery.isMobile ? 0 : 10,
marginBottom: store.ui.mediaQuery.isMobile ? 10 : 10, marginBottom: store.ui.mediaQuery.isMobile ? 10 : 10,
}} }}
icon={<UploadOutlined />} icon={<UploadOutlined/>}
> >
Upload Product Upload Product
</Button> </Button>
</Upload> </Upload>
{/* <Button {loadingState}
onClick={() => (store.product.visibleModalProduct = true)}
>
<PlusSquareOutlined /> New
</Button> */}
</div> </div>
</div> </div>
</Col> </Col>

View File

@ -107,6 +107,24 @@ export class Product {
console.error(e); console.error(e);
} }
} }
async uploadExcel(data) {
try {
const response = await http.upload(data);
return response;
} catch (e) {
console.error(e);
}
}
async uploadProduct(data) {
try {
const response = await http.post('/product/upload-product').send(data);
return response;
} catch (e) {
console.error(e);
}
}
} }