-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontacts.jsx
More file actions
172 lines (153 loc) · 4.65 KB
/
contacts.jsx
File metadata and controls
172 lines (153 loc) · 4.65 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// libs
import _ from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import { autobind } from 'core-decorators';
import { connect } from 'react-redux';
// redux
import * as CustomerContactActions from '../../modules/customers/contacts';
// components
import ContentBox from 'components/core/content-box';
import FormField from '../forms/formfield';
import Form from '../forms/form';
import { ApiErrors } from 'components/utils/errors';
import { EditButton } from 'components/core/button';
import SaveCancel from 'components/core/save-cancel';
function mapDispatchToProps(dispatch, props) {
return _.transform(CustomerContactActions, (result, action, key) => {
result[key] = (...args) => {
return dispatch(action(props.customerId, ...args));
};
});
}
@connect((state, props) => ({
...state.customers.details[props.customerId]
}), mapDispatchToProps)
export default class CustomerContacts extends React.Component {
static propTypes = {
customerId: PropTypes.number.isRequired,
toggleEditCustomer: PropTypes.func.isRequired,
cleanErrors: PropTypes.func.isRequired,
updateCustomerContacts: PropTypes.func.isRequired,
details: PropTypes.object,
isContactsEditing: PropTypes.bool,
err: PropTypes.any,
};
constructor(props, ...args) {
super(props, ...args);
this.state = {
name: props.details.name,
email: props.details.email,
phoneNumber: props.details.phoneNumber
};
}
static validateName(newName) {
if (_.includes(newName, '@')) {
return '@ symbol disallowed';
}
}
static validateEmail(newEmail) {
if (!_.includes(newEmail, '@')) {
return '@ symbol is omitted';
}
}
@autobind
onSubmit() {
this.props.toggleEditCustomer();
this.props.updateCustomerContacts(this.state);
}
@autobind
onEditClick(event) {
this.props.toggleEditCustomer();
event.preventDefault();
}
get nameField() {
if (!this.props.isContactsEditing) {
return <dd id="customer-contacts-name">{ this.props.details.name }</dd>;
}
return (
<FormField validator={ CustomerContacts.validateName }>
<input id='nameField'
className='fc-customer-form-input'
name='Name'
maxLength='255'
type='text'
required
onChange={ ({target}) => this.setState({name: target.value}) }
value={ this.state.name } />
</FormField>
);
}
get emailField() {
if (!this.props.isContactsEditing) {
return <dd id="customer-contacts-email">{ this.props.details.email }</dd>;
}
return (
<FormField validator={ CustomerContacts.validateEmail }>
<input id='emailField'
className='fc-customer-form-input'
name='Email'
maxLength='255'
type='text'
required
onChange={ ({target}) => this.setState({email: target.value}) }
value={ this.state.email } />
</FormField>
);
}
get phoneField() {
if (!this.props.isContactsEditing) {
return <dd id="customer-contacts-phone">{ this.props.details.phoneNumber }</dd>;
}
return (
<FormField validator='ascii'>
<input id='phoneField'
className='fc-customer-form-input'
name='Phone'
maxLength='255'
type='text'
required
onChange={ ({target}) => this.setState({phoneNumber: target.value}) }
value={ this.state.phoneNumber } />
</FormField>
);
}
get formActions() {
if (this.props.isContactsEditing) {
return <SaveCancel onCancel={ this.props.toggleEditCustomer } />;
}
}
get actionBlock() {
if (!this.props.isContactsEditing) {
return (
<EditButton id="fct-edit-btn__customer-contacts" onClick={ this.onEditClick } />
);
}
}
render() {
return (
<ContentBox title='Contact Information'
className='fc-customer-contacts'
actionBlock={ this.actionBlock }>
<ApiErrors response={this.props.err} closeAction={this.props.cleanErrors} />
<Form className='fc-customer-contacts-form fc-form-vertical'
onChange={ this.onChange }
onSubmit={ this.onSubmit }>
<dl>
<dt>First & Last Name</dt>
{ this.nameField }
</dl>
<dl>
<dt>Email Address</dt>
{ this.emailField }
</dl>
<dl>
<dt>Phone Number</dt>
{ this.phoneField }
</dl>
{ this.formActions }
</Form>
</ContentBox>
);
}
}