Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import { LoginActions, LoginActionKeys } from "actions/loginActions";
  2.  
  3. // Interfaces
  4. export interface LoginState {
  5. isLoggedIn: boolean;
  6. isLoggingIn: boolean;
  7. loginError: boolean;
  8. username: string;
  9. password: string;
  10. profile?: Profile | null;
  11. }
  12.  
  13. export interface Profile {
  14. nickname: string;
  15. name: string;
  16. picture: string;
  17. }
  18.  
  19. // Initial State
  20. const loginInitialState: LoginState = {
  21. isLoggedIn: false,
  22. isLoggingIn: false,
  23. loginError: false,
  24. username: "",
  25. password: "",
  26. profile: null
  27. };
  28.  
  29. export default (
  30. state: LoginState = loginInitialState,
  31. action: LoginActions
  32. ): LoginState => {
  33. switch (action.type) {
  34. case LoginActionKeys.LOGIN_START: {
  35. return {
  36. ...state,
  37. isLoggingIn: true,
  38. loginError: false
  39. };
  40. }
  41. case LoginActionKeys.LOGIN_SUCCESS: {
  42. return {
  43. ...state,
  44. isLoggedIn: true,
  45. isLoggingIn: false
  46. };
  47. }
  48.  
  49. case LoginActionKeys.LOGIN_ERROR: {
  50. return {
  51. ...state,
  52. isLoggedIn: false,
  53. isLoggingIn: true,
  54. loginError: true
  55. };
  56. }
  57.  
  58. case LoginActionKeys.LOGOUT: {
  59. return {
  60. ...state,
  61. isLoggedIn: false,
  62. profile: null,
  63. loginError: false
  64. };
  65. }
  66.  
  67. case LoginActionKeys.STORE_USER_PROFILE: {
  68. return {
  69. ...state,
  70. profile: action.data.profile
  71. };
  72. }
  73.  
  74. case LoginActionKeys.UPDATE_INFO: {
  75. return {
  76. ...state,
  77. [action.data.field]: action.data.value
  78. };
  79. }
  80.  
  81. default: {
  82. return state;
  83. }
  84. }
  85. };
  86.  
  87. const loginState = state => state.login as LoginState;
  88.  
  89. // Selectors
  90. export const selectors = {
  91. isLoggedIn: state => loginState(state).isLoggedIn,
  92. isLoggingIn: state => loginState(state).isLoggingIn,
  93. loginError: state => loginState(state).loginError,
  94. userProfile: state => loginState(state).profile
  95. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement