-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_d2.html
134 lines (124 loc) · 3.51 KB
/
index_d2.html
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
<!DOCTYPE html>
<html>
<head>
<title>First React App</title>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id='app'></div>
<script type='text/babel'>
function FriendsList (props) {
var active = props.activeFriend
var activeString = props.activeStatus
return (
<ul>
{props.list.filter(friend => friend.status == active).map((friend) => (
<li key={friend.name}>
<span>{friend.name}</span>
<button onClick={() => props.onRemoveFriend(friend.name)}>Remove</button>
<button onClick={() => props.onActivityChange(friend.name, friend.status)}>{activeString}</button>
</li>
))}
</ul>
)
}
class App extends React.Component {
constructor(props){
super(props)
this.state = {
friendsList: [
{name: 'Jordyn', status: true},
{name: 'Emily', status: true},
{name: 'Jacob', status: true},
{name: 'Jolene', status: false}
],
input: ''
}
this.handleRemoveFriend = this.handleRemoveFriend.bind(this)
this.handleAddFriend = this.handleAddFriend.bind(this)
this.handleClearAll = this.handleClearAll.bind(this)
this.handleActivityChange = this.handleActivityChange.bind(this)
this.updateInput = this.updateInput.bind(this)
}
handleAddFriend() {
this.setState((prevState) => {
var newFriend = this.state.input
if (newFriend) {
return {
friendsList: prevState.friendsList.concat({name: newFriend, status: true}),
input: ''
}
}
})
}
handleRemoveFriend(name) {
this.setState((currentState) => {
return {
friendsList: currentState.friendsList.filter((friend) => friend.name !== name)
}
})
}
handleClearAll() {
this.setState({
friendsList: []
})
}
// should handle active and deactive
// take in a bool of specifying current status
// if status is active, then move to deactive, vice versa
handleActivityChange(friendName, currentStatus) {
const index = this.state.friendsList.findIndex(friend => friend.name == friendName)
let newFriendsList = [...this.state.friendsList]
var newStatus = !currentStatus
newFriendsList[index] = {name: friendName, status: newStatus}
this.setState({
friendsList: newFriendsList
})
}
updateInput(e) {
const value = e.target.value
this.setState({
input: value
})
}
render() {
return (
<div>
<input
type='text'
placeholder='new friend'
value={this.state.input}
onChange={this.updateInput}
/>
<button onClick={this.handleAddFriend}>Submit</button>
<br></br>
<button onClick={this.handleClearAll}>clear all</button>
<h1>Active Friends</h1>
<FriendsList
list={this.state.friendsList}
onRemoveFriend={this.handleRemoveFriend}
onActivityChange={this.handleActivityChange}
activeFriend={true}
activeStatus={'deactivate'}
/>
<h1>Inactive Friends</h1>
<FriendsList
list={this.state.friendsList}
onRemoveFriend={this.handleRemoveFriend}
onActivityChange={this.handleActivityChange}
activeFriend={false}
activeStatus={'activate'}
/>
</div>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('app')
)
</script>
</body>
</html>