-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserStats.tsx
113 lines (103 loc) · 4.2 KB
/
UserStats.tsx
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import * as React from 'react';
import {Link, useLocation} from 'react-router-dom';
import {fetchUserStatsByUserId} from '../../services/usersService';
import {UserStats} from '../../domain/UserStats.js';
import {ProblemModel} from '../../services/media/ProblemModel.js';
import {isSuccessful} from '../utils/responseData';
import {UserStatsOutput} from '../../services/models/users/UserStatsOutputModel';
import {webRoutes} from '../../App';
type State = { tag: 'loading' } | { tag: 'loaded'; data: UserStats } | { tag: 'error'; message: string };
type Action = { type: 'load' } | { type: 'success'; data: UserStats } | { type: 'error'; message: string };
function reducer(state: State, action: Action): State {
switch (state.tag) {
case 'loaded':
if (action.type === 'load') {
return {tag: 'loading'};
}
return state;
case 'loading':
if (action.type === 'success') {
return {tag: 'loaded', data: action.data};
} else if (action.type === 'error') {
return {tag: 'error', message: action.message};
}
return state;
case 'error':
if (action.type === 'load') {
return {tag: 'loading'};
}
return state;
}
}
export function UserStats() {
const [state, dispatch] = React.useReducer(reducer, {tag: 'loading'});
const location = useLocation();
const userId = location.pathname.split('/')[2];
React.useEffect(() => {
dispatch({type: 'load'});
fetchUserStatsByUserId(userId)
.then(result => {
console.log('result: ' + JSON.stringify(result));
if (!isSuccessful(result.contentType)) {
const errorData = result.json as ProblemModel;
dispatch({type: 'error', message: errorData.detail});
} else {
const successData = result.json as UserStatsOutput;
const properties = successData.properties as UserStats;
dispatch({type: 'success', data: properties});
}
})
.catch((err: { message: string }) => {
dispatch({type: 'error', message: err.message});
});
}, [userId]);
switch (state.tag) {
case 'loaded':
return (
<div>
<div>
<h2>UserStats</h2>
<table>
<tbody>
<tr>
<td>Rank</td>
<td>{state.data.rank.value}</td>
</tr>
<tr>
<td>Username</td>
<td>{state.data.username.value}</td>
</tr>
<tr>
<td>Points</td>
<td>{state.data.points.value}</td>
</tr>
<tr>
<td>Games Played</td>
<td>{state.data.gamesPlayed.value}</td>
</tr>
<tr>
<td>Games Won</td>
<td>{state.data.wins.value}</td>
</tr>
<tr>
<td>Games Lost</td>
<td>{state.data.losses.value}</td>
</tr>
<tr>
<td>Games Drawn</td>
<td>{state.data.draws.value}</td>
</tr>
</tbody>
</table>
<Link to={webRoutes.rankings}>
<button>Back to Rankings</button>
</Link>
</div>
</div>
);
case 'loading':
return <div>Loading...</div>;
case 'error':
return <div>Error: {state.message}</div>;
}
}