Last active
October 12, 2016 03:22
-
-
Save RyanCCollins/5190029024cbdde99c0e393cb38d632d to your computer and use it in GitHub Desktop.
GraphQL, Rails, React: https://medium.com/@ryancollinsio/building-a-full-on-graphql-app-b261f6cfea93#.qtjma57sx
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
class CreateEvent extends Component { | |
// ... | |
handleSubmit() { | |
const { | |
actions, | |
mutate, | |
fields, | |
guestList, | |
user, | |
} = this.props; | |
const variables = { | |
variables: CreateEventActionCreators.fieldsToData( | |
fields, | |
guestList, | |
user | |
), | |
}; | |
mutate(variables) | |
.then(res => { | |
if (!res.data) { | |
throw new Error('An error has occured.'); | |
} | |
actions.createEventMessage('Event submitted succe...'); | |
setTimeout(() => { | |
this.context.router.push('/events'); | |
}, 2000); | |
}) | |
.catch(err => { | |
actions.createEventError(err.message); | |
}); | |
} | |
// ... | |
render() { | |
const { | |
fields, | |
guests, | |
hosts, | |
loading, | |
eventTypes, | |
errorMessage, | |
message, | |
guestList, | |
} = this.props; | |
return ( | |
<Section className={styles.createEvent}> | |
<Heading align="center"> | |
Create an Event | |
</Heading> | |
{loading ? | |
<LoadingIndicator isLoading={loading} /> | |
: | |
<Section> | |
<EventForm | |
{...fields} | |
onAddGuest={this.handleAddingGuest} | |
onRemoveGuest={this.handleRemovingGuest} | |
guestList={guestList} | |
eventTypes={eventTypes} | |
pastHosts={hosts} | |
pastGuests={guests} | |
onSubmit={this.handleSubmit} | |
/> | |
</Section> | |
} | |
{errorMessage && | |
<ToastMessage | |
message={errorMessage} | |
status="critical" | |
onClose={() => this.handleClear('error')} | |
/> | |
} | |
{message && | |
<ToastMessage | |
message={message} | |
onClose={() => this.handleClear('message')} | |
/> | |
} | |
</Section> | |
); | |
} | |
} | |
CreateEvent.propTypes = { | |
user: PropTypes.object.isRequired, | |
submitEvent: PropTypes.func.isRequired, | |
mutate: PropTypes.func.isRequired, | |
fields: PropTypes.object.isRequired, | |
eventTypes: PropTypes.array, | |
guests: PropTypes.object, | |
hosts: PropTypes.object, | |
loading: PropTypes.bool.isRequired, | |
errorMessage: PropTypes.string, | |
message: PropTypes.string, | |
actions: PropTypes.object.isRequired, | |
guestList: PropTypes.array, | |
}; | |
const Container = cssModules(CreateEvent, styles); | |
const createEventQuery = gql` | |
query createEvent{ | |
hosts { | |
name | |
} | |
guests { | |
name | |
} | |
eventTypes | |
} | |
`; | |
const createEventMutation = gql` | |
mutation createEvent($authToken: String!, $event: EventInput) { | |
CreateEvent(input: { auth_token: $authToken, event: $event }) { | |
event { | |
id | |
name | |
type: event_type | |
guests { | |
name | |
} | |
host { | |
name | |
} | |
} | |
} | |
} | |
`; | |
const FormContainer = reduxForm({ | |
form: 'CreateEvent', | |
fields: formFields, | |
validate: validation, | |
})(Container); | |
const ContainerWithData = graphql(createEventQuery, { | |
props: ({ data: { guests, eventTypes, hosts, loading } }) => ({ | |
loading, | |
guests, | |
eventTypes, | |
hosts, | |
}), | |
})(FormContainer); | |
const ContainerWithMutations = graphql(createEventMutation)(ContainerWithData); | |
export default connect( | |
mapStateToProps, | |
mapDispatchToProps | |
)(ContainerWithMutations); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment