UserHistory.jsx
2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import React, { useState, useEffect } from "react";
import { Grid, Row, Col, Table } from "react-bootstrap";
import UserDataCard from '../components/UserHistory/UserDataCard';
import UserHistoryCard from '../components/UserHistory/UserHistoryCard';
import SearchButton from "../components/UserHistory/SearchButton";
import Fallback from '../components/UserHistory/Fallback';
const convertQueryStringToObject = s => {
const hashParams = {};
let e;
const a = /\+/g, // Regex for replacing addition symbol with a space
r = /([^&;=]+)=?([^&;]*)/g,
d = function(s) {
return decodeURIComponent(s.replace(a, ' '));
},
q = s.replace(/\?/g, '');
while ((e = r.exec(q))) hashParams[d(e[1])] = d(e[2]);
return hashParams;
};
const UserHistory = () => {
const [userId, setUserId] = useState('0');
const [userData, setUserData] = useState({});
const [rentData, setRentData] = useState([]);
useEffect(() => {
// component did mount
console.log('component did mount')
const queryString = window.location.href.split('?')[1];
const queryObject = queryString ? convertQueryStringToObject(queryString) : {};
if(queryObject && queryObject.userId) {
setTimeout(() => setUserId(queryObject.userId), 300)
}
},[]);
useEffect(() => {
if(Number(userId)<0) return;
console.log(userId);
fetch(`http://1.201.143.67:5901/user/${userId}`)
.then(r => r.json())
.then(d => {
console.log(d);
if(!d.success) setUserId('0'); // 유효하지 않은 userId인 경우
if(d.data && d.data.length) setUserData(d.data[0]);
fetch(`http://1.201.143.67:5901/user/rent/${userId}`)
.then(r => r.json())
.then(d => {
if(d.data && d.data.length) setRentData(d.data);
})
})
.catch(err => console.log(err));
},[userId]);
return (
<div className="content">
<Grid fluid>
<Row>
<Col md={4} mdOffset={8} sm={5} smOffset={7} style={{marginBottom:15}}>
<SearchButton setUserId={setUserId}/>
</Col>
</Row>
{
Number(userId)>0
? (
<Row>
<Col md={12}>
<UserDataCard userId={userId} userData={userData}/>
</Col>
<Col md={12}>
<UserHistoryCard userId={userId} rentData={rentData}/>
</Col>
</Row>
)
: (
<Row>
<Col md={8} mdOffset={2}>
<Fallback userId={userId}/>
</Col>
</Row>
)
}
</Grid>
</div>
);
};
export default UserHistory;