Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // @flow
- import {
- combineActions,
- createAction,
- handleActions
- } from 'redux-actions';
- import { fromJS } from 'immutable';
- import { pickAll } from 'ramda';
- import moment from 'moment';
- import * as authActions from './auth';
- import LoginService from '../services/auth/login';
- import {
- createThunk,
- SUFFIX as ThunkSuffix
- } from '../utils/createThunk';
- // /////////////////////
- // constants
- // /////////////////////
- const dateFormat = 'DD/MM/YYYY HH:mm';
- const ENTER_LOGIN_VIEW: string = 'app/login/ENTER_LOGIN_VIEW';
- const LEAVE_LOGIN_VIEW: string = 'app/login/LEAVE_LOGIN_VIEW';
- const ASYNC_LOGIN = 'app/login/ASYNC_LOGIN';
- export const ACTIONS = {
- ENTER_LOGIN_VIEW,
- LEAVE_LOGIN_VIEW
- };
- export const initialState = fromJS({
- currentView : 'login',
- enterTime : null,
- leaveTime : null,
- isLoggingIn : false,
- isLoginInvalid: false,
- isLoggedIn : false
- });
- // /////////////////////
- // action creators
- // /////////////////////
- export const enterLogin = createAction(ENTER_LOGIN_VIEW, (time = moment()
- .format(dateFormat)) => ({
- enterTime: time,
- leaveTime: null
- }));
- export const leaveLogin = createAction(
- LEAVE_LOGIN_VIEW, (time = moment()
- .format(dateFormat)) => ({
- enterTime: null,
- leaveTime: time
- }));
- // /////////////////////
- // epics
- // /////////////////////
- // /////////////////////
- // thunks
- // /////////////////////
- /**
- * Async login. Keep in mind that redux-actions 2nd argument is the payload creator so we could not use it to create an
- * action as a function, need to create it ourselves
- *
- * @param username {string} - the username
- * @param password {string} - the user password
- * @param service {object} - the service that is used to login which is set by default to LoginService
- * @returns {*}
- */
- export const doAsyncLogin = (username, password, service = LoginService) => createThunk(ASYNC_LOGIN, {
- SUCCEEDED: result => result
- }, dispatch => {
- return service.login(username, password)
- .then(result => {
- dispatch(authActions.authSuccess(result.data));
- return result;
- });
- });
- // /////////////////////
- // reducers
- // /////////////////////
- export const login = handleActions({
- [combineActions(ENTER_LOGIN_VIEW, LEAVE_LOGIN_VIEW)]: (state, action) => state.merge({
- ...pickAll(['currentView', 'enterTime', 'leaveTime'], action.payload)
- }),
- [`${ ASYNC_LOGIN }_${ ThunkSuffix.STARTED }`] : state => state.merge({
- isLoggingIn : true,
- isLoginInvalid: false
- }),
- [`${ ASYNC_LOGIN }_${ ThunkSuffix.SUCCEEDED }`] : (state, action) => state.merge({
- isLoggedIn : true,
- isLoggingIn : false,
- isLoginInvalid: false
- }),
- [`${ ASYNC_LOGIN }_${ ThunkSuffix.FAILED }`] : (state, action) => state.merge({
- isLoggedIn : true,
- isLoggingIn : false,
- isLoginInvalid: true
- })
- }, initialState);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement