From a6af043686e4375d7944e91cae3d5b63c59edab0 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 22 Mar 2018 16:29:31 -0400 Subject: Migrate to render props --- frontend/components/EditUser.js | 69 ++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 32 deletions(-) (limited to 'frontend/components/EditUser.js') diff --git a/frontend/components/EditUser.js b/frontend/components/EditUser.js index b885fe6..a7a8f1c 100644 --- a/frontend/components/EditUser.js +++ b/frontend/components/EditUser.js @@ -1,59 +1,64 @@ import React from 'react'; -import { compose } from 'react-apollo'; -import PropTypes from 'prop-types'; +import { Query, Mutation } from 'react-apollo'; import Form from './styles/Form'; -import { userEnhancer, updateUserEnhancer } from '../enhancers/enhancers'; +import { CURRENT_USER_QUERY, UPDATE_USER_MUTATION } from '../queries/index'; +import Error from './ErrorMessage'; class EditUser extends React.Component { - static propTypes = { - currentUser: PropTypes.object.isRequired, - updateUser: PropTypes.func.isRequired, - }; - state = { changes: {}, }; handleChange = e => { const { name, value } = e.target; - const { me } = this.props.currentUser; const changes = { ...this.state.changes, [name]: value, }; - if (value === me[e.target.name]) { - delete changes[name]; - } this.setState({ changes }); }; - handleSubmit = async e => { + handleSubmit = async (e, updateUser) => { e.preventDefault(); - await this.props.updateUser({ - variables: this.state.changes, - }); - this.props.currentUser.refetch(); + // only submit if there are real changes + if (Object.keys(this.state.changes).length === 0) return; + await updateUser(); this.setState({ changes: {} }); }; render() { - const { me } = this.props.currentUser; - if (!me) return

You must be logged in

; return ( -
- - - me: -
{JSON.stringify(me.name)}
- Change: -
{JSON.stringify(this.state.changes)}
-
+ + {({ data: { me } }) => { + if (!me) return

You must be logged in

; + return ( + + {(updateUser, { loading, error, called, data }) => ( +
this.handleSubmit(e, updateUser)}> + +
+ + + me: +
{JSON.stringify(me.name)}
+ Change: +
{JSON.stringify(this.state.changes)}
+
+ + )} +
+ ); + }} +
); } } -export default compose(userEnhancer, updateUserEnhancer)(EditUser); -export { EditUser }; +export default EditUser; -- cgit v1.3