Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. import { combineReducers } from 'redux'
  2. import { createStore, applyMiddleware } from 'redux';
  3. import thunk from 'redux-thunk';
  4.  
  5. const initialState = {
  6. currentUser: {
  7. credentials: {},
  8. user: {}
  9. },
  10. test: {},
  11. users: []
  12. }
  13.  
  14. export const SUBMIT_LOGIN = 'SUBMIT_LOGIN'
  15. export const SET_USER = 'SET_USER'
  16. export const TEST = 'TEST'
  17. export const SET_USERS = 'SET_USERS'
  18. export const SET_CREDENTIALS = 'SET_CREDENTIALS'
  19.  
  20. //actions
  21. const submitLogin = () => (dispatch) => {
  22. return postLoginRequest()
  23. .then(response => {
  24. dispatch(setCredentials(
  25. response.headers.get('access-token'),
  26. response.headers.get('client'),
  27. response.headers.get('expiry'),
  28. response.headers.get('token-type'),
  29. response.headers.get('uid')
  30. ));
  31. return response
  32. })
  33. .then(response => {
  34. return response.json();
  35. })
  36. .then(
  37. (user) => dispatch(setUser(user.data)),
  38. );
  39. }
  40.  
  41. const performRequest = (api) => (dispatch) => {
  42. return api()
  43. .then(response => {
  44. dispatch(setCredentials(
  45. response.headers.get('access-token'),
  46. response.headers.get('client'),
  47. response.headers.get('expiry'),
  48. response.headers.get('token-type'),
  49. response.headers.get('uid')
  50. ));
  51. return response
  52. })
  53. .then(response => {return response.json()})
  54. .then(json => {console.log(json)})
  55. }
  56.  
  57. const setUsers = (users) => {
  58. return {
  59. type: SET_USERS,
  60. users
  61. }
  62. }
  63.  
  64. const setUser = (user) => {
  65. return {
  66. type: SET_USER,
  67. user
  68. }
  69. }
  70.  
  71. const setCredentials = (
  72. access_token,
  73. client,
  74. expiry,
  75. token_type,
  76. uid
  77. ) => {
  78. return {
  79. type: SET_CREDENTIALS,
  80. credentials: {
  81. 'access-token': access_token,
  82. client,
  83. expiry,
  84. 'token-type': token_type,
  85. uid
  86. }
  87. }
  88. }
  89.  
  90. const currentUserInitialState = {
  91. credentials: {},
  92. user: {}
  93. }
  94.  
  95. const currentUser = (state = currentUserInitialState, action) => {
  96. switch (action.type) {
  97. case SET_USER:
  98. return Object.assign({}, state, {user: action.user})
  99. case SET_CREDENTIALS:
  100. return Object.assign({}, state, {credentials: action.credentials})
  101. default:
  102. return state
  103. }
  104. }
  105.  
  106. const rootReducer = combineReducers({
  107. currentUser,
  108. test
  109. })
  110.  
  111. const getAuthorizedHeader = (store) => {
  112. const credentials = store.getState().currentUser.credentials
  113. const headers = new Headers(credentials)
  114. return headers
  115. }
  116.  
  117. //store creation
  118.  
  119. const createStoreWithMiddleware = applyMiddleware(
  120. thunk
  121. )(createStore);
  122.  
  123. const store = createStoreWithMiddleware(rootReducer);
  124.  
  125. const postLoginRequest = () => {
  126. return fetch('http://localhost:3000/auth/sign_in', {
  127. method: 'POST',
  128. headers: {
  129. 'Content-Type': 'application/json'
  130. },
  131. body: JSON.stringify({
  132. email: 'test@test.com',
  133. password: 'password',
  134. })
  135. })
  136. }
  137.  
  138. const getUsers = () => {
  139. const autorizedHeader = getAuthorizedHeader(store)
  140. debugger
  141. return fetch('http://localhost:3000/users',
  142. {
  143. method: 'GET',
  144. headers : autorizedHeader
  145. }
  146. )
  147. }
  148.  
  149.  
  150. store.dispatch(submitLogin())
  151.  
  152. setTimeout(() => {
  153. console.log(store.dispatch(performRequest(getUsers)))
  154. }, 2000)
  155.  
  156. config.batch_request_buffer_throttle = 5.seconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement