Skip to content

[WIP] generate LoginPage container and LoginForm component #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/containers/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Switch, Route } from 'react-router-dom';

import HomePage from 'containers/HomePage/Loadable';
import NotFoundPage from 'containers/NotFoundPage/Loadable';
import LoginPage from 'containers/LoginPage/Loadable';

import '../../global-styles.scss';

Expand All @@ -20,6 +21,7 @@ export default function App() {
<div>
<Switch>
<Route exact path="/" component={HomePage} />
<Route exact path="/login" component={LoginPage} />
<Route component={NotFoundPage} />
</Switch>
</div>
Expand Down
6 changes: 6 additions & 0 deletions app/containers/LoginPage/Loadable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Asynchronously loads the component for LoginPage
*/
import loadable from 'loadable-components';

export default loadable(() => import('./index'));
14 changes: 14 additions & 0 deletions app/containers/LoginPage/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
*
* LoginPage actions
*
*/

import { AUTHENTICATION } from './constants';

export function login(credentials) {
console.log({ credentials });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to remove all console.log

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course I will that's why I labeled my PR [WIP] (work in progress).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahaa, sorry didn't notice that.

return {
type: AUTHENTICATION.LOGIN_REQUEST,
};
}
11 changes: 11 additions & 0 deletions app/containers/LoginPage/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
*
* LoginPage constants
*
*/

export const AUTHENTICATION = {
LOGIN_REQUEST: 'USER_LOGIN_REQUEST',
LOGIN_SUCCESS: 'USER_LOGIN_SUCCESS',
LOGIN_FAILURE: 'USER_LOGIN_FAILURE',
};
70 changes: 70 additions & 0 deletions app/containers/LoginPage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';

import { login } from './actions';

/* eslint-disable react/prefer-stateless-function */
class LoginPage extends React.PureComponent {
static propTypes = {
login: PropTypes.func.isRequired,
};

state = {
username: '',
password: '',
};

handleSubmit = event => {
event.preventDefault();
this.props.login(this.state);
};

handleChange = event => {
this.setState({ [event.target.name]: event.target.value });
};

render() {
const { username, password } = this.state;

return (
<form className="login-form" onSubmit={this.handleSubmit}>
<div className="login-form__field">
<label htmlFor="username">Username</label>
<input
type="text"
id="username"
name="username"
placeholder="Username"
value={username}
onChange={this.handleChange}
required
/>
</div>
<div className="login-form__field">
<label htmlFor="password">Password</label>
<input
type="password"
id="password"
name="password"
placeholder="Password"
value={password}
onChange={this.handleChange}
required
/>
</div>
<button type="submit">Login</button>
</form>
);
}
}

const mapStateToProps = state => ({
username: state.username,
password: state.password,
});

export default connect(
mapStateToProps,
{ login },
)(LoginPage);
25 changes: 25 additions & 0 deletions app/containers/LoginPage/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
*
* LoginPage reducer
*
*/

import { fromJS } from 'immutable';

import { AUTHENTICATION } from './constants';
import { retrieveFromLocalStorage } from '../../utils/localStorage';

export const initialState = fromJS({
isAuthenticated: !!retrieveFromLocalStorage('xSessionToken'),
});

function loginPageReducer(state = initialState, action) {
switch (action.type) {
case AUTHENTICATION.LOGIN_REQUEST:
return state.set('isAuthenticated', false);
default:
return state;
}
}

export default loginPageReducer;
39 changes: 39 additions & 0 deletions app/containers/LoginPage/tests/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import { mount } from 'enzyme';
import { browserHistory } from 'react-router-dom';
import { Provider } from 'react-redux';

import LoginPage from 'containers/LoginPage';

import configureStore from '../../../configureStore';

describe('<LoginPage />', () => {
let mountedLoginPage;

let store;

beforeAll(() => {
store = configureStore({}, browserHistory);
});

beforeEach(() => {
mountedLoginPage = mount(
<Provider store={store}>
<LoginPage />
</Provider>,
);
});

it('should render without crashing', () => {
mount(
<Provider store={store}>
<LoginPage />
</Provider>,
);
});

it('should renders its children ', () => {
const children = mountedLoginPage.find('form.login-form');
expect(children.length).toBe(1);
});
});
11 changes: 11 additions & 0 deletions app/utils/localStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function storeInLocalStorage({ label, value }) {
localStorage.setItem(label, value);
}

export function retrieveFromLocalStorage(item) {
return localStorage.getItem(item);
}

export function removeFromLocalStorage(item) {
return localStorage.removeItem(item);
}