-
-
Save pleunv/9a932cdf0e6a87a748ed0ff546dc6a5e to your computer and use it in GitHub Desktop.
This file contains 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 { gql, graphql } from 'react-apollo'; | |
import { Formik } from 'formik'; | |
const validate = values => { | |
return {}; //simplified for now | |
}; | |
const getInitialValues = user => ({ | |
firstName: user && user.firstName || '', | |
lastName: user && user.lastName || '', | |
userId: user && user.userId || '' | |
}); | |
const doSubmit = (values, { setSubmitting, setErrors }) => { | |
console.log('UserForm submitted', values); //simplified for now | |
}; | |
const editUserQuery = gql` | |
query GetUserForEdit($userId: ID!) { | |
user(userId: $userId) { | |
firstName | |
lastName | |
userId | |
} | |
} | |
`; | |
const UserForm = ({ data: { user, loading, error } }) => { | |
if (loading) return null; | |
console.log('in userform', user); | |
return ( | |
<Formik | |
enableReinitialize | |
initialValues={getInitialValues(user)} | |
validate={validate} | |
onSubmit={doSubmit} | |
render={({ values, errors, handleChange, handleBlur, handleSubmit, isSubmitting }) => { | |
console.log('in formik', values); | |
return ( | |
<form className="form-horizontal" role="form" onSubmit={handleSubmit}> | |
<div className="row"> | |
<div className="col-md-4"> | |
<input | |
type="text" | |
name="userId" | |
id="userId" | |
value={values.userId} | |
onChange={handleChange} | |
onBlur={handleBlur} | |
/> | |
</div> | |
</div> | |
</form> | |
); | |
}} | |
/> | |
); | |
}; | |
/* | |
I'm using next-routes (https://github.com/fridays/next-routes) for custom routing | |
When I include <UserForm> in a screen, I do it thusly: | |
<UserForm userId={props.url.query.userId} /> | |
So the userId is part of the props passed to the options function below, and is | |
used to fill in the userId variable for the graphql query. | |
*/ | |
export default graphql(editUserQuery, { | |
options: ({ userId }) => ({ variables: { userId } }) | |
})(UserForm); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment