Guest User

Untitled

a guest
Jan 9th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. import { Action, Reducer } from 'redux';
  2.  
  3. // -----------------
  4. // STATE - This defines the type of data maintained in the Redux store.
  5.  
  6. export interface NoneAuthorizedUser {
  7. type: 'NONE_AUTHORIZED_USER',
  8. message: string,
  9. }
  10. export interface AuthorizedUser {
  11. type: 'AUTHORIZED_USER',
  12. userName: string,
  13. userId: string,
  14. }
  15.  
  16.  
  17. export type AuthorizedUserState = NoneAuthorizedUser | AuthorizedUser;
  18. // export type AuthorizedUserState =
  19. // {
  20. // type: 'NONE_AUTHORIZED_USER',
  21. // message: string,
  22. // }
  23. // |
  24. // {
  25. // type: 'AUTHORIZED_USER',
  26. // userName: string,
  27. // userId: string,
  28. // };
  29.  
  30. export interface AuthorizationState {
  31. count: number;
  32. authorize: AuthorizedUserState;
  33. }
  34.  
  35. // -----------------
  36. // ACTIONS - These are serializable (hence replayable) descriptions of state transitions.
  37. // They do not themselves have any side-effects; they just describe something that is going to happen.
  38. // Use @typeName and isActionType for type detection that works even after serialization/deserialization.
  39.  
  40. interface LoginAction { type: 'LOGIN', login: string, password: string }
  41. interface LogoutAction { type: 'LOGOUT' }
  42. interface RegisterAction { type: 'REGISTER' }
  43. interface ClearErrorAction { type: 'CLEARERRORACTION' }
  44.  
  45. // Declare a 'discriminated union' type. This guarantees that all references to 'type' properties contain one of the
  46. // declared type strings (and not any other arbitrary string).
  47. type KnownAction = LoginAction | LogoutAction | ClearErrorAction;
  48.  
  49. // ----------------
  50. // ACTION CREATORS - These are functions exposed to UI components that will trigger a state transition.
  51. // They don't directly mutate state, but they can have external side-effects (such as loading data).
  52.  
  53. export const actionLogin = {
  54. //login: () => <LoginAction>{ type: 'LOGIN' },
  55. login: (username: string, password: string) => <LoginAction>{ type: 'LOGIN', login: username, password: password },
  56. logout: () => <LogoutAction>{ type: 'LOGOUT' },
  57. clearError: () => <ClearErrorAction>{ type: 'CLEARERRORACTION' }
  58. };
  59.  
  60. export const actionRegister = {
  61. register: () => <RegisterAction>{ type: 'REGISTER' }
  62. };
  63.  
  64. // ----------------
  65. // REDUCER - For a given state and action, returns the new state. To support time travel, this must not mutate the old state.
  66.  
  67. const noneAuthorizedUser: NoneAuthorizedUser = { type: 'NONE_AUTHORIZED_USER', message: '' };
  68.  
  69. // function handleResponse(response:any) {
  70. // return new Promise((resolve, reject) => {
  71. // if (response.ok) {
  72. // // return json if it was returned in the response
  73. // var contentType = response.headers.get("content-type");
  74. // if (contentType && contentType.includes("application/json")) {
  75. // response.json().then(json => resolve(json));
  76. // } else {
  77. // resolve();
  78. // }
  79. // } else {
  80. // // return error message from response body
  81. // response.text().then(text => reject(text));
  82. // }
  83. // });
  84. // }
  85.  
  86. // function handleError(error:any) {
  87. // return Promise.reject(error && error.message);
  88. // }
  89.  
  90. export const reducer: Reducer<AuthorizationState> = (state: AuthorizationState, action: KnownAction) => {
  91.  
  92. //alert(JSON.stringify(state));
  93. switch (action.type) {
  94. case 'LOGIN':
  95. const requestOptions = {
  96. method: 'POST',
  97. headers: { 'Content-Type': 'application/json' },
  98. body: JSON.stringify({ login:action.login, password:action.password })
  99. };
  100.  
  101. // var user = fetch('/users/authenticate', requestOptions)
  102. //         .then(handleResponse, handleError)
  103. //         .then(user => {
  104. //             // login successful if there's a jwt token in the response
  105. //             if (user && user.token) {
  106. //                 // store user details and jwt token in local storage to keep user logged in between page refreshes
  107. //                 localStorage.setItem('user', JSON.stringify(user));
  108. //             }
  109. //  
  110. //             return user;
  111. //         });
  112.  
  113. if (action.login == '111') {
  114. return {
  115. count: state.count + 1,
  116. authorize: {
  117. type: 'AUTHORIZED_USER',
  118. userName: '111',
  119. userId: '222222',
  120. }
  121. };
  122. } else {
  123. return {
  124. count: state.count + 1,
  125. authorize: { type: 'NONE_AUTHORIZED_USER', message: 'wrongUser' }
  126. };
  127. };
  128. case 'LOGOUT':
  129. if (state.count <= 0)
  130. return { count: 0, authorize: noneAuthorizedUser };
  131. else
  132. return { count: state.count - 1, authorize: noneAuthorizedUser };
  133. case 'CLEARERRORACTION':
  134. return {
  135. count: 0,
  136. authorize: { type: 'NONE_AUTHORIZED_USER', message: '' }
  137. };
  138. default:
  139. // The following line guarantees that every action in the KnownAction union has been covered by a case above
  140. const exhaustiveCheck: never = action;
  141. }
  142.  
  143. // For unrecognized actions (or in cases where actions have no effect), must return the existing state
  144. // (or default initial state if none was supplied)
  145. return state || { count: 0, authorize: noneAuthorizedUser };
  146. };
  147.  
  148.  
  149.  
  150. export const userConstants = {
  151. REGISTER_REQUEST: 'USERS_REGISTER_REQUEST',
  152. REGISTER_SUCCESS: 'USERS_REGISTER_SUCCESS',
  153. REGISTER_FAILURE: 'USERS_REGISTER_FAILURE',
  154.  
  155. LOGIN_REQUEST: 'USERS_LOGIN_REQUEST',
  156. LOGIN_SUCCESS: 'USERS_LOGIN_SUCCESS',
  157. LOGIN_FAILURE: 'USERS_LOGIN_FAILURE',
  158.  
  159. LOGOUT: 'USERS_LOGOUT',
  160.  
  161. GETALL_REQUEST: 'USERS_GETALL_REQUEST',
  162. GETALL_SUCCESS: 'USERS_GETALL_SUCCESS',
  163. GETALL_FAILURE: 'USERS_GETALL_FAILURE',
  164.  
  165. DELETE_REQUEST: 'USERS_DELETE_REQUEST',
  166. DELETE_SUCCESS: 'USERS_DELETE_SUCCESS',
  167. DELETE_FAILURE: 'USERS_DELETE_FAILURE'
  168. };
  169.  
  170.  
  171. // export function registerUser({ email, firstName, lastName, password }) {
  172. // return function(dispatch) {
  173. // axios.post(`${API_URL}/auth/register`, { email, firstName, lastName, password })
  174. // .then(response => {
  175. // cookie.save('token', response.data.token, { path: '/' });
  176. // dispatch({ type: AUTH_USER });
  177. // window.location.href = CLIENT_ROOT_URL + '/dashboard';
  178. // })
  179. // .catch((error) => {
  180. // errorHandler(dispatch, error.response, AUTH_ERROR)
  181. // });
  182. // }
  183. // }
Add Comment
Please, Sign In to add comment