Skip to content

Instantly share code, notes, and snippets.

@PCreations
Created September 25, 2017 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PCreations/956c3d9fe69844f5ec4c893c1987824f to your computer and use it in GitHub Desktop.
Save PCreations/956c3d9fe69844f5ec4c893c1987824f to your computer and use it in GitHub Desktop.
import React from 'react';
import PropTypes from 'prop-types';
import withState from 'recompose/withState';
import withHandlers from 'recompose/withHandlers';
import compose from 'recompose/compose';
import { graphql } from 'react-apollo';
import SUBMIT_MESSAGE_MUTATION from './SubmitMessage.graphql';
import GET_MESSAGES_QUERY from './GetMessages.graphql';
const enhance = compose(
graphql(SUBMIT_MESSAGE_MUTATION, {
props: ({ ownProps, mutate }) => ({
sendMessage: content => mutate({
variables: { content },
update: (store, { data: { submitMessage } }) => {
const data = store.readQuery({ query: GET_MESSAGES_QUERY });
data.getMessages = [
...data.getMessages.filter(msg => msg.id !== submitMessage.id),
submitMessage,
];
store.writeQuery({ query: GET_MESSAGES_QUERY, data });
},
}),
}),
}),
withState('value', 'updateValue', ''),
withHandlers({
onChange: props => event => {
props.updateValue(event.target.value);
},
onSubmit: props => event => {
event.preventDefault();
props.updateValue('');
props.sendMessage(props.value);
},
})
);
const MessageInput = enhance(({ value, onChange, onSubmit }) =>
<form onSubmit={onSubmit}>
<label>Value
<input type="text" value={value} onChange={onChange} />
<input type="submit"/>
</label>
</form>
)
MessageInput.propTypes = {
sendMessage: PropTypes.func.isRequired,
};
export default MessageInput;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment