Implement logout and workaround an issue with redux-persist not persisting.

For some reason it doesn't persist when credentials === undefined.
master
Tom Hacohen 7 years ago
parent 94c6916447
commit 3c3ffb15d3

@ -1,4 +1,5 @@
import * as React from 'react'; import * as React from 'react';
import { connect } from 'react-redux';
import { HashRouter, NavLink } from 'react-router-dom'; import { HashRouter, NavLink } from 'react-router-dom';
import getMuiTheme from 'material-ui/styles/getMuiTheme'; import getMuiTheme from 'material-ui/styles/getMuiTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'; import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
@ -13,6 +14,7 @@ import ActionCode from 'material-ui/svg-icons/action/code';
import ActionHome from 'material-ui/svg-icons/action/home'; import ActionHome from 'material-ui/svg-icons/action/home';
import ActionBugReport from 'material-ui/svg-icons/action/bug-report'; import ActionBugReport from 'material-ui/svg-icons/action/bug-report';
import ActionQuestionAnswer from 'material-ui/svg-icons/action/question-answer'; import ActionQuestionAnswer from 'material-ui/svg-icons/action/question-answer';
import LogoutIcon from 'material-ui/svg-icons/action/power-settings-new';
import NavigationMenu from 'material-ui/svg-icons/navigation/menu'; import NavigationMenu from 'material-ui/svg-icons/navigation/menu';
@ -22,6 +24,7 @@ import { EteSyncContext } from './EteSyncContext';
import { RouteResolver } from './routes'; import { RouteResolver } from './routes';
import * as C from './Constants'; import * as C from './Constants';
import * as store from './store';
const logo = require('./images/logo.svg'); const logo = require('./images/logo.svg');
@ -71,12 +74,17 @@ class App extends React.Component {
drawerOpen: boolean, drawerOpen: boolean,
}; };
props: {
credentials?: store.CredentialsData;
};
constructor(props: any) { constructor(props: any) {
super(props); super(props);
this.state = { drawerOpen: false }; this.state = { drawerOpen: false };
this.toggleDrawer = this.toggleDrawer.bind(this); this.toggleDrawer = this.toggleDrawer.bind(this);
this.closeDrawer = this.closeDrawer.bind(this); this.closeDrawer = this.closeDrawer.bind(this);
this.logout = this.logout.bind(this);
} }
toggleDrawer() { toggleDrawer() {
@ -87,6 +95,11 @@ class App extends React.Component {
this.setState({drawerOpen: false}); this.setState({drawerOpen: false});
} }
logout() {
store.store.dispatch(store.logout());
this.closeDrawer();
}
render() { render() {
return ( return (
<MuiThemeProvider muiTheme={muiTheme}> <MuiThemeProvider muiTheme={muiTheme}>
@ -115,6 +128,7 @@ class App extends React.Component {
> >
<ListItem primaryText="Main" leftIcon={<ActionHome />} onClick={this.closeDrawer} /> <ListItem primaryText="Main" leftIcon={<ActionHome />} onClick={this.closeDrawer} />
</NavLink> </NavLink>
<ListItem primaryText="Log Out" leftIcon={<LogoutIcon/>} onClick={this.logout} />
<Divider /> <Divider />
<Subheader>External Links</Subheader> <Subheader>External Links</Subheader>
<ListItem primaryText="Website" leftIcon={<ActionHome />} href={C.homePage} /> <ListItem primaryText="Website" leftIcon={<ActionHome />} href={C.homePage} />
@ -132,4 +146,12 @@ class App extends React.Component {
} }
} }
export default App; const mapStateToProps = (state: store.StoreState) => {
return {
credentials: state.credentials,
};
};
export default connect(
mapStateToProps
)(App);

@ -125,7 +125,7 @@ export class EteSyncContextInner extends React.Component {
render() { render() {
if (((this.props.credentials.status === store.FetchStatus.Initial) && if (((this.props.credentials.status === store.FetchStatus.Initial) &&
(this.props.credentials.credentials === undefined)) || (this.props.credentials.credentials === null)) ||
(this.props.credentials.status === store.FetchStatus.Failure)) { (this.props.credentials.status === store.FetchStatus.Failure)) {
let advancedSettings = null; let advancedSettings = null;

@ -27,8 +27,8 @@ export interface CredentialsData {
export interface CredentialsType { export interface CredentialsType {
status: FetchStatus; status: FetchStatus;
credentials: CredentialsData | null;
error?: Error; error?: Error;
credentials?: CredentialsData;
} }
export interface StoreState { export interface StoreState {
@ -59,17 +59,33 @@ export function credentialsFailure(error: Error) {
}; };
} }
function credentials(state: CredentialsType = {status: FetchStatus.Initial}, action: any) { export function logout() {
return {
type: Actions.FETCH_CREDENTIALS,
status: FetchStatus.Initial,
};
}
function credentials(state: CredentialsType = {status: FetchStatus.Initial, credentials: null},
action: any): CredentialsType {
switch (action.type) { switch (action.type) {
case Actions.FETCH_CREDENTIALS: case Actions.FETCH_CREDENTIALS:
if (action.status === FetchStatus.Success) { switch (action.status) {
case FetchStatus.Success:
return { return {
status: action.status, status: action.status,
credentials: action.credentials, credentials: action.credentials,
}; };
} else { case FetchStatus.Failure:
return { return {
status: action.status, status: action.status,
credentials: null,
error: action.error,
};
default:
return {
status: action.status,
credentials: null,
}; };
} }
default: default:
@ -81,10 +97,14 @@ function fetchCount(state: number = 0, action: any) {
// FIXME: Make it automatic by action properties. // FIXME: Make it automatic by action properties.
switch (action.type) { switch (action.type) {
case Actions.FETCH_CREDENTIALS: case Actions.FETCH_CREDENTIALS:
if (action.status === FetchStatus.Request) { switch (action.status) {
case FetchStatus.Request:
return state + 1; return state + 1;
} else { case FetchStatus.Success:
case FetchStatus.Failure:
return state - 1; return state - 1;
default:
return state;
} }
default: default:
return state; return state;

Loading…
Cancel
Save