import React, { useRef, useState } from 'react' import { Image, ImageSourcePropType, NativeScrollEvent, ScrollView, StyleProp, StyleSheet, Text, View, ViewStyle, } from 'react-native' import { NativeSafeAreaViewProps, SafeAreaView } from 'react-native-safe-area-context' import AppAnalytics from 'src/analytics/AppAnalytics' import { OnboardingEvents } from 'src/analytics/Events' import { ScrollDirection } from 'src/analytics/types' import Button, { BtnSizes, BtnTypes } from 'src/components/Button' import Pagination from 'src/components/Pagination' import BackChevron from 'src/icons/BackChevron' import Times from 'src/icons/Times' import { navigateBack } from 'src/navigator/NavigationService' import { TopBarIconButton } from 'src/navigator/TopBarButton' import colors from 'src/styles/colors' import { typeScale } from 'src/styles/fonts' import progressDots from 'src/styles/progressDots' import variables from 'src/styles/variables' export enum EducationTopic { backup = 'backup', celo = 'celo', } interface EducationStep { image: ImageSourcePropType | null topic: EducationTopic title: string // If set to true, title is displayed at the top isTopTitle?: boolean text?: string } export type Props = NativeSafeAreaViewProps & { stepInfo: EducationStep[] buttonType?: BtnTypes buttonText: string finalButtonType?: BtnTypes finalButtonText: string dotStyle?: StyleProp activeDotStyle?: StyleProp onFinish: () => void } const Education = (props: Props) => { const { style, stepInfo, buttonType = BtnTypes.SECONDARY, buttonText, finalButtonType = BtnTypes.PRIMARY, finalButtonText, dotStyle = progressDots.circlePassive, activeDotStyle = progressDots.circleActive, onFinish, ...passThroughProps } = props const [currentIndex, setCurrentIndex] = useState(0) // Scroll View Ref for button clicks const scrollViewRef = useRef(null) const handleScroll = (event: { nativeEvent: NativeScrollEvent }) => { const { topic } = stepInfo[currentIndex] const nextIndex = Math.round(event.nativeEvent.contentOffset.x / variables.width) if (nextIndex === currentIndex) { return } const direction = nextIndex > currentIndex ? ScrollDirection.next : ScrollDirection.previous if (topic === EducationTopic.backup) { AppAnalytics.track(OnboardingEvents.backup_education_scroll, { currentStep: currentIndex, direction: direction, }) } else if (topic === EducationTopic.celo) { AppAnalytics.track(OnboardingEvents.celo_education_scroll, { currentStep: currentIndex, direction: direction, }) } setCurrentIndex(Math.round(event.nativeEvent.contentOffset.x / variables.width)) } if (!stepInfo.length) { // No Steps, no slider return null } const goBack = () => { const { topic } = stepInfo[currentIndex] if (currentIndex === 0) { if (topic === EducationTopic.backup) { AppAnalytics.track(OnboardingEvents.backup_education_cancel) } else if (topic === EducationTopic.celo) { AppAnalytics.track(OnboardingEvents.celo_education_cancel) } navigateBack() } else { scrollViewRef.current?.scrollTo({ x: variables.width * (currentIndex - 1), animated: true }) } } const nextStep = () => { // If we are on the last step, call the onFinish function otherwise scroll to the next step currentIndex === stepInfo.length - 1 ? onFinish() : scrollViewRef.current?.scrollTo({ x: variables.width * (currentIndex + 1), animated: true }) } return ( : } /> {stepInfo.map((step: EducationStep, i: number) => ( {step.isTopTitle && {step.title}} {!!step.image && ( )} {!step.isTopTitle && {step.title}} {!!step.text && {step.text}} ))}