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
| export interface State { | |
| messages: string[]; | |
| isFormValid: boolean; | |
| isLoadingQuote: boolean; | |
| hasQuoteError: boolean; | |
| } | |
| export const defaultState: State = { | |
| messages: [], | |
| isFormValid: true, |
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
| export default () => ( | |
| <Provider | |
| defaultState={defaultState} | |
| reducer={rootReducer} | |
| > | |
| <Status /> | |
| <MessageForm /> | |
| <MessageList /> | |
| </Provider> | |
| ); |
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
| const mapStateToProps = ({ isFormValid, hasQuoteError, isLoadingQuote }: StateProps) => ({ | |
| isFormValid, | |
| hasQuoteError, | |
| isLoadingQuote, | |
| }); | |
| const mapDispatchToProps = (dispatch: React.Dispatch<Action>) => ({ | |
| addMessage: (message: string) => dispatch(addMessage(message)), | |
| addRonSwansonQuote: () => dispatch(addRonSwansonQuote()), | |
| }); |
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
| interface Context { | |
| state: State; | |
| dispatch: React.Dispatch<Action>; | |
| } | |
| const StateContext = React.createContext<Context>({ | |
| state: defaultState, | |
| dispatch: defaultDispatch, | |
| }); |
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
| export const Provider: React.FC<ProviderProps> = ({ | |
| reducer, | |
| children, | |
| }) => { | |
| const [state, dispatch] = React.useReducer(reducer, defaultState); | |
| return ( | |
| <StateContext.Provider value={{ | |
| state, | |
| dispatch, |
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
| export const connect = <TStateProps, TDispatchProps, TOwnProps = {}>( | |
| mapStateToProps?: (state: State, ownProps: TOwnProps) => TStateProps, | |
| mapDispatchToProps?: (dispatch: AugmentedDispatch, ownProps: TOwnProps) => TDispatchProps, | |
| ) => | |
| (Component: React.ComponentType<TStateProps & TDispatchProps & TOwnProps>) => ( | |
| (props: TOwnProps) => | |
| <StateContext.Consumer> | |
| {({ state, dispatch }) => ( | |
| <Component | |
| {...props} |
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
| export const addRonSwansonQuote = () => | |
| (dispatch: React.Dispatch<Action>) => { | |
| dispatch(setQuoteLoading()); | |
| return fetch('https://ron-swanson-quotes.herokuapp.com/v2/quotes') | |
| .then(res => res.json()) | |
| .then(([quote]: string[]) => | |
| dispatch(addMessage(quote)), | |
| ) | |
| .catch(() => dispatch(setQuoteError())); |
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
| export type Thunk = (dispatch: React.Dispatch<Action>, state: State) => void; | |
| export type AugmentedDispatch = React.Dispatch<Thunk | Action>; | |
| const augmentDispatch = (dispatch: React.Dispatch<Action>, state: State) => | |
| (input: Thunk | Action) => | |
| input instanceof Function ? input(dispatch, state) : dispatch(input); | |
| export const Provider: React.FC<ProviderProps> = ({ | |
| reducer, | |
| children, |
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
| const rootReducer: React.Reducer<State, Action> = (state, action) => { | |
| if (isAddMessage(action)) { | |
| const { message } = action.payload; | |
| return { | |
| ...state, | |
| isLoadingQuote: false, | |
| hasQuoteError: false, | |
| isFormValid: !!message, | |
| messages: [ |
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
| export const addMessage = (message: string) => ({ | |
| type: ADD_MESSAGE, | |
| payload: { | |
| message, | |
| }, | |
| }); |