This file contains 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/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. | |
* |
This file contains 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/NetlifyFunction.js | |
import { AsyncEffect, isEqual, Maybe, map } from '@7urtle/lambda'; | |
import axios from 'axios'; | |
const api_url = isEqual('development')(process.env.NODE_ENV) ? 'http://localhost:5000' : '/.netlify/functions'; | |
/** | |
* Ansynchronous post request to a Netlify Function on an input path | |
* that maybe returns a returns response data based on input data. |
This file contains 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] |
This file contains 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
return ({ | |
...state, // add everything from the previous state | |
quote: action.payload.quote, // change quote based on payload | |
author: action.payload.author // change author based on payload | |
}); // return new state |
This file contains 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/reducers.js | |
import types from "./types"; | |
const initialState = { | |
quote: 'Creativity is intelligence having fun.', | |
author: 'Albert Einstein' | |
}; | |
const reducer = (state = initialState, action) => { |
This file contains 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/actions.js | |
import types from "./types"; | |
export const useActions = (state, dispatch) => ({ | |
requestRandomQuote: payload => dispatch({type: types.REQUEST_RANDOM_QUOTE, payload: payload}) | |
}); |
This file contains 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/types.js | |
const types = { | |
REQUEST_RANDOM_QUOTE: 'REQUEST_RANDOM_QUOTE', | |
RECEIVE_RANDOM_QUOTE: 'RECEIVE_RANDOM_QUOTE' | |
}; | |
export default types; |
This file contains 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
import React, { useState, useEffect } from 'react'; | |
import axios from 'axios'; | |
const MyComponent = () => { | |
const [numberOfUsers, setNumberOfUsers] = useState(0); | |
useEffect(() => { | |
axios.get('/users/count') | |
.then(response => setNumberOfUsers(response.data.count)) | |
.catch(error => console.log('that is an error my friends: ' + error)); | |
}, []); |
This file contains 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
import React, { useContext } from 'react'; | |
import { StoreContext } from '../store/StoreContext'; | |
const MyComponent = () => { | |
const { state, actions } = useContext(StoreContext); | |
const myAction = () => actions.triggerAction('data'); | |
return( | |
<> | |
Number of button clicks: {state.counter}. |
This file contains 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
const expression = input => input.toLowerCase(); | |
const expression2 = function(input) { | |
return input.toLowerCase(); | |
}; | |
const statement = () => console.log('hello world'); | |
const statement2 = function() { | |
console.log('hello world'); | |
}; |