Skip to content

Commit

Permalink
refactored login handle for first-time enter in the website (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
lyudmylatodoriko committed Jul 16, 2020
1 parent 00af6be commit c44c651
Show file tree
Hide file tree
Showing 9 changed files with 1,777 additions and 195 deletions.
3 changes: 2 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"jest-dom": "^4.0.0",
"joi-browser": "^13.4.0",
"prettier": "^1.19.1",
"react-test-render": "^1.1.2"
"react-test-render": "^1.1.2",
"webpack": "4.42.0"
}
}
77 changes: 9 additions & 68 deletions client/src/App.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,21 @@
import React from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import { baseUrl, uriClusters, uriCurrentUser } from './utils/endpoints';
import { baseUrl } from './utils/endpoints';
import Routes from './utils/Routes';
import history from './utils/history';
import api, { get } from './utils/api';
import Loading from '../src/containers/Loading';
import { MuiPickersUtilsProvider } from '@material-ui/pickers';
import MomentUtils from '@date-io/moment';
import { ToastContainer } from 'react-toastify';
import { organizeRoles } from './utils/converters';

class App extends React.Component {
state = {
clusterId: '',
clusters: []
};

componentDidMount() {
if (
history.location.pathname !== '/ui/login' &&
history.location.pathname !== '/ui/page-not-found'
) {
api.get(uriClusters()).then(res => {
this.setState({ clusters: res.data, clusterId: res.data ? res.data[0].id : '' }, () => {
history.replace({
loading: false
});
});
});
}
}

getCurrentUser(callback = () => {}) {
get(uriCurrentUser())
.then(res => {
let currentUserData = res.data;
if (currentUserData.logged) {
sessionStorage.setItem('login', true);
sessionStorage.setItem('user', currentUserData.username);
sessionStorage.setItem('roles', organizeRoles(currentUserData.roles));
} else {
sessionStorage.setItem('login', false);
sessionStorage.setItem('user', 'default');
if (currentUserData.roles) {
sessionStorage.setItem('roles', organizeRoles(currentUserData.roles));
} else {
sessionStorage.setItem('roles', JSON.stringify({}));
}
}
callback();
})
.catch(err => {
console.error('Error:', err);
});
}

render() {
const { clusterId, clusters } = this.state;
if (clusterId) {
this.getCurrentUser();
return (
<MuiPickersUtilsProvider utils={MomentUtils}>
<Router>
<Routes clusters={clusters} clusterId={clusterId} location={baseUrl} />
<ToastContainer draggable={false} closeOnClick={false} />
</Router>
</MuiPickersUtilsProvider>
);
} else {
return (
<div>
<Loading show="true" />
<ToastContainer draggable={false} />
</div>
);
}
return (
<MuiPickersUtilsProvider utils={MomentUtils}>
<Router>
<Routes location={baseUrl} />
<ToastContainer draggable={false} closeOnClick={false} />
</Router>
</MuiPickersUtilsProvider>
);
}
}

Expand Down
6 changes: 3 additions & 3 deletions client/src/components/Base/Base.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class Base extends Component {
const topicId = nextProps.match.params.topicId;
const action = nextProps.match.params.action;
const { loading, tab } = nextProps.location;

return {
topicId: topicId,
clusterId: clusterId,
Expand Down Expand Up @@ -65,13 +64,14 @@ class Base extends Component {
render() {
const { children, clusters } = this.props;
const { loading, selectedTab, expanded } = this.state;
return (
return (
<>
<Helmet title={this.handleTitle()} />
<Loading show={loading} />
{this.props.location.pathname !== '/ui/login' &&
this.props.location.pathname !== '/ui/page-not-found' && (
<Sidebar clusters={clusters}
<Sidebar
clusters={clusters}
expanded={expanded}
toggleSidebar={newExpanded => {
this.setState({ expanded: newExpanded });
Expand Down
1 change: 0 additions & 1 deletion client/src/containers/Header/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class Header extends Component {
pathname: '/ui/login',
...this.props.history
});
window.location.reload(false);
toast.success('Logged out successfully');
});
});
Expand Down
2 changes: 0 additions & 2 deletions client/src/containers/Login/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import 'react-toastify/dist/ReactToastify.css';

class Login extends Form {
state = {
clusterId: '',
formData: {
username: '',
password: ''
Expand Down Expand Up @@ -76,7 +75,6 @@ class Login extends Form {
pathname: '/ui',
loading: false
});
window.location.reload(true);
toast.success(`User '${currentUserData.username}' logged in successfully`);
} else {
this.props.history.replace({
Expand Down

0 comments on commit c44c651

Please sign in to comment.