Skip to content

Commit

Permalink
🐛 fix infite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
pouyio committed Nov 6, 2024
1 parent 1051d3a commit 23ac111
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 36 deletions.
18 changes: 6 additions & 12 deletions src/pages/movies/MoviesWatched.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MovieWatched } from 'models';
import React, { useEffect, useState } from 'react';
import React, { useState } from 'react';
import { filterByGenres } from 'state/slices/movies';
import { useAppSelector } from 'state/store';
import ImageLink from '../../components/ImageLink';
Expand All @@ -9,19 +8,14 @@ import { EmptyState } from 'components/EmptyState';
import { NoResults } from 'components/NoResults';

export const MoviesWatched: React.FC = () => {
const [orderedMovies, setOrderedMovies] = useState<MovieWatched[]>([]);
const { getItemsByPage } = usePagination(orderedMovies);
const [genres, setGenres] = useState<string[]>([]);
const { watched } = useAppSelector(filterByGenres(genres));

useEffect(() => {
const newItems = watched
.map((m) => m)
.sort((a, b) =>
new Date(a.watched_at) < new Date(b.watched_at) ? 1 : -1
);
setOrderedMovies(newItems);
}, [watched]);
const orderedMovies = watched
.map((m) => m)
.sort((a, b) => (new Date(a.watched_at) < new Date(b.watched_at) ? 1 : -1));

const { getItemsByPage } = usePagination(orderedMovies);

return !genres.length && !orderedMovies.length ? (
<EmptyState />
Expand Down
36 changes: 12 additions & 24 deletions src/pages/shows/ShowsWatched.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ShowWatched } from 'models';
import React, { useEffect, useState } from 'react';
import React, { useState } from 'react';
import { filterByGenres } from 'state/slices/shows';
import { useAppSelector } from 'state/store';
import ImageLink from '../../components/ImageLink';
Expand All @@ -9,31 +8,20 @@ import { EmptyState } from 'components/EmptyState';
import { NoResults } from 'components/NoResults';

const ShowsWatched: React.FC = () => {
const [orderedShows, setOrderedShows] = useState<ShowWatched[]>([]);
const [genres, setGenres] = useState<string[]>([]);
const { watched } = useAppSelector(filterByGenres(genres));

useEffect(() => {
setOrderedShows(
watched.sort((a, b) => {
if (
!a.progress?.next_episode ||
a.progress?.next_episode?.season === 0
) {
return 1;
}
if (
!b.progress?.next_episode ||
b.progress?.next_episode?.season === 0
) {
return -1;
}
const aDate = new Date(a.progress?.last_watched_at ?? '');
const bDate = new Date(b.progress?.last_watched_at ?? '');
return aDate < bDate ? 1 : -1;
})
);
}, [watched]);
const orderedShows = watched.sort((a, b) => {
if (!a.progress?.next_episode || a.progress?.next_episode?.season === 0) {
return 1;
}
if (!b.progress?.next_episode || b.progress?.next_episode?.season === 0) {
return -1;
}
const aDate = new Date(a.progress?.last_watched_at ?? '');
const bDate = new Date(b.progress?.last_watched_at ?? '');
return aDate < bDate ? 1 : -1;
});

const { getItemsByPage } = usePagination(orderedShows);

Expand Down

0 comments on commit 23ac111

Please sign in to comment.