Skip to content

Commit

Permalink
[#8] added mobx & user saved in mobx store
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Kyrylenko committed Nov 23, 2020
1 parent 3c1fa85 commit a40ae75
Show file tree
Hide file tree
Showing 8 changed files with 1,627 additions and 16,555 deletions.
15,095 changes: 0 additions & 15,095 deletions SafeCity/ClientApp/package-lock.json

This file was deleted.

1 change: 1 addition & 0 deletions SafeCity/ClientApp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"bootstrap": "^4.5.3",
"mobx": "^6.0.4",
"react": "^16.13.1",
"react-bootstrap": "^1.3.0",
"react-dom": "^16.13.1",
Expand Down
3 changes: 2 additions & 1 deletion SafeCity/ClientApp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import Experts from './pages/Experts';
import Report from './pages/Report';
import About from './pages/About';
import NotFound from './pages/NotFound';
import authStore from './mobx/authStore';

import 'bootstrap/dist/css/bootstrap.css';
import './main.css';
import './app.css';

function App() {
return (
<Layout>
<Layout authStore={authStore}>
<Switch>
<Route exact path='/' component={Home} />
<Route exact path='/projects/:id' component={Project} />
Expand Down
10 changes: 9 additions & 1 deletion SafeCity/ClientApp/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import { BrowserRouter } from 'react-router-dom';
import './index.css';
import App from './App';
import auth from './services/auth';
import usersService from './services/usersService';
import * as serviceWorker from './serviceWorker';
import authStore from './mobx/authStore';

auth.load(updateSigninStatus);

function updateSigninStatus(isSignedIn: boolean) {
console.log(`app loaded, signed in: ${isSignedIn} token: ${auth.getToken()}`);
if (isSignedIn) {
usersService.getMe()
.then(me => authStore.setUser(me))
.catch(err => console.log('getMe err ', err));
} else {
authStore.setUser(null);
}
}

const rootElement = document.getElementById('root');
Expand Down
14 changes: 14 additions & 0 deletions SafeCity/ClientApp/src/mobx/authStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import { observable, configure } from 'mobx';

configure({enforceActions: 'observed'});

const authStore = observable({
user: null,

setUser(u?: any) {
this.user = u
},
});

export default authStore;
23 changes: 16 additions & 7 deletions SafeCity/ClientApp/src/pages/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import React from 'react';
import { observer } from 'mobx-react-lite';
import { NavLink } from 'react-router-dom';
import Navbar from 'react-bootstrap/Navbar';
import Nav from 'react-bootstrap/Nav';
import auth from '../services/auth';

function Layout(props: any) {
const Layout = observer(({ children, authStore }: { children: any, authStore: any }) => {

const handleLogin = () => {
if (authStore.user) {
auth.signOut()
} else {
auth.signIn()
}
};

return (
<>
<header>
Expand All @@ -25,15 +35,12 @@ function Layout(props: any) {
<li className='nav-item mx-3'>
<Nav.Link as={NavLink} to='/about'>Про нас</Nav.Link>
</li>
<li>
<button onClick={auth.signIn}>login</button>
</li>
</Nav>
</Navbar.Collapse>
</Navbar>
</header>
<main>
{props.children}
{children}
</main>
<footer className='mt-auto'>
<div className='container'>
Expand All @@ -43,12 +50,14 @@ function Layout(props: any) {
<br />
тел: <a href='tel:+380959242349'>+3 8095 9242349</a>
</div>
<div className='col-md-8'></div>
<div className='col-md-8 text-right'>
<button onClick={handleLogin} className='btn btn-link'>{authStore.user ? 'log out' : 'log in'}</button>
</div>
</div>
</div>
</footer>
</>
);
}
})

export default Layout;
11 changes: 11 additions & 0 deletions SafeCity/ClientApp/src/services/usersService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { get } from './crud';

const baseUrl = '/api/v1/auth';

async function getMe(): Promise<any> {
return await get(`${baseUrl}`);
};

export default {
getMe: getMe,
}
Loading

0 comments on commit a40ae75

Please sign in to comment.