Last active
July 25, 2021 06:52
-
-
Save MeetMartin/37198e889cc9f5dcfb4cd0b92985d7b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // src/store/effects/QuoteEffect.js | |
| import { AsyncEffect, Maybe, randomOf, keysOf } from '@7urtle/lambda'; | |
| import quotes from '../../../quotesList'; | |
| /** | |
| * Finds a random quote in an array of quotes and returns it. | |
| * | |
| * @HindleyMilner getRandomQuote :: [[a]] -> [a] | |
| * | |
| * @pure | |
| * @param {object} action global state action | |
| * @returns {AsyncEffect} AsyncEffect of Maybe of quote object | |
| * | |
| * @example | |
| * getRandomQuote(quotesList); | |
| * // => { | |
| * quote: 'If you can dream it, you can do it.' | |
| * author: 'Walt Disney' | |
| * } | |
| */ | |
| const getRandomQuote = quotes => | |
| (author => | |
| ({ | |
| 'quote': randomOf(quotes[author]), | |
| 'author': author | |
| }) | |
| ) | |
| (randomOf(keysOf(quotes))); | |
| /** | |
| * Ansynchronous request that maybe returns a random quote. | |
| * Internally it just uses a timeout to return a quote with 1 second delay. | |
| * | |
| * @HindleyMilner requestQuote :: object -> AsyncEffect.of(Maybe.of(object)) | |
| * | |
| * @pure | |
| * @param {object} action global state action | |
| * @returns {AsyncEffect} AsyncEffect of Maybe of quote object | |
| * | |
| * @example | |
| * requestQuote() | |
| * .trigger | |
| * (error => console.log(error)) | |
| * (MaybeQuote => | |
| * maybe | |
| * (() => console.log('Quote is Nothing.')) | |
| * (quote => console.log(`"${quote.quote}" by ${quote.author}`)) | |
| * (MaybeQuote) | |
| * ); | |
| */ | |
| export const requestQuote = action => | |
| AsyncEffect | |
| .of(reject => resolve => | |
| setTimeout(() => resolve( | |
| Maybe.of(getRandomQuote(quotes)) | |
| ), 1000) | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment