From eae38bb9ec8588856f319387d2f262d7ee3f7e9c Mon Sep 17 00:00:00 2001 From: sct Date: Tue, 15 Sep 2020 00:09:11 +0900 Subject: [PATCH] feat(frontend): new dashboard concept (#82) --- src/components/Discover/index.tsx | 128 ++++++++++-------- src/components/Layout/Notifications/index.tsx | 2 +- src/components/Layout/SearchInput/index.tsx | 2 +- src/components/Layout/UserDropdown/index.tsx | 10 +- src/components/Layout/index.tsx | 4 +- src/components/Search/index.tsx | 12 +- src/components/TitleCard/index.tsx | 10 +- src/styles/globals.css | 9 ++ tailwind.config.js | 4 +- 9 files changed, 100 insertions(+), 81 deletions(-) diff --git a/src/components/Discover/index.tsx b/src/components/Discover/index.tsx index 22b6586c..21bb5d71 100644 --- a/src/components/Discover/index.tsx +++ b/src/components/Discover/index.tsx @@ -1,17 +1,27 @@ import React, { useRef } from 'react'; -import { useSWRInfinite } from 'swr'; -import type { MovieResult } from '../../../server/models/Search'; +import useSWR, { useSWRInfinite } from 'swr'; +import type { MovieResult, TvResult } from '../../../server/models/Search'; import TitleCard from '../TitleCard'; import useVerticalScroll from '../../hooks/useVerticalScroll'; -interface SearchResult { +interface MovieDiscoverResult { page: number; totalResults: number; totalPages: number; results: MovieResult[]; } -const getKey = (pageIndex: number, previousPageData: SearchResult | null) => { +interface TvDiscoverResult { + page: number; + totalResults: number; + totalPages: number; + results: TvResult[]; +} + +const getKey = ( + pageIndex: number, + previousPageData: MovieDiscoverResult | null +) => { if (previousPageData && pageIndex + 1 > previousPageData.totalPages) { return null; } @@ -20,61 +30,30 @@ const getKey = (pageIndex: number, previousPageData: SearchResult | null) => { }; const Discover: React.FC = () => { - const { data, error, size, setSize } = useSWRInfinite(getKey, { - initialSize: 3, - }); - - const isLoadingInitialData = !data && !error; - const isLoadingMore = - isLoadingInitialData || - (size > 0 && data && typeof data[size - 1] === 'undefined'); - - useVerticalScroll(() => { - setSize(size + 1); - }, !isLoadingMore && !isLoadingInitialData); - - if (error) { - return
{error}
; - } - - const titles = data?.reduce( - (a, v) => [...a, ...v.results], - [] as MovieResult[] + const { data: movieData, error: movieError } = useSWR( + '/api/v1/discover/movies' + ); + const { data: tvData, error: tvError } = useSWR( + '/api/v1/discover/tv' ); return ( <> -
+
-

- Discover +

+ Popular Movies

-
- - - - -
-
    - {titles?.map((title) => ( -
  • +
    + {movieData?.results.map((title) => ( +
    { year={title.releaseDate} mediaType={title.mediaType} /> -
  • +
))} - {(isLoadingInitialData || - (isLoadingMore && (titles?.length ?? 0) > 0)) && - [...Array(8)].map((_item, i) => ( -
  • + {!movieData && + !movieError && + [...Array(10)].map((_item, i) => ( +
    -
  • + ))} - + +
    +
    +

    + Popular TV Shows +

    +
    +
    +
    + {tvData?.results.map((title) => ( +
    + +
    + ))} + {!tvData && + !tvError && + [...Array(10)].map((_item, i) => ( +
    + +
    + ))} +
    ); }; diff --git a/src/components/Layout/Notifications/index.tsx b/src/components/Layout/Notifications/index.tsx index d30d5a07..9a23529c 100644 --- a/src/components/Layout/Notifications/index.tsx +++ b/src/components/Layout/Notifications/index.tsx @@ -3,7 +3,7 @@ import React from 'react'; const Notifications: React.FC = () => { return ( @@ -45,28 +45,28 @@ const UserDropdown: React.FC = () => { ref={dropdownRef} >
    Your Profile Settings logout()} > diff --git a/src/components/Layout/index.tsx b/src/components/Layout/index.tsx index 791f91cc..0b66105b 100644 --- a/src/components/Layout/index.tsx +++ b/src/components/Layout/index.tsx @@ -14,7 +14,7 @@ const Layout: React.FC = ({ children }) => {