Total Complexity | 2 |
Complexity/F | 0 |
Lines of Code | 31 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { GENRES_URL, makeMoviesListURL } from 'shared/config/api' |
||
2 | import { fetchData } from 'shared/api' |
||
3 | import { IMoviesItem, IGenres } from 'types/common' |
||
4 | |||
5 | const mapGenresIdsToNames = (movies: IMoviesItem[], genres: IGenres[]) => |
||
6 | movies?.map((movie: IMoviesItem) => ({ |
||
7 | ...movie, |
||
8 | genre_ids: movie.genre_ids.map( |
||
9 | (id: number | string) => |
||
10 | genres.find((genre: IGenres) => genre.id === id)?.name, |
||
11 | ), |
||
12 | })) |
||
13 | |||
14 | export const fetchMoviesList = (page = 1) => |
||
15 | Promise.all([ |
||
16 | fetchData(GENRES_URL), |
||
17 | fetchData(makeMoviesListURL('top_rated', page)), |
||
18 | ]).then(([{ genres }, { results: movies, total_pages }]) => { |
||
19 | const preparedData = mapGenresIdsToNames(movies, genres) |
||
20 | |||
21 | const totalPagesEvent = new CustomEvent('gotTotalPages', { |
||
22 | detail: total_pages, |
||
23 | }) |
||
24 | |||
25 | window.dispatchEvent(totalPagesEvent) |
||
26 | |||
27 | return { |
||
28 | movies: preparedData, |
||
29 | totalPages: total_pages, |
||
30 | } |
||
31 | }) |
||
32 |