-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
272 lines (239 loc) · 7.9 KB
/
App.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import React, { Component } from 'react';
import Group from './components/Group';
import Timer from './components/Timer';
import GroupField from './components/GroupField';
import StudentPicker from './components/StudentPicker';
const roster = require('./students.json');
class App extends Component {
state = {
numInGroups: 2,
groupNames: [
'🧠🦁',
'🕶🐸',
'☄️🐈',
'⛸🌝',
'🚀🦕',
'🦞🐝',
'🌹🍄',
'🍊🎉',
'🌅💎',
'🏕🐲',
'🍌🐛',
'🥨📟',
'🌵🦈',
'🌮🥯'
],
studentsMaster: [],
presentStudents: [],
process: [],
minutes: '0',
seconds: '00',
initialMin: '10',
initialSec: '00',
initialInput: '',
timerButton: 'Start timer',
buttonColor: 'green',
timerInput: false,
modalOpen: false
}
// Timer
secondsRemaining;
intervalHandle;
tick = () => {
var min = Math.floor(this.secondsRemaining / 60);
var sec = this.secondsRemaining - (min * 60);
this.setState({
minutes: min,
seconds: sec
})
if (sec < 10) {
this.setState({
seconds: '0' + this.state.seconds,
})
}
if (min < 10) {
this.setState({
value: '0' + min,
})
}
if (min === 0 & sec === 0) {
clearInterval(this.intervalHandle);
}
this.secondsRemaining--;
}
handleTimerButton = () => {
if(this.state.timerButton === 'Start timer') {
this.handleStartTimer();
this.setState({ timerButton: 'Stop and reset timer', buttonColor: 'red', timerInput: false });
} else {
this.handleStopTimer();
this.setState({ timerButton: 'Start timer', buttonColor: 'green' });
}
}
handleStartTimer = () => {
this.intervalHandle = setInterval(this.tick, 1000);
let time = this.state.initialInput;
this.secondsRemaining = time * 60;
this.setState({ initialMin: time })
}
handleStopTimer = () => {
clearInterval(this.intervalHandle);
this.setState({ minutes: this.state.initialMin, seconds: this.state.initialSec })
}
handleTimerInputChange = e => {
this.setState({ minutes: e.target.value });
}
handleTimerCountClick = () => {
this.setState({ timerInput: true });
}
inputChangeTimerTime = e => {
let inputTime = e.target.value;
this.setState({ initialInput: inputTime });
}
// Student array shuffle
shuffle = array => {
let currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// Group generation
handleNumberChange = e => {
const newVal = e.target.value;
this.setState({ numInGroups: newVal });
};
handleMakeGroupsButton = () => {
const randomArr = this.shuffle(this.state.presentStudents);
const randomGroupNames = this.shuffle(this.state.groupNames);
const numInGroups = parseInt(this.state.numInGroups);
let processArr = [];
let count = 0;
let totalRemaining = randomArr.length;
let tempArr = [];
let remainderArr = [];
for (let i = 0; i < randomArr.length; i++) {
/* If the current count is less than the size of each group
push that name into the tempArr array, increase the count,
decrease the totalRemaining */
// -Works-
if(count < numInGroups) {
tempArr.push(randomArr[i]);
count++;
totalRemaining--;
}
/* If the count is equal to the size of each grop
push the tempArr array into the processArr array
because the tempArr array is full. Then push the name
into the tempArr after clearing it and reset the count
to 1 because 1 name is in the tempArr array. Decrease
the totalRemaining */
// -Works-
else if (count === numInGroups) {
processArr.push(tempArr);
tempArr = [];
tempArr.push(randomArr[i]);
count = 1;
totalRemaining--;
}
// If the total remaining to be sorted is less than the number in groups minus count, and the name isn't already in the process array
// push the remaining name into the remainderArr array
// -Works-
if (totalRemaining < numInGroups-count && !processArr.includes(randomArr[i])) {
remainderArr.push(randomArr[i]);
}
}
/* If the processArr array doesn't include
the last tempArr array (because the last
even array will not trigger the else if
(count === numInGroups)) then push
the last tempArr into the processArr */
// Hack fix
if(!processArr.includes(tempArr[0])) {
processArr.push(tempArr);
}
/* If there are elements in the remainder array
cycle through the remainders and add one to each
of the existing process arrays.
Then remove the last array in the process array
since it is incomplete and holds the remainders */
// There has to be a better way to do this
// This does dumb stuff like make groups of five instead of one group of 3 when there are supposed to be groups of four. This needs fixing.
if(remainderArr.length >= 1) {
remainderArr.forEach((element, i) => {
processArr[i].push(remainderArr[i]);
});
processArr.splice(processArr.length-1, 1);
}
this.setState({ process: processArr, groupNames: randomGroupNames });
};
// Student picker modal
modalShow = () => this.setState({ modalOpen: true });
modalClose = () => this.setState({ modalOpen: false });
syncAvailableStudents = studentsMasterUpdated => {
let presentStudentsUpdated = [];
studentsMasterUpdated.forEach(student => {
if(student.present) {
presentStudentsUpdated.push(student.name);
}
});
this.setState({ presentStudents: presentStudentsUpdated, studentsMaster: studentsMasterUpdated });
}
handleCheckboxToggle = i => {
let studentsMasterUpdated = this.state.studentsMaster;
studentsMasterUpdated[i].present = !studentsMasterUpdated[i].present;
this.syncAvailableStudents(studentsMasterUpdated);
}
componentDidMount() {
let studentsMasterProcess = [];
roster.forEach(element => {
let processObj = {
name: element,
present: true
};
studentsMasterProcess.push(processObj);
});
this.syncAvailableStudents(studentsMasterProcess);
}
render() {
return (
<div className='app'>
<StudentPicker
modalShow={this.modalShow}
modalClose={this.modalClose}
modalOpen={this.state.modalOpen}
handleCheckboxToggle={this.handleCheckboxToggle}
studentsMaster={this.state.studentsMaster} />
<div className='topWrap'>
<Timer
minutes={this.state.minutes}
seconds={this.state.seconds}
handleTimerInputChange={this.handleTimerInputChange}
handleTimerButton={this.handleTimerButton}
timerButton={this.state.timerButton}
buttonColor={this.state.buttonColor}
timerInput={this.state.timerInput}
handleTimerCountClick={this.handleTimerCountClick}
inputChangeTimerTime={this.inputChangeTimerTime}
initialInput={this.state.initialInput} />
<div className='myhr'></div>
<GroupField
handleMakeGroupsButton={this.handleMakeGroupsButton}
numInGroups={this.state.numInGroups}
handleNumberChange={this.handleNumberChange}
modalShow={this.modalShow} />
</div>
<div className='groupsWrap'>
{this.state.process.map((group, i) => (
<Group group={group} key={i} groupName={this.state.groupNames[i]} />
))}
</div>
</div>
);
}
}
export default App;