src/entities/MoviesCardsGrid/model.ts   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 31
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 23
mnd 2
bc 2
fnc 0
dl 0
loc 31
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 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