Added photos reducer.
This commit is contained in:
24
redux/photos/photos.actions.js
Normal file
24
redux/photos/photos.actions.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import PhotosActionTypes from "./photos.types";
|
||||
|
||||
export const addPhoto = (photo) => ({
|
||||
type: PhotosActionTypes.ADD_PHOTO,
|
||||
payload: photo,
|
||||
});
|
||||
|
||||
export const removePhotos = (photoIds) => ({
|
||||
type: PhotosActionTypes.REMOVE_PHOTOS,
|
||||
payload: photoIds,
|
||||
});
|
||||
|
||||
export const removeAllPhotos = () => ({
|
||||
type: PhotosActionTypes.REMOVE_ALL_PHOTOS,
|
||||
});
|
||||
|
||||
export const uploadAllPhotos = () => ({
|
||||
type: PhotosActionTypes.UPLOAD_ALL_PHOTOS_START,
|
||||
});
|
||||
|
||||
export const uploadSelectedPhotos = (photoIds) => ({
|
||||
type: PhotosActionTypes.UPLOAD_SELECTED_PHOTOS_START,
|
||||
payload: photoIds,
|
||||
});
|
||||
44
redux/photos/photos.reducer.js
Normal file
44
redux/photos/photos.reducer.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import PhotosActionTypes from "./photos.types";
|
||||
|
||||
const INITIAL_STATE = {
|
||||
photos: [],
|
||||
uploadInProgress: false,
|
||||
uploadError: null,
|
||||
};
|
||||
|
||||
const photosReducer = (state = INITIAL_STATE, action) => {
|
||||
switch (action.type) {
|
||||
case PhotosActionTypes.ADD_PHOTO:
|
||||
return {
|
||||
...state,
|
||||
photos: [...state.photos, action.payload],
|
||||
};
|
||||
case PhotosActionTypes.REMOVE_ALL_PHOTOS:
|
||||
return {
|
||||
...state,
|
||||
photos: [],
|
||||
};
|
||||
case PhotosActionTypes.REMOVE_PHOTOS:
|
||||
return {
|
||||
...state,
|
||||
photos: state.photos.filter((p) => !action.payload.includes(p.id)),
|
||||
};
|
||||
case PhotosActionTypes.UPLOAD_PHOTO_FAILURE:
|
||||
return {
|
||||
...state,
|
||||
uploadInProgress: false,
|
||||
uploadError: action.payload,
|
||||
};
|
||||
case PhotosActionTypes.UPLOAD_ALL_PHOTOS_START:
|
||||
case PhotosActionTypes.UPLOAD_SELECTED_PHOTOS_START:
|
||||
return {
|
||||
...state,
|
||||
uploadInProgress: true,
|
||||
uploadError: null,
|
||||
};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export default photosReducer;
|
||||
29
redux/photos/photos.sagas.js
Normal file
29
redux/photos/photos.sagas.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { all, call, takeLatest } from "redux-saga/effects";
|
||||
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);
|
||||
//yield put(signInFailure(error));
|
||||
//logImEXEvent("redux_sign_in_failure", { user: email, error });
|
||||
}
|
||||
}
|
||||
|
||||
export function* photosSagas() {
|
||||
yield all([call(onRemoveAllPhotos)]);
|
||||
}
|
||||
16
redux/photos/photos.selectors.js
Normal file
16
redux/photos/photos.selectors.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import { createSelector } from "reselect";
|
||||
|
||||
const selectPhotosState = (state) => state.photos;
|
||||
|
||||
export const selectPhotos = createSelector(
|
||||
[selectPhotosState],
|
||||
(photos) => photos.photos
|
||||
);
|
||||
export const selectUploadInProgress = createSelector(
|
||||
[selectPhotosState],
|
||||
(photos) => photos.uploadInProgress
|
||||
);
|
||||
export const selectUploadError = createSelector(
|
||||
[selectPhotosState],
|
||||
(photos) => photos.uploadError
|
||||
);
|
||||
10
redux/photos/photos.types.js
Normal file
10
redux/photos/photos.types.js
Normal file
@@ -0,0 +1,10 @@
|
||||
const PhotosActionTypes = {
|
||||
ADD_PHOTO: "ADD_PHOTO",
|
||||
REMOVE_PHOTOS: "REMOVE_PHOTOS",
|
||||
REMOVE_ALL_PHOTOS: "REMOVE_ALL_PHOTOS",
|
||||
UPLOAD_SELECTED_PHOTOS_START: "UPLOAD_SELECTED_PHOTOS_START",
|
||||
UPLOAD_ALL_PHOTOS_START: "UPLOAD_ALL_PHOTOS_START",
|
||||
UPLOAD_PHOTO_SUCCESS: "UPLOAD_PHOTO_SUCCESS",
|
||||
UPLOAD_PHOTO_FAILURE: "UPLOAD_PHOTO_FAILURE",
|
||||
};
|
||||
export default PhotosActionTypes;
|
||||
@@ -2,12 +2,13 @@ import AsyncStorage from "@react-native-community/async-storage";
|
||||
import { combineReducers } from "redux";
|
||||
import { persistReducer } from "redux-persist";
|
||||
import appReducer from "./app/app.reducer";
|
||||
import photosReducer from "./photos/photos.reducer";
|
||||
import userReducer from "./user/user.reducer";
|
||||
|
||||
const persistConfig = {
|
||||
key: "root",
|
||||
storage: AsyncStorage,
|
||||
whitelist: [],
|
||||
whitelist: ["photos"],
|
||||
blacklist: ["user", "app"],
|
||||
// whitelist: ["messaging", "tech", "application"],
|
||||
// blacklist: ["user", "email", "modals"],
|
||||
@@ -16,6 +17,7 @@ const persistConfig = {
|
||||
const rootReducer = combineReducers({
|
||||
user: userReducer,
|
||||
app: appReducer,
|
||||
photos: photosReducer,
|
||||
});
|
||||
|
||||
export default persistReducer(persistConfig, rootReducer);
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { all, call } from "redux-saga/effects";
|
||||
import { appSagas } from "./app/app.sagas";
|
||||
import { photosSagas } from "./photos/photos.sagas";
|
||||
import { userSagas } from "./user/user.sagas";
|
||||
|
||||
export default function* rootSaga() {
|
||||
yield all([call(userSagas), call(appSagas)]);
|
||||
yield all([call(userSagas), call(appSagas), call(photosSagas)]);
|
||||
}
|
||||
|
||||
@@ -10,12 +10,11 @@ const sagaMiddleWare = createSagaMiddleware();
|
||||
|
||||
const middlewares = [sagaMiddleWare];
|
||||
if (process.env.NODE_ENV === "development") {
|
||||
middlewares
|
||||
.push
|
||||
// createLogger({
|
||||
// collapsed: true,
|
||||
// })
|
||||
();
|
||||
middlewares.push(
|
||||
createLogger({
|
||||
collapsed: true,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
//Add in for React Native Debugger.
|
||||
|
||||
Reference in New Issue
Block a user