From d4c5945526dc43d49baf4339985c6b1102a1b432 Mon Sep 17 00:00:00 2001 From: Aditya Pawar Date: Thu, 28 Mar 2024 16:59:08 -0700 Subject: [PATCH] Use rpc function to get all stories instead of loop --- src/app/(tabs)/genre/index.tsx | 85 ++++++++++++---------- src/app/(tabs)/search/index.tsx | 10 ++- src/components/PreviewCard/PreviewCard.tsx | 4 +- src/queries/stories.tsx | 20 +++++ 4 files changed, 75 insertions(+), 44 deletions(-) diff --git a/src/app/(tabs)/genre/index.tsx b/src/app/(tabs)/genre/index.tsx index 5857607d..28218e24 100644 --- a/src/app/(tabs)/genre/index.tsx +++ b/src/app/(tabs)/genre/index.tsx @@ -14,9 +14,8 @@ import { SafeAreaView } from 'react-native-safe-area-context'; import styles from './styles'; import BackButton from '../../../components/BackButton/BackButton'; -import GenreStoryPreviewCard from '../../../components/GenreStoryPreviewCard/GenreStoryPreviewCard'; import { fetchGenreStoryPreviews, fetchGenres } from '../../../queries/genres'; -import { fetchStoryPreviewById } from '../../../queries/stories'; +import { fetchStoryPreviewById, fetchStoryPreviewByIds } from '../../../queries/stories'; import { StoryPreview, GenreStories, Genre } from '../../../queries/types'; import globalStyles from '../../../styles/globalStyles'; import PreviewCard from '../../../components/PreviewCard/PreviewCard'; @@ -36,12 +35,7 @@ function GenreScreen() { const [currTone, setCurrTone] = useState(''); const [currTopic, setCurrTopic] = useState(''); const [toneTopicFilters, setToneTopicFilters] = useState([]); - const params = useLocalSearchParams<{ genreId: string }>(); - const params2 = useLocalSearchParams<{ genreType: string }>(); - const params3 = useLocalSearchParams<{ genreName: string }>(); - const { genreId } = params; - const { genreType } = params2; - const { genreName } = params3; + const { genreId, genreType, genreName } = useLocalSearchParams<{ genreId: string, genreType: string, genreName: string }>(); console.log('passing in genreId params:', genreId); console.log('testing passing in genreType', genreType); @@ -77,6 +71,7 @@ function GenreScreen() { setLoading(false); return []; } + console.log('testing find story IDs by Name Function:', filteredStoryIds); return filteredStoryIds; } @@ -152,39 +147,51 @@ function GenreScreen() { useEffect(() => { const showAllStoryPreviews = async () => { + setLoading(true); if (genreStoryIds.length > 0) { - setLoading(true); - const previews: StoryPreview[] = []; - const tones: string[] = []; - const topics: string[] = []; - for (const idString of genreStoryIds) { - const id = parseInt(idString, 10); - try { - const storyPreview: StoryPreview[] = - await fetchStoryPreviewById(id); - previews.push(storyPreview[0]); - storyPreview[0].tone.forEach(item => { - tones.push(item); - }); - storyPreview[0].topic.forEach(item => { - topics.push(item); - }); - console.log('testing storyPreview outputs:', storyPreview); - } catch (error) { - console.log( - `There was an error while trying to fetch a story preview by id: ${error}`, - ); - } - } - const filteredTopics: string[] = topics.filter( - (item): item is string => item !== null, - ); - const filteredTones: string[] = tones.filter( - (item): item is string => item !== null, - ); + const previews: StoryPreview[] = await fetchStoryPreviewByIds(genreStoryIds); + + const tones: string[] = previews + .reduce((acc: string[], current: StoryPreview) => { + return acc.concat(current.tone); + }, [] as string[]) + .filter(tone => tone !== null); + const topics: string[] = previews + .reduce((acc: string[], current: StoryPreview) => { + return acc.concat(current.topic); + }, [] as string[]) + .filter(topic => topic !== null); + + // for (const idString of genreStoryIds) { + // const id = parseInt(idString, 10); + // try { + // const storyPreview: StoryPreview[] = + // await fetchStoryPreviewById(id); + // previews.push(storyPreview[0]); + // storyPreview[0].tone.forEach(item => { + // tones.push(item); + // }); + // storyPreview[0].topic.forEach(item => { + // topics.push(item); + // }); + // console.log('testing storyPreview outputs:', storyPreview); + // } catch (error) { + // console.log( + // `There was an error while trying to fetch a story preview by id: ${error}`, + // ); + // } + // } + // const filteredTopics: string[] = topics.filter( + // (item): item is string => item !== null, + // ); + // const filteredTones: string[] = tones.filter( + // (item): item is string => item !== null, + // ); + + console.log('testing storyPreview outputs:', previews); setAllStoryPreviews(previews.flat()); - setgenreTopics(filteredTopics); - setgenreTones(filteredTones); + setgenreTopics(topics); + setgenreTones(tones); console.log('testing tone usestate'); setLoading(false); } else { diff --git a/src/app/(tabs)/search/index.tsx b/src/app/(tabs)/search/index.tsx index 63e8b28c..6fce7c8a 100644 --- a/src/app/(tabs)/search/index.tsx +++ b/src/app/(tabs)/search/index.tsx @@ -29,7 +29,7 @@ const getRecentSearch = async () => { const jsonValue = await AsyncStorage.getItem('GWN_RECENT_SEARCHES_ARRAY'); return jsonValue != null ? JSON.parse(jsonValue) : []; } catch (error) { - console.log(error); + console.error(error); } }; @@ -38,7 +38,7 @@ const setRecentSearch = async (searchResult: RecentSearch[]) => { const jsonValue = JSON.stringify(searchResult); await AsyncStorage.setItem('GWN_RECENT_SEARCHES_ARRAY', jsonValue); } catch (error) { - console.log(error); + console.error(error); } }; @@ -47,7 +47,7 @@ const getRecentStory = async () => { const jsonValue = await AsyncStorage.getItem('GWN_RECENT_STORIES_ARRAY'); return jsonValue != null ? JSON.parse(jsonValue) : []; } catch (error) { - console.log(error); + console.error(error); } }; @@ -56,7 +56,7 @@ const setRecentStory = async (recentStories: StoryPreview[]) => { const jsonValue = JSON.stringify(recentStories); await AsyncStorage.setItem('GWN_RECENT_STORIES_ARRAY', jsonValue); } catch (error) { - console.log(error); + console.error(error); } }; @@ -91,12 +91,14 @@ function SearchScreen() { setSearchResults([]); return; } + const updatedData = allStories.filter((item: StoryPreview) => { const title = `${item.title.toUpperCase()})`; const author = `${item.author_name.toUpperCase()})`; const text_data = text.toUpperCase(); return title.indexOf(text_data) > -1 || author.indexOf(text_data) > -1; }); + setSearch(text); setSearchResults(updatedData); setShowGenreCarousals(false); diff --git a/src/components/PreviewCard/PreviewCard.tsx b/src/components/PreviewCard/PreviewCard.tsx index ac894d63..e54d0343 100644 --- a/src/components/PreviewCard/PreviewCard.tsx +++ b/src/components/PreviewCard/PreviewCard.tsx @@ -10,6 +10,8 @@ import * as cheerio from 'cheerio'; import styles from './styles'; import globalStyles from '../../styles/globalStyles'; +const placeholderImage = "https://gwn-uploads.s3.amazonaws.com/wp-content/uploads/2021/10/10120952/Girls-Write-Now-logo-avatar.png" + type PreviewCardProps = { title: string; image: string; @@ -38,7 +40,7 @@ function PreviewCard({ - + diff --git a/src/queries/stories.tsx b/src/queries/stories.tsx index 8a2c21a8..330abbbc 100644 --- a/src/queries/stories.tsx +++ b/src/queries/stories.tsx @@ -101,3 +101,23 @@ export async function fetchStoryPreviewById( return data; } } + +export async function fetchStoryPreviewByIds( + storyIds: number[], +): Promise { + const { data, error } = await supabase.rpc( + 'curr_story_preview_by_ids', + { + input_ids: storyIds, + }, + ); + if (error) { + console.log(error); + throw new Error( + `An error occured when trying to fetch story preview by IDs: ${error}`, + ); + } else { + return data; + } +} +