Created
June 18, 2017 12:09
-
-
Save idkjs/f95f8fdedb6cd8e64aefa1f1f9592d1c to your computer and use it in GitHub Desktop.
Recompose clean code with const
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
| import React from 'react'; | |
| import { compose, withHandlers } from 'recompose'; | |
| import { injectIntl } from 'react-intl'; | |
| import { graphql } from 'react-apollo'; | |
| import gql from 'graphql-tag'; | |
| type MutationButtonType = { | |
| toggleMutation: Function, | |
| label: React.Element<any>, | |
| }; | |
| function MutationButton({ toggleMutation, label }: MutationButtonType): React.Element<any> { | |
| return ( | |
| <button onClick={toggleMutation}> | |
| {label} | |
| </button> | |
| ); | |
| } | |
| const withMutation = graphql(gql` | |
| mutation toggleMutation($someId: String!) { | |
| toggle(someId: $someId) | |
| } | |
| `) | |
| const mutationHandler = withHandlers({ | |
| toggleMutation: ({ mutate, _id, intl }: { mutate: Function, _id: string, intl: Object }): Function => { | |
| return (): Promise<any> => { | |
| return mutate({ | |
| variables: { | |
| someId: _id, | |
| }, | |
| }).then((data: string) => { | |
| console.log(data, 'Return value'); | |
| }).catch((e: Object) => { | |
| console.error(e, 'Error'); | |
| }); | |
| }; | |
| } | |
| }) | |
| export default compose( | |
| injectIntl, | |
| withMutation, | |
| mutationHandler | |
| )(MutationButton); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment