-
Notifications
You must be signed in to change notification settings - Fork 141
/
CatPage.js
180 lines (152 loc) · 5.19 KB
/
CatPage.js
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
173
import React, {PropTypes} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as catActions from '../../actions/catActions';
import HobbyList from '../hobbies/HobbyList';
import CatForm from './CatForm';
import {browserHistory} from 'react-router';
// import toastr from 'toastr';
class CatPage extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
cat: this.props.cat,
catHobbies: this.props.catHobbies,
checkBoxHobbies: props.checkBoxHobbies,
saving: false,
isEditing: false
};
this.saveCat = this.saveCat.bind(this);
this.updateCatState = this.updateCatState.bind(this);
this.updateCatHobbies = this.updateCatHobbies.bind(this);
this.toggleEdit = this.toggleEdit.bind(this);
this.deleteCat = this.deleteCat.bind(this);
this.redirect = this.redirect.bind(this);
}
componentWillReceiveProps(nextProps) {
if (this.props.cat.id != nextProps.cat.id) {
this.setState({cat: nextProps.cat});
}
if (this.props.checkBoxHobbies.length < nextProps.checkBoxHobbies.length) {
this.setState({catHobbies: nextProps.catHobbies, checkBoxHobbies: nextProps.checkBoxHobbies});
}
this.setState({saving: false, isEditing: false});
}
toggleEdit() {
this.setState({isEditing: true});
}
updateCatHobbies(event) {
const cat = this.state.cat;
const hobbyId = event.target.value;
const hobby = this.state.checkBoxHobbies.filter(hobby => hobby.id == hobbyId)[0];
const checked = !hobby.checked;
hobby['checked'] = !hobby.checked;
if (checked) {
cat.hobby_ids.push(hobby.id);
} else {
cat.hobby_ids.splice(cat.hobby_ids.indexOf(hobby.id));
}
this.setState({cat: cat});
}
updateCatState(event) {
const field = event.target.name;
const cat = this.state.cat;
cat[field] = event.target.value;
return this.setState({cat: cat});
}
saveCat(event) {
event.preventDefault();
this.setState({saving: true});
this.props.actions.updateCat(this.state.cat);
}
deleteCat(event) {
this.props.actions.deleteCat(this.state.cat)
}
redirect() {
browserHistory.push('/cats');
}
render() {
if (this.state.isEditing) {
return (
<div>
<h1>edit cat</h1>
<CatForm
cat={this.state.cat}
hobbies={this.state.checkBoxHobbies}
onSave={this.saveCat}
onChange={this.updateCatState}
onHobbyChange={this.updateCatHobbies}
saving={this.state.saving}/>
</div>
)
}
return (
<div className="col-md-8 col-md-offset-2">
<h1>{this.state.cat.name}</h1>
<p>breed: {this.state.cat.breed}</p>
<p>weight: {this.state.cat.weight}</p>
<p>temperament: {this.state.cat.temperament}</p>
<HobbyList hobbies={this.state.catHobbies} />
<button onClick={this.toggleEdit} className="btn btn-default ">edit</button>
<button onClick={this.deleteCat} className="btn btn-default ">delete</button>
</div>
);
}
}
CatPage.propTypes = {
cat: PropTypes.object.isRequired,
catHobbies: PropTypes.array.isRequired,
checkBoxHobbies: PropTypes.array.isRequired,
actions: PropTypes.object.isRequired
};
function getCatById(cats, id) {
let cat = cats.find(cat => cat.id == id)
return Object.assign({}, cat)
}
function hobbiesForCheckBoxes(hobbies, cat=null) {
return hobbies.map(hobby => {
if (cat && cat.hobby_ids.filter(hobbyId => hobbyId == hobby.id).length > 0) {
hobby['checked'] = true;
} else {
hobby['checked'] = false;
}
return hobby;
});
}
function collectCatHobbies(hobbies, cat) {
let selected = hobbies.map(hobby => {
if (cat.hobby_ids.filter(hobbyId => hobbyId == hobby.id).length > 0) {
return hobby;
}
})
return selected.filter(el => el != undefined)
}
function mapStateToProps(state, ownProps) {
const stateHobbies = Object.assign([], state.hobbies)
let checkBoxHobbies = [];
let catHobbies = [];
let cat = {name: '', breed: '', weight: '', temperament: '', hobby_ids: []};
const catId = ownProps.params.id;
if (catId && state.cats.length > 0 && state.hobbies.length > 0) {
cat = getCatById(state.cats, ownProps.params.id);
if (cat.id && cat.hobby_ids.length > 0) {
checkBoxHobbies = hobbiesForCheckBoxes(stateHobbies, cat);
catHobbies = collectCatHobbies(stateHobbies, cat);
} else {
checkBoxHobbies = hobbiesForCheckBoxes(stateHobbies)
}
}
return {cat: cat, checkBoxHobbies: checkBoxHobbies, catHobbies: catHobbies};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(catActions, dispatch)
};
}
export default connect(mapStateToProps, mapDispatchToProps)(CatPage);
// connect:
// + will invoke mapDispatchToProps, with an argument of the store's dispatch function
// + it has access to the store, b/c you passed store in via the provider
// + bindActionCreators will take your collection of action creator functions
// + iterate over it, wrap each AC function in store.dispatch(AC function)
// + make them available to your component as this.props.actions = {name of an action: store.dispatch(ac function)}