mirror of
https://github.com/movie-web/native-app.git
synced 2025-09-13 08:23:26 +00:00
refactor: address code review
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "movie-web",
|
"name": "@movie-web/mobile",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"main": "expo-router/entry",
|
"main": "expo-router/entry",
|
||||||
|
@@ -47,28 +47,13 @@ export default function SearchScreen() {
|
|||||||
async function fetchSearchResults(query: string): Promise<ItemData[]> {
|
async function fetchSearchResults(query: string): Promise<ItemData[]> {
|
||||||
const results = await searchTitle(query);
|
const results = await searchTitle(query);
|
||||||
|
|
||||||
return results
|
return results.map((result) => ({
|
||||||
.map((result) => {
|
id: result.id.toString(),
|
||||||
switch (result.media_type) {
|
title: result.media_type === "tv" ? result.name : result.title,
|
||||||
case "movie":
|
posterUrl: getMediaPoster(result.poster_path),
|
||||||
return {
|
year: new Date(
|
||||||
id: result.id.toString(),
|
result.media_type === "tv" ? result.first_air_date : result.release_date,
|
||||||
title: result.title,
|
).getFullYear(),
|
||||||
posterUrl: getMediaPoster(result.poster_path),
|
type: result.media_type,
|
||||||
year: new Date(result.release_date).getFullYear(),
|
}));
|
||||||
type: result.media_type,
|
|
||||||
};
|
|
||||||
case "tv":
|
|
||||||
return {
|
|
||||||
id: result.id.toString(),
|
|
||||||
title: result.name,
|
|
||||||
posterUrl: getMediaPoster(result.poster_path),
|
|
||||||
year: new Date(result.first_air_date).getFullYear(),
|
|
||||||
type: result.media_type,
|
|
||||||
};
|
|
||||||
default:
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.filter((item): item is ItemData => item !== undefined);
|
|
||||||
}
|
}
|
||||||
|
29
packages/tmdb/src/details.ts
Normal file
29
packages/tmdb/src/details.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import type { MovieDetails, TvShowDetails } from "tmdb-ts";
|
||||||
|
|
||||||
|
import { tmdb } from "./util";
|
||||||
|
|
||||||
|
export async function fetchMediaDetails(
|
||||||
|
id: string,
|
||||||
|
type: "movie" | "tv",
|
||||||
|
): Promise<
|
||||||
|
{ type: "movie" | "tv"; result: TvShowDetails | MovieDetails } | undefined
|
||||||
|
> {
|
||||||
|
try {
|
||||||
|
let result: TvShowDetails | MovieDetails;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case "tv":
|
||||||
|
result = await tmdb.tvShows.details(parseInt(id, 10));
|
||||||
|
break;
|
||||||
|
case "movie":
|
||||||
|
result = await tmdb.movies.details(parseInt(id, 10));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { type, result };
|
||||||
|
} catch (ex) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
@@ -1,2 +1,4 @@
|
|||||||
export const name = "tmdb";
|
export const name = "tmdb";
|
||||||
export * from "./search";
|
export * from "./search";
|
||||||
|
export * from "./details";
|
||||||
|
export * from "./util";
|
||||||
|
@@ -1,9 +1,6 @@
|
|||||||
import type { MovieDetails, TvShowDetails } from "tmdb-ts";
|
import type { MovieWithMediaType, TVWithMediaType } from "tmdb-ts";
|
||||||
import { TMDB } from "tmdb-ts";
|
|
||||||
|
|
||||||
const TMDB_API_KEY =
|
import { tmdb } from "./util";
|
||||||
"eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJkYTM1ZTgyMzE4OTc0NTgxNDJmZjljZTE4ODExNWRlNiIsInN1YiI6IjY0OTM0ZDQ1ODliNTYxMDExYzliZDVhMiIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.AzWnIcxPNgDwGdzeIZ_C3mRC_5_qy-Z-SRPglLjzlNc";
|
|
||||||
const tmdb = new TMDB(TMDB_API_KEY);
|
|
||||||
|
|
||||||
export async function searchTitle(query: string) {
|
export async function searchTitle(query: string) {
|
||||||
try {
|
try {
|
||||||
@@ -18,38 +15,8 @@ export async function searchTitle(query: string) {
|
|||||||
|
|
||||||
if (!results.length) throw new Error("No results found");
|
if (!results.length) throw new Error("No results found");
|
||||||
|
|
||||||
return results;
|
return results as unknown as MovieWithMediaType[] | TVWithMediaType[];
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
throw new Error(`Error searching for title: ${(ex as Error).message}`);
|
throw new Error(`Error searching for title: ${(ex as Error).message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchMediaDetails(
|
|
||||||
id: string,
|
|
||||||
type: "movie" | "tv",
|
|
||||||
): Promise<
|
|
||||||
{ type: "movie" | "tv"; result: TvShowDetails | MovieDetails } | undefined
|
|
||||||
> {
|
|
||||||
try {
|
|
||||||
let result: TvShowDetails | MovieDetails;
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case "tv":
|
|
||||||
result = await tmdb.tvShows.details(parseInt(id, 10));
|
|
||||||
break;
|
|
||||||
case "movie":
|
|
||||||
result = await tmdb.movies.details(parseInt(id, 10));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return { type, result };
|
|
||||||
} catch (ex) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getMediaPoster(posterPath: string): string {
|
|
||||||
return `https://image.tmdb.org/t/p/w185/${posterPath}`;
|
|
||||||
}
|
|
||||||
|
9
packages/tmdb/src/util.ts
Normal file
9
packages/tmdb/src/util.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { TMDB } from "tmdb-ts";
|
||||||
|
|
||||||
|
const TMDB_API_KEY =
|
||||||
|
"eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJkYTM1ZTgyMzE4OTc0NTgxNDJmZjljZTE4ODExNWRlNiIsInN1YiI6IjY0OTM0ZDQ1ODliNTYxMDExYzliZDVhMiIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.AzWnIcxPNgDwGdzeIZ_C3mRC_5_qy-Z-SRPglLjzlNc";
|
||||||
|
export const tmdb = new TMDB(TMDB_API_KEY);
|
||||||
|
|
||||||
|
export function getMediaPoster(posterPath: string): string {
|
||||||
|
return `https://image.tmdb.org/t/p/w185/${posterPath}`;
|
||||||
|
}
|
Reference in New Issue
Block a user