]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/pages/User.js
add helmet
[alttp.git] / resources / js / components / pages / User.js
1 import axios from 'axios';
2 import React, { useEffect, useState } from 'react';
3 import { Helmet } from 'react-helmet';
4 import { useParams } from 'react-router-dom';
5
6 import ErrorBoundary from '../common/ErrorBoundary';
7 import ErrorMessage from '../common/ErrorMessage';
8 import Loading from '../common/Loading';
9 import NotFound from '../pages/NotFound';
10 import Profile from '../users/Profile';
11
12 const User = () => {
13         const params = useParams();
14         const { id } = params;
15
16         const [error, setError] = useState(null);
17         const [loading, setLoading] = useState(true);
18         const [user, setUser] = useState(null);
19
20         useEffect(() => {
21                 setLoading(true);
22                 const ctrl = new AbortController();
23                 axios
24                         .get(`/api/users/${id}`, { signal: ctrl.signal })
25                         .then(response => {
26                                 setError(null);
27                                 setLoading(false);
28                                 setUser(response.data);
29                         })
30                         .catch(error => {
31                                 setError(error);
32                                 setLoading(false);
33                                 setUser(null);
34                         });
35                 return () => {
36                         ctrl.abort();
37                 };
38         }, [id]);
39
40         useEffect(() => {
41                 const cb = (e) => {
42                         if (e.user) {
43                                 setUser(user => e.user.id === user.id ? { ...user, ...e.user } : user);
44                         }
45                 };
46                 window.Echo.channel('App.Control')
47                         .listen('UserChanged', cb);
48                 return () => {
49                         window.Echo.channel('App.Control')
50                                 .stopListening('UserChanged', cb);
51                 };
52         }, []);
53
54         if (loading) {
55                 return <Loading />;
56         }
57
58         if (error) {
59                 return <ErrorMessage error={error} />;
60         }
61
62         if (!user) {
63                 return <NotFound />;
64         }
65
66         return <ErrorBoundary>
67                 <Helmet>
68                         <title>{user.nickname || user.username}</title>
69                 </Helmet>
70                 <Profile user={user} />
71         </ErrorBoundary>;
72 };
73
74 export default User;