Skip to content

Instantly share code, notes, and snippets.

@idkjs
Created June 18, 2017 12:09
Show Gist options
  • Select an option

  • Save idkjs/f95f8fdedb6cd8e64aefa1f1f9592d1c to your computer and use it in GitHub Desktop.

Select an option

Save idkjs/f95f8fdedb6cd8e64aefa1f1f9592d1c to your computer and use it in GitHub Desktop.
Recompose clean code with const
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