From 35a470c4ae1f9de572c9bb30f923f24ef76b1fee Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 7 Sep 2024 10:56:05 +0200 Subject: [PATCH] possible suggested episodes bandaid --- app/(auth)/(tabs)/(home)/index.tsx | 45 ++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/app/(auth)/(tabs)/(home)/index.tsx b/app/(auth)/(tabs)/(home)/index.tsx index 69733e9b..bb9fbc2a 100644 --- a/app/(auth)/(tabs)/(home)/index.tsx +++ b/app/(auth)/(tabs)/(home)/index.tsx @@ -245,15 +245,22 @@ export default function index() { { title: "Suggested Episodes", queryKey: ["suggestedEpisodes", user?.Id], - queryFn: async () => - ( - await getSuggestionsApi(api).getSuggestions({ - userId: user?.Id, - limit: 10, - mediaType: ["Video"], - type: ["Episode"], - }) - ).data.Items || [], + queryFn: async () =>{ + try { + const userId = user?.Id; + if (!userId) return []; + + const suggestions = await getSuggestions(api, userId); + const nextUpPromises = suggestions.map(series => getNextUp(api, userId, series.Id!)); + const nextUpResults = await Promise.all(nextUpPromises); + + return nextUpResults.filter(item => item !== null); + } catch (error) { + console.error('Error fetching data:', error); + return []; + } + } + , type: "ScrollingCollectionList", orientation: "horizontal", }, @@ -371,3 +378,23 @@ export default function index() { ); } + +// Function to get suggestions +async function getSuggestions(api: any, userId: string) { + const response = await getSuggestionsApi(api).getSuggestions({ + userId, + limit: 10, + mediaType: ["Unknown"], + type: ["Series"], + }); + return response.data.Items ?? []; +} + +// Function to get the next up TV show for a series +async function getNextUp(api: any, userId: string, seriesId: string) { + const response = await getTvShowsApi(api).getNextUp({ + userId, + seriesId, + }); + return response.data.Items?.[0] ?? null; +} \ No newline at end of file