-
Notifications
You must be signed in to change notification settings - Fork 121
/
movies.ts
61 lines (50 loc) · 1.84 KB
/
movies.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { MovieListApiResponse } from '../api/movies';
import { MovieApiResponse } from '../api/types';
import {
Movie,
MovieId,
MovieRequiredPropsKey,
MovieStoreProps,
NormalizedMovieProps,
ParsedMovie,
} from '../redux/movies/types';
import { theme } from '../theme';
/* ------------- Key Extractor ------------- */
export const movieIdsKeyExtractor = (movieId: MovieId) => movieId.toString();
const { success, danger, warning } = theme.colors;
/* ------------- Normalization ------------- */
export const requiredMovieProps: MovieRequiredPropsKey[] = [
'id',
'title',
'overview',
'release_date',
'poster_path',
'backdrop_path',
];
export const isGoodMovieRating = (rating: number) => rating >= 7;
export const isNormalMovieRating = (rating: number) => rating >= 5;
export const isBadMovieRating = (rating: number) => rating < 5;
export const getMovieScoreColor = (score: number) =>
isGoodMovieRating(score) ? success : isNormalMovieRating(score) ? warning : danger;
export const isLastMovieList = (data: MovieListApiResponse) => data.page >= data.total_pages;
const isEnoughMovieInfo = (movie: MovieApiResponse) => requiredMovieProps.every(prop => !!movie[prop]);
const filterNotEnoughInfoMovies = (movies: MovieApiResponse[]) => movies.filter(isEnoughMovieInfo) as ParsedMovie[];
export const normalizeMovies = (movies: MovieApiResponse[] = []) =>
filterNotEnoughInfoMovies(movies).map(movie => normalizeMovie(movie));
const movieStoreProps: MovieStoreProps = {
isFetching: false,
isWatchlistPending: false,
isInWatchlist: false,
isFavoritesPending: false,
isInFavorites: false,
};
export const normalizeMovie = (movie: ParsedMovie): Movie => {
const normalizedMovieProps: NormalizedMovieProps = {
year: movie.release_date.substr(0, 4),
};
return {
...movie,
...normalizedMovieProps,
...movieStoreProps,
};
};