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

Get currentUser state in component Updater #68

Closed
dgsunesen opened this issue Dec 16, 2016 · 3 comments
Closed

Get currentUser state in component Updater #68

dgsunesen opened this issue Dec 16, 2016 · 3 comments

Comments

@dgsunesen
Copy link

dgsunesen commented Dec 16, 2016

Hi,

So I have the following in my boilerplate file:

const reducers = combineReducers({
      root: updater,
      routing: routerReducer,
      user: userReducer,
      form: formReducer
    });

Where user contains the currentUser logged in.
How would I access that users property in a redux-elm components Updater?.. I have a saga in an Updater that needs the users id in order to call an endpoint in there.

In my components Updater I want to be able to do something like the following:

function* fetchLocations (): IterableIterator<Effect> {
  try {
    yield put( getLocations() );
    const locations = yield call(loadLocations, "http://myApiEndpoint/" + userId + "/locations");
    yield put( receiveLocations(locations) );
  }
  catch (error) {
    console.log(error.message, error.name);
    yield put( errorHandlerLocations(error));
  }
}

Where userId is the id from the currentUser logged in.

Best regards,
Dan

@tomkis
Copy link
Collaborator

tomkis commented Dec 19, 2016

I suppose your fetchLocations is being executed when some actions kicks in, right? You might want to keep the userId in the payload of the action so that you can easily pass it to fetchLocations saga.

@dgsunesen
Copy link
Author

declare function require(name: string): any;

import { takeEvery } from "redux-saga";
import { take, put, call, fork, select, Effect } from "redux-saga/effects";
import { getRoles, receiveRoles, errorHandler, getLocations, receiveLocations, errorHandlerLocations } from "./actions";
import { loadRoles, loadLocations } from "./effects";
import { Map } from "immutable";
import { GET_ROLES, RECEIVE_ROLES, GET_LOCATIONS, RECEIVE_LOCATIONS } from "./actions";

const { Updater } = require("redux-elm");

// Switch this for users account id
const tempAccountId: number = 3;

// Initial immutable model/state passed to the Updater.
type Init = Map<string, any>;
export const init: Init = Map({
  isGettingRoles: false,
  isGettingLocations: false
});

function* fetchRoles (): IterableIterator<Effect> {
  try {
    yield put( getRoles() );
    const roles = yield call(loadRoles, "https://pathToApiEndpoint/api/Roles");
    yield put( receiveRoles(roles) );
  }
  catch (error) {
    console.log(error.message, error.name);
    yield put( errorHandler(error));
  }
}

function* fetchLocations (): IterableIterator<Effect> {
  try {
    yield put( getLocations() );
    const locations = yield call(loadLocations, "pathToApiEndpoint/" + tempAccountId + "/locations");
    yield put( receiveLocations(locations) );
  }
  catch (error) {
    console.log(error.message, error.name);
    yield put( errorHandlerLocations(error));
  }
}

function* locationSaga() {
  yield* fetchLocations();
}

// Main saga passed to the Updater.
function* saga() {
  yield fork(locationSaga);
  yield* fetchRoles();
}

export default new Updater(init, saga)
  .case(GET_ROLES, model => model.set("isGettingRoles", true))
  .case(RECEIVE_ROLES, (model, {roles}) =>
    model
      .set("isGettingRoles", false)
      .set("roles", roles))
  .case(GET_LOCATIONS, model => model.set("isGettingLocations", true))
  .case(RECEIVE_LOCATIONS, (model, {locations}) =>
    model
      .set("isGettingLocations", false)
      .set("locations", locations))
  .toReducer();

So I'm getting locations once I enter the route with this component.

@tomkis
Copy link
Collaborator

tomkis commented Feb 23, 2017

Closing because of irrelevance. prism v4 does not deal with Sagas anymore.

@tomkis tomkis closed this as completed Feb 23, 2017
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

2 participants