Skip to content

Commit

Permalink
chore(fe): various error catches + split redux genres
Browse files Browse the repository at this point in the history
  • Loading branch information
AshDyson committed Apr 20, 2024
1 parent b0ee725 commit d951bd6
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pkg/frontend/src/components/reviewButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function ReviewButtons({
}

function isReviewed() {
if (redux_reviews && redux_reviews.length > 0 && currentUser) {
if (data && redux_reviews && redux_reviews.length > 0 && currentUser) {
const review = redux_reviews.filter((r) => {
return r.tmdb_id === data.id.toString() && r.user === currentUser.id;
});
Expand Down
9 changes: 5 additions & 4 deletions pkg/frontend/src/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ import Admin from './admin/index';
// Pages
import Home from './index';
import Movie from './movie/[pid]';
import Studio from './movie/studio/[pid]';
import Company from './multi/company/[pid]';
import Genre from './multi/genre/[pid]';
import MyAccount from './myAccount';
import People from './people/[pid]';
import Requests from './requests';
import Search from './search';
import Setup from './setup';
import Show from './tv/[pid]';
import Network from './tv/network/[pid]';

const mapStateToProps = (state) => {
return {
Expand Down Expand Up @@ -340,10 +339,11 @@ function Petio({ redux_pos }) {
/>
</Route>
<Route exact path="/movie/studio/:pid">
<Studio
<Company
currentUser={currentUser}
config={globalConfig}
newNotification={newNotification}
type="studio"
/>
</Route>
<Route exact path="/people/:pid">
Expand All @@ -370,10 +370,11 @@ function Petio({ redux_pos }) {
/>
</Route>
<Route exact path="/tv/network/:pid">
<Network
<Company
currentUser={currentUser}
config={globalConfig}
newNotification={newNotification}
type="network"
/>
</Route>
<Route exact path="/search">
Expand Down
6 changes: 4 additions & 2 deletions pkg/frontend/src/pages/movie/[pid].js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function Movie({
const { pid } = useParams();
const movieData = redux_movies[pid];
const [collection, setCollection] = useState(false);
const [error, setError] = useState(false);

useEffect(() => {
function handleResize() {
Expand Down Expand Up @@ -89,9 +90,10 @@ function Movie({
useEffect(() => {
async function getMovieDetails() {
try {
media.getMovie(pid);
await media.getMovie(pid);
getReviews();
} catch (e) {
setError(true);
console.log(e);
}
}
Expand Down Expand Up @@ -205,7 +207,7 @@ function Movie({
}
}

if (movieData === 'error') {
if (error) {
return <NotFound />;
}

Expand Down
159 changes: 159 additions & 0 deletions pkg/frontend/src/pages/multi/company/[pid].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { useEffect, useState } from 'react';
import { LazyLoadImage } from 'react-lazy-load-image-component';
import 'react-lazy-load-image-component/src/effects/opacity.css';
import { connect } from 'react-redux';
import { useParams } from 'react-router-dom';

import NotFound from '../../404';
import Grid from '../../../components/grid';
import Hero from '../../../components/hero';
import { Loading } from '../../../components/loading';
import Meta from '../../../components/meta';
import media from '../../../services/media.service';
import hero from '../../../styles/components/hero.module.scss';
import styles from '../../../styles/views/company.module.scss';

const mapStateToProps = (state) => {
return {
redux_studios: state.media.studios,
redux_networks: state.media.networks,
redux_studioData: state.media.studioData,
redux_networkData: state.media.networkData,
};
};

function Company({
newNotification,
type,
redux_studios,
redux_studioData,
redux_networks,
redux_networkData,
}) {
const [companyName, setCompanyName] = useState(null);
const [companyLogo, setCompanyLogo] = useState(null);
const [total, setTotal] = useState(1);
const [featured, setFeatured] = useState(false);
const { pid } = useParams();
const [loadingMore, setLoadingMore] = useState(false);
const [page, setPage] = useState(1);
const [error, setError] = useState(false);
const companyData =
type === 'studio'
? redux_studioData[pid] || null
: redux_networkData[pid] || null;
const companyInfo =
type === 'studio'
? redux_studios[pid] || null
: redux_networks[pid] || null;

useEffect(() => {
if (companyData) {
if (companyData.results) setFeatured(companyData.results[0]);
setPage(companyData.page);
setTotal(companyData.total_pages);
}
}, [companyData]);

useEffect(() => {
if (companyInfo) {
setCompanyName(companyInfo.name);
setCompanyLogo(companyInfo.logo_path);
}
}, [companyInfo]);

useEffect(() => {
async function getCoDetails() {
try {
await media.companyLookup(type, 1, pid);
if (type === 'studio') {
await media.getStudio(pid);
} else {
await media.getNetwork(pid);
}
} catch (e) {
console.log(e);
setError(true);
}
}

getCoDetails();
}, [pid, type]);

useEffect(() => {
async function loadMore() {
if (loadingMore || page === total) return;
try {
setLoadingMore(true);
await media.companyLookup(type, page + 1, pid);
setLoadingMore(false);
} catch (e) {
console.log(e);
}
}

function handleScroll() {
if (
window.innerHeight * 2 + window.scrollY >=
document.body.offsetHeight
) {
// you're at the bottom of the page
loadMore();
}
}

window.addEventListener('scroll', handleScroll);

return () => {
window.removeEventListener('scroll', handleScroll);
};
}, [loadingMore, page, total, pid, type]);

// if (!coData) return <Loading />;

if (error) return <NotFound />;

return (
<div className={styles.wrap} key={`${type}_single_${pid}`}>
<Meta title={companyName || 'Loading'} />
<div className={`${hero.discovery} ${hero.company}`}>
<div className="container">
<div
className={`${hero.discovery__content} ${hero.company__content}`}
>
<div className={hero.company__logo}>
{companyLogo ? (
<LazyLoadImage
src={`https://image.tmdb.org/t/p/w780_filter(duotone,ffffff,bdbdbd)${companyLogo}`}
alt={companyName || ' '}
effect="opacity"
visibleByDefault={true}
/>
) : (
<p>{companyName || ' '}</p>
)}
</div>
</div>
</div>
<div className={hero.company__background}>
{featured ? (
<Hero
className={hero.discovery__image}
image={featured.backdrop_path}
/>
) : null}
</div>
</div>
<Grid
title={`${companyName} ${type === 'network' ? 'Shows' : 'Movies'}`}
data={companyData ? companyData.results : null}
type={type === 'network' ? 'tv' : 'movie'}
key={`${type}_${pid}_${type === 'network' ? 'tv' : 'movie'}`}
id={`${type}_${pid}_${type === 'network' ? 'tv' : 'movie'}`}
newNotification={newNotification}
/>
</div>
);
}

export default connect(mapStateToProps)(Company);
11 changes: 6 additions & 5 deletions pkg/frontend/src/pages/multi/genre/[pid].js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ function Genre({ type, newNotification, redux_genreData }) {
const [genreName, setGenreName] = useState('');
// const [movies, setMovies] = useState(false);
const [total, setTotal] = useState(1);
const [featuredMovie, setFeaturedMovie] = useState(false);
const [featured, setFeatured] = useState(false);
const { pid } = useParams();
const [loadingMore, setLoadingMore] = useState(false);
const [page, setPage] = useState(1);
const [query, setQuery] = useState(null);
const genreData = redux_genreData[pid] || null;
const mediaType = type === 'movie' ? 'movies' : 'tv';
const genreData = redux_genreData[mediaType][pid] || null;

useEffect(() => {
if (genreData) {
if (genreData.results) setFeaturedMovie(genreData.results[0]);
if (genreData.results) setFeatured(genreData.results[0]);
setPage(genreData.page);
setTotal(genreData.total_pages);
}
Expand Down Expand Up @@ -139,10 +140,10 @@ function Genre({ type, newNotification, redux_genreData }) {
</div>
</div>
<div className={hero.genre__background}>
{featuredMovie ? (
{featured ? (
<Hero
className={hero.discovery__image}
image={featuredMovie.backdrop_path}
image={featured.backdrop_path}
/>
) : null}
</div>
Expand Down
6 changes: 4 additions & 2 deletions pkg/frontend/src/pages/tv/[pid].js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function Tv({
const [mobile, setMobile] = useState(false);
const [trailer, setTrailer] = useState(false);
const [issuesOpen, setIssuesOpen] = useState(false);
const [error, setError] = useState(false);
const { pid } = useParams();
const tvData = redux_tv[pid];

Expand All @@ -61,8 +62,9 @@ function Tv({
useEffect(() => {
async function getTvDetails() {
try {
media.getTv(pid);
await media.getTv(pid);
} catch (e) {
setError(true);
console.log(e);
}
}
Expand Down Expand Up @@ -182,7 +184,7 @@ function Tv({
}
}

if (tvData === 'error') {
if (error) {
return <NotFound />;
}

Expand Down
20 changes: 15 additions & 5 deletions pkg/frontend/src/pages/tv/network/[pid].js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ export default function Network({ newNotification }) {
async function getCoDetails() {
try {
const showData = await media.getNetwork(pid);
const tvLookup = await media.lookup('show', 1, {
with_networks: pid,
});
const tvLookup = await media.companyLookup(
'show',
1,
{
with_networks: pid,
},
pid,
);
setCoData(showData);
setShows(tvLookup.results);
setTotal(tvLookup.totalPages);
Expand All @@ -49,7 +54,12 @@ export default function Network({ newNotification }) {
async function loadMore() {
if (loadingMore || !query || page === total) return;
setLoadingMore(true);
const showsLookup = await media.lookup('show', page + 1, query);
const showsLookup = await media.companyLookup(
'show',
page + 1,
query,
pid,
);
setShows([...shows, ...showsLookup.results]);
setPage(page + 1);
setLoadingMore(false);
Expand All @@ -70,7 +80,7 @@ export default function Network({ newNotification }) {
return () => {
window.removeEventListener('scroll', handleScroll);
};
}, [loadingMore, query, page, total, shows]);
}, [loadingMore, query, page, total, shows, pid]);

if (!coData) return <Loading />;

Expand Down
Loading

0 comments on commit d951bd6

Please sign in to comment.