Skip to content

Commit

Permalink
add data loader to GenreExplore page
Browse files Browse the repository at this point in the history
  • Loading branch information
Endo committed May 9, 2023
1 parent 72ba3b3 commit c0e6e5e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
46 changes: 36 additions & 10 deletions src/pages/GenreExplore.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
import { useParams } from "react-router-dom";
import {
LoaderFunctionArgs,
useLoaderData,
// useParams
} from "react-router-dom";
import { COMMON_TITLES } from "src/constant";
import GridPage from "src/components/GridPage";
import { MEDIA_TYPE } from "src/types/Common";
import { CustomGenre, Genre } from "src/types/Genre";
import { useGetGenresQuery } from "src/store/slices/genre";
import {
genreSliceEndpoints,
// useGetGenresQuery
} from "src/store/slices/genre";
import store from "src/store";

export function Component() {
const { genreId } = useParams();
const { data: genres } = useGetGenresQuery(MEDIA_TYPE.Movie);
let genre: Genre | CustomGenre | undefined;
if (isNaN(parseInt(genreId as string))) {
genre = COMMON_TITLES.find((t) => t.apiString === genreId);
} else {
genre = genres?.find((t) => t.id.toString() === genreId);
export async function loader({ params }: LoaderFunctionArgs) {
let genre: CustomGenre | Genre | undefined = COMMON_TITLES.find(
(t) => t.apiString === (params.genreId as string)
);
if (!genre) {
const genres = await store
.dispatch(genreSliceEndpoints.getGenres.initiate(MEDIA_TYPE.Movie))
.unwrap();
genre = genres?.find((t) => t.id.toString() === (params.genreId as string));
}

return genre;
}

export function Component() {
const genre: CustomGenre | Genre | undefined = useLoaderData() as
| CustomGenre
| Genre
| undefined;
// const { genreId } = useParams();
// const { data: genres } = useGetGenresQuery(MEDIA_TYPE.Movie);
// let genre: Genre | CustomGenre | undefined;
// if (isNaN(parseInt(genreId!))) {
// genre = COMMON_TITLES.find((t) => t.apiString === genreId);
// } else {
// genre = genres?.find((t) => t.id.toString() === genreId);
// }
if (genre) {
return <GridPage mediaType={MEDIA_TYPE.Movie} genre={genre} />;
}
Expand Down
4 changes: 0 additions & 4 deletions src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ const router = createBrowserRouter([
{
path: ":genreId",
lazy: () => import("src/pages/GenreExplore"),
loader: async () => {
const { loader: mainLoader } = await import("src/pages/HomePage");
return mainLoader();
},
},
],
},
Expand Down

0 comments on commit c0e6e5e

Please sign in to comment.