Skip to content

Commit

Permalink
WJ-18 - New: Popular movie api and parse;
Browse files Browse the repository at this point in the history
WJ-18 - New: Format date and tmdb images;
  • Loading branch information
Lucas Teixeira committed Aug 24, 2020
1 parent 7d5ae8e commit 7440f98
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/domains/Movie/api/Popular/RawResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default interface RawResponse {
poster_path?: string;
overview: string;
release_date: string;
genre_ids: number[];
id: number;
original_title: string;
original_language: string;
title: string;
backdrop_path?: string;
popularity: number;
vote_count: number;
video: boolean;
vote_average: number;
}
13 changes: 13 additions & 0 deletions src/domains/Movie/api/Popular/Response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default interface Response {
poster?: string;
backdrop?: string;
overview: string;
releaseDate: string;
genreIds: number[];
id: number;
originalTitle: string;
title: string;
popularity: number;
voteCount: number;
voteAverage: number;
}
45 changes: 45 additions & 0 deletions src/domains/Movie/api/Popular/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import tmdb from 'services/api/tmdb';

import RawResponse from 'domains/Movie/api/Popular/RawResponse';
import Response from 'domains/Movie/api/Popular/Response';
import formatDate from 'shared/utils/formatDate';
import formatTmdbImage from 'shared/utils/formatTmdbImage';

const Popular = async (): Promise<Response[]> => {
const response = await rawPopular();

return parseResponse(response);
};

export const rawPopular = async (): Promise<RawResponse[]> => {
const response = await tmdb.get('/movie/popular');

return response.data.results;
};

const parseResponse = (rawResponse: RawResponse[]): Response[] => {
let response = [] as Response[];

rawResponse.forEach(movie => {
const parsedMovie = {
overview: movie.overview,
genreIds: movie.genre_ids,
id: movie.id,
originalTitle: movie.original_title,
title: movie.title,
popularity: movie.popularity,
voteCount: movie.vote_count,
voteAverage: movie.vote_average,

releaseDate: formatDate({ value: movie.release_date }),
poster: formatTmdbImage({ value: movie.poster_path }),
backdrop: formatTmdbImage({ value: movie.backdrop_path }),
} as Response;

response = [...response, parsedMovie];
});

return response;
};

export default Popular;
3 changes: 3 additions & 0 deletions src/shared/utils/dtos/FormatDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default interface FormatDate {
value?: string;
}
6 changes: 6 additions & 0 deletions src/shared/utils/dtos/FormatTmdbImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import TmdbImageSize from 'shared/utils/enums/TmdbImageSize';

export default interface FormatTmdbImage {
value?: string;
size?: TmdbImageSize;
}
8 changes: 8 additions & 0 deletions src/shared/utils/enums/TmdbImageSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum TmdbImageSize {
SMALL = 'w300',
MEDIUM = 'w780',
LARGE = 'w1280',
ORIGINAL = 'original',
}

export default TmdbImageSize;
16 changes: 16 additions & 0 deletions src/shared/utils/formatDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import FormatDate from 'shared/utils/dtos/FormatDate';

const formatDate = ({ value }: FormatDate): string => {
if (!value) {
return '-';
}

const date = new Date(value);
date.setDate(date.getDate() + 1);

const formattedDate = Intl.DateTimeFormat('pt-BR').format(date);

return formattedDate;
};

export default formatDate;
15 changes: 15 additions & 0 deletions src/shared/utils/formatTmdbImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import FormatTmdbImage from 'shared/utils/dtos/FormatTmdbImage';
import TmdbImageSize from 'shared/utils/enums/TmdbImageSize';

const formatTmdbImage = ({
value,
size = TmdbImageSize.MEDIUM,
}: FormatTmdbImage): string | null => {
if (!value) {
return null;
}

return `${process.env.REACT_APP_TMDB_IMAGE_URL}/${size}${value}`;
};

export default formatTmdbImage;

0 comments on commit 7440f98

Please sign in to comment.