Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. // @flow
  2. import {
  3. combineActions,
  4. createAction,
  5. handleActions
  6. } from 'redux-actions';
  7. import { fromJS } from 'immutable';
  8. import { pickAll } from 'ramda';
  9. import moment from 'moment';
  10. import * as authActions from './auth';
  11. import LoginService from '../services/auth/login';
  12. import {
  13. createThunk,
  14. SUFFIX as ThunkSuffix
  15. } from '../utils/createThunk';
  16.  
  17. // /////////////////////
  18. // constants
  19. // /////////////////////
  20. const dateFormat = 'DD/MM/YYYY HH:mm';
  21.  
  22. const ENTER_LOGIN_VIEW: string = 'app/login/ENTER_LOGIN_VIEW';
  23. const LEAVE_LOGIN_VIEW: string = 'app/login/LEAVE_LOGIN_VIEW';
  24. const ASYNC_LOGIN = 'app/login/ASYNC_LOGIN';
  25.  
  26. export const ACTIONS = {
  27. ENTER_LOGIN_VIEW,
  28. LEAVE_LOGIN_VIEW
  29. };
  30.  
  31. export const initialState = fromJS({
  32. currentView : 'login',
  33. enterTime : null,
  34. leaveTime : null,
  35. isLoggingIn : false,
  36. isLoginInvalid: false,
  37. isLoggedIn : false
  38. });
  39.  
  40. // /////////////////////
  41. // action creators
  42. // /////////////////////
  43. export const enterLogin = createAction(ENTER_LOGIN_VIEW, (time = moment()
  44. .format(dateFormat)) => ({
  45. enterTime: time,
  46. leaveTime: null
  47. }));
  48.  
  49. export const leaveLogin = createAction(
  50. LEAVE_LOGIN_VIEW, (time = moment()
  51. .format(dateFormat)) => ({
  52. enterTime: null,
  53. leaveTime: time
  54. }));
  55. // /////////////////////
  56. // epics
  57. // /////////////////////
  58.  
  59. // /////////////////////
  60. // thunks
  61. // /////////////////////
  62.  
  63. /**
  64. * Async login. Keep in mind that redux-actions 2nd argument is the payload creator so we could not use it to create an
  65. * action as a function, need to create it ourselves
  66. *
  67. * @param username {string} - the username
  68. * @param password {string} - the user password
  69. * @param service {object} - the service that is used to login which is set by default to LoginService
  70. * @returns {*}
  71. */
  72. export const doAsyncLogin = (username, password, service = LoginService) => createThunk(ASYNC_LOGIN, {
  73. SUCCEEDED: result => result
  74. }, dispatch => {
  75. return service.login(username, password)
  76. .then(result => {
  77. dispatch(authActions.authSuccess(result.data));
  78.  
  79. return result;
  80. });
  81. });
  82. // /////////////////////
  83. // reducers
  84. // /////////////////////
  85. export const login = handleActions({
  86. [combineActions(ENTER_LOGIN_VIEW, LEAVE_LOGIN_VIEW)]: (state, action) => state.merge({
  87. ...pickAll(['currentView', 'enterTime', 'leaveTime'], action.payload)
  88. }),
  89. [`${ ASYNC_LOGIN }_${ ThunkSuffix.STARTED }`] : state => state.merge({
  90. isLoggingIn : true,
  91. isLoginInvalid: false
  92. }),
  93. [`${ ASYNC_LOGIN }_${ ThunkSuffix.SUCCEEDED }`] : (state, action) => state.merge({
  94. isLoggedIn : true,
  95. isLoggingIn : false,
  96. isLoginInvalid: false
  97. }),
  98. [`${ ASYNC_LOGIN }_${ ThunkSuffix.FAILED }`] : (state, action) => state.merge({
  99. isLoggedIn : true,
  100. isLoggingIn : false,
  101. isLoginInvalid: true
  102. })
  103. }, initialState);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement