Remove user and inProgressState from the Auth store

- user: will be changed to something else later anyways
 - inProgressState: not used anywhere
This commit is contained in:
Kimmo Puputti 2017-02-20 15:08:12 +02:00
parent bbddb7ae27
commit 0e8fd878aa
3 changed files with 41 additions and 64 deletions

View file

@ -11,7 +11,7 @@ const House = () => <span dangerouslySetInnerHTML={{ __html: '&#127968;' }} />;
/* eslint-enable react/no-danger */
const Topbar = props => {
const { isAuthenticated, onLogout, user, push: historyPush } = props;
const { isAuthenticated, onLogout, push: historyPush } = props;
const hamburger = { dangerouslySetInnerHTML: { __html: '&#127828;' } };
const handleChange = e => {
@ -52,11 +52,7 @@ const Topbar = props => {
</div>
<div className={css.user}>
{isAuthenticated
? <div>
Logged in as{' '}
{user.email}
<button className={css.logoutButton} onClick={handleLogout}>Logout</button>
</div>
? <button className={css.logoutButton} onClick={handleLogout}>Logout</button>
: <NamedLink name="LogInPage">Login</NamedLink>}
</div>
</div>
@ -65,20 +61,16 @@ const Topbar = props => {
Topbar.defaultProps = { user: null };
const { bool, object, func } = PropTypes;
const { bool, func } = PropTypes;
Topbar.propTypes = {
isAuthenticated: bool.isRequired,
user: object,
onLogout: func.isRequired,
// history.push prop from withRouter
push: func.isRequired,
};
const mapStateToProps = state => ({
isAuthenticated: state.Auth.isAuthenticated,
user: state.Auth.user,
});
const mapStateToProps = state => ({ isAuthenticated: state.Auth.isAuthenticated });
const mapDispatchToProps = dispatch => ({ onLogout: historyPush => dispatch(logout(historyPush)) });

View file

@ -16,37 +16,23 @@ export const LOGOUT_ERROR = 'app/Auth/LOGOUT_ERROR';
// ================ Reducer ================ //
export const NOTHING_IN_PROGRESS = 'NOTHING_IN_PROGRESS';
export const LOGIN_IN_PROGRESS = 'LOGIN_IN_PROGRESS';
export const LOGOUT_IN_PROGRESS = 'LOGOUT_IN_PROGRESS';
const initialState = {
isAuthenticated: false,
user: null,
inProgressState: NOTHING_IN_PROGRESS,
error: null,
};
const initialState = { isAuthenticated: false, error: null };
export default function reducer(state = initialState, action = {}) {
const { type, payload } = action;
switch (type) {
case LOGIN_REQUEST:
return { ...state, inProgressState: LOGIN_IN_PROGRESS, error: null };
return { ...state, error: null };
case LOGIN_SUCCESS:
return {
...state,
inProgressState: NOTHING_IN_PROGRESS,
isAuthenticated: true,
user: payload,
};
return { ...state, isAuthenticated: true };
case LOGIN_ERROR:
return { ...state, inProgressState: NOTHING_IN_PROGRESS, error: payload };
return { ...state, error: payload };
case LOGOUT_REQUEST:
return { ...state, inProgressState: LOGOUT_IN_PROGRESS, error: null };
return { ...state, error: null };
case LOGOUT_SUCCESS:
return { ...state, inProgressState: NOTHING_IN_PROGRESS, isAuthenticated: false, user: null };
return { ...state, isAuthenticated: false };
case LOGOUT_ERROR:
return { ...state, inProgressState: NOTHING_IN_PROGRESS, error: payload };
return { ...state, error: payload };
default:
return state;
}
@ -54,8 +40,11 @@ export default function reducer(state = initialState, action = {}) {
// ================ Action creators ================ //
export const login = (email, password) => ({ type: LOGIN_REQUEST, payload: { email, password } });
export const loginSuccess = user => ({ type: LOGIN_SUCCESS, payload: user });
export const login = (username, password) => ({
type: LOGIN_REQUEST,
payload: { username, password },
});
export const loginSuccess = () => ({ type: LOGIN_SUCCESS });
export const loginError = error => ({ type: LOGIN_ERROR, payload: error, error: true });
export const logout = historyPush => ({ type: LOGOUT_REQUEST, payload: { historyPush } });
@ -66,10 +55,10 @@ export const logoutError = error => ({ type: LOGOUT_ERROR, payload: error, error
export function* callLogin(action, sdk) {
const { payload } = action;
const { email, password } = payload;
const { username, password } = payload;
try {
yield call(sdk.login, email, password);
yield put(loginSuccess(payload));
yield call(sdk.login, { username, password });
yield put(loginSuccess());
} catch (e) {
yield put(loginError(e));
}

View file

@ -22,67 +22,63 @@ describe('Auth duck', () => {
it('should be logged out by default', () => {
const state = reducer();
expect(state.isAuthenticated).toEqual(false);
expect(state.user).toBeNull();
expect(state.error).toBeNull();
expect(state.inProgressState).toEqual(NOTHING_IN_PROGRESS);
});
it('should login successfully', () => {
const email = 'x@x.x';
const username = 'x@x.x';
const password = 'pass';
let state = reducer();
state = reducer(state, login(email, password));
state = reducer(state, login(username, password));
expect(state.isAuthenticated).toEqual(false);
expect(state.user).toBeNull();
expect(state.inProgressState).toEqual(LOGIN_IN_PROGRESS);
expect(state.error).toBeNull();
state = reducer(state, loginSuccess({ email, password }));
state = reducer(state, loginSuccess());
expect(state.isAuthenticated).toEqual(true);
expect(state.user).toEqual({ email, password });
expect(state.inProgressState).toEqual(NOTHING_IN_PROGRESS);
expect(state.error).toBeNull();
});
it('should handle failed login', () => {
let state = reducer();
state = reducer(state, login('email', 'pass'));
state = reducer(state, login('username', 'pass'));
expect(state.isAuthenticated).toEqual(false);
expect(state.user).toBeNull();
expect(state.inProgressState).toEqual(LOGIN_IN_PROGRESS);
expect(state.error).toBeNull();
const error = new Error('test error');
state = reducer(state, loginError(error));
expect(state.isAuthenticated).toEqual(false);
expect(state.user).toBeNull();
expect(state.inProgressState).toEqual(NOTHING_IN_PROGRESS);
expect(state.error).toEqual(error);
});
});
describe('login worker', () => {
it('should succeed when API call fulfills', () => {
const email = 'email';
const username = 'username';
const password = 'pass';
const payload = { email, password };
const payload = { username, password };
const sdk = { login: jest.fn() };
const loginAction = login(email, password);
const loginAction = login(username, password);
const worker = callLogin(loginAction, sdk);
expect(worker.next()).toEqual({ done: false, value: call(sdk.login, email, password) });
expect(worker.next(payload)).toEqual({ done: false, value: put(loginSuccess(payload)) });
expect(worker.next()).toEqual({
done: false,
value: call(sdk.login, { username, password }),
});
expect(worker.next(payload)).toEqual({ done: false, value: put(loginSuccess()) });
expect(worker.next().done).toEqual(true);
expect(sdk.login).not.toHaveBeenCalled();
});
it('should fail when API call rejects', () => {
const email = 'email';
const username = 'username';
const password = 'pass';
const sdk = { login: jest.fn() };
const loginAction = login(email, password);
const loginAction = login(username, password);
const worker = callLogin(loginAction, sdk);
expect(worker.next()).toEqual({ done: false, value: call(sdk.login, email, password) });
expect(worker.next()).toEqual({
done: false,
value: call(sdk.login, { username, password }),
});
const error = new Error('Test login failed');
expect(worker.throw(error)).toEqual({ done: false, value: put(loginError(error)) });
expect(worker.next().done).toEqual(true);
@ -122,7 +118,7 @@ describe('Auth duck', () => {
it('calls login', () => {
const sdk = { login: jest.fn() };
const watcher = watchAuth(sdk);
const loginAction = login('email', 'password');
const loginAction = login('username', 'password');
const takeLoginOrLogout = take([LOGIN_REQUEST, LOGOUT_REQUEST]);
const forkLogin = fork(callLogin, loginAction, sdk);
@ -162,8 +158,8 @@ describe('Auth duck', () => {
it('should cancel login if another login comes', () => {
const sdk = { login: jest.fn(), logout: jest.fn() };
const watcher = watchAuth(sdk);
const loginAction1 = login('email1', 'password1');
const loginAction2 = login('email2', 'password2');
const loginAction1 = login('username1', 'password1');
const loginAction2 = login('username2', 'password2');
const task = createMockTask();
const takeLoginOrLogout = take([LOGIN_REQUEST, LOGOUT_REQUEST]);
const forkLogin1 = fork(callLogin, loginAction1, sdk);
@ -197,7 +193,7 @@ describe('Auth duck', () => {
const sdk = { login: jest.fn(), logout: jest.fn() };
const historyPush = jest.fn();
const watcher = watchAuth(sdk);
const loginAction = login('email', 'password');
const loginAction = login('username', 'password');
const logoutAction = logout(historyPush);
const task = createMockTask();
const takeLoginOrLogout = take([LOGIN_REQUEST, LOGOUT_REQUEST]);