Skip to content

Commit

Permalink
fix(DIA-168): onboarding quiz crash (#9305)
Browse files Browse the repository at this point in the history
* fix: activeIndex race condition

* chore: refactor again

* refact: better fix and a comment

* chore: address review comment
  • Loading branch information
MounirDhahri authored Sep 21, 2023
1 parent d9e7251 commit e07dad0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
18 changes: 6 additions & 12 deletions src/app/Components/FancySwiper/FancySwiper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ export const OFFSET_X = 100

interface FancySwiperProps {
cards: Card[]
activeIndex: number
onSwipeRight: (index: number) => void
onSwipeLeft: (index: number) => void
onSwipeRight: () => void
onSwipeLeft: () => void
}

export const FancySwiper = ({
cards,
activeIndex,
onSwipeRight,
onSwipeLeft,
}: FancySwiperProps) => {
export const FancySwiper = ({ cards, onSwipeRight, onSwipeLeft }: FancySwiperProps) => {
const remainingCards = cards.reverse()
const swiper = useRef<Animated.ValueXY>(new Animated.ValueXY()).current

Expand All @@ -28,12 +22,12 @@ export const FancySwiper = ({
swiper.setValue({ x: 0, y: 0 })

if (swipeDirection === "right") {
onSwipeRight(activeIndex)
onSwipeRight()
} else {
onSwipeLeft(activeIndex)
onSwipeLeft()
}
},
[remainingCards, activeIndex, swiper]
[remainingCards, swiper]
)

const panResponder = PanResponder.create({
Expand Down
21 changes: 15 additions & 6 deletions src/app/Scenes/ArtQuiz/ArtQuizArtworks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ArtQuizLoader } from "app/Scenes/ArtQuiz/ArtQuizLoader"
import { GlobalStore } from "app/store/GlobalStore"
import { goBack, navigate } from "app/system/navigation/navigate"
import { extractNodes } from "app/utils/extractNodes"
import { isEmpty } from "lodash"
import { Suspense, useEffect, useState } from "react"
import { Image } from "react-native"

Expand All @@ -21,6 +22,7 @@ const ArtQuizArtworksScreen = () => {
const { width } = useScreenDimensions()
const space = useSpace()
const artworks = extractNodes(queryResult.me?.quiz.quizArtworkConnection)

const lastInteractedArtworkIndex = queryResult.me?.quiz.quizArtworkConnection?.edges?.findIndex(
(edge) => edge?.interactedAt === null
)
Expand All @@ -43,16 +45,24 @@ const ArtQuizArtworksScreen = () => {
}
}, [])

const handleSwipe = (swipeDirection: "left" | "right", activeIndex: number) => {
setActiveCardIndex(activeIndex + 1)
handleNext(swipeDirection === "right" ? "Like" : "Dislike", activeIndex)
const handleSwipe = (swipeDirection: "left" | "right") => {
handleNext(swipeDirection === "right" ? "Like" : "Dislike", activeCardIndex)

// No need to swipe through the last card, just navigate to the results screen
if (activeCardIndex + 1 < artworks.length) {
setActiveCardIndex(activeCardIndex + 1)
}
}

const handleNext = (action: "Like" | "Dislike", activeIndex: number) => {
popoverMessage.hide()

const currentArtwork = artworks[activeIndex]

if (isEmpty(currentArtwork)) {
return
}

if (action === "Like") {
submitSave({
variables: {
Expand Down Expand Up @@ -176,9 +186,8 @@ const ArtQuizArtworksScreen = () => {
<Screen.Body>
<FancySwiper
cards={artworkCards}
activeIndex={activeCardIndex}
onSwipeRight={(index) => handleSwipe("right", index)}
onSwipeLeft={(index) => handleSwipe("left", index)}
onSwipeRight={() => handleSwipe("right")}
onSwipeLeft={() => handleSwipe("left")}
/>
</Screen.Body>
</Screen>
Expand Down

0 comments on commit e07dad0

Please sign in to comment.