export const signInSaga = function * () {
const auth = firebase.auth();
const action = yield take(SIGN_IN_REQUEST);
const user = yield call([auth, auth.signInWithEmailAndPassword], action.payload.email, action.payload.password);
yield put({
type: SIGN_IN_SUCCESS,
payload: user
});
yield put(replace("/"));
};
export const signOutSaga = function * () {
const auth = firebase.auth();
yield apply(auth, auth.signOut);
}
export const signUpSaga = function * () {
const auth = firebase.auth();
while (true) {
const action = yield take(SIGN_UP_REQUEST);
try {
const user = yield call([auth, auth.createUserWithEmailAndPassword], action.payload.email, action.payload.password);
yield put({
type: SIGN_UP_SUCCESS,
payload: {
user
}
});
firebase.database().ref("users/" + user.uid).set({
email: user.email
});
yield put(replace("/"));
} catch (error) {
yield put({
type: SIGN_UP_ERROR,
error
});
}
}
};
const createAuthChannel = () => eventChannel(emit => firebase.auth().onAuthStateChanged(user => emit({ user })))
export const watchStatusChangeSaga = function * () {
const chan = yield call(createAuthChannel);
while (true) {
const { user } = yield take(chan);
if (user) {
yield put({
type: SIGN_IN_SUCCESS,
payload: { user }
});
} else {
yield put({
type: SIGN_OUT_SUCCESS,
payload: { user }
});
yield put(replace('/'));
}
}
}
export const saga = function * () {
yield all([
takeEvery(SIGN_IN_REQUEST, signInSaga),
takeEvery(SIGN_OUT_REQUEST, signOutSaga),
signUpSaga(),
watchStatusChangeSaga()
]);
};
export const fetchAllProductsToCartSaga = function * () {
const userUid = yield select(state => state["auth"].user.uid);
const productsRef = firebase.database().ref("users/" + userUid + "/products");
const products = yield call([productsRef, productsRef.once], "value");
yield put({
type: FETCH_ALL_PRODUCTS_TO_CART_SUCCESS,
payload: products.val()
});
};
export const addProductToCartSaga = function * (action) {
const { productUid, userUid } = action.payload;
const productRef = firebase.database().ref(`catalog/${productUid}`);
const product = yield call([productRef, productRef.once], "value");
yield put({
type: ADD_PRODUCT_TO_CART_SUCCESS,
payload: {
productUid,
product: product.val()
}
});
pushProductToUser(productUid, userUid);
};
export const saga = function * () {
yield all([
takeEvery(FETCH_ALL_PRODUCTS_TO_CART_REQUEST, fetchAllProductsToCartSaga),
takeEvery(ADD_PRODUCT_TO_CART_REQUEST, addProductToCartSaga)
]);
}
import { saga as authSaga } from "../ducks/auth";
import { saga as catalogSaga } from "../ducks/catalog";
import { saga as usersSaga } from "../ducks/users";
import { saga as cartSaga } from "../ducks/cart";
import { all } from "redux-saga/effects";
export default function * rootSaga() {
yield all([
authSaga(),
usersSaga(),
catalogSaga(),
cartSaga()
]);
}
const sagaMiddleware = createSagaMiddleware();
const enhancer = applyMiddleware(sagaMiddleware, routerMiddleware(history));
const store = createStore(reducer, window.__REDUX_DEVTOOLS_EXTENSION__(), enhancer);
sagaMiddleware.run(rootSaga);
export default store;
try {
const data = yield call(...);
// some stuff
} catch e {
// handle error
}
import { all, fork } from 'redux-saga/effects';
export default function * rootSaga() {
yield all([
authSaga,
usersSaga,
catalogSaga,
cartSaga,
].map(saga => fork(saga));
}
export const fetchAllProductsToCartSaga = function * () {
const userUid = yield select(state => state["auth"].user.uid); // !!!
const productsRef = firebase.database().ref("users/" + userUid + "/products");
const products = yield call([productsRef, productsRef.once], "value");
yield put({
type: FETCH_ALL_PRODUCTS_TO_CART_SUCCESS,
payload: products.val()
});
};