]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/components/pages/User.js
allow users to set their stream link
[alttp.git] / resources / js / components / pages / User.js
diff --git a/resources/js/components/pages/User.js b/resources/js/components/pages/User.js
new file mode 100644 (file)
index 0000000..8dfdba4
--- /dev/null
@@ -0,0 +1,66 @@
+import axios from 'axios';
+import React, { useEffect, useState } from 'react';
+import { useParams } from 'react-router-dom';
+
+import ErrorBoundary from '../common/ErrorBoundary';
+import ErrorMessage from '../common/ErrorMessage';
+import Loading from '../common/Loading';
+import NotFound from '../pages/NotFound';
+import Profile from '../users/Profile';
+
+const User = () => {
+       const params = useParams();
+       const { id } = params;
+
+       const [error, setError] = useState(null);
+       const [loading, setLoading] = useState(true);
+       const [user, setUser] = useState(null);
+
+       useEffect(() => {
+               setLoading(true);
+               axios
+                       .get(`/api/users/${id}`)
+                       .then(response => {
+                               setError(null);
+                               setLoading(false);
+                               setUser(response.data);
+                       })
+                       .catch(error => {
+                               setError(error);
+                               setLoading(false);
+                               setUser(null);
+                       });
+       }, [id]);
+
+       useEffect(() => {
+               const cb = (e) => {
+                       if (e.user) {
+                               setUser(user => e.user.id === user.id ? { ...user, ...e.user } : user);
+                       }
+               };
+               window.Echo.channel('App.Control')
+                       .listen('UserChanged', cb);
+               return () => {
+                       window.Echo.channel('App.Control')
+                               .stopListening('UserChanged', cb);
+               };
+       }, []);
+
+       if (loading) {
+               return <Loading />;
+       }
+
+       if (error) {
+               return <ErrorMessage error={error} />;
+       }
+
+       if (!user) {
+               return <NotFound />;
+       }
+
+       return <ErrorBoundary>
+               <Profile user={user} />
+       </ErrorBoundary>;
+};
+
+export default User;