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