Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatch a saga in a saga #4

Open
amad9720 opened this issue Jan 30, 2020 · 0 comments
Open

Dispatch a saga in a saga #4

amad9720 opened this issue Jan 30, 2020 · 0 comments

Comments

@amad9720
Copy link

amad9720 commented Jan 30, 2020

Hello I'm of your students in udemy

After the course I started my own application, in the same way as the ecommerce we built together, I have users and companies, in the signUp I want to create the user and then the company.
I have two sagas , the userSagas and the companiesSagas.
What I want is to create a company with an actionCompany inside of the onSignUp method in userSagas just after the account creation and before the user is signed In.

CompaniesActions

import CompanyActionTypes from './company.types';

export const createCompanyStart = (user, companyData) => ({
    type: CompanyActionTypes.SIGN_UP_START,
    payload: {user, companyData}
});

export const createCompanySuccess = (createdCompanyData) => ({
    type: CompanyActionTypes.SIGN_UP_SUCCESS,
    payload: createdCompanyData
});

export const createCompanyFailure = error => ({
    type: CompanyActionTypes.SIGN_UP_FAILURE,
    payload: error
});

CompaniesSagas

import { takeLatest, put, all, call } from 'redux-saga/effects';

import CompanyActionTypes from './company.types';

import {
  createCompanySuccess,
  createCompanyFailure,
} from './company.actions';

import {createCompanyDocument} from '../../firebase/firebase.company';

export function* getSnapshotFromUserCompany(userAuth, companyData) {
  try {
    const userRef = yield call(
      createCompanyDocument,
      userAuth,
      companyData
    );
    const userSnapshot = yield userRef.get();
    yield put(createCompanySuccess({ id: userSnapshot.id, ...userSnapshot.data() }));
  } catch (error) {
    yield put(createCompanyFailure(error));
  }
}

export function* onCreateCompanyStart() {
  yield takeLatest(CompanyActionTypes.CREATE_COMPANY_START, getSnapshotFromUserCompany);
}

export function* companySagas() {
  yield all([
    call(onCreateCompanyStart),
  ]);
}

userSagas

import {createCompanyStart} from "../company/company.actions";

export function* getSnapshotFromUserAuth(userAuth, additionalData) {
  try {
    const userRef = yield call(
            createUserProfileDocument,
            userAuth,
            additionalData.userInfos
        )
    ;
    const userSnapshot = yield userRef.get();
    yield put(signInSuccess({ id: userSnapshot.id, ...userSnapshot.data() }));
   /*===  yield put(createCompanyStart(userAuth, additionalData)); ===== this is the part I added but I don't feel it's the good way */ 
  } catch (error) {
    yield put(signInFailure(error));
  }
}

By the way the course is awesome, I feel more knoledgeable on how to work with React and it's tools... thank you for that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant