Guest User

Untitled

a guest
Sep 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. import articleListSeeds from './seeds/article-list';
  2. import { createReducer, createAction, handle } from 'modules/utils/dux';
  3. import { createSelector } from 'reselect';
  4. // import { createSelector } from 'reselect';
  5.  
  6. // import { getArticleList } from '../ducks/';
  7.  
  8. /* REDUCER NAME */
  9. export const NAME = 'articleList';
  10.  
  11. /* TYPES */
  12. const LIKE = 'blog/article-list/LIKE';
  13. const UNLIKE = 'blog/article-list/UNLIKE';
  14. const SHARE = 'blog/article-list/SHARE';
  15.  
  16. /* REDUCERS */
  17.  
  18. const reducer = createReducer(
  19. handle(LIKE, (state, { slug }) => ({
  20. ...state,
  21. [slug]: {
  22. ...state[slug],
  23. details: {
  24. ...state[slug].details,
  25. isArticleLiked: true,
  26. likes: state[slug].details.likes + 1
  27. }
  28. }
  29. })),
  30.  
  31. handle(UNLIKE, (state, { slug }) => ({
  32. ...state,
  33. [slug]: {
  34. ...state[slug],
  35. details: {
  36. ...state[slug].details,
  37. isArticleLiked: false,
  38. likes: state[slug].details.likes - 1
  39. }
  40. }
  41. })),
  42.  
  43. handle(SHARE, (state, { slug, name }) => ({
  44. ...state,
  45. [slug]: {
  46. ...state[slug],
  47. details: {
  48. ...state[slug].details,
  49. shares: {
  50. ...state[slug].details.shares,
  51. [name]: {
  52. ...state[slug].details.shares[name],
  53. amount: state[slug].details.shares[name].amount + 1
  54. }
  55. }
  56. }
  57. }
  58. }))
  59. );
  60.  
  61. export default reducer(articleListSeeds);
  62.  
  63. /* SELECTORS */
  64.  
  65. export const getRoot = state => state.blog.articleList;
  66.  
  67. export const getSlugFromRouteParams = (_, { match }) => {
  68. return match.params.slug;
  69. };
  70.  
  71. export const getAll = getRoot;
  72.  
  73. export const getItemByRouteParams = createSelector(
  74. getAll,
  75. getSlugFromRouteParams,
  76. (all, slug) => all[slug]
  77. );
  78.  
  79. const getSlug = (_, { slug }) => slug;
  80.  
  81. export const getItemBySlug = createSelector(
  82. getAll,
  83. getSlug,
  84. function _getItemBySlug(all, slug) {
  85. if (!all || !slug) {
  86. return null;
  87. }
  88.  
  89. const article = all[slug];
  90.  
  91. if (!article) {
  92. return null;
  93. }
  94.  
  95. return article;
  96. }
  97. );
  98.  
  99. export const getDetailsBySlug = createSelector(
  100. getItemBySlug,
  101. function _getDetailsBySlug(article) {
  102. if (!article || !article.details) {
  103. return null;
  104. }
  105.  
  106. return article.details;
  107. }
  108. );
  109.  
  110. export const getSharesBySlug = createSelector(
  111. getDetailsBySlug,
  112. function _getSharesBySlug(details) {
  113. return Object.values(details.shares);
  114. }
  115. );
  116.  
  117. export const getExploreArticleList = createSelector(
  118. getDetailsBySlug,
  119. details => details.exploreArticleList
  120. );
  121.  
  122. export const getItemAdBySlug = createSelector(
  123. getItemBySlug,
  124. function _getDetailsBySlug(article) {
  125. if (!article || !article.details) {
  126. return null;
  127. }
  128.  
  129. return {
  130. title: article.title,
  131. slug: article.slug,
  132. cover: article.cover
  133. };
  134. }
  135. );
  136.  
  137. /* ACTION_CREATORS */
  138.  
  139. export const actions = {
  140. like(slug) {
  141. return createAction(LIKE, { slug });
  142. },
  143.  
  144. unlike(slug) {
  145. return createAction(UNLIKE, { slug });
  146. },
  147.  
  148. share(slug, name) {
  149. return createAction(SHARE, { slug, name });
  150. }
  151. };
  152.  
  153. /* HELPERS */
Add Comment
Please, Sign In to add comment