-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
330 lines (291 loc) · 8.15 KB
/
App.jsx
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import React, { useEffect, useState, useRef, useLayoutEffect } from "react"
import { useDispatch, useSelector } from "react-redux"
import {
fetchSquadrons,
fetchWinners,
deleteSquadron,
updateSquadron,
updateWinner,
createSquadron,
selectData,
} from "./src/services/squadrons"
import { gsap } from "gsap"
import Modal from "./src/components/Portal"
import Squadron from "./src/components/Squadron"
gsap.registerPlugin(ScrollTrigger)
const MAX_SPEED = 3500
const MIN_SPEED = 0
const MIN_WEIGHT = 1
const MAX_WEIGHT = 6
const App = () => {
const [starShips, setStarShips] = useState(null)
const [deployed, setDeployed] = useState(false)
const [showModal, setShowModal] = useState(true)
const [type, setType] = useState("start")
const [squadron, setSquadron] = useState(null)
const [yourStarShip, setYourStartShip] = useState(null)
const [fieldWidth, setFieldWidth] = useState(window.innerWidth)
const dispatch = useDispatch()
const squadrons = useSelector(selectData)
const ref = useRef(null)
const refSquadrons = useRef(null)
const refTop = useRef(null)
const refWinner = useRef(null)
const refAction = useRef(false)
const setStart = () => {
if (refSquadrons.current) {
let sqArray = [...refSquadrons.current.childNodes]
sqArray.forEach((el) => gsap.to(el, { y: "0px" }))
}
}
const pushWinner = (el) => {
refWinner.current = el
setType("info")
toggleModal()
saveWinner(el.id)
}
const scrollToTop = () => {
refTop.current?.scrollIntoView({ behavior: "smooth" })
}
const onScroll = (e) => {
if (!refAction.current) addAction()
if (e.deltaY > 0) {
if (refSquadrons.current && !refWinner.current) {
let sqArray = [...refSquadrons.current.childNodes]
sqArray.some((el) => {
if (!ScrollTrigger.isInViewport(el, 0.2)) {
pushWinner(el)
return true
}
})
}
}
}
const onResize = () => {
const windowsWidth =
window.innerWidth && document.documentElement.clientWidth
? Math.min(window.innerWidth, document.documentElement.clientWidth)
: window.innerWidth ||
document.documentElement.clientWidth ||
document.getElementsByTagName("body")[0].clientWidth
setFieldWidth(windowsWidth)
getParticlesAnimation()
scrollToTop()
}
const getParticlesAnimation = () => {
let i = 50
while (--i > -1) {
let dot = document.createElement("div")
let dotContainer = document.getElementById("space")
dot.className = "dot"
dotContainer.append(dot)
gsap.to(dot, {
x: Math.random() * window.innerWidth,
y: Math.random() * window.innerHeight,
z: Math.random() * Math.PI * 10,
})
gsap.to(dot, {
duration: 30,
z: 100,
})
}
}
useEffect(() => {
dispatch(fetchSquadrons())
window.addEventListener("wheel", onScroll)
window.addEventListener("resize", onResize)
getParticlesAnimation()
scrollToTop()
return () => {
window.removeEventListener("wheel", onScroll)
window.removeEventListener("resize", onResize)
}
}, [])
useEffect(() => {
const countElements = ref.current.childNodes.length
setDeployed(countElements >= 1 ? true : false)
})
useEffect(() => {
setStarShips(squadrons)
}, [squadrons])
const saveWinner = (id) => {
try {
dispatch(updateWinner({ id }))
} catch (err) {
console.error("Failed to update the Winner", err)
}
}
const getY = (el) => {
const speed = el.getAttribute("data-speed")
const weight = el.getAttribute("data-weight")
if (speed === 0) return speed
return 1 - speed / weight
}
const addAction = () => {
if (refSquadrons.current) {
gsap.utils.toArray("[data-speed]").forEach((el) => {
gsap.to(el, {
y: () => getY(el),
ease: "none",
scrollTrigger: {
trigger: el,
start: "-=800",
end: "top",
scrub: false,
},
})
})
}
refAction.current = true
}
const addColorTo = (id) => {
if (refSquadrons.current) {
const sqArray = [...refSquadrons.current.childNodes]
const el = sqArray[id]
gsap.set(el, {
borderBottom:
"100px solid rgb(random(0,190), random(0,190), random(0,1)",
})
}
}
const addColorToAll = () => {
if (refSquadrons.current) {
gsap.set(".squadron", {
borderBottom:
"100px solid rgb(random(0,190), random(0,190), random(0,1)",
})
}
}
useEffect(() => {
addColorToAll()
}, [deployed, starShips])
const deploySquadrons = () => (
<div ref={refSquadrons}>
{starShips.map((el, index) => (
<Squadron
key={index}
fieldWidth={fieldWidth}
name={
yourStarShip && yourStarShip.id === index
? `${el.name} -> ${yourStarShip.name}`
: el.name
}
speed={el.speed}
weight={el.weight}
id={el.squadron_id}
index={index}
total={starShips.length}
sendSquadronToAction={sendSquadronToAction}
/>
))}
</div>
)
const sendSquadronToAction = (id, name, speed, type) => {
setSquadron({ id, name, speed })
setType(type)
toggleModal()
}
const createNewSquadron = () => {
setSquadron(null)
setType("create")
toggleModal()
}
const showWinners = () => {
dispatch(fetchWinners())
setType("show")
toggleModal()
}
const assignSquadron = (e) => {
e.preventDefault()
const randomId = Math.floor(Math.random() * starShips.length)
setYourStartShip({ id: randomId, name: `You` })
addColorTo(randomId)
}
const toggleModal = () => {
setShowModal((prev) => !prev)
}
const controlParams = (param, min, max) => {
let currentParam = Number(param)
if (param <= min) currentParam = min
if (param >= max) currentParam = max
return currentParam
}
const handleUpdate = (id, currentSpeed) => {
const speed = controlParams(currentSpeed, MIN_SPEED, MAX_SPEED)
try {
dispatch(updateSquadron({ id, speed }))
} catch (err) {
console.error("Failed to update the Squadron", err)
}
toggleModal()
resetActions()
}
const handleDelete = (id) => {
try {
dispatch(deleteSquadron({ id }))
} catch (err) {
console.error("Failed to delete the Squadron", err)
}
toggleModal()
resetActions()
}
const handleCreate = ({ name, speed, weight }) => {
speed = controlParams(speed, MIN_SPEED, MAX_SPEED)
weight = controlParams(weight, MIN_WEIGHT, MAX_WEIGHT)
try {
dispatch(createSquadron({ name, speed, weight }))
} catch (err) {
console.error("Failed to create the Squadron", err)
}
toggleModal()
resetActions()
}
const handleShow = () => {
toggleModal()
dispatch(fetchSquadrons())
resetActions()
}
const playAgain = () => {
resetActions()
}
const resetActions = () => {
refWinner.current = null
refAction.current = false
scrollToTop()
setStart()
}
return (
<>
<div ref={refTop}></div>
<div className={`controls-${showModal}`}>
<button onClick={(e) => createNewSquadron(e)}>Create Squadron</button>
<button onClick={(e) => showWinners(e)}>Show Winners</button>
<button onClick={(e) => assignSquadron(e)}>Select Squadron</button>
<button onClick={(e) => playAgain()}>Play again</button>
</div>
{showModal ? (
<Modal
className="fade-up"
text={refWinner.current?.firstChild.innerText}
type={type}
squadron={squadron}
winners={type === "show" ? squadrons : []}
toggleModal={toggleModal}
handleUpdate={handleUpdate}
handleDelete={handleDelete}
handleCreate={handleCreate}
handleShow={handleShow}
/>
) : null}
<div className="field" ref={ref}>
{starShips ? deploySquadrons() : null}
</div>
<footer>
<a className="github" href="https://github.com/dithiane">
@ 2023 Dithiane
</a>
</footer>
</>
)
}
export default App