import * as FileSystem from "expo-file-system"; import { all, call, select, takeLatest } from "redux-saga/effects"; import { handleUpload } from "../../util/document-upload.utility"; import PhotosActionTypes from "./photos.types"; export function* onRemoveAllPhotos() { yield takeLatest(PhotosActionTypes.REMOVE_ALL_PHOTOS, removeAllPhotosAction); } export function* removeAllPhotosAction() { try { //Physically delete all photosSagas. const fps = (yield FileSystem.readDirectoryAsync( FileSystem.documentDirectory + "photos" )).map((f) => { return FileSystem.documentDirectory + "photos/" + f; }); const all = []; fps.forEach((f) => all.push(FileSystem.deleteAsync(f))); yield Promise.all(all); console.log("All photos deleted."); } catch (error) { console.log("Saga Error: onRemoveAllPhotos", error); } } export function* onUploadAllPhotos() { yield takeLatest( PhotosActionTypes.UPLOAD_ALL_PHOTOS_START, uploadAllPhotosAction ); } export function* uploadAllPhotosAction() { try { const photos = yield select((state) => state.photos.photos); const bodyshop = yield select((state) => state.user.bodyshop); const user = yield select((state) => state.user); const actions = []; photos.forEach(async (p) => actions.push( handleUpload( { file: await (await fetch(p.uri)).blob(), onError: (props) => { console.log("Error Callback", props); }, onProgress: (props) => { console.log("Progress Calback", props); }, onSuccess: (props) => { console.log("Success Calback", props); }, }, { bodyshop: bodyshop, jobId: p.jobId, uploaded_by: user.currentUser.email, callback: (props) => { console.log("Context Callback", props); }, photo: { ...p, name: p.uri.substring(p.uri.lastIndexOf("/") + 1), }, } ) ) ); yield Promise.all(actions); } catch (error) { console.log("Saga Error: onRemoveAllPhotos", error); } } export function* photosSagas() { yield all([call(onRemoveAllPhotos), call(onUploadAllPhotos)]); }