-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (68 loc) · 1.97 KB
/
index.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
import dogs from './data.js';
import Dog from './Dog.js'
// buttons
const likeBtn = document.getElementById('likeBtn')
const rejectBtn = document.getElementById('rejectBtn')
// add event listeners to buttons
likeBtn.addEventListener('click', like)
rejectBtn.addEventListener('click', reject)
// on screen touch swipe left or right
document.addEventListener('touchstart', (e) => {
if (e.target.classList.contains('swipe-left')) {
like()
} else if (e.target.classList.contains('swipe-right')) {
reject()
}
} )
// create an array of dogs from the data.js file
const dogsArray = dogs.map(dog => new Dog(dog))
let dog = getNewDog()
function getNewDog() {
// get next dog in the array
const nextDog = dogsArray.shift()
return nextDog ? new Dog(nextDog) : {}
}
// gets next dog in the array and renders it, if no more dogs, end
function nextDog() {
setTimeout(() => {
if (dogsArray.length > 0) {
dog = getNewDog()
render()
} else {
end()
}
}, 1000)
}
// like = swipe left
function like() {
dog.hasBeenLiked = true
dog.hasBeenSwiped = true
swipeAnimation('left')
nextDog()
}
// reject = swipe right
function reject() {
dog.hasBeenSwiped = true
swipeAnimation('right')
nextDog()
}
function swipeAnimation(direction) {
const profileContainer = document.getElementById('profileContainer')
const stamp = document.getElementById('stamp')
stamp.classList.add(`stamp-${direction}`)
setTimeout(() => {
profileContainer.classList.add(`swipe-${direction}`)
setTimeout(() => {
profileContainer.classList.remove(`swipe-${direction}`)
}, 800)
}, 500)
}
function end() {
console.log('end')
document.getElementById('mainContainer').innerHTML =
`<p class="end-message">No more dogs in your area</p>`
}
function render() {
document.getElementById('profileContainer').innerHTML = dog.getProfileHtml()
}
render()