-
Notifications
You must be signed in to change notification settings - Fork 11
/
DiscoverDetailScreen.tsx
131 lines (120 loc) · 4 KB
/
DiscoverDetailScreen.tsx
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { useEffect, useState, useMemo } from 'react';
import { ShowData } from '../../types';
import {
actionAdventureHandler,
comedyHandler,
highRatedHandler,
horrorHandler,
huluHandler,
netflixHandler,
newlyAddedHandler,
primeHandler,
trendingHandler,
} from './discoverRequests';
import { useProfileContext } from '../../hooks';
import SearchResultCards from '../search_results/SearchResultsCards';
import DetailScreen from '../DetailScreen';
import { Typography } from '@mui/material';
const PATHS = [
{ path: 'trending', title: 'Trending' },
{ path: 'best', title: 'Highest Rated' },
{ path: 'new', title: 'Newly Added' },
{ path: 'action', title: 'Action & Adventure' },
{ path: 'comedy', title: 'Comedy' },
{ path: 'horror', title: 'Horror' },
{ path: 'netflix', title: 'Popular on Netflix' },
{ path: 'hulu', title: 'Popular on Hulu' },
{ path: 'prime', title: 'Popular on Prime' },
];
interface RequestHandlerProps {
path: string;
setState: React.Dispatch<React.SetStateAction<ShowData[] | null>>;
setLoading: React.Dispatch<React.SetStateAction<boolean>>;
}
/**
* Make a MovieDB API request based on the URL path
* @todo paginate data #838
*/
const requestHandler = async (props: RequestHandlerProps) => {
const { path, ...rest } = props;
switch (path) {
case 'trending':
await trendingHandler(rest);
break;
case 'best':
await highRatedHandler(rest);
break;
case 'new':
await newlyAddedHandler(rest);
break;
case 'action':
await actionAdventureHandler(rest);
break;
case 'comedy':
await comedyHandler(rest);
break;
case 'horror':
await horrorHandler(rest);
break;
case 'netflix':
await netflixHandler(rest);
break;
case 'hulu':
await huluHandler(rest);
break;
case 'prime':
await primeHandler(rest);
break;
}
};
const DiscoverDetailScreen: React.FC = () => {
const viewStateKey: string = 'streamabilityDiscoverDetailView';
const { profile, setProfile } = useProfileContext();
const path = window.location.pathname
.match(/\/\w+$/)
?.join()
.slice(1);
const [loading, setLoading] = useState<boolean>(true);
const storageItem = localStorage.getItem(viewStateKey);
const initialView = storageItem === 'grid' ? 'grid' : 'list';
const [viewState, setViewState] = useState<'list' | 'grid'>(initialView);
const [hash, setHash] = useState<number>(1);
const [data, setData] = useState<ShowData[] | null>(null);
useEffect(() => {
if (path) requestHandler({ path: path, setState: setData, setLoading: setLoading });
}, []);
const title = path ? PATHS[PATHS.findIndex((p) => p.path === path)].title : '';
if (!storageItem) localStorage.setItem(viewStateKey, initialView);
const cards = useMemo(() => {
return (
<SearchResultCards
details={data}
viewState={viewState}
profile={profile}
setProfile={setProfile}
/>
);
}, [data, hash, viewState, profile]);
// TODO: Create loader #839r
if (loading) return <p>Loading...</p>;
return (
<div
className='flex flex-col items-center w-full m-6'
data-testid={`discover-details-${path}-screen`}
>
<Typography variant='h4'>{title}</Typography>
<DetailScreen
viewStateKey={viewStateKey}
viewState={viewState}
setViewState={setViewState}
data={data}
setData={setData}
setHash={setHash}
cards={cards}
disableAlphabeticOrderFilter={true}
disableResultTypeFilter={true}
/>
</div>
);
};
export default DiscoverDetailScreen;