Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. /* Third party import */
  2. import { createAction, handleActions } from 'redux-actions';
  3. import { ofType } from 'redux-observable';
  4. import {
  5. catchError,
  6. mergeMap,
  7. switchMap,
  8. takeUntil,
  9. map,
  10. } from 'rxjs/operators';
  11. import {
  12. from,
  13. of,
  14. iif,
  15. } from 'rxjs';
  16. /* Relative webpack imports */
  17. import { getImages } from 'api/establishments';
  18. import { getProductImages } from 'api/products';
  19. /* Relative imports */
  20. import {
  21. closeLoadingBar,
  22. showLoadingBar,
  23. showMessage,
  24. } from './common';
  25.  
  26. const FETCH_IMAGES_SUCCEEDED = 'images/FETCH_IMAGES_SUCCEEDED';
  27. const FETCH_IMAGES_STARTED = 'images/FETCH_IMAGES_STARTED';
  28. const FETCH_IMAGES_FAILED = 'images/FETCH_IMAGES_FAILED';
  29. const FETCH_PRODUCT_IMAGES_SUCCEEDED = 'images/FETCH_PRODUCT_IMAGES_SUCCEEDED';
  30. const FETCH_PRODUCT_IMAGES_STARTED = 'images/FETCH_PRODUCT_IMAGES_STARTED';
  31. const FETCH_PRODUCT_IMAGES_FAILED = 'images/FETCH_PRODUCT_IMAGES_FAILED';
  32. const SHOW_IMAGE_GALLERY = 'images/SHOW_IMAGE_GALLERY';
  33. const CLOSE_IMAGE_GALLERY = 'images/CLOSE_IMAGE_GALLERY';
  34. const SHOW_PRODUCT_IMAGE_GALLERY = 'images/SHOW_PRODUCT_IMAGE_GALLERY';
  35. const CLOSE_PRODUCT_IMAGE_GALLERY = 'images/CLOSE_PRODUCT_IMAGE_GALLERY';
  36.  
  37. export const showImageGallery = createAction(SHOW_IMAGE_GALLERY);
  38. export const closeImageGallery = createAction(CLOSE_IMAGE_GALLERY);
  39. export const showProductImageGallery = createAction(SHOW_PRODUCT_IMAGE_GALLERY);
  40. export const closeProductImageGallery = createAction(CLOSE_PRODUCT_IMAGE_GALLERY);
  41.  
  42. export const fetchImagesSucceeded = createAction(
  43. FETCH_IMAGES_SUCCEEDED,
  44. images => ({ images }),
  45. );
  46. export const fetchImagesStarted = createAction(
  47. FETCH_IMAGES_STARTED,
  48. id => ({ id }),
  49. );
  50. export const fetchImagesFailed = createAction(
  51. FETCH_IMAGES_FAILED,
  52. error => ({ error }),
  53. );
  54.  
  55. export const fetchProductImagesSucceeded = createAction(
  56. FETCH_PRODUCT_IMAGES_SUCCEEDED,
  57. productImages => ({ productImages }),
  58. );
  59. export const fetchProductImagesStarted = createAction(
  60. FETCH_PRODUCT_IMAGES_STARTED,
  61. (id, isImageGallery) => ({ id, isImageGallery }),
  62. );
  63. export const fetchProductImagesFailed = createAction(
  64. FETCH_PRODUCT_IMAGES_FAILED,
  65. error => ({ error }),
  66. );
  67.  
  68.  
  69. export function fetchImages(id) {
  70. return (dispatch) => {
  71. dispatch(showLoadingBar());
  72. dispatch(fetchImagesStarted(id));
  73. };
  74. }
  75.  
  76. export const fetchImagesEpic = action$ =>
  77. action$.pipe(
  78. ofType(fetchImagesStarted),
  79. switchMap(action =>
  80. from(getImages(action.payload.id)).pipe(
  81. mergeMap(response => of(
  82. closeLoadingBar(),
  83. fetchImagesSucceeded(response.data),
  84. )),
  85. takeUntil(action$.pipe(
  86. ofType(fetchImagesSucceeded),
  87. )),
  88. catchError(error => of(
  89. fetchImagesFailed(error.message),
  90. closeLoadingBar(),
  91. )),
  92. )),
  93. );
  94.  
  95. // export function fetchProductImages(id, isImageGallery) {
  96. // return (dispatch) => {
  97. // dispatch(showLoadingBar());
  98. // getProductImages(id)
  99. // .then((response) => {
  100. // dispatch(closeLoadingBar());
  101. // dispatch(fetchProductImagesSucceeded(response.data));
  102. // if (response.data.length === 0 && isImageGallery === true) {
  103. // dispatch(showMessage(true, 'backoffice:product.noImageFound'));
  104. // } else if (isImageGallery) { dispatch(showProductImageGallery()); }
  105. // })
  106. // .catch((error) => {
  107. // dispatch(fetchProductImagesFailed(error.message));
  108. // dispatch(closeLoadingBar());
  109. // });
  110. // };
  111. // }
  112. export function fetchProductImages(id, isImageGallery) {
  113. return (dispatch) => {
  114. dispatch(showLoadingBar());
  115. dispatch(fetchProductImagesStarted(id, isImageGallery));
  116. };
  117. }
  118.  
  119. export const fetchProductImagesEpic = action$ =>
  120. action$.pipe(
  121. ofType(fetchProductImagesStarted),
  122. switchMap(action =>
  123. from(getProductImages(Number(action.payload.id))).pipe(
  124. mergeMap(response =>
  125. iif(
  126. () => action.payload.isImageGallery,
  127. iif(
  128. () => response.data.length === 0,
  129. of(
  130. closeLoadingBar(),
  131. fetchProductImagesSucceeded(response.data),
  132. showMessage(true, 'backoffice:product.noImageFound'),
  133. ),
  134. of(
  135. closeLoadingBar(),
  136. fetchProductImagesSucceeded(response.data),
  137. showProductImageGallery(),
  138. ),
  139. ),
  140. of(
  141. closeLoadingBar(),
  142. fetchProductImagesSucceeded(response.data),
  143. ),
  144. )),
  145. takeUntil(action$.pipe(
  146. ofType(fetchProductImagesStarted),
  147. )),
  148. catchError(error => of(
  149. fetchProductImagesFailed(error.message),
  150. closeLoadingBar(),
  151. )),
  152. )),
  153. );
  154.  
  155. const initialState = {
  156. images: [],
  157. productImages: [],
  158. openImageGallery: false,
  159. openProductImageGallery: false,
  160. };
  161.  
  162. const reducer = handleActions({
  163. [fetchImagesStarted]: state => ({
  164. ...state,
  165. images: [],
  166. }),
  167. [fetchImagesSucceeded]: (state, action) => ({
  168. ...state,
  169. images: action.payload.images || [],
  170. }),
  171. [fetchImagesFailed]: (state, action) => ({
  172. ...state,
  173. error: action.payload.error,
  174. }),
  175. [fetchProductImagesStarted]: state => ({
  176. ...state,
  177. productImages: [],
  178. }),
  179. [fetchProductImagesSucceeded]: (state, action) => ({
  180. ...state,
  181. productImages: action.payload.productImages || [],
  182. }),
  183. [fetchProductImagesFailed]: (state, action) => ({
  184. ...state,
  185. error: action.payload.error,
  186. }),
  187. [showImageGallery]: state => ({
  188. ...state,
  189. openImageGallery: true,
  190. }),
  191. [closeImageGallery]: state => ({
  192. ...state,
  193. openImageGallery: false,
  194. }),
  195. [showProductImageGallery]: state => ({
  196. ...state,
  197. openProductImageGallery: true,
  198. }),
  199. [closeProductImageGallery]: state => ({
  200. ...state,
  201. openProductImageGallery: false,
  202. productImages: [],
  203. }),
  204. }, initialState);
  205.  
  206. export default reducer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement