diff --git a/src/App.tsx b/src/App.tsx index 1859c58608d6ea86059c8b7d9962b6c489885bc7..14fdcf34a991f70428e2e3088b7fba948ad0def0 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,8 +1,8 @@ import React, {useEffect, useState} from 'react'; import {createUseStyles} from "react-jss"; import {useApiClient} from "./api/ApiClientContext"; -import {Title} from "./api/models/Title"; -import {Locale, LocalePriority, selectLocaleVersion} from "./locale/selectLocaleData"; +import {getLocalizedDescription, getLocalizedName, getLocalizedRating, Title} from "./api/models/Title"; +import {Locale} from "./locale/Locale"; import {usePlayabilityRating} from "./mime/usePlayabilityRating"; import {sortLexicallyAsc} from "./util/sortLexically"; import {sortNumericallyDesc} from "./util/sortNumerically"; @@ -23,48 +23,50 @@ export default function App() { return ( <div> - {data?.sort(sortLexicallyAsc(item => - selectLocaleVersion(locale, LocalePriority.REGION_BEFORE_LOCALE, item.titles)?.name || "")) + {data?.sort(sortLexicallyAsc(item => getLocalizedName(item, locale)?.name || "")) .map((item: Title) => { - const title = selectLocaleVersion(locale, LocalePriority.REGION_BEFORE_LOCALE, item.titles); - const description = selectLocaleVersion(locale, LocalePriority.LOCALE_BEFORE_REGION, item.descriptions); - const rating = selectLocaleVersion(locale, LocalePriority.REGION_ONLY, item.ratings); - const poster = item.images.find(it => it.kind === "poster"); - const backdrop = item.images.find(it => it.kind === "backdrop"); - const mediaList = item.media.map(it => { - return { - item: it, - value: playabilityRating(it), - }; - }).sort(sortNumericallyDesc(it => it.value)); - const media = mediaList[0]?.item; + const title = getLocalizedName(item, locale); + const description = getLocalizedDescription(item, locale); + const rating = getLocalizedRating(item, locale); + const poster = item.images.find(it => it.kind === "poster"); + const backdrop = item.images.find(it => it.kind === "backdrop"); + const mediaList = item.media.map(it => { + return { + item: it, + value: playabilityRating(it), + }; + }).sort(sortNumericallyDesc(it => it.value)); + const media = mediaList[0]?.item; - return ( - <div key={item.ids.uuid} className={classes.movie}> - <h1>{title?.name}</h1> - <p><strong>{description?.tagline}</strong></p> - <p>{description?.overview}</p> - <p><strong>{rating?.certification}</strong></p> - {poster && ( - <img - className={classes.poster} - alt={`Movie poster for ${title?.name}`} - src={poster.src} - /> - )} - {backdrop && ( - <img - className={classes.poster} - alt={`Movie backdrop for ${title?.name}`} - src={backdrop.src} - /> - )} - {media && media.src} - <pre>{JSON.stringify(item.subtitles, null, 2)}</pre> - <pre>{JSON.stringify(item.preview, null, 2)}</pre> - </div> - ) - })} + return ( + <div key={item.ids.uuid} className={classes.movie}> + <h1>{title?.name}</h1> + <p><strong>{description?.tagline}</strong></p> + <p>{description?.overview}</p> + <p><strong>{rating?.certification}</strong></p> + {poster && ( + <img + className={classes.poster} + alt={`Movie poster for ${title?.name}`} + src={poster.src} + /> + )} + {backdrop && ( + <img + className={classes.poster} + alt={`Movie backdrop for ${title?.name}`} + src={backdrop.src} + /> + )} + {media && ( + <p><a href={media.src}>{media.src}</a></p> + )} + {item.preview && ( + <p><a href={item.preview}>{item.preview}</a></p> + )} + </div> + ) + })} </div> ); } diff --git a/src/api/models/LocalizedData.ts b/src/api/models/LocalizedData.ts deleted file mode 100644 index 71b27d2d971e3693b7e73bd530fcea00e7fdef32..0000000000000000000000000000000000000000 --- a/src/api/models/LocalizedData.ts +++ /dev/null @@ -1,5 +0,0 @@ -export interface LocalizedData { - region: string | null, - languages?: string[], - kind?: string, -} diff --git a/src/api/models/Rating.ts b/src/api/models/Rating.ts index e4b85e7e189fa7b31ba7e538f6bac6a23298c944..898cfa1ca98e04f22ca1debb4ce4a9a9902d790e 100644 --- a/src/api/models/Rating.ts +++ b/src/api/models/Rating.ts @@ -1,4 +1,4 @@ -import {LocalizedData} from "./LocalizedData"; +import {LocalizedData} from "../../locale/LocalizedData"; export interface Rating extends LocalizedData { region: string | null, diff --git a/src/api/models/Title.ts b/src/api/models/Title.ts index 5c60c51b91482799e5fb99f2ab1d4291ca071389..0b0fb8254eaa1a6a6c48e5a03ff0cabfaf8fc7ee 100644 --- a/src/api/models/Title.ts +++ b/src/api/models/Title.ts @@ -1,3 +1,7 @@ +import {Locale} from "../../locale/Locale"; +import {LocalePriority} from "../../locale/LocalePriority"; +import {selectLocaleVersion} from "../../locale/selectLocaleData"; +import {selectRegionalVersion} from "../../locale/selectRegionalData"; import {Cast} from "./Cast"; import {Genre} from "./Genre"; import {Image} from "./Image"; @@ -28,3 +32,12 @@ export interface Title { createdAt: string, updatedAt: string } + +export const getLocalizedName = (title: Title, locale: Locale): TitleName | null => + selectLocaleVersion(locale, LocalePriority.REGION, title.titles) + +export const getLocalizedDescription = (title: Title, locale: Locale): TitleDescription | null => + selectLocaleVersion(locale, LocalePriority.LOCALE, title.descriptions) + +export const getLocalizedRating = (title: Title, locale: Locale): Rating | null => + selectRegionalVersion(locale, title.ratings) diff --git a/src/api/models/TitleDescription.ts b/src/api/models/TitleDescription.ts index cc2b03a7f56d1aded258a58d45753e0eb35ca024..a9c56652d74efd7201cbec21d4210bb7f12583b7 100644 --- a/src/api/models/TitleDescription.ts +++ b/src/api/models/TitleDescription.ts @@ -1,4 +1,4 @@ -import {LocalizedData} from "./LocalizedData"; +import {LocalizedData} from "../../locale/LocalizedData"; export interface TitleDescription extends LocalizedData { overview: string, diff --git a/src/api/models/TitleName.ts b/src/api/models/TitleName.ts index b21be8e8e09d1a5534f0c21b67302fb68f4c8ba5..9299eaf951a7f00bbb1b47044b3099e367c8e9e2 100644 --- a/src/api/models/TitleName.ts +++ b/src/api/models/TitleName.ts @@ -1,4 +1,4 @@ -import {LocalizedData} from "./LocalizedData"; +import {LocalizedData} from "../../locale/LocalizedData"; export interface TitleName extends LocalizedData { name: string, diff --git a/src/locale/Locale.ts b/src/locale/Locale.ts new file mode 100644 index 0000000000000000000000000000000000000000..cd3d7ca619d4164e0f0b6f643d80820084266a6d --- /dev/null +++ b/src/locale/Locale.ts @@ -0,0 +1,4 @@ +export interface Locale { + language: string, + region: string +} diff --git a/src/locale/LocalePriority.ts b/src/locale/LocalePriority.ts new file mode 100644 index 0000000000000000000000000000000000000000..bf62cc6f6144d31962cd7cde96bb9e54b98ec545 --- /dev/null +++ b/src/locale/LocalePriority.ts @@ -0,0 +1,4 @@ +export enum LocalePriority { + REGION, + LOCALE +} diff --git a/src/locale/LocalizedData.ts b/src/locale/LocalizedData.ts new file mode 100644 index 0000000000000000000000000000000000000000..8451a7261369226512d0c9d1596283b9eeb9b923 --- /dev/null +++ b/src/locale/LocalizedData.ts @@ -0,0 +1,6 @@ +import {RegionalData} from "./RegionalData"; + +export interface LocalizedData extends RegionalData { + languages: string[], + kind: string, +} diff --git a/src/locale/RegionalData.ts b/src/locale/RegionalData.ts new file mode 100644 index 0000000000000000000000000000000000000000..32aa6162dea89af603e6c9438b2dc8030537a6c8 --- /dev/null +++ b/src/locale/RegionalData.ts @@ -0,0 +1,3 @@ +export interface RegionalData { + region: string | null, +} diff --git a/src/locale/selectLocaleData.ts b/src/locale/selectLocaleData.ts index f2d49a448f3788f84bcb8e0087ef6d0c837e69ed..37e61fceb9d180c9b490a1b080d8b069660656f6 100644 --- a/src/locale/selectLocaleData.ts +++ b/src/locale/selectLocaleData.ts @@ -1,15 +1,6 @@ -import {LocalizedData} from "../api/models/LocalizedData"; - -export interface Locale { - language: string, - region: string -} - -export enum LocalePriority { - REGION_ONLY, - REGION_BEFORE_LOCALE, - LOCALE_BEFORE_REGION -} +import {Locale} from "./Locale"; +import {LocalePriority} from "./LocalePriority"; +import {LocalizedData} from "./LocalizedData"; function filterVersions<T extends LocalizedData>( locale: Locale, @@ -17,20 +8,20 @@ function filterVersions<T extends LocalizedData>( data: T[] ): T | undefined { switch (method) { - case LocalePriority.REGION_ONLY: - return data.find((el) => { - return (el.region === locale.region); - }); - case LocalePriority.REGION_BEFORE_LOCALE: + case LocalePriority.REGION: return data.sort((a, b) => +(b.languages?.includes(locale.language) || false) - +(a.languages?.includes(locale.language) || false) - ).find((el) => el.region === locale.region); - case LocalePriority.LOCALE_BEFORE_REGION: + ).find((el) => + el.region === locale.region + || el.region === null); + case LocalePriority.LOCALE: return data.sort((a, b) => +(b.region === locale.region) - +(a.region === locale.region) - ).find((el) => el.languages?.includes(locale.language)); + ).find((el) => + el.languages?.includes(locale.language) + || el.languages.length === 0); } } diff --git a/src/locale/selectRegionalData.ts b/src/locale/selectRegionalData.ts new file mode 100644 index 0000000000000000000000000000000000000000..a4a48ac13c8b0610e5660435b7a0df3050d9725a --- /dev/null +++ b/src/locale/selectRegionalData.ts @@ -0,0 +1,11 @@ +import {Locale} from "./Locale"; +import {RegionalData} from "./RegionalData"; + +export function selectRegionalVersion<T extends RegionalData>( + locale: Locale, + data: T[] +): T | null { + return data.find((el) => { + return (el.region === locale.region); + }) || null; +}