From dc43a085b9c57f5bbf88e1b76ab0c093d94d0fe7 Mon Sep 17 00:00:00 2001 From: Cody Garcia Date: Sat, 26 Oct 2024 11:46:10 -0700 Subject: [PATCH] finished sprint, video plays bc of useref instead of usestate --- src/screens/LegalRights/VideoPage/index.tsx | 29 ++++++++++++++------- src/screens/LegalRights/index.tsx | 10 +++---- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/screens/LegalRights/VideoPage/index.tsx b/src/screens/LegalRights/VideoPage/index.tsx index 0d088a5..03927bb 100644 --- a/src/screens/LegalRights/VideoPage/index.tsx +++ b/src/screens/LegalRights/VideoPage/index.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { Pressable, ScrollView, Text, View } from 'react-native'; import { Video } from 'expo-av'; import supabase from '@/supabase/createClient'; @@ -21,11 +21,13 @@ export default function VideoPage(testProp: any) { const [index, setIndex] = useState(0); // index of current page in full array of pages const [language, setLanguage] = useState('english'); // which language associated to array of pages - // video link associated to current page; used for src of video element - const [videoLink, setVideoLink] = useState( + const videoLinkRef = useRef( 'https://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4', ); + // + const [renderTrigger, setRenderTrigger] = useState(0); + const nextPage = () => { setIndex(prevIndex => Math.min(prevIndex + 1, preaData.length - 1)); // next index in array of pages }; @@ -42,17 +44,26 @@ export default function VideoPage(testProp: any) { }, []); useEffect(() => { - let response = supabase.storage // fetch current http link for current page - .from('PREA_videos') - .getPublicUrl(language + '/' + preaData[index]['video_id'] + '.mp4'); - let { data } = response; - setVideoLink(data['publicUrl']); // set link for video link + const fetchVideoLink = async () => { + let response = supabase.storage + .from('PREA_videos') + .getPublicUrl(`${language}/${preaData[index]['video_id']}.mp4`); + + let { data } = response; + + videoLinkRef.current = data['publicUrl']; // Update the ref with the new video link + + setRenderTrigger(prev => prev + 1); // Trigger a re-render to apply the new link + }; + + fetchVideoLink(); }, [index]); // run useEffect every time value of index changes return (