-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaddress-book.jsx
More file actions
118 lines (106 loc) · 3.54 KB
/
address-book.jsx
File metadata and controls
118 lines (106 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import _ from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import ContentBox from 'components/core/content-box';
import Addresses, { createAddressBox } from '../addresses/addresses';
import AddressBox from '../addresses/address-box';
import AddressForm from '../addresses/address-form';
import ItemCardContainer from '../item-card-container/item-card-container';
import { AddButton } from 'components/core/button';
import { connect } from 'react-redux';
import { autobind } from 'core-decorators';
import * as AddressesDetailsActions from '../../modules/customers/addresses-details';
import * as AddressesActions from '../../modules/customers/addresses';
@connect((state, props) => ({
...state.customers.addressesDetails
}), {
...AddressesDetailsActions,
...AddressesActions
})
export default class CustomerAddressBook extends React.Component {
static propTypes = {
customerId: PropTypes.number.isRequired,
fetchAddresses: PropTypes.func,
patchAddress: PropTypes.func,
createAddress: PropTypes.func,
startAddingAddress: PropTypes.func,
stopAddingAddress: PropTypes.func,
stopEditingAddress: PropTypes.func,
addresses: PropTypes.array.isRequired,
};
@autobind
injectNewAddressCard(addresses) {
const props = this.props;
if (props.isAdding) {
const title = <div className="fc-address-new-title">New Address</div>;
return [
...addresses,
<ItemCardContainer
key="address-new"
leftControls={ title }
className="fc-address is-editing">
<AddressForm
customerId={props.customerId}
onCancel={() => props.stopAddingAddress()}
showFormTitle={ false }
submitAction={this.handleSubmitAddress}
/>
</ItemCardContainer>
];
}
return addresses;
}
@autobind
handleSubmitAddress(address) {
if (address.id) {
this.props.patchAddress(this.props.customerId, address.id, address);
this.props.stopEditingAddress(address.id);
} else {
this.props.createAddress(this.props.customerId, address);
this.props.stopAddingAddress();
}
}
@autobind
createAddressBox(address, idx, props) {
if (_.includes(props.editingIds, address.id)) {
return (
<AddressBox
key={`address-${idx}`}
address={address}
toggleDefaultAction={() => props.setAddressDefault(props.customerId, address.id, !address.isDefault)}
editAction={() => props.stopEditingAddress(address.id)}
className="is-editing">
<AddressForm
address={address}
customerId={props.customerId}
onCancel={() => props.stopEditingAddress(address.id)}
showFormTitle={ false }
submitAction={this.handleSubmitAddress}
/>
</AddressBox>
);
} else {
return createAddressBox(address, idx, props);
}
}
render() {
const props = this.props;
return (
<ContentBox title="Address Book"
className="fc-customer-address-book"
actionBlock={
<AddButton id="fct-add-btn__new-address"
onClick={() => props.startAddingAddress(props.customerId)}
/>
}
editAction={() => props.startEditingAddress(address.id)}
>
<Addresses
{...props}
processContent={ this.injectNewAddressCard }
createAddressBox={ this.createAddressBox }
/>
</ContentBox>
);
}
}