From a4ba756dbce32278ae2dc866909447956d69ca75 Mon Sep 17 00:00:00 2001 From: caturbgs Date: Tue, 21 Dec 2021 14:42:53 +0700 Subject: [PATCH] feat: add upload image payback --- src/pages/Payback/PaybackFromUser.js | 6 +- src/pages/Payback/PaybackModal.js | 226 ++++++++++++--------------- src/utils/http.js | 9 +- 3 files changed, 106 insertions(+), 135 deletions(-) diff --git a/src/pages/Payback/PaybackFromUser.js b/src/pages/Payback/PaybackFromUser.js index 3df9981..cc258f2 100644 --- a/src/pages/Payback/PaybackFromUser.js +++ b/src/pages/Payback/PaybackFromUser.js @@ -20,8 +20,10 @@ export const PaybackFromUser = observer(() => { const init = async () => { try { modalLoader.setLoading(true); - await store.payback.getDataUser(); - await store.authentication.getProfile(); + await Promise.allSettled([ + store.payback.getDataUser(), + store.authentication.getProfile() + ]); modalLoader.setLoading(false); } catch (e) { modalLoader.setLoading(false); diff --git a/src/pages/Payback/PaybackModal.js b/src/pages/Payback/PaybackModal.js index a6bf6de..9079ad9 100644 --- a/src/pages/Payback/PaybackModal.js +++ b/src/pages/Payback/PaybackModal.js @@ -1,37 +1,28 @@ -import React, {useState, useEffect, useContext} from "react"; -import { - Button, - Form, - Image, - Input, - message, - Modal, - Upload, -} from "antd"; +import React, {useContext, useState} from "react"; +import {Form, Input, message, Modal, Upload,} from "antd"; import {useStore} from "../../utils/useStore"; -import {appConfig} from "../../config/app"; -import {LoadingOutlined, UploadOutlined} from "@ant-design/icons"; +import {LoadingOutlined, PlusOutlined} from "@ant-design/icons"; import {ModalLoaderContext} from "../../utils/modal"; +import {http} from "../../utils/http"; +import {appConfig} from "../../config/app"; export const PaybackModal = ({visible, onCreate, onCancel, initialData}) => { const [form] = Form.useForm(); const store = useStore(); + const [image, setImage] = useState(""); const [fileList, setFileList] = useState([]); - const [previewTitle, setPreviewTitle] = useState(""); const [previewImage, setPreviewImage] = useState(""); - const [fileUrl, setFileUrl] = useState(""); - const firstIndexFileList = fileList[0]; + const [previewVisible, setPreviewVisible] = useState(false); const [loading, setLoading] = useState(false); const modalLoader = useContext(ModalLoaderContext); const beforeUpload = (file) => { - let isPdf, isLt2M; - let allowedFile = ["image/jpeg", "image/png", "application/pdf"]; + let isLt2M; + let allowedFile = ["image/jpeg", "image/png"]; let isValid = allowedFile.includes(file.type); if (!isValid) { - message.error("You can only upload PDF or Image file!"); + message.error("You can only upload Image file!"); } - // return file.type === 'application/pdf' ? true : Upload.LIST_IGNORE; isLt2M = file.size / 1024 / 1024 < 2; if (!isLt2M) { message.error("File must smaller than 2MB!"); @@ -39,40 +30,31 @@ export const PaybackModal = ({visible, onCreate, onCancel, initialData}) => { return isValid && isLt2M ? true : Upload.LIST_IGNORE; }; - const handlePreview = async (file) => { - const fileUrl = appConfig.apiUrl + file.response.path; - setPreviewTitle(file.url?.substring(file.url?.lastIndexOf("/") + 1)); + const uploadHandler = async (args) => { + const file = args.file; + const res = await http.upload(file); + setImage(`${appConfig.apiUrl}/config/image/${res.body.filename}`); + setFileList([{ + uid: '-1', + name: 'image', + status: 'done', + url: `${appConfig.apiUrl}/config/image/${res.body.filename}`, + }]); }; - const handleChange = ({fileList}) => { - setFileList(fileList); - if (fileList.length && fileList[0].status === "done") { - form.setFieldsValue({ - file_url: fileList[0].response.path, - }); - console.log(fileList, "apaaaaaa"); - setFileUrl(fileList[0].response.path); - setPreviewImage(fileList[0].response.path); - setPreviewTitle(fileList[0].name); + const handleChange = (info) => { + if (info.file.status === 'uploading') { + setLoading(true); + return; } + setLoading(false) }; const uploadButton = ( -
- {loading ? ( - - ) : ( - - )} -
- ); - - const previewUpload = ( - +
+ {loading ? : } +
Click to Upload
+
); const handleSubmit = async (data) => { @@ -95,101 +77,85 @@ export const PaybackModal = ({visible, onCreate, onCancel, initialData}) => { }; return ( - { - form.resetFields(); - onCancel(); - }} - onOk={() => { - form - .validateFields() - .then((values) => { - handleSubmit(values); - console.log(values); + { form.resetFields(); - }) - .catch((info) => { - console.log("Validate Failed:", info); - }); - }} - > -
{ + form + .validateFields() + .then((values) => { + handleSubmit(values); + console.log(values); + form.resetFields(); + }) + .catch((info) => { + console.log("Validate Failed:", info); + }); + }} > - - - - -
+ + +
{ + setPreviewImage(file.url || file.filename); + setPreviewVisible(file.url || file.filename); + }} + showUploadList={true} + onChange={handleChange} + beforeUpload={(file) => beforeUpload(file)} + customRequest={(args) => uploadHandler(args)} + onRemove={(file) => { + setImage(''); + setLoading(false); + setFileList([]); + }} > - {!firstIndexFileList ? uploadButton : null} + + {image === "" ? uploadButton : null}
Max size of file 2 mb
-
-
Preview
-
- preview -
-
{previewTitle}
-
- {previewUpload} - {previewTitle && {`${previewTitle ?? ""}`}} -
-
-
-
- - - -
-
+ + + + + +
); }; diff --git a/src/utils/http.js b/src/utils/http.js index a752a36..4bb2fe8 100644 --- a/src/utils/http.js +++ b/src/utils/http.js @@ -50,13 +50,16 @@ export const http = { return req; }, upload: (file) => { - const request = superagent - .post(appConfig.apiUrl + '/files') + let req = superagent + .post(appConfig.apiUrl + '/config/upload-files') .attach('file', file) .use(authIntercept) .use(attachSuperagentLogger); + if (TokenUtil.accessToken) { + req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken); + } - return request; + return req; }, uploadAntd: (args) => { const file = args.file;