How to access redux repository from library?

I am creating a library for my responsive application that will handle all the API calls associated with the session (authenticate, register, update tokens, etc.), and this library will have to access the reducer store on its own to send actions. The reason for this is in that whenever a user access token expires using setTimeout, I need to call the creator of the action, which will call my API to update the token, and update the application reduction storage with new session data.

So, I was thinking of passing the send method to the library whenever I call it the first time, but I'm not sure if this is the best way to do this.

Or I could also create a method initialize()that will pass the send to the library.

import { authenticate } from 'libraries/session';

export function login(email, password) {
    return (dispatch) => {
        dispatch(loginRequest());

        return authenticate(email, password, dispatch) // dispatch is passed here, so I can use it later in the library
        .then(() => (
            dispatch(loginSuccess())
        ))
        .catch((json) => (
            dispatch(loginError());
        ));
    };
};

I have not tried this code yet, it is rather a brainstorm.

Do you have any suggestions on the best way to handle this?

Thank!

+4
source share
1 answer

My impulse will change the parameters:

import { authenticate } from 'libraries/session';


export function configureLogin(store) {
    var {dispatch, subscribe, getState} = store;
    return (email, password) => {
        dispatch(loginRequest());

        return authenticate(email, password, dispatch) // <- I don't why dispatch needs to be included, but I'll take ur word for it
        .then(() => dispatch(loginSuccess())
        .catch(json => dispatch(loginError()));
    };
};

// usage
import { configureLogin } from './service/login';
var store = createStore();
var login = configureLogin(store);
login(email, password);
+4
source

All Articles