Guest User

Untitled

a guest
May 23rd, 2018
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. const { createStore, applyMiddleware, combineReducers } = require('redux');
  2. const thunk = require('redux-thunk').default;
  3. const Axios = require("axios");
  4.  
  5. const ROOT = ""
  6.  
  7. // users edition of users
  8. const usersReducer = (state = { list: []} , action) => {
  9. const { payload, type } = action
  10.  
  11. switch(type) {
  12. case "USER_LIST_FETCHED":
  13. return { ...state, list: payload.resource , count: payload.meta.count }
  14. break;
  15. default:
  16. return state
  17. }
  18. }
  19.  
  20.  
  21. // controls the resource links to interactions of session
  22. const sessionReducers = (state = { token: null, links: [], currentUser: null }, action) => {
  23. const { payload, type, origin } = action
  24.  
  25.  
  26. switch(type) {
  27. case "USER_LOGIN":
  28. return { ...state, token: payload.token, links: payload.links, origin }
  29. case "USER_CURRENT":
  30. return { ...state, links: payload.links, currentUser: payload.user, origin }
  31. case "USER_LIST_FETCHED":
  32. // return { ...state, links: [ ...state.links, payload.links ] }
  33. return { ...state, links: payload.links, origin: origin }
  34.  
  35. case "USER_LOGIN_FAIL":
  36. return { ...state, error: payload, origin: origin }
  37. case "USER_LOGOUT":
  38. return {}
  39. default:
  40. return state
  41. }
  42. }
  43.  
  44. const combined = combineReducers({
  45. users: usersReducer,
  46. session: sessionReducers,
  47. });
  48.  
  49. const store = createStore(combined, {}, applyMiddleware(thunk));
  50.  
  51. class ClientApi {
  52.  
  53. constructor(dispatch) {
  54. this.token = null
  55. this.dispatch = dispatch
  56. this.resources = {
  57. "/users/current" : { method: "GET", success: "USER_CURRENT" },
  58. "/users/list" : { method: "GET", success: "USER_LIST_FETCHED" },
  59. }
  60. }
  61.  
  62. consumeResource({ rel, href }, data) {
  63. const link = rel === "self" ? { method: "GET", href, success: "SELF" } : this.resources[rel]
  64.  
  65. return Axios({
  66. method: link.method ,
  67. url: href,
  68. headers: { Authorization: this.token },
  69. data
  70. }).then(res => {
  71. this.dispatch({ type: link.success, payload: res.data, origin: rel })
  72. })
  73. }
  74.  
  75. login({ email, password }) {
  76.  
  77. Axios.post(`${ROOT}/api/admin/v1/login`, {
  78. email, password
  79. })
  80. .then(res => {
  81. this.token = res.data.token;
  82. return this.dispatch({ type: "USER_LOGIN", payload: res.data, origin: "login" })
  83. })
  84. .catch(err => (
  85. this.dispatch({ type: "USER_LOGIN_FAIL", payload: err })
  86. ))
  87. }
  88. }
  89.  
  90.  
  91. const clientApi = new ClientApi(store.dispatch)
  92.  
  93.  
  94. store.subscribe(() => {
  95.  
  96. const { session, profile, users } = store.getState()
  97. console.log(session)
  98. const profileResource = session.links.find(link => link.rel === "/users/current")
  99.  
  100. if(profileResource)
  101. clientApi.consumeResource(profileResource)
  102.  
  103. const firstPage = session.links.find(link => link.rel === "/users/list")
  104.  
  105. if(firstPage)
  106. clientApi.consumeResource(firstPage)
  107.  
  108. debugger
  109. if(session.origin === "/users/list") {
  110. for(const user of users.list) {
  111.  
  112. console.log(user.name)
  113. const getUserLink = user.links.find(l => l.rel === "self")
  114. // clientApi.consumeResource(getUserLink)
  115. }
  116. }
  117. })
  118.  
  119. store.subscribe(() => {
  120. console.log("subscribe --------------------------------------------------")
  121. })
  122.  
  123. clientApi.login({ email: "admin@hotmail.com" , password: "staging" })
  124.  
  125.  
  126. // ClientApi.login({ email: "admin@hotmail.com" , password: "stging" })
  127. // ClientApi.getUsers({ page: 1 , size: 20})
Add Comment
Please, Sign In to add comment