/
contact-view.jsx
117 lines (102 loc) · 3.11 KB
/
contact-view.jsx
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
/**
* In this file, we create a React component
* which incorporates components providedby material-ui.
*/
import React from 'react';
import {observable, action} from 'mobx';
import {observer} from 'mobx-react';
import FlatButton from 'material-ui/FlatButton';
import Avatar from 'material-ui/Avatar';
import {Card, CardActions, CardTitle, CardText} from 'material-ui/Card';
import Toggle from 'material-ui/Toggle';
import TextField from 'material-ui/TextField';
import AutoComplete from 'material-ui/AutoComplete';
@observer
export class ContactView extends React.Component {
@observable firstNameValue;
@observable lastNameValue;
@observable autoSave;
@observable tagId = 0; // AutoComplete has no reset api, abuse react keys..
componentWillMount() {
this.resetInputValues(this.props);
}
componentWillReceiveProps(nextProps) {
this.resetInputValues(nextProps);
}
render() {
const {contact} = this.props;
return <Card>
<CardTitle
title={contact.displayName}
subtitle={contact.username}
children={<span style={{ float: "left", marginLeft: "43%" }}>
Auto Save: <Toggle style={{ width: "auto", float: "right" }} defaultToggled={contact.autoSave} onToggle={this.onToggle} />
</span>}
/>
<CardText>
<Avatar src={contact.picture.large} size={120} />
<br/>
<TextField
floatingLabelText="First name"
value={this.firstNameValue}
onChange={this.onChangeFirstName}
/>
<TextField
floatingLabelText="Last name"
value={this.lastNameValue}
onChange={this.onChangeLastName}
/>
<br/>
<h2>Tags</h2>
<p>{ contact.tags.map(tag => tag.name).join(", ") }</p>
<AutoComplete
floatingLabelText="Add tag"
key={this.tagId}
dataSource={this.getAvailableTags()}
onNewRequest={this.onSelectTag}
/>
</CardText>
<CardActions>
<FlatButton label="Delete" onClick={this.onDelete} />
<FlatButton label="Cancel" onClick={this.onCancel} />
<FlatButton label="Save" primary={true} onClick={this.onSave} disabled={this.autoSave} />
</CardActions>
</Card>
}
@action onChangeFirstName = (e) => {
this.firstNameValue = e.target.value;
this.autoSave && this.onSave();
}
@action onChangeLastName = (e) => {
this.lastNameValue = e.target.value;
this.autoSave && this.onSave();
}
@action onToggle = (e, disabled) => {
this.autoSave = disabled;
this.onSave();
}
@action onDelete = () => {
this.props.viewState.selectNothing();
this.props.contact.delete();
}
@action onSave = () => {
this.props.contact.updateFirstName(this.firstNameValue);
this.props.contact.updateLastName(this.lastNameValue);
this.props.contact.updateAutoSave(this.autoSave);
}
@action onCancel = () => {
this.resetInputValues(this.props);
}
getAvailableTags = () => this.props.contact.getAvailableTags().map(tag => tag.name);
@action onSelectTag = (value) => {
this.props.contact.addTag(value);
this.autoSave && this.onSave();
this.tagId++;
}
@action resetInputValues(props) {
this.firstNameValue = props.contact.firstName;
this.lastNameValue = props.contact.lastName;
this.autoSave = props.contact.autoSave;
this.tagId++;
}
}