Add photos saga work.

This commit is contained in:
Patrick Fic
2025-10-08 16:20:23 -07:00
parent 83993be284
commit 620b5135d1
5 changed files with 70 additions and 158 deletions

View File

@@ -1,106 +1,65 @@
import * as FileSystem from "expo-file-system";
import Constants from "expo-constants";
import * as ImagePicker from "expo-image-picker";
import { all, call, put, select, takeLatest } from "redux-saga/effects";
import { handleUpload } from "../../util/document-upload.utility";
import {
uploadPhotosFailure,
uploadPhotosSuccess,
removeAllPhotos,
} from "./photos.actions";
import { selectBodyshop } from "../user/user.selectors";
import { mediaUploadStart } from "./photos.actions";
import PhotosActionTypes from "./photos.types";
export function* onRemovePhotos() {
yield takeLatest(PhotosActionTypes.REMOVE_PHOTOS, removePhotosAction);
export function* onOpenImagePicker() {
yield takeLatest(PhotosActionTypes.OPEN_IMAGE_PICKER, openImagePickerAction);
}
export function* removePhotosAction({ payload: photoIdsToRemove }) {
export function* openImagePickerAction(jobid) {
try {
const photos = yield select((state) => state.photos.photos);
const fps = photos
.filter((p) => !photoIdsToRemove.includes(p.id))
.map((p) => p.uri);
const all = [];
fps.forEach((f) => all.push(FileSystem.deleteAsync(f)));
yield Promise.all(all);
} catch (error) {
console.log("Saga Error: removePhotos", error);
}
}
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;
if (Constants.platform.ios) {
const cameraRollStatus =
yield ImagePicker.requestMediaLibraryPermissionsAsync();
const cameraStatus = yield ImagePicker.requestCameraPermissionsAsync();
if (
cameraRollStatus.status !== "granted" ||
cameraStatus.status !== "granted"
) {
alert("Sorry, we need these permissions to make this work!");
return;
}
}
let result = yield ImagePicker.launchImageLibraryAsync({
mediaTypes: ["images", "videos"],
aspect: [4, 3],
quality: 1,
allowsMultipleSelection: true,
});
const all = [];
fps.forEach((f) => all.push(FileSystem.deleteAsync(f)));
yield Promise.all(all);
console.log("All photos deleted.");
if (!(result.canceled)) {
yield put(mediaUploadStart(result.assets));
}
} catch (error) {
console.log("Saga Error: onRemoveAllPhotos", error);
console.log("Saga Error: open Picker", error);
}
}
export function* onUploadAllPhotos() {
yield takeLatest(
PhotosActionTypes.UPLOAD_ALL_PHOTOS_START,
uploadAllPhotosAction
);
export function* onMediaUploadStart() {
yield takeLatest(PhotosActionTypes.MEDIA_UPLOAD_START, mediaUploadStartAction);
}
export function* uploadAllPhotosAction() {
export function* mediaUploadStartAction(photos) {
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(function (p) {
actions.push(
call(
handleUpload,
...[
{
uri: p.uri,
onError: handleOnError,
onProgress: handleOnProgress,
onSuccess: handleOnSuccess,
},
{
bodyshop: bodyshop,
jobId: p.jobId,
uploaded_by: user.currentUser.email,
photo: p,
},
]
)
);
});
yield all(actions);
yield put(removeAllPhotos());
yield put(uploadPhotosSuccess());
console.log("Got to the Photo Saga.", photos);
console.log("upload", photos)
//get bodyshop state
const bodyshop = yield select(selectBodyshop);
if (bodyshop.uselocalmediaserver) {
//upload to LMS
} else {
//Upload to img proxy
}
} catch (error) {
console.log("Saga Error: uploadAllPhotosAction", error);
yield put(uploadPhotosFailure(error));
console.log("Saga Error: open upload", error);
}
}
function handleOnError(...props) {
console.log("HandleOnError", props);
}
function handleOnProgress(...props) {
console.log("HandleOnProgress", props);
}
function handleOnSuccess(...props) {
console.log("handleOnSuccess", props);
}
export function* photosSagas() {
yield all([call(onRemoveAllPhotos), call(onUploadAllPhotos)]);
yield all([call(onOpenImagePicker), call(onMediaUploadStart)]);
}