Skip to content

Commit

Permalink
add promise to anime layout server requests
Browse files Browse the repository at this point in the history
  • Loading branch information
olexh committed Mar 27, 2024
1 parent d8876d4 commit a7d55f6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 67 deletions.
92 changes: 50 additions & 42 deletions app/(pages)/anime/[slug]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { PropsWithChildren } from 'react';
import IconamoonCommentFill from '~icons/iconamoon/comment-fill';

import Link from 'next/link';
import { redirect } from 'next/navigation';

import { dehydrate } from '@tanstack/query-core';
import { HydrationBoundary } from '@tanstack/react-query';
Expand All @@ -22,15 +23,14 @@ import getAnimeStaff from '@/services/api/anime/getAnimeStaff';
import getFavourite from '@/services/api/favourite/getFavourite';
import getWatch from '@/services/api/watch/getWatch';
import { ANIME_NAV_ROUTES, RELEASE_STATUS } from '@/utils/constants';
import _generateMetadata from '@/utils/generateMetadata';
import getDeclensionWord from '@/utils/getDeclensionWord';
import getQueryClient from '@/utils/getQueryClient';
import parseTextFromMarkDown from '@/utils/parseTextFromMarkDown';

import Actions from './_components/actions';
import Cover from './_components/cover';
import Title from './_components/title';
import parseTextFromMarkDown from '@/utils/parseTextFromMarkDown';
import _generateMetadata from '@/utils/generateMetadata';
import { redirect } from 'next/navigation';


interface Props extends PropsWithChildren {
Expand Down Expand Up @@ -62,7 +62,9 @@ export async function generateMetadata(
const title =
(anime.title_ua || anime.title_en || anime.title_ja) +
(startDate ? ` (${startDate})` : '');
let synopsis: string | undefined = await parseTextFromMarkDown(anime.synopsis_ua || anime.synopsis_en);
let synopsis: string | undefined = await parseTextFromMarkDown(
anime.synopsis_ua || anime.synopsis_en,
);

synopsis =
synopsis &&
Expand All @@ -78,7 +80,7 @@ export async function generateMetadata(
return _generateMetadata({
title: {
default: title,
template: title + ' / %s / Hikka'
template: title + ' / %s / Hikka',
},
description: synopsis,
images: `https://hikka.io/generate/preview/anime/${slug}/${anime.updated}`,
Expand All @@ -89,47 +91,53 @@ const Component = async ({ params: { slug }, children }: Props) => {
const queryClient = getQueryClient();
const auth = await getCookie('auth');

let anime: AnimeResponse | undefined = undefined;

try {
anime = await queryClient.fetchQuery({
queryKey: ['anime', slug],
queryFn: () => getAnimeInfo({ slug }),
});
} catch (e) {
return redirect('/');
}

await queryClient.prefetchInfiniteQuery({
queryKey: ['characters', slug],
queryFn: ({ pageParam = 1 }) =>
getAnimeCharacters({ slug, page: pageParam }),
initialPageParam: 1,
await queryClient.prefetchQuery({
queryKey: ['anime', slug],
queryFn: () => getAnimeInfo({ slug }),
});

await queryClient.prefetchInfiniteQuery({
queryKey: ['franchise', slug, { auth }],
queryFn: ({ pageParam = 1 }) =>
getAnimeFranchise({ slug, auth, page: pageParam }),
initialPageParam: 1,
});
const anime: API.AnimeInfo | undefined = queryClient.getQueryData([
'anime',
slug,
]);

await queryClient.prefetchInfiniteQuery({
queryKey: ['staff', slug],
queryFn: ({ pageParam = 1 }) => getAnimeStaff({ slug, page: pageParam }),
initialPageParam: 1,
});

await queryClient.prefetchQuery({
queryKey: ['watch', slug, { auth }],
queryFn: () => getWatch({ slug: slug, auth: String(auth) }),
});
if (!anime) {
return redirect('/');
}

await queryClient.prefetchQuery({
queryKey: ['favorite', slug, { auth, content_type: 'anime' }],
queryFn: () =>
getFavourite({ slug: String(slug), auth: String(auth), content_type: 'anime' }),
});
await Promise.all([
queryClient.prefetchInfiniteQuery({
queryKey: ['characters', slug],
queryFn: ({ pageParam = 1 }) =>
getAnimeCharacters({ slug, page: pageParam }),
initialPageParam: 1,
}),
queryClient.prefetchInfiniteQuery({
queryKey: ['franchise', slug, { auth }],
queryFn: ({ pageParam = 1 }) =>
getAnimeFranchise({ slug, auth, page: pageParam }),
initialPageParam: 1,
}),
queryClient.prefetchInfiniteQuery({
queryKey: ['staff', slug],
queryFn: ({ pageParam = 1 }) =>
getAnimeStaff({ slug, page: pageParam }),
initialPageParam: 1,
}),
queryClient.prefetchQuery({
queryKey: ['watch', slug, { auth }],
queryFn: () => getWatch({ slug: slug, auth: String(auth) }),
}),
queryClient.prefetchQuery({
queryKey: ['favorite', slug, { auth, content_type: 'anime' }],
queryFn: () =>
getFavourite({
slug: String(slug),
auth: String(auth),
content_type: 'anime',
}),
}),
]);

const dehydratedState = dehydrate(queryClient);

Expand Down
25 changes: 0 additions & 25 deletions services/api/collections/getUserCollections.ts

This file was deleted.

0 comments on commit a7d55f6

Please sign in to comment.