AddressesDisplay.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import React from 'react';
  2. import Form from '../Modules/Form';
  3. import AddressBox from '../Modules/AddressBox';
  4. import ArraySelector from '../Modules/ArraySelector';
  5. import AddressInputs from '../Inputs/AddressInput';
  6. import Toast from '../helpers/Toast';
  7. import Merge from '../helpers/Merge';
  8. import Dictionary from '../Services/DictionaryService';
  9. import { Button } from 'react-materialize';
  10. class Addresses extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. addresses: [],
  15. showAddressForm: false,
  16. selectedAddress: {},
  17. edit: false
  18. }
  19. this.updateService(props.service);
  20. }
  21. componentWillReceiveProps(props) {
  22. if(props.service) {
  23. let service = props.service;
  24. this.updateService(service);
  25. }
  26. }
  27. updateService(service) {
  28. service.getPage(1, 100).then((addresses) => {
  29. this.setState({
  30. addresses,
  31. showAddressForm: false
  32. });
  33. })
  34. }
  35. edit(address) {
  36. this.setState({
  37. showAddressForm: true,
  38. edit: true,
  39. editAddress: address
  40. })
  41. }
  42. delete(address) {
  43. this.props.update && this.props.update('' + Math.random() * Math.random());
  44. if(window.confirm(Dictionary.get("Delete address ?")))
  45. this.props.service.update(address.id, {deleted: true}).then(() => this.updateService(this.props.service))
  46. }
  47. getAddressForm() {
  48. return <div>
  49. <Form FormInputs={this.state.edit ? Merge(AddressInputs, this.state.editAddress) : AddressInputs} formData={(address) => this.setState({newAddress: address})}/>
  50. <Button onClick={() => {
  51. let action = this.state.edit ?
  52. this.props.service.update(this.state.editAddress.id, this.state.newAddress) :
  53. this.props.service.add(this.state.newAddress);
  54. action
  55. .then(() => this.updateService(this.props.service))
  56. .then(() => {
  57. this.props.update && this.props.update(Math.random());
  58. this.setState({
  59. showAddressForm: false
  60. })
  61. })
  62. }}>{this.state.edit ? Dictionary.get('Update') : Dictionary.get('Add')}</Button>
  63. {!!this.state.addresses.length && <Button onClick={() => {
  64. this.setState({
  65. showAddressForm: false
  66. })
  67. }}>{Dictionary.get('Cancel')}</Button>}
  68. </div>
  69. }
  70. getSelector() {
  71. return <React.Fragment>
  72. <ArraySelector Array={this.state.addresses} displayItem={(address, indx) => {
  73. return <AddressBox key={indx}
  74. Address={address}
  75. Selected={this.state.selectedAddress.id == address.id}
  76. onSelect={() => {
  77. this.setState({selectedAddress: address});
  78. this.props.onSelect && this.props.onSelect(address);
  79. }}
  80. onEdit={() => this.edit(address)}
  81. onDelete={() => this.delete(address)}
  82. />
  83. }}/>
  84. <Button onClick={() => this.setState({ showAddressForm: true, edit: false })}>{Dictionary.get("Add address")}</Button>
  85. </React.Fragment>;
  86. }
  87. render() {
  88. if (!this.state.showAddressForm && !this.state.addresses.length) {
  89. this.setState({
  90. showAddressForm: true
  91. });
  92. }
  93. return this.state.showAddressForm ? this.getAddressForm() : this.getSelector();
  94. }
  95. }
  96. export default Addresses;