/
practice.vue
190 lines (187 loc) · 5.41 KB
/
practice.vue
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
<template>
<div id="practice">
<section class="card-container">
<a class="quit" href="#" v-on:click.prevent="stopPeriod">Stop</a>
<div class="card" v-for="problem in workingProblems" v-show="problem === currentProb" :key="problem.id">
<div class="minuend">{{ problem.minuend }}</div>
<div class="subtrahend">- {{ problem.subtrahend }}</div>
</div>
<div class="hint-card card">
<div class="hint-frame" v-if="!timer">
<div class="manipulative" v-for="disc in manipulatives()" v-on:click="toggleColor" v-on:mouseenter="toggleColor" v-show="!timer"></div>
</div>
</div>
<div class="card">
<input type="text" class="answer" v-on:keyup="evaluate" ref="input" v-bind:value="answer"/>
</div>
</section>
<div class="buttons">
<button v-on:click="changeAnswer('1')">1</button>
<button v-on:click="changeAnswer('2')">2</button>
<button v-on:click="changeAnswer('3')">3</button>
<button v-on:click="changeAnswer('4')">4</button>
<button v-on:click="changeAnswer('5')">5</button>
<button v-on:click="changeAnswer('6')">6</button>
<button v-on:click="changeAnswer('7')">7</button>
<button v-on:click="changeAnswer('8')">8</button>
<button v-on:click="changeAnswer('9')">9</button>
<button v-on:click="changeAnswer('0')">0</button>
<button v-on:click="backspace" class="backspace">◀</button>
</div>
</div>
</template>
<script>
/*
The practice component:
Sets startTime when session starts.
contains an element for each card.
displays each problem
When transition ends:
1. input receives focus
2. timer starts
responds to user input and timer
If incorrect: do nothing.
On timeOut
1. clear timer
2. display manipulatives (with v-if !timer)
On correct input,
1. increment reps
2. if timer,
1. increment success_times
2. add problem to problemsToUpdate
3. clear timer
3. start transition to next problem
1. determine what next problem is
2. start transition
At end of practice session,
1. Send event and data back to parent.
*/
export default {
props: ["workingProblems", "username", "maxSeconds"],
data() {
return {
startTime: 1,
timer: 1,
problemsToUpdate: [],
currentProb: this.workingProblems[0],
reps: 0,
targetTime: 5000,
seconds: 0,
answer: "",
}
},
mounted() {
this.startPeriod()
},
methods: {
backspace() {
this.answer = this.answer.slice(0, -1);
},
clear() {
this.answer = "";
this.$refs.input.classList.remove('correct', 'incorrect');
},
changeAnswer(digit) {
this.answer += digit;
this.evaluate();
},
startPeriod() {
this.seconds = 0;
this.reps = 0;
this.startTime = Date.now();
this.getNextProblem(this.workingProblems[0]);
},
getNextProblem() {
if (this.workingProblems.length === 1) {
this.currentProb = this.workingProblems[0];
this.startProblem();
return;
}
let nextProblem;
let idx;
do {
idx = Math.floor(Math.random() * (this.workingProblems.length));
nextProblem = this.workingProblems[idx];
} while (nextProblem === this.currentProb)
this.currentProb = nextProblem;
this.startProblem();
},
startProblem() {
this.startTimer();
},
startTimer: function() {
this.timer = setTimeout( () => {
this.timer = null;
}, this.targetTime)
},
manipulatives() {
return new Array(this.currentProb.minuend);
},
toggleColor(event) {
event.target.classList.toggle('clicked');
},
keyInput(event) {
console.log(event.key);
},
evaluate(e) {
console.log(this.timer)
const answer = Number(this.currentProb.minuend) - Number(this.currentProb.subtrahend);
if (e) {
this.answer = e.target.value;
}
if (this.answer != answer) {
this.$refs.input.classList.add('incorrect');
return;
}
this.$refs.input.classList.add('correct');
setTimeout( this.respondToCorrectAnswer, 400 );
},
respondToCorrectAnswer() {
if (this.timer) {
this.handleSuccess();
}
this.reps += 1;
this.clear();
this.checkPeriod();
},
handleSuccess() {
this.currentProb.success_times += 1;
this.removeIfMastered(this.currentProb);
this.addToUpdateList(this.currentProb);
clearInterval(this.timer);
},
addToUpdateList(problem) {
if (!this.problemsToUpdate.includes(problem)){
this.problemsToUpdate.push(problem);
}
},
removeIfMastered() {
if (this.currentProb.success_times >= 2) {
const idx = this.workingProblems.indexOf(this.currentProb);
this.workingProblems.splice(idx, 1);
}
},
checkPeriod() {
if (this.workingProblems.length === 0 ||
this.getSecondsPracticed() >= this.maxSeconds) {
this.stopPeriod();
} else {
this.getNextProblem();
}
},
getSecondsPracticed() {
return new Date(Date.now() - this.startTime).getSeconds();
},
stopPeriod() {
const periodData = {
username: this.username,
problems: this.problemsToUpdate,
seconds: this.getSecondsPracticed(),
reps: this.reps
}
console.log(periodData)
this.$emit('finished', periodData)
},
},
}
</script>