Skip to content

Instantly share code, notes, and snippets.

@MeetMartin
Last active July 30, 2021 02:07
Show Gist options
  • Save MeetMartin/5d0dcd3e874bb6e39628a5b8957311e8 to your computer and use it in GitHub Desktop.
Save MeetMartin/5d0dcd3e874bb6e39628a5b8957311e8 to your computer and use it in GitHub Desktop.
// /src/store/hooks/QuoteHook.js
import { maybe } from '@7urtle/lambda';
import types from "../types";
import { requestQuote, getQuoteFromNetlify } from '../effects/QuoteEffect';
/**
* getQuote is a hook that processes requesting a quote through QuoteEffect asynchronously.
*
* It logs error on failure of QuoteEffect or in case that QuoteEffect result is Nothing (null,
* undefined, or empty).
*
* On success it dispatches RECEIVE_RANDOM_QUOTE to reducer with the value of the quote.
*
* @impure
* @param {function} dispatch // dispatch function of the reducer middleware
* @param {object} action // action object containing request data
* @returns {null}
*/
export const getQuote = dispatch => action =>
requestQuote(action) // returns AsyncEffect, can be replaced by getQuoteFromNetlify(action)
.trigger
(console.error) // error in AsyncEffect
(maybe // success in AsyncEffect
(() => console.error('Requesting quote returned invalid data,')) // Maybe result is Nothing
(value => dispatch({type: types.RECEIVE_RANDOM_QUOTE, payload: value})) // Maybe result is Just
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment