From f48c1d2a2c29906334decfb95c4aef93c52e1c3c Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Wed, 25 Aug 2021 17:31:10 +0300 Subject: [PATCH] feat: ui store & updated nav --- src/App.tsx | 11 ++++-- src/components/Auth/AuthForm.tsx | 3 -- src/components/Header/Header.tsx | 3 +- src/components/Header/User/UserInfo.tsx | 4 +- .../Header/User/UserNotifications.tsx | 11 +++++- src/components/Layout/Layout.tsx | 3 +- src/components/Loading/LoadingPage.tsx | 14 +++++++ src/components/Nav/Desktop/Nav.tsx | 7 +--- src/components/Nav/Mobile/Nav.tsx | 7 +--- src/components/Nav/{Desktop => }/NavLink.tsx | 25 +++++------- src/components/Nav/NavList.tsx | 34 ++++++++++++++++ src/components/PrivateRoute/PrivateRoute.tsx | 11 ++++-- .../{Header => }/User/UserBadge.tsx | 0 src/config.ts | 11 +++--- src/helpers/index.ts | 14 +++++++ src/interfaces/index.ts | 11 ++++++ src/pages/control.tsx | 39 +++++++++++++++++++ src/pages/main.tsx | 2 +- src/stores/AuthStore.ts | 1 + src/stores/RootStore.ts | 4 ++ src/stores/UIStore.ts | 27 +++++++++++++ src/stores/UserStore.ts | 16 ++++---- 22 files changed, 203 insertions(+), 55 deletions(-) create mode 100644 src/components/Loading/LoadingPage.tsx rename src/components/Nav/{Desktop => }/NavLink.tsx (56%) create mode 100644 src/components/Nav/NavList.tsx rename src/components/{Header => }/User/UserBadge.tsx (100%) create mode 100644 src/helpers/index.ts create mode 100644 src/pages/control.tsx create mode 100644 src/stores/UIStore.ts diff --git a/src/App.tsx b/src/App.tsx index 3f411c0..6570971 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { BrowserRouter as Router, Switch } from 'react-router-dom'; +import { Router, Switch } from 'react-router-dom'; /** * Components @@ -14,17 +14,22 @@ import MainPage from 'pages/main'; import CoursesPage from 'pages/courses'; import MessagesPage from 'pages/messages'; import ProfilePage from 'pages/profile'; +import ControlPage from 'pages/control'; import AuthPage from 'pages/auth'; import NotFoundPage from 'pages/404'; +import { UserRoleEnum } from 'enums'; +import { useRootStore } from 'hooks/useRootStore'; const App = () => { + const { uiStore } = useRootStore(); return ( - + - + + diff --git a/src/components/Auth/AuthForm.tsx b/src/components/Auth/AuthForm.tsx index e1fa620..c609ec1 100644 --- a/src/components/Auth/AuthForm.tsx +++ b/src/components/Auth/AuthForm.tsx @@ -2,7 +2,6 @@ import React from 'react'; import { MdAccountCircle, MdVpnKey } from 'react-icons/md'; import { FormControl, FormLabel, InputGroup, Input, InputLeftElement, Button, Icon, useToast } from '@chakra-ui/react'; import { useRootStore } from 'hooks/useRootStore'; -import { useHistory } from 'react-router-dom'; import { observer } from 'mobx-react'; import { SubmitHandler, useForm } from 'react-hook-form'; import { useErrorHandler } from 'react-error-boundary'; @@ -15,14 +14,12 @@ type AuthInputs = { const AuthForm: React.FC = () => { const { register, reset, handleSubmit } = useForm(); const { authStore } = useRootStore(); - const history = useHistory(); const handleError = useErrorHandler(); const toast = useToast(); const onSubmit: SubmitHandler = async data => { try { await authStore.login(data.login, data.password); - history.push('/'); toast({ title: `👋 Welcome back!`, description: 'You are successfully logged in.', diff --git a/src/components/Header/Header.tsx b/src/components/Header/Header.tsx index b7b8267..77e2aa5 100644 --- a/src/components/Header/Header.tsx +++ b/src/components/Header/Header.tsx @@ -5,6 +5,7 @@ import UserInfo from './User/UserInfo'; import UserNotifications from './User/UserNotifications'; import useWindowDimensions from 'hooks/useWindowDimensions'; import NavMobile from '../Nav/Mobile/Nav'; +import { observer } from 'mobx-react'; const Header: React.FC = () => { const { isDesktop } = useWindowDimensions(); @@ -45,4 +46,4 @@ const Header: React.FC = () => { ); }; -export default Header; +export default observer(Header); diff --git a/src/components/Header/User/UserInfo.tsx b/src/components/Header/User/UserInfo.tsx index d76adff..61291e2 100644 --- a/src/components/Header/User/UserInfo.tsx +++ b/src/components/Header/User/UserInfo.tsx @@ -18,7 +18,7 @@ import { MdSettings, MdExitToApp } from 'react-icons/md'; import { observer } from 'mobx-react'; import { useRootStore } from 'hooks/useRootStore'; import { useHistory, Link } from 'react-router-dom'; -import UserBadge from 'components/Header/User/UserBadge'; +import UserBadge from 'components/User/UserBadge'; const UserInfo: React.FC = () => { const { authStore, userStore } = useRootStore(); @@ -36,7 +36,7 @@ const UserInfo: React.FC = () => { }; if (userStore.me === null) { - return ; + return ; } return ( diff --git a/src/components/Header/User/UserNotifications.tsx b/src/components/Header/User/UserNotifications.tsx index f906cb7..79d776f 100644 --- a/src/components/Header/User/UserNotifications.tsx +++ b/src/components/Header/User/UserNotifications.tsx @@ -1,9 +1,18 @@ import React from 'react'; import { Box } from '@chakra-ui/layout'; +import { Skeleton } from '@chakra-ui/skeleton'; import { Button } from '@chakra-ui/button'; import { MdNotifications } from 'react-icons/md'; +import { useRootStore } from 'hooks/useRootStore'; +import { observer } from 'mobx-react'; const UserNotifications: React.FC = () => { + const { userStore } = useRootStore(); + + if (userStore.me === null) { + return ; + } + return (