UNPKG

@brizy/media-gallery

Version:
106 lines (105 loc) 6.92 kB
import { forkJoin, from, of } from "rxjs"; import { catchError, distinctUntilChanged, filter, map, switchMap } from "rxjs/operators"; import * as Actions from "./types/Actions"; import * as State from "./types/State"; import { fetchError } from "./notifications"; import { batchActions } from "redux-batched-actions"; import { onNothing } from "../../utils/maybe/onNothing"; import { convertApiFilesToSelectedFiles, fromBlobToFile, fromSelectedFileToInsertFile } from "./converters"; import { selectState } from "./selectors"; import { isT } from "fp-utilities"; import { ORDER_BY_DESC, STOCK_PHOTOS_LIMIT } from "../../constants"; import { combineEpics } from "redux-observable"; import { fromFetch } from "rxjs/fetch"; import { isAcceptableType, wrongTypeErrorMessage } from "../../ports/Message/utils"; import { always } from "ramda"; import { toString as fromItemIdToString } from "../../types/ItemId"; import { fromString } from "../../types/Name"; import { ApiFileTypes } from "../mediaLibrary/types/types"; import { convertApiDataToSelectedFile, convertApiFilesToSelectedFiles as convertApiFilesToMLSelectedFiles } from "../mediaLibrary/converters"; import { getErrorText, goToInsertFilesOrImportToML, isReadyOrMultipleInsert } from "./utils"; import { IMAGE_UPLOAD_ERROR } from "../mediaLibrary/constants"; import { isImageUploadError } from "../mediaLibrary/utils"; var init = function(action$, state$, param) { var c = param.context, t = param.t; return state$.pipe(map(selectState), filter(isT), filter(State.isInit), distinctUntilChanged(), switchMap(function(param) { var search = param.search, page = param.page; return from(c.stockPhotos.getItems({ search: search, page: page, count: STOCK_PHOTOS_LIMIT })).pipe(filter(isT), map(onNothing(t("Unable to fetch data"))), map(convertApiFilesToSelectedFiles), map(onNothing(t("Unable to fetch data"))), map(Actions.loadSuccess), catchError(function(param) { var message = param.message; return of(batchActions([ Actions.loadError(message), fetchError(t("Unable to fetch data")) ])); })); })); }; export var insertFileEpic = function(action$, state$, param) { var c = param.context, t = param.t; return state$.pipe(map(selectState), filter(isT), filter(isReadyOrMultipleInsert), distinctUntilChanged(), switchMap(function(s) { return of(s.items).pipe(map(function(items) { return items.filter(function(v) { return v.isSelected; }); }), map(function(items) { return items.length <= 10 ? items : undefined; }), map(onNothing(t("Please select a maximum of 10 images at a time"))), switchMap(function(items) { return forkJoin(items.map(function(i) { return from(c.mediaLibrary.getItems({ name: fromString(fromItemIdToString(i.id)), page: 1, count: STOCK_PHOTOS_LIMIT, orderBy: ORDER_BY_DESC, type: ApiFileTypes.IMAGE })).pipe(map(onNothing(getErrorText(t, items, s.insertFilesType))), filter(isT), map(onNothing(getErrorText(t, items, s.insertFilesType))), switchMap(function(images) { return images.length ? of(images).pipe(map(onNothing(getErrorText(t, items, s.insertFilesType))), filter(isT), map(onNothing(getErrorText(t, items, s.insertFilesType))), map(convertApiFilesToMLSelectedFiles), map(onNothing(getErrorText(t, items, s.insertFilesType))), map(function(v) { return isAcceptableType(c.acceptableTypes)(v.items); }), map(onNothing(wrongTypeErrorMessage(t, c.acceptableTypes, items.filter(function(item) { return item.isSelected; }).length > 1))), filter(isT), map(onNothing(getErrorText(t, items, s.insertFilesType))), map(fromSelectedFileToInsertFile(false)), catchError(always(of(IMAGE_UPLOAD_ERROR)))) : fromFetch(i.url).pipe(switchMap(function(res) { return from(res.blob()).pipe(filter(isT), map(onNothing(getErrorText(t, items, s.insertFilesType))), map(fromBlobToFile(i)), switchMap(function(file) { return from(c.mediaLibrary.uploadItem({ file: file, altTitle: i.altTitle, name: i.name })).pipe(switchMap(function(uploadedItem) { return from(c.stockPhotos.downloadItem(fromItemIdToString(i.id))).pipe(map(function(downloadedItem) { return downloadedItem.url ? uploadedItem : undefined; }), map(onNothing(getErrorText(t, items, s.insertFilesType))), filter(isT), map(onNothing(getErrorText(t, items, s.insertFilesType))), map(convertApiDataToSelectedFile), map(onNothing(getErrorText(t, items, s.insertFilesType))), map(function(v) { return isAcceptableType(c.acceptableTypes)([ v ]); }), map(onNothing(wrongTypeErrorMessage(t, c.acceptableTypes, items.filter(function(item) { return item.isSelected; }).length > 1))), filter(isT), map(onNothing(getErrorText(t, items, s.insertFilesType))), map(fromSelectedFileToInsertFile(true))); }), catchError(always(of(IMAGE_UPLOAD_ERROR)))); }), catchError(always(of(IMAGE_UPLOAD_ERROR)))); }), catchError(always(of(IMAGE_UPLOAD_ERROR)))); }), catchError(always(of(IMAGE_UPLOAD_ERROR)))); })).pipe(map(function(v) { return v.filter(isT); }), map(function(v) { return v.length ? v : undefined; }), map(onNothing(getErrorText(t, items, s.insertFilesType))), switchMap(function(v) { var data = v.flat(); return data.every(isImageUploadError) ? of(batchActions([ Actions.insertFilesError(), fetchError(getErrorText(t, items, s.insertFilesType)) ])) : goToInsertFilesOrImportToML(c, t, s, items, data); }), catchError(always(of(batchActions([ Actions.insertFilesError(), fetchError(getErrorText(t, items, s.insertFilesType)) ]))))); }), catchError(function(param) { var message = param.message; return of(batchActions([ Actions.insertFilesError(), fetchError(message) ])); })); })); }; export var epicCreator = combineEpics(init, insertFileEpic);