|
| 1 | +import React, { Component, PropTypes } from 'react'; |
| 2 | +import { push } from 'react-router-redux'; |
| 3 | +import { connect } from 'react-redux'; |
| 4 | + |
| 5 | +import { ErrorAlert, Loading, EditHeader } from '../UI'; |
| 6 | +import { withResource } from '../../hocs'; |
| 7 | +import ProductForm from './ProductForm'; |
| 8 | +import { getMany, fetchList } from '../../store/api'; |
| 9 | + |
| 10 | +export class ProductEdit extends Component { |
| 11 | + componentWillMount() { |
| 12 | + const { params, fetchResource } = this.props; |
| 13 | + |
| 14 | + if (params.id) { |
| 15 | + fetchResource({ id: params.id }); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + render() { |
| 20 | + const { isNew, error, loading, resource, onSubmit } = this.props; |
| 21 | + |
| 22 | + if (error) { |
| 23 | + return (<ErrorAlert {...error} />); |
| 24 | + } |
| 25 | + |
| 26 | + if (loading) { |
| 27 | + return (<Loading />); |
| 28 | + } |
| 29 | + |
| 30 | + return ( |
| 31 | + <div> |
| 32 | + <EditHeader {...this.props}>{ isNew ? 'New Product' : resource.productName }</EditHeader> |
| 33 | + <ProductForm initialValues={resource} onSubmit={onSubmit}></ProductForm> |
| 34 | + </div> |
| 35 | + ); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +export const mapDispatchToProps = dispatch => ({ |
| 41 | + redirectToIndex: () => dispatch(push('/products')), |
| 42 | +}); |
| 43 | + |
| 44 | +export default connect(null, mapDispatchToProps)( |
| 45 | + withResource('products')(ProductEdit), |
| 46 | +); |
0 commit comments