From 132f70f14cd077665e3569ef452325166f2d33ea Mon Sep 17 00:00:00 2001 From: Lucas Teixeira Date: Fri, 4 Sep 2020 21:37:57 -0300 Subject: [PATCH] WJ-17 - Update: Minifying favorite list method; --- src/domains/Favorites/api/List/index.ts | 22 ++++++++-------------- src/screens/Home/index.tsx | 1 - 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/domains/Favorites/api/List/index.ts b/src/domains/Favorites/api/List/index.ts index 0152d8f..0241318 100644 --- a/src/domains/Favorites/api/List/index.ts +++ b/src/domains/Favorites/api/List/index.ts @@ -19,27 +19,21 @@ export const rawFavorites = async (): Promise => { const parseResponse = async ( rawResponse: RawResponse[], ): Promise => { - let response = [] as any[]; + const response = rawResponse.map(async (favorite: RawResponse) => { + // Get favorites details from TMDB API and merge + const details = await Details(favorite.movie_id); - // Parse API user favorites - rawResponse.forEach(favorite => { - const parsedFavorite = { + // Merge API and TMDB results + return { + ...details, id: favorite.id, userId: favorite.user_id, movieId: favorite.movie_id, }; - - response = [...response, parsedFavorite]; - }); - - // Get favorites details from TMDB API and merge - const parsedResponse = response.map(async (favorite: Response) => { - const details = await Details(favorite.movieId); - - return { ...details, ...favorite, id: favorite.id }; }); - const resolvedResponse = await Promise.all(parsedResponse); + // Resolve async requests and promises + const resolvedResponse = Promise.all(response); return resolvedResponse; }; diff --git a/src/screens/Home/index.tsx b/src/screens/Home/index.tsx index 05dd9a6..d4f3b5b 100644 --- a/src/screens/Home/index.tsx +++ b/src/screens/Home/index.tsx @@ -18,7 +18,6 @@ const Home: React.FC = () => { NowPlaying().then(response => setNowPlayingList(response)); Popular().then(response => setPopularList(response)); Favorites().then(response => { - console.log('response', response); setFavoriteList(response); }); }, []);