Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. /*********** redux/api.js ***********/
  2. class ReduxApi {
  3. constructor(appName, relativePath) {
  4. this.APP_NAME = appName;
  5. this.URL = `{relativePath}`
  6.  
  7. this.GET_REQUEST = `${this.APP_NAME}/GET_REQUEST`;
  8. this.GET_SUCCESS = `${this.APP_NAME}/GET_SUCCESS`;
  9. this.GET_ERROR = `${this.APP_NAME}/GET_ERROR`;
  10.  
  11. this.reducer = this.reducer.bind(this);
  12. this.selectGlobal = this.selectGlobal.bind(this);
  13. this.makeSelectBase = this.makeSelectBase.bind(this);
  14. this.saga = this.saga.bind(this);
  15. this.get = this.get.bind(this);
  16. }
  17.  
  18. getRequest() {
  19. return {
  20. type: this.GET_REQUEST
  21. };
  22. }
  23.  
  24. getSuccess(data) {
  25. return {
  26. type: this.GET_SUCCESS,
  27. data
  28. };
  29. }
  30.  
  31. getError(data) {
  32. return {
  33. type: this.GET_ERROR,
  34. data
  35. };
  36. }
  37.  
  38. getRequestReducerHandler(state, action) {
  39. return state
  40. .set('requesting', true);
  41. }
  42.  
  43. getSuccessReducerHandler(state, action) {
  44. return state
  45. .set('requesting', false)
  46. .set('initialized', true)
  47. .set('list', action.data);
  48. }
  49.  
  50. getErrorReducerHandler(state, action) {
  51. return state
  52. .set('error', action.error)
  53. .set('requesting', false);
  54. }
  55.  
  56. /*
  57. *
  58. * NOT INCLUDING GET (detail), POST, PUT, DELETE or PATCH, for sake of berivity
  59. *
  60. */
  61.  
  62. reducer(state = fromJS({ list: [],
  63. detail: {},
  64. requesting: false,
  65. initialized: false,
  66. }), action) {
  67.  
  68. switch (action.type) {
  69. case this.GET_REQUEST: {
  70. return getRequestReducerHandler(state, action);
  71. }
  72.  
  73. case this.GET_SUCCESS: {
  74. return getSuccessReducerHandler(state, action)
  75. }
  76.  
  77. case this.GET_ERROR: {
  78. return getErrorReducerHandler(state, action)
  79. }
  80.  
  81. default:
  82. return state;
  83. }
  84. }
  85.  
  86. * get(action = {}) {
  87. try {
  88. // Call using the request helper
  89. const resp = yield Axios.get(action.url || this.URL);
  90. yield put(this.getSuccess(resp.data, action.append));
  91. } catch (err) {
  92. yield put(this.getError(err));
  93. }
  94. }
  95.  
  96. // Saga
  97. * saga() {
  98. yield all([
  99. takeLatest(this.GET_REQUEST, this.get),
  100. ]);
  101. }
  102.  
  103. // Selectors
  104. selectGlobal(state) {
  105. return state.get(this.APP_NAME);
  106. }
  107.  
  108. /*
  109. * Key corresponds to the redux state keys e.g. list, detail, etc.
  110. */
  111. makeSelectBase = (key) => {
  112. return createSelector(
  113. this.selectGlobal,
  114. (globalState) => globalState.get(key)
  115. );
  116. }
  117. }
  118.  
  119. export default ReduxApi;
  120.  
  121.  
  122. /*********** redux/index.js ***********/
  123. import { all } from 'redux-saga/effects';
  124. import ReduxAPI from './api';
  125.  
  126. const user = new ReduxAPI('user', '/api/v1/user/');
  127. const feed = new ReduxAPI('feed', '/api/v1/feed/');
  128. const comment = new ReduxAPI('comment', '/api/v1/comment/');
  129.  
  130. export default {
  131. user: user.reducer,
  132. feed: board.feed,
  133. comment: dance.comment
  134. };
  135.  
  136. export function* saga() {
  137. yield all([
  138. user.saga(),
  139. feed.saga(),
  140. comment.saga()
  141. ]);
  142. }
  143.  
  144.  
  145. /*********** container/component.js ***********/
  146. import React from 'react';
  147. import { createStructuredSelector } from 'reselect';
  148. import { connect } from 'react-redux';
  149.  
  150. // App
  151. import ReduxApi from 'containers/redux/api';
  152. const user = new ReduxApi('user', '/api/v1/user/');
  153. const feed = new ReduxAPI('feed', '/api/v1/feed/');
  154.  
  155.  
  156. class Header extends React.Component {
  157. constructor(props) {
  158. super(props);
  159. this.props.getUser();
  160. this.props.getFeed();
  161. }
  162.  
  163. render() {
  164. const { currentUser, currentFeed } = this.props;
  165. return (
  166. <div className={classes.root}>
  167. { currentUser && currentUser.username}
  168. { currentFeed && currentFeed.length}
  169. </div>
  170. );
  171. }
  172. }
  173.  
  174. const mapDispatchToProps = (dispatch) => ({
  175. getUser: () => dispatch(user.getRequest()),
  176. getFeed: () => dispatch(feed.getRequest())
  177. });
  178.  
  179. const mapStateToProps = createStructuredSelector({
  180. currentUser: user.makeSelectBase('list'),
  181. currentFeed: feed.makeSelectBase('list')
  182. });
  183.  
  184. export default connect(
  185. mapStateToProps,
  186. mapDispatchToProps
  187. )(Header));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement