-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.js
207 lines (184 loc) · 4.58 KB
/
bootstrap.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { createStore, bindActionCreators } from 'redux';
import React from 'react';
import { Provider, connect } from 'react-redux';
import { render } from 'react-dom';
// Dummy data for app
const attendeeList = [{
name: 'Charlie Kelly',
color: '#E74C3C',
id: 1
}, {
name: 'Mac',
color: '#553285',
id: 2
}, {
name: 'Frank Reynolds',
color: '#296AA8',
id: 3
}, {
name: 'Deandra Reynolds',
color: '#202020',
id: 4
}, {
name: 'Dennis Reynolds',
color: '#287572',
id: 5
} ];
/* --- COMPONENTS --- */
/**
* Componente inicial
*/
class App extends React.Component {
render() {
return (
<div>
<h1>Attendees</h1>
<hr />
<AddAttendee {...this.props} />
<hr />
<Attendees {...this.props} />
</div>
);
}
}
/**
* Componentes complementarios
*/
class Attendees extends React.Component {
render() {
return(
<ul className="attendees">
{this.props.attendees.map((attendee, index) =>
<li className="attendees__attendee" key={index}>
<Badge attendee={attendee} />
<RemoveAttendee removeAttendee={this.props.removeAttendee} index={index} />
</li>
)}
</ul>
);
}
}
class Badge extends React.Component {
render() {
let style = {backgroundColor: this.props.attendee.color};
return (
<div className="hello-badge" style={style}>
<p className="hello-badge__title">
<span className="hello-badge__hello">Hello<br /></span> my name is
</p>
<p className="hello-badge__name">
{this.props.attendee.name}
</p>
</div>
);
}
}
class AddAttendee extends React.Component {
handleSubmit(e) {
// Stop page refreshing
e.preventDefault();
// Store reference to our form references
let refs = this.refs;
// Users name
let name = refs.name.value;
// Users favourite colour
let color = refs.color.value
// Trigger action
this.props.addAttendee(name, color);
// Reset form so our inputs are empty again
refs.addAttendee.reset();
}
render() {
return (
<div className="row">
<div className="col-xs-6 col-xs-offset-3">
<form ref="addAttendee" onSubmit={this.handleSubmit.bind(this)}>
<label for="name">Name</label>
<br />
<input id="name" className="form-control" type="text" ref="name" placeholder="John Doe" />
<br />
<label for="color">Favourite color</label>
<br />
<input id="color" className="form-control" type="text" ref="color" placeholder="#2e2e2e" />
<br />
<button type="submit" className="button btn">Add attendee</button>
</form>
</div>
</div>
);
}
}
class RemoveAttendee extends React.Component {
handleOnClick() {
let index = this.props.index;
this.props.removeAttendee(index);
}
render() {
return (
<button className="alert button tiny btn
" onClick={this.handleOnClick.bind(this)}> × Remove attendee</button>
);
}
}
/* --- END-COMPONENTS --- */
/* --- REDUCERS --- */
function reducer(state = [], action) {
switch (action.type) {
case 'ADD_ATTENDEE':
// Return a new array with old state and added attendee.
return [{
name: action.name,
color: action.color
},
...state
];
case 'REMOVE_ATTENDEE':
return [
// In the array grab the state from beginning to index of one to delete
...state.slice(0, action.index),
// Grab state from the one after one we want to delete
...state.slice(action.index + 1)
];
default:
return state;
}
};
/* --- ACTIONS --- */
const actions = {
addAttendee: (name, color) => {
return {
// String for Reducer to pick up
type: 'ADD_ATTENDEE',
// Randomly generated id
id: 5,
// Name and colour we sent through from the form
name,
color
}
},
removeAttendee: (index) => {
return {
type: 'REMOVE_ATTENDEE',
index
}
}
};
/* --- STORE --- */
// esta funcion es para enviar toda la data necesaria al prop
function mapStateToProps(state) {
return {
attendees: state
}
}
function mapDispatchToProps(dispatch){
return bindActionCreators(actions, dispatch);
}
const AppContainer = connect(mapStateToProps, mapDispatchToProps)(App);
const store = createStore(reducer, attendeeList);
/* --- Render --- */
render(
<Provider store={store}>
<AppContainer />
</Provider>,
document.getElementById('app')
);