UNPKG

@api-platform/client-generator

Version:

Generate apps built with Next, Nuxt, Quasar, React, React Native, Vue or Vuetify for any API documented using Hydra or OpenAPI

65 lines (55 loc) 1.68 kB
import React, { Component } from 'react'; import { connect } from 'react-redux'; import { Link, Redirect } from 'react-router-dom'; import PropTypes from 'prop-types'; import Form from './Form'; import { create, reset } from '../../actions/{{{lc}}}/create'; class Create extends Component { static propTypes = { error: PropTypes.string, loading: PropTypes.bool.isRequired, created: PropTypes.object, create: PropTypes.func.isRequired, reset: PropTypes.func.isRequired }; componentWillUnmount() { this.props.reset(); } render() { if (this.props.created) return ( <Redirect to={`edit/${encodeURIComponent(this.props.created['@id'])}`} /> ); return ( <div> <h1>New {{{title}}}</h1> {this.props.loading && ( <div className="alert alert-info" role="status"> Loading... </div> )} {this.props.error && ( <div className="alert alert-danger" role="alert"> <span className="fa fa-exclamation-triangle" aria-hidden="true" />{' '} {this.props.error} </div> )} <Form onSubmit={this.props.create} values={this.props.item} /> <Link to="." className="btn btn-primary"> Back to list </Link> </div> ); } } const mapStateToProps = state => { const { created, error, loading } = state.{{{lc}}}.create; return { created, error, loading }; }; const mapDispatchToProps = dispatch => ({ create: values => dispatch(create(values)), reset: () => dispatch(reset()) }); export default connect(mapStateToProps, mapDispatchToProps)(Create);