Skip to content

Instantly share code, notes, and snippets.

@LBWright
Created September 30, 2019 13:15
Show Gist options
  • Select an option

  • Save LBWright/6bc1cc3f9f871fbc0845eb4af7f561c2 to your computer and use it in GitHub Desktop.

Select an option

Save LBWright/6bc1cc3f9f871fbc0845eb4af7f561c2 to your computer and use it in GitHub Desktop.

Revisions

  1. LBWright revised this gist Sep 30, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion thunks for zach
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ export const createWellsTask = ({ serverId }) => async (dispatch) => {
    });
    }
    };
    // Where it comes together
    // Where it comes together inside of container.js
    const mapDispatchToProps = {
    subscribeAppForAsset,
    unsubscribeAppFromAsset,
  2. LBWright created this gist Sep 30, 2019.
    56 changes: 56 additions & 0 deletions thunks for zach
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    // inside of my actions.js, my actions look like this...
    // because I'm creating a function that returns a function, I have access to dispatch
    // through the Thunk middleware. I technically have access to state too, as a second argument
    // Because I'm using dispatch here, this specific function can be bound to the redux store
    // You could maybe think about it like a singleton?

    export const createWellsTask = ({ serverId }) => async (dispatch) => {
    const payload = {
    properties: {
    operation: 'list_wells',
    witsml_server: serverId,
    },
    };
    dispatch({
    type: TYPES.CREATE_WELLS_TASK,
    });
    try {
    const response = await post('/v2/tasks', { task: payload });
    dispatch({
    type: TYPES.CREATE_WELLS_TASK_SUCCESS,
    payload: response.toJS(),
    });
    } catch (e) {
    dispatch({
    type: TYPES.CREATE_WELLS_TASK_ERROR,
    payload: e,
    });
    }
    };
    // Where it comes together
    const mapDispatchToProps = {
    subscribeAppForAsset,
    unsubscribeAppFromAsset,
    createWellsTask,
    };
    export default
    connect(
    mapStateToProps,
    mapDispatchToProps
    )(StreamMapper);
    /**************************/

    //it could also be done like this with no 'actions file' (using .then just for variance)

    const mapDispatchToProps = (dispatch) => {
    createWellsTask: (myArgs) => dispatch({type: MYTYPE, payload: myArgs})
    // You could break up your bound functions here and 'assemble them' inside of your component
    }

    export default
    connect(
    mapStateToProps,
    mapDispatchToProps
    )(StreamMapper);

    /************************/