-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathAppClass.js
170 lines (154 loc) · 5.24 KB
/
AppClass.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
import React, {useState} from 'react'
import axios from 'axios'
import * as yup from 'yup'
const formSchema = yup.object().shape({
formValue: yup
.string()
.email('Ouch: email must be a valid email')
.required('Ouch: email is required')
.notOneOf(['[email protected]'],'[email protected] failure #71')
})
// Suggested initial states
const initialMessage = ''
const initialEmail = ''
const initialSteps = 0
const initialIndex = 4 // the index the "B" is at
const initialX = 2
const initialY = 2
const initialState = {
message: initialMessage,
email: initialEmail,
index: initialIndex,
steps: initialSteps,
}
export default class AppClass extends React.Component {
constructor(){
super()
this.state= {
x: initialX,
y: initialY,
steps: initialSteps,
xy: initialIndex,
message: initialMessage,
formValues: ''
}
}
// THE FOLLOWING HELPERS ARE JUST RECOMMENDATIONS.
// You can delete them and build your own logic from scratch.
getXY = () => {
return(`(${this.state.x},${this.state.y})`)
// It it not necessary to have a state to track the coordinates.
// It's enough to know what index the "B" is at, to be able to calculate them.
}
reset = () => {
this.setState({
x: initialX,
y: initialY,
steps: initialSteps,
message: initialMessage,
xy: initialIndex,
formValues: ''
})
}
getNextIndex = (direction) => {
if(direction === 'left'){
if(this.state.x - 1 === 0){
return ({"x": this.state.x, "y":this.state.y})
}
return ({"x": this.state.x - 1, "y":this.state.y,"xy":this.state.xy -1,"steps": this.state.steps + 1})
}
if(direction === 'right'){
if(this.state.x + 1 === 4){
return ({"x": this.state.x, "y":this.state.y})
}
return ({"x": this.state.x + 1, "y":this.state.y,"xy":this.state.xy + 1,"steps": this.state.steps + 1})
}
if(direction === 'up'){
if(this.state.y - 1 === 0){
return ({"x": this.state.x, "y":this.state.y})
}
return ({"x": this.state.x, "y":this.state.y - 1,"xy":this.state.xy - 3,"steps": this.state.steps + 1})
}
if(direction === 'down'){
if(this.state.y + 1 === 4){
return ({"x": this.state.x, "y":this.state.y})
}
return ({"x": this.state.x, "y":this.state.y + 1,"xy":this.state.xy + 3,"steps": this.state.steps + 1})
}
// This helper takes a direction ("left", "up", etc) and calculates what the next index
// of the "B" would be. If the move is impossible because we are at the edge of the grid,
// this helper should return the current index unchanged.
}
move = (evt) => {
let nextMove = this.getNextIndex(evt.target.id)
if (`(${nextMove.x},${nextMove.y})` === this.getXY()){
return this.setState({message: `You can't go ${evt.target.id}`})
}
this.setState({...this.state,
message: initialMessage,
x: nextMove.x,
y: nextMove.y,
steps: nextMove.steps,
xy: nextMove.xy})
// This event handler can use the helper above to obtain a new index for the "B",
// and change any states accordingly.
}
onChange = (evt) => {
this.setState({formValues: evt.target.value})
}
validate = (name,value) => {
yup.reach(formSchema, name)
.validate(value)
.then(() => this.post())
.catch(err => this.setState({message:err.errors[0]}))
}
post = () => {
const toSend = {
"x": this.state.x,
"y": this.state.y,
"steps": this.state.steps,
"email": this.state.formValues
}
axios.post('http://localhost:9000/api/result', toSend)
.then(({data}) => {this.setState({message: data.message})})
.finally(this.setState({formValues: ''}))
}
onSubmit = (evt) => {
evt.preventDefault()
this.validate('formValue', this.state.formValues)
}
render() {
const { className } = this.props
return (
<div id="wrapper" className={className}>
<div className="info">
<h3 id="coordinates">{`Coordinates ${this.getXY()}`}</h3>
<h3 id="steps">{`You moved ${this.state.steps} ${this.state.steps === 1 ? 'time' : 'times'}`}</h3> {/*Stateful move tracker*/}
</div>
<div id="grid">
{
[0, 1, 2, 3, 4, 5, 6, 7, 8].map(idx => (
<div key={idx} className={`square${idx === this.state.xy ? ' active' : ''}`}>
{idx === this.state.xy ? 'B' : null}
</div>
))
}
</div>
<div className="info">
<h3 id="message">{this.state.message}</h3>{/*display message from API call*/}
</div>
<div id="keypad">
<button id="left" onClick={(e) => this.move(e)}>LEFT</button>
<button id="up" onClick={(e) => this.move(e)}>UP</button>
<button id="right" onClick={(e) => this.move(e)}>RIGHT</button>
<button id="down" onClick={(e) => this.move(e)}>DOWN</button>
<button id="reset" onClick={() => this.reset()}>reset</button>
</div>
<form onSubmit={(e) => this.onSubmit(e)}>
<input id="email" type="text" placeholder="type email" value={this.state.formValues} onChange={(e) => this.onChange(e)}></input>
<input id="submit" type="submit"></input>
</form>
</div>
)
}
}