Integrate APIs

This commit is contained in:
Viet Anh Nguyen 2024-02-21 18:11:02 +07:00
parent 5fed071439
commit 15a550ae36
2 changed files with 229 additions and 74 deletions

View File

@ -1,6 +1,6 @@
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { Button, Input, Table, Tag, DatePicker, Form, Modal, Select, Space, Checkbox } from 'antd'; import { Button, Input, Table, Tag, DatePicker, Form, Modal, Select, Space, Checkbox } from 'antd';
import { useState } from 'react'; import { useState, useEffect } from 'react';
import { Layout } from 'antd'; import { Layout } from 'antd';
import { import {
EditOutlined, DownloadOutlined, CheckCircleOutlined, EditOutlined, DownloadOutlined, CheckCircleOutlined,
@ -13,6 +13,8 @@ import {
import FileViewer from '@cyntler/react-doc-viewer'; import FileViewer from '@cyntler/react-doc-viewer';
import styled from 'styled-components'; import styled from 'styled-components';
const { Sider, Content } = Layout; const { Sider, Content } = Layout;
import { baseURL } from "request/api";
import moment from 'moment';
const siderStyle: React.CSSProperties = { const siderStyle: React.CSSProperties = {
backgroundColor: '#fafafa', backgroundColor: '#fafafa',
@ -38,22 +40,6 @@ const StyledEditOutlined = styled(EditOutlined)`
} }
`; `;
const fileList = [
{
name: "invoice.pdf",
url: "/dummpy.pdf",
type: "invoice",
isBadQuality: false,
},
{
name: "invoice.pdf",
url: "/dummpy.pdf",
type: "imei",
isBadQuality: true,
}
]
const dataSource = [ const dataSource = [
{ {
key: 'retailer_name', key: 'retailer_name',
@ -102,6 +88,7 @@ const columns = [
title: 'Key', title: 'Key',
dataIndex: 'key', dataIndex: 'key',
key: 'key', key: 'key',
width: 200,
}, },
{ {
title: 'Accuracy', title: 'Accuracy',
@ -110,6 +97,7 @@ const columns = [
render: (text, record) => { render: (text, record) => {
return <div>100%</div>; return <div>100%</div>;
}, },
width: 150,
}, },
{ {
title: 'Predicted', title: 'Predicted',
@ -142,6 +130,8 @@ const columns = [
const FileCard = ({ file, isSelected, onClick }) => { const FileCard = ({ file, isSelected, onClick }) => {
const fileName = file["File Name"];
return ( return (
<div style={{ <div style={{
border: '1px solid #ccc', border: '1px solid #ccc',
@ -160,14 +150,19 @@ const FileCard = ({ file, isSelected, onClick }) => {
fontWeight: 'bold', fontWeight: 'bold',
padding: '4px 8px', padding: '4px 8px',
cursor: 'default', cursor: 'default',
}}>{file.type.toUpperCase()}</span> }}>{file["Doc Type"].toUpperCase()}</span>
<span style={{ <span style={{
fontSize: '12px', fontSize: '12px',
color: '#aaa', color: '#aaa',
fontWeight: 'bold', fontWeight: 'bold',
padding: '4px 8px', padding: '4px 8px',
cursor: 'default', cursor: 'default',
}}>{file.name}</span> maxWidth: '50px',
overflow: 'hidden',
textOverflow: 'ellipsis',
}}>
{fileName? fileName.substring(0, 10) : fileName }
</span>
</div> </div>
<div style={{ <div style={{
padding: '4px', padding: '4px',
@ -191,6 +186,48 @@ const FileCard = ({ file, isSelected, onClick }) => {
}; };
const fetchAllRequests = async (filterDateRange, filterSubsidiaries, filterReviewState, filterIncludeTests, page=1, page_size=20) => {
const startDate = (filterDateRange && filterDateRange[0]) ? filterDateRange[0].format('YYYY-MM-DD'): '';
const endDate = (filterDateRange && filterDateRange[1]) ? filterDateRange[1].format('YYYY-MM-DD'): '';
let filterStr = "";
filterStr += `page=${page}&page_size=${page_size}&`;
if (filterSubsidiaries) {
filterStr += `subsidiary=${filterSubsidiaries}&`;
}
if (filterReviewState) {
filterStr += `is_reviewed=${filterReviewState}&`;
}
if (filterIncludeTests) {
filterStr += `includes_tests=${filterIncludeTests}&`;
}
if (startDate && endDate) {
filterStr += `start_date=${startDate}&end_date=${endDate}&`;
}
const token = localStorage.getItem('sbt-token') || '';
const data = await fetch(`${baseURL}/ctel/request_list/?${filterStr}`, {
method: 'GET',
headers: {
"Authorization": `${JSON.parse(token)}`
}
})
.then(async (res) => {
const data = await res.json();
return data;
});
return data;
};
const fetchRequest = async (id) => {
const token = localStorage.getItem('sbt-token') || '';
const response = await fetch(`${baseURL}/ctel/request/${id}/`, {
method: 'GET',
headers: {
"Authorization": `${JSON.parse(token)}`
}
});
return await (await response.json()).subscription_requests[0];
};
const ReviewPage = () => { const ReviewPage = () => {
const [fullscreen, setFullscreen] = useState(false); const [fullscreen, setFullscreen] = useState(false);
const [isModalOpen, setIsModalOpen] = useState(false); const [isModalOpen, setIsModalOpen] = useState(false);
@ -198,9 +235,86 @@ const ReviewPage = () => {
const selectFileByIndex = (index) => { const selectFileByIndex = (index) => {
setSelectedFileId(index); setSelectedFileId(index);
}; };
const [filterDateRange, setFilterDateRange] = useState(null);
const [filterSubsidiaries, setFilterSubsidiaries] = useState(''); // Default date range: 1 month ago to today
const [filterReviewState, setFilterReviewState] = useState(''); const [filterDateRange, setFilterDateRange] = useState([
moment().subtract(1,'month'),
moment(),
]);
const [filterSubsidiaries, setFilterSubsidiaries] = useState('ALL');
const [filterReviewState, setFilterReviewState] = useState('all');
const [filterIncludeTests, setFilterIncludesTests] = useState('true');
const [requests, setRequests] = useState([]);
const [currentRequest, setCurrentRequest] = useState(null);
const [currentRequestIndex, setCurrentRequestIndex] = useState(1);
const [hasNextRequest, setHasNextRequest] = useState(true);
const [totalPages, setTotalPages] = useState(0);
const gotoNextRequest = () => {
const nextRequestIndex = currentRequestIndex + 1;
setCurrentRequestIndex(nextRequestIndex);
fetchAllRequests(filterDateRange, filterSubsidiaries, filterReviewState, filterIncludeTests, nextRequestIndex, 2).then((data) => {
setRequests(data?.subscription_requests);
setHasNextRequest(data?.subscription_requests.length > 1);
setTotalPages(data?.page?.total_pages);
const requestData = fetchRequest(data?.subscription_requests[0].RequestID);
requestData.then(async (data) => {
console.log(data)
if (data) setCurrentRequest(data);
});
});
};
const gotoPreviousRequest = () => {
if (currentRequestIndex === 1) {
return;
}
const previousRequestIndex = currentRequestIndex - 1;
setCurrentRequestIndex(previousRequestIndex);
fetchAllRequests(filterDateRange, filterSubsidiaries, filterReviewState, filterIncludeTests, previousRequestIndex, 2).then((data) => {
setRequests(data?.subscription_requests);
setHasNextRequest(data?.subscription_requests.length > 1);
setTotalPages(data?.page?.total_pages);
const requestData = fetchRequest(data?.subscription_requests[0].RequestID);
requestData.then(async (data) => {
console.log(data)
if (data) setCurrentRequest(data);
});
});
};
const reloadFilters = () => {
setCurrentRequestIndex(1);
fetchAllRequests(filterDateRange, filterSubsidiaries, filterReviewState, filterIncludeTests, currentRequestIndex, 2).then((data) => {
setTotalPages(data?.page?.total_pages);
setRequests(data?.subscription_requests);
setHasNextRequest(data?.subscription_requests.length > 1);
const firstRequest = fetchRequest(data?.subscription_requests[0].RequestID);
firstRequest.then(async (data) => {
console.log(firstRequest)
if (data) setCurrentRequest(data);
});
});
};
useEffect(() => {
setCurrentRequestIndex(1);
fetchAllRequests(filterDateRange, filterSubsidiaries, filterReviewState, filterIncludeTests, currentRequestIndex, 2).then((data) => {
setTotalPages(data?.page?.total_pages);
setRequests(data?.subscription_requests);
setHasNextRequest(data?.subscription_requests.length > 1);
const firstRequest = fetchRequest(data?.subscription_requests[0].RequestID);
firstRequest.then(async (data) => {
console.log(firstRequest)
if (data) setCurrentRequest(data);
});
});
}, []);
const fileURL = currentRequest ? baseURL + currentRequest["Files"][selectedFileId]["File URL"].replace("http://be-ctel-sbt:9000/api", "") : "dummy.pdf";
return ( return (
<div style={fullscreen ? { <div style={fullscreen ? {
@ -225,8 +339,8 @@ const ReviewPage = () => {
width: '100%', width: '100%',
height: '100%', height: '100%',
maxWidth: '100%', maxWidth: '100%',
minHeight: '60%', minHeight: '70%',
maxHeight: '60%', maxHeight: '70%',
display: 'flex', display: 'flex',
padding: '8px', padding: '8px',
}}> }}>
@ -249,10 +363,10 @@ const ReviewPage = () => {
style={{ style={{
color: "#333", color: "#333",
padding: 10, padding: 10,
fontWeight: 'bold' fontWeight: 'bold'
}} }}
>Files ({fileList.length})</h2> >Files ({currentRequest?.Files?.length})</h2>
{fileList.map((file, index) => ( {currentRequest?.Files.map((file, index) => (
<FileCard key={index} file={file} isSelected={index === selectedFileId} onClick={ <FileCard key={index} file={file} isSelected={index === selectedFileId} onClick={
() => { () => {
setSelectedFileId(index); setSelectedFileId(index);
@ -267,7 +381,7 @@ const ReviewPage = () => {
}}> }}>
<FileViewer documents={ <FileViewer documents={
[ [
{ uri: "/dummy.pdf" } { uri: fileURL }
] ]
} config={{ } config={{
header: { header: {
@ -280,9 +394,11 @@ const ReviewPage = () => {
}} /> }} />
</div> </div>
</Content> </Content>
<Sider width="300px" style={siderStyle}> <Sider width="400px" style={siderStyle}>
<Space.Compact style={{ width: '100%', marginBottom: 16 }}> <Space.Compact style={{ width: '100%', marginBottom: 16 }}>
<Input defaultValue="All" readOnly /> <Input value={
`Sub: ${filterSubsidiaries}, Date:${filterDateRange[0] ? (filterDateRange[0]?.format('YYYY-MM-DD') + " to " + filterDateRange[1].format('YYYY-MM-DD')) : "All"}, Reviewed: ${filterReviewState}, Tests: ${filterIncludeTests}`
} readOnly />
<Button type="primary" size="large" <Button type="primary" size="large"
onClick={() => { onClick={() => {
setIsModalOpen(true); setIsModalOpen(true);
@ -293,11 +409,24 @@ const ReviewPage = () => {
</Space.Compact> </Space.Compact>
<div style={{ display: "flex", justifyContent: "space-between", marginBottom: 8 }}> <div style={{ display: "flex", justifyContent: "space-between", marginBottom: 8 }}>
<div> <div>
<Button type="default"> <Button type="default"
disabled={currentRequestIndex === 1}
onClick={() => {
gotoPreviousRequest();
}}
>
<ArrowLeftOutlined /> <ArrowLeftOutlined />
Previous Previous
</Button> </Button>
<Button type="default"> <Button type="default"
disabled={!hasNextRequest}
onClick={() => {
if (!hasNextRequest) {
return;
}
gotoNextRequest();
}}
>
Next Next
<ArrowRightOutlined /> <ArrowRightOutlined />
</Button> </Button>
@ -306,12 +435,12 @@ const ReviewPage = () => {
</Button> </Button>
</div> </div>
</div> </div>
<h2 style={{ margin: "20px 0 10px 0" }}>Request Review</h2> <h2 style={{ margin: "20px 0 10px 0" }}>{totalPages ? ("Request: " + currentRequestIndex + "/" + totalPages) : "No Request. Adjust your search criteria to see more results."}</h2>
<Input size='small' addonBefore="Request ID" style={{ marginBottom: "4px" }} readOnly /> <Input size='small' addonBefore="Request ID" style={{ marginBottom: "4px" }} readOnly value={currentRequest ? currentRequest.RequestID : ""} />
<Input size='small' addonBefore="Redemption" style={{ marginBottom: "4px" }} readOnly /> <Input size='small' addonBefore="Redemption" style={{ marginBottom: "4px" }} readOnly value={currentRequest?.RedemptionID ? currentRequest.RedemptionID : "<Unknown>"} />
<Input size='small' addonBefore="Uploaded date" style={{ marginBottom: "4px" }} readOnly /> <Input size='small' addonBefore="Uploaded date" style={{ marginBottom: "4px" }} readOnly value={currentRequest ? currentRequest.created_at : ""} />
<Input size='small' addonBefore="Request time" style={{ marginBottom: "4px" }} readOnly /> <Input size='small' addonBefore="Request time" style={{ marginBottom: "4px" }} readOnly value={currentRequest ? currentRequest["Client Request Time (ms)"] : ""} />
<Input size='small' addonBefore="Processing time" style={{ marginBottom: "4px" }} readOnly /> <Input size='small' addonBefore="Processing time" style={{ marginBottom: "4px" }} readOnly value={currentRequest ? currentRequest["Server Processing Time (ms)"] : ""} />
<div style={{ marginBottom: "8px", marginTop: "8px", display: "flex" }}> <div style={{ marginBottom: "8px", marginTop: "8px", display: "flex" }}>
<Button type="primary">Mark Not-reviewed</Button> <Button type="primary">Mark Not-reviewed</Button>
{/* <Tag icon={<ClockCircleOutlined />} color="warning" style={{ padding: "4px 16px", marginLeft: 8 }}> {/* <Tag icon={<ClockCircleOutlined />} color="warning" style={{ padding: "4px 16px", marginLeft: 8 }}>
@ -330,6 +459,7 @@ const ReviewPage = () => {
onOk={ onOk={
() => { () => {
setIsModalOpen(false); setIsModalOpen(false);
reloadFilters();
} }
} }
onCancel={ onCancel={
@ -359,40 +489,42 @@ const ReviewPage = () => {
<DatePicker.RangePicker <DatePicker.RangePicker
value={filterDateRange} value={filterDateRange}
onChange={(value) => { onChange={(value) => {
console.log(value);
setFilterDateRange(value); setFilterDateRange(value);
}} }}
style={{ width: 200 }} style={{ width: 200 }}
/> />
</Form.Item> </Form.Item>
<div style={{ marginTop: 10, display: 'flex' }}> <Form.Item
<Form.Item name='subsidiary'
name='subsidiary' label={t`Subsidiary`}
label={t`Subsidiary`} rules={[
rules={[ {
{ required: true,
required: true, message: 'Please select a subsidiary',
message: 'Please select a subsidiary', },
}, ]}
style={{
marginBottom: 10,
}}
>
<Select
placeholder='Select a subsidiary'
style={{ width: 200 }}
options={[
{ value: 'ALL', label: 'ALL' },
{ value: 'SEAU', label: 'SEAU' },
{ value: 'SESP', label: 'SESP' },
{ value: 'SME', label: 'SME' },
{ value: 'SEPCO', label: 'SEPCO' },
{ value: 'TSE', label: 'TSE' },
{ value: 'SEIN', label: 'SEIN' },
]} ]}
style={{ value={filterSubsidiaries}
marginBottom: 10, defaultValue={filterSubsidiaries}
}} onChange={setFilterSubsidiaries}
> />
<Select </Form.Item>
placeholder='Select a subsidiary' <div style={{ marginTop: 10, display: 'flex', marginLeft: 0, padding: 0 }}>
style={{ width: 200 }}
options={[
{ value: 'ALL', label: 'ALL' },
{ value: 'SEAU', label: 'SEAU' },
{ value: 'SESP', label: 'SESP' },
{ value: 'SME', label: 'SME' },
{ value: 'SEPCO', label: 'SEPCO' },
{ value: 'TSE', label: 'TSE' },
{ value: 'SEIN', label: 'SEIN' },
]}
/>
</Form.Item>
<Form.Item <Form.Item
name='reviewed' name='reviewed'
label={t`Reviewed`} label={t`Reviewed`}
@ -402,28 +534,51 @@ const ReviewPage = () => {
message: 'Please select review status', message: 'Please select review status',
}, },
]} ]}
>
<Select
style={{ width: 200 }}
options={[
{ label: 'All', value: 'all' },
{ label: 'Reviewed', value: 'reviewed' },
{ label: 'Not Reviewed', value: 'not_reviewed' },
]}
value={filterReviewState}
defaultValue={filterReviewState}
onChange={setFilterReviewState}
/>
</Form.Item>
<Form.Item
name='is_test'
label={t`Is Test`}
rules={[
{
required: true,
message: 'Please select test status',
},
]}
style={{ marginLeft: 16 }} style={{ marginLeft: 16 }}
> >
<Select <Select
style={{ width: 200 }} style={{ width: 200 }}
options={[ options={[
{ value: 'ALL', label: 'ALL' }, { label: 'Include tests', value: 'true' },
{ value: 'YES', label: 'YES' }, { label: 'Exclude tests', value: 'false' },
{ value: 'NO', label: 'NO' },
]} ]}
defaultValue={'ALL'} value={filterIncludeTests}
defaultValue={filterIncludeTests}
onChange={setFilterIncludesTests}
/> />
</Form.Item> </Form.Item>
</div> </div>
</Form> </Form>
</Modal> </Modal>
<div <div
style={{ style={{
height: '40%', height: '25%',
overflowY: 'auto', overflowY: 'auto',
}} }}
> >
<StyledTable dataSource={dataSource} columns={columns} <StyledTable dataSource={dataSource} columns={columns}
/> />
</div> </div>
</div> </div>

View File

@ -11,7 +11,7 @@ const environment = process.env.NODE_ENV;
const AXIOS_TIMEOUT_MS = 30 * 60 * 1000; // This config sastified long-live upload file request const AXIOS_TIMEOUT_MS = 30 * 60 * 1000; // This config sastified long-live upload file request
const EXPIRED_PASSWORD_SIGNAL = 'expired_password'; const EXPIRED_PASSWORD_SIGNAL = 'expired_password';
export const baseURL = environment === 'development' ? 'http://42.96.42.13:9000/api' : '/api'; export const baseURL = environment === 'development' ? 'http://42.96.42.13:9881/api' : '/api';
export const API = axios.create({ export const API = axios.create({
timeout: AXIOS_TIMEOUT_MS, timeout: AXIOS_TIMEOUT_MS,