-
Notifications
You must be signed in to change notification settings - Fork 68
/
Advanced.js
126 lines (114 loc) · 3.59 KB
/
Advanced.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
import React, { useState, useMemo, useRef } from 'react'
import TinderCard from 'react-tinder-card'
const db = [
{
name: 'Richard Hendricks',
url: './img/richard.jpg'
},
{
name: 'Erlich Bachman',
url: './img/erlich.jpg'
},
{
name: 'Monica Hall',
url: './img/monica.jpg'
},
{
name: 'Jared Dunn',
url: './img/jared.jpg'
},
{
name: 'Dinesh Chugtai',
url: './img/dinesh.jpg'
}
]
function Advanced () {
const [currentIndex, setCurrentIndex] = useState(db.length - 1)
const [lastDirection, setLastDirection] = useState()
// used for outOfFrame closure
const currentIndexRef = useRef(currentIndex)
const childRefs = useMemo(
() =>
Array(db.length)
.fill(0)
.map((i) => React.createRef()),
[]
)
const updateCurrentIndex = (val) => {
setCurrentIndex(val)
currentIndexRef.current = val
}
const canGoBack = currentIndex < db.length - 1
const canSwipe = currentIndex >= 0
// set last direction and decrease current index
const swiped = (direction, nameToDelete, index) => {
setLastDirection(direction)
updateCurrentIndex(index - 1)
}
const outOfFrame = (name, idx) => {
console.log(`${name} (${idx}) left the screen!`, currentIndexRef.current)
// handle the case in which go back is pressed before card goes outOfFrame
currentIndexRef.current >= idx && childRefs[idx].current.restoreCard()
// TODO: when quickly swipe and restore multiple times the same card,
// it happens multiple outOfFrame events are queued and the card disappear
// during latest swipes. Only the last outOfFrame event should be considered valid
}
const swipe = async (dir) => {
if (canSwipe && currentIndex < db.length) {
await childRefs[currentIndex].current.swipe(dir) // Swipe the card!
}
}
// increase current index and show card
const goBack = async () => {
if (!canGoBack) return
const newIndex = currentIndex + 1
updateCurrentIndex(newIndex)
await childRefs[newIndex].current.restoreCard()
}
return (
<div>
<link
href='https://fonts.googleapis.com/css?family=Damion&display=swap'
rel='stylesheet'
/>
<link
href='https://fonts.googleapis.com/css?family=Alatsi&display=swap'
rel='stylesheet'
/>
<h1>React Tinder Card</h1>
<div className='cardContainer'>
{db.map((character, index) => (
<TinderCard
ref={childRefs[index]}
className='swipe'
key={character.name}
onSwipe={(dir) => swiped(dir, character.name, index)}
onCardLeftScreen={() => outOfFrame(character.name, index)}
>
<div
style={{ backgroundImage: 'url(' + character.url + ')' }}
className='card'
>
<h3>{character.name}</h3>
</div>
</TinderCard>
))}
</div>
<div className='buttons'>
<button style={{ backgroundColor: !canSwipe && '#c3c4d3' }} onClick={() => swipe('left')}>Swipe left!</button>
<button style={{ backgroundColor: !canGoBack && '#c3c4d3' }} onClick={() => goBack()}>Undo swipe!</button>
<button style={{ backgroundColor: !canSwipe && '#c3c4d3' }} onClick={() => swipe('right')}>Swipe right!</button>
</div>
{lastDirection ? (
<h2 key={lastDirection} className='infoText'>
You swiped {lastDirection}
</h2>
) : (
<h2 className='infoText'>
Swipe a card or press a button to get Restore Card button visible!
</h2>
)}
</div>
)
}
export default Advanced