import React from 'react'; import Form from '../Modules/Form'; import AddressBox from '../Modules/AddressBox'; import ArraySelector from '../Modules/ArraySelector'; import AddressInputs from '../Inputs/AddressInput'; import Toast from '../helpers/Toast'; import Merge from '../helpers/Merge'; import Dictionary from '../Services/DictionaryService'; import { Button } from 'react-materialize'; class Addresses extends React.Component { constructor(props) { super(props); this.state = { addresses: [], showAddressForm: false, selectedAddress: {}, edit: false } this.updateService(props.service); } componentWillReceiveProps(props) { if(props.service) { let service = props.service; this.updateService(service); } } updateService(service) { service.getPage(1, 100).then((addresses) => { this.setState({ addresses, showAddressForm: false }); }) } edit(address) { this.setState({ showAddressForm: true, edit: true, editAddress: address }) } delete(address) { this.props.update && this.props.update('' + Math.random() * Math.random()); if(window.confirm(Dictionary.get("Delete address ?"))) this.props.service.update(address.id, {deleted: true}).then(() => this.updateService(this.props.service)) } getAddressForm() { return
this.setState({newAddress: address})}/> {!!this.state.addresses.length && }
} getSelector() { return { return { this.setState({selectedAddress: address}); this.props.onSelect && this.props.onSelect(address); }} onEdit={() => this.edit(address)} onDelete={() => this.delete(address)} /> }}/> ; } render() { if (!this.state.showAddressForm && !this.state.addresses.length) { this.setState({ showAddressForm: true }); } return this.state.showAddressForm ? this.getAddressForm() : this.getSelector(); } } export default Addresses;