aboutsummaryrefslogtreecommitdiff
path: root/pages/user.tsx
blob: 97412756e6d106d5b03b1b95dbe6fc7065116f16 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import Icon from '@mdi/react';
import axios from 'axios';
import { ReactNode, useContext, useEffect, useState } from 'react';

import { userGames, userInfo } from '../api/api';
import { AccountAvatar } from '../components/account';
import { Footer } from '../components/footer';
import { NavBar } from '../components/navbar';
import { CenteredPage, PageTitle } from '../components/page';
import RecentGames from '../components/recentGames';
import { SocketContext } from '../components/socketContext';
import { ToastContext } from '../components/toast';
import { IconLabelButton, Vierkant } from '../components/ui';

import ArrowDownwardOutlinedIcon from '@material-ui/icons/ArrowDownwardOutlined';
import ArrowUpwardOutlinedIcon from '@material-ui/icons/ArrowUpwardOutlined';
import AssignmentIndOutlinedIcon from '@material-ui/icons/AssignmentIndOutlined';
import DoneOutlinedIcon from '@material-ui/icons/DoneOutlined';
import EditOutlinedIcon from '@material-ui/icons/EditOutlined';
import PeopleOutlineOutlinedIcon from '@material-ui/icons/PeopleOutlineOutlined';
import PersonAddOutlinedIcon from '@material-ui/icons/PersonAddOutlined';
import SettingsOutlinedIcon from '@material-ui/icons/SettingsOutlined';
import {
	mdiAccountCancelOutline,
	mdiAccountMinusOutline,
	mdiAccountRemoveOutline,
	mdiCheckboxBlankCircle,
	mdiClipboardTextOutline,
	mdiEarth,
	mdiEqual,
	mdiGamepadSquareOutline,
} from '@mdi/js';

function InfoModule(props: {
	label: string;
	icon: ReactNode;
}) {
	return <div className='infoModule posrel'>
		<div className='iconWrapper posabs'>
			{props.icon}
		</div>
		<div className='labelWrapper posabs h0 b0'>
			<span className='label posabs center fullwidth'>
				{props.label}
			</span>
		</div>
	</div>;
}

export default function AccountPage() {
	var server = typeof window === 'undefined';
	var loggedIn = !server && document.cookie.includes('token');
	var pageID = server ? '' : new URLSearchParams(window.location.search).get('id');

	if (!loggedIn && !pageID) !server && window.history.go(-1);
	var reqData = loggedIn && pageID ? { 'id': pageID } : undefined;

	var [user, setUser] = useState<userInfo>();
	var [gameInfo, setGameInfo] = useState<userGames>();
	var [editingStatus, setEditingStatus] = useState(false);
	var [relation, setRelation] = useState<userInfo['relation']>('none');
	var [ownPage, setOwnPage] = useState(loggedIn && !pageID);

	var { toast } = useContext(ToastContext);
	var { io } = useContext(SocketContext);

	async function getUserData(): Promise<userInfo> {
		var userReq = await axios.request<userInfo>({
			method: 'post',
			url: `/api/user/info`,
			headers: { 'content-type': 'application/json' },
			data: reqData,
		});
		setUser(userReq.data);
		return userReq.data;
	}

	async function getRelationTo(user: userInfo) {
		var user = await getUserData();
		setRelation(user.relation || 'none');
	}

	function setIOListeners(user: userInfo) {
		io.on('changedRelation', (data: { id: string; }) => {
			if (data.id != user.id) return;
			getRelationTo(user);
		});
		io.on('incomingFriendRequest', getRelationTo);
	}

	useEffect(() => {
		(async () => {
			var user = await getUserData();

			getRelationTo(user);
			setIOListeners(user);
		})();
	}, []);

	useEffect(() => {
		(async () => {
			var userReq = await axios.request<userInfo>({
				method: 'post',
				url: `/api/user/info`,
				headers: { 'content-type': 'application/json' },
			});
			setOwnPage(ownPage || userReq.data.id == pageID);
		})();
	}, []);

	// Get recent games
	useEffect(() => {
		(async () => {
			var userGamesReq = await axios.request<userGames>({
				method: 'post',
				url: `/api/user/games`,
				headers: { 'content-type': 'application/json' },
				data: reqData,
			});
			setGameInfo(userGamesReq.data);
		})();
	}, []);

	return <div>
		<NavBar />
		<CenteredPage width={802}>
			<PageTitle>Profiel</PageTitle>
			<Vierkant className='accountHeader w100m2m pad-l bg-800'>
				<div className='inner posrel'>
					<AccountAvatar size={128} id={user?.id || ''} />
					<div className='userInfo dispinbl valigntop'>
						<h2 className='username'>{user?.username}</h2>
						<p
							id='status'
							className='status round-t'
							contentEditable={editingStatus ? 'true' : 'false'}
							suppressContentEditableWarning={true}
						>
							{user?.status}
						</p>
					</div>
					<div className='posabs b0 r0'>
						{loggedIn && <div>
							{ownPage
								? <div>
									<IconLabelButton
										icon={<SettingsOutlinedIcon />}
										href='/settings'
										text='Instellingen'
									/>
									{!editingStatus
										? <IconLabelButton
											icon={<EditOutlinedIcon />}
											text='Status bewerken'
											onclick={() => setEditingStatus(true)}
										/>
										: <IconLabelButton
											icon={<DoneOutlinedIcon />}
											text='Status opslaan'
											onclick={() => {
												setEditingStatus(false);
												axios.request({
													method: 'post',
													url: `/api/user/status`,
													headers: { 'content-type': 'application/json' },
													data: { 'status': document.getElementById('status').innerText },
												});
											}}
										/>}
								</div>
								: <div>
									{(() => {
										var icon = {
											'blocked': <Icon size={1} path={mdiAccountCancelOutline} />,
										}[relation] || <Icon size={1} path={mdiAccountCancelOutline} />;

										var text = {
											'blocked': 'Deblokkeren',
										}[relation] || 'Blokkeren';

										return <IconLabelButton
											icon={icon}
											text={text}
											onclick={() => {
												var nextRelation = {
													'blocked': {
														'endpoint': '/api/social/unblock',
														'action': `${user.username} gedeblokkeerd`,
														'relation': 'none',
														'icon': <Icon size={1} path={mdiAccountCancelOutline} />,
													},
												}[relation] || {
													'endpoint': '/api/social/block',
													'action': `${user.username} geblokkeerd`,
													'relation': 'blocked',
													'icon': <Icon size={1} path={mdiAccountCancelOutline} />,
												};

												axios.request({
													method: 'post',
													url: nextRelation.endpoint,
													headers: { 'content-type': 'application/json' },
													data: { 'id': user?.id },
												})
													.then(() => {
														toast({
															message: nextRelation.action,
															type: 'confirmation',
															icon: nextRelation.icon,
														});
														setRelation(nextRelation.relation);
													});
											}}
										/>;
									})()}
									{(() => {
										var icon = {
											'friends': <Icon size={1} path={mdiAccountMinusOutline} />,
											'outgoing': <Icon size={1} path={mdiAccountRemoveOutline} />,
											'incoming': <PersonAddOutlinedIcon />,
										}[relation] || <PersonAddOutlinedIcon />;

										var text = {
											'friends': 'Vriend verwijderen',
											'outgoing': 'Vriendschapsverzoek annuleren',
											'incoming': 'Vriendschapsverzoek accepteren',
										}[relation] || 'Vriendschapsverzoek sturen';

										return <IconLabelButton
											icon={icon}
											text={text}
											onclick={() => {
												var nextRelation = {
													'friends': {
														'endpoint': '/api/social/remove',
														'action': `${user.username} succesvol verwijderd als vriend`,
														'relation': 'none',
														'icon': <Icon size={1} path={mdiAccountMinusOutline} />,
													},
													'outgoing': {
														'endpoint': '/api/social/remove',
														'action':
															`Vriendschapsverzoek naar ${user.username} geannuleerd`,
														'relation': 'none',
														'icon': <Icon size={1} path={mdiAccountMinusOutline} />,
													},
													'incoming': {
														'endpoint': '/api/social/accept',
														'action':
															`Vriendschapsverzoek van ${user.username} geaccepteerd`,
														'relation': 'friends',
														'icon': <PersonAddOutlinedIcon />,
													},
												}[relation] || {
													'endpoint': '/api/social/request',
													'action': `Vriendschapsverzoek gestuurd naar ${user.username}`,
													'relation': 'outgoing',
													'icon': <PersonAddOutlinedIcon />,
												};

												axios.request({
													method: 'post',
													url: nextRelation.endpoint,
													headers: { 'content-type': 'application/json' },
													data: { 'id': user?.id },
												})
													.then(() => {
														toast({
															message: nextRelation.action,
															type: 'confirmation',
															icon: nextRelation.icon,
														});
														setRelation(nextRelation.relation);
													});
											}}
										/>;
									})()}
								</div>}
						</div>}
					</div>
				</div>
			</Vierkant>
			<Vierkant className='infosection pad-l w100m2m bg-800'>
				<div className='inner sidebyside'>
					<InfoModule
						icon={<Icon size={1} path={mdiCheckboxBlankCircle} className='outcome win' />}
						label='Online'
					/>
					<InfoModule
						icon={<AssignmentIndOutlinedIcon />}
						label={(() => {
							var memberSince = 'Lid sinds';

							var registered = new Date(user?.registered);
							memberSince += ' ' + registered.toLocaleString('nl-nl', { month: 'long', day: 'numeric' });

							var currentYear = new Date().getFullYear();
							var memberYear = registered.getFullYear();
							if (currentYear != memberYear) memberSince += ' ' + memberYear;

							return memberSince;
						})()}
					/>
					<InfoModule
						icon={<PeopleOutlineOutlinedIcon />}
						label={(() => {
							var label = user?.friends.toString() + ' ';
							label += user?.friends == 1 ? 'vriend' : 'vrienden';
							return label;
						})()}
					/>
					<InfoModule icon={<Icon size={1} path={mdiEarth} />} label='Nederland' />
				</div>
			</Vierkant>
			<Vierkant className='infosection pad-l w100m2m sidebyside bg-800'>
				<div className='inner sidebyside'>
					<InfoModule
						icon={<ArrowUpwardOutlinedIcon className='outcome win' />}
						label={gameInfo?.totals.win + ' keer gewonnen'}
					/>
					<InfoModule
						icon={<Icon size={1} path={mdiEqual} className='subtile' />}
						label={gameInfo?.totals.draw + ' keer gelijkspel'}
					/>
					<InfoModule
						icon={<ArrowDownwardOutlinedIcon className='outcome lose' />}
						label={gameInfo?.totals.lose + ' keer verloren'}
					/>
					<InfoModule
						icon={<Icon size={1} path={mdiClipboardTextOutline} />}
						label={'Score: ' + user?.rating}
					/>
					<InfoModule
						icon={<Icon size={1} path={mdiGamepadSquareOutline} />}
						label={(() => {
							var label = gameInfo?.totals.games.toString() + ' ';
							label += gameInfo?.totals.games == 1 ? 'potje' : 'potjes';
							return label;
						})()}
					/>
				</div>
			</Vierkant>
			<Vierkant className='pad-l bg-800'>
				<RecentGames games={gameInfo?.games} />
			</Vierkant>
		</CenteredPage>
		<Footer />
	</div>;
}