Skip to content

Commit

Permalink
feature: add general comments
Browse files Browse the repository at this point in the history
  • Loading branch information
olexh committed May 13, 2024
1 parent 4d22412 commit 2a7cd8c
Show file tree
Hide file tree
Showing 19 changed files with 438 additions and 46 deletions.
12 changes: 10 additions & 2 deletions app/(pages)/(content)/anime/[slug]/components/actions/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import { FC } from 'react';
import { useParams } from 'next/navigation';

import WatchListButton from '@/components/watchlist-button/watchlist-button';
import useAnimeInfo from '@/services/hooks/anime/useAnimeInfo';
import useSession from '@/services/hooks/auth/useSession';

import CommentsButton from './components/comments-button';
import CommentsButton from '../../../../../../../components/comments-button';
import WatchStats from './components/watch-stats';


const Actions: FC = () => {
const params = useParams();
const { user } = useSession();
const { data: anime } = useAnimeInfo({ slug: String(params.slug) });

return (
<div className="flex flex-col gap-12">
Expand All @@ -24,7 +26,13 @@ const Actions: FC = () => {
slug={String(params.slug)}
/>
<WatchStats />
<CommentsButton />
{anime && (
<CommentsButton
comments_count={anime.comments_count}
slug={anime?.slug}
content_type="anime"
/>
)}
</div>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion app/(pages)/(content)/anime/[slug]/components/title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const Title = () => {
/>
)}
</div>
<P>{data.title_ja}</P>
<P className="text-sm text-muted-foreground">{data.title_ja}</P>
</div>
<div className="flex flex-col gap-2">
{data.score > 0 && (
Expand Down
4 changes: 2 additions & 2 deletions app/(pages)/(root)/components/comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const Comments: FC<Props> = ({ className }) => {
image={item.author.avatar}
imageRatio={1}
description={item.text}
descriptionHref={`${CONTENT_TYPE_LINKS[item.content_type]}/${item.slug}`}
descriptionHref={`/comments/${item.content_type}/${item.slug}`}
key={item.created}
title={item.author.username}
href={`/u/${item.author.username}`}
Expand All @@ -36,7 +36,7 @@ const Comments: FC<Props> = ({ className }) => {
<ContentCard
className="w-10"
poster={item.image}
href={`${CONTENT_TYPE_LINKS[item.content_type]}/${item.slug}`}
href={`/comments/${item.content_type}/${item.slug}`}
/>
</HorizontalCard>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React from 'react';
import Link from 'next/link';
import { useParams } from 'next/navigation';

import CommentsButton from '@/components/comments-button';
import FavoriteButton from '@/components/favorite-button';
import { Badge } from '@/components/ui/badge';
import Block from '@/components/ui/block';
Expand All @@ -21,6 +22,7 @@ import CollectionAuthor from './components/collection-author';
import CollectionDeleteModal from './components/collection-delete-modal';
import CollectionVote from './components/collection-vote';


const CollectionInfo = () => {
const params = useParams();
const { nsfw, spoiler, tags } = useCollectionContext();
Expand All @@ -41,9 +43,9 @@ const CollectionInfo = () => {
}

return (
<Block>
<Block className="h-full">
<Header title="Деталі" />
<div className="flex w-full flex-col gap-4">
<div className="flex h-full w-full flex-col gap-4">
<Card className="w-full gap-6">
<CollectionAuthor />
{tags.length > 0 && (
Expand Down Expand Up @@ -89,14 +91,21 @@ const CollectionInfo = () => {
)}
</Card>

<div className="flex w-full items-center gap-2">
<CollectionVote />
<FavoriteButton
disabled={!loggedUser}
<div className="flex w-full flex-col gap-4 lg:sticky lg:top-20 lg:self-start">
<div className="flex w-full items-center gap-2">
<CollectionVote />
<FavoriteButton
disabled={!loggedUser}
slug={collection.reference}
content_type="collection"
size="icon"
variant="secondary"
/>
</div>
<CommentsButton
comments_count={collection.comments_count}
slug={collection.reference}
content_type="collection"
size="icon"
variant="secondary"
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const CollectionTitle = () => {

return (
<div className="flex flex-col gap-2">
<Header title={title || 'Нова колекція'} />
<Header variant="h2" title={title || 'Нова колекція'} />
{description && (
<TextExpand>
<MDViewer className="text-sm text-muted-foreground">
Expand Down
6 changes: 1 addition & 5 deletions app/(pages)/collections/[reference]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,8 @@ const CollectionPage = async ({
</div>
<CollectionGroups />
</Block>
<Comments
slug={reference}
content_type="collection"
/>
</div>
<div className="order-1 hidden w-full lg:order-2 lg:block">
<div className="order-1 hidden w-full lg:order-2 lg:flex lg:flex-col lg:h-full">
<CollectionInfo />
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use client';

import { FC } from 'react';



import useContent from '@/app/(pages)/comments/[content_type]/[slug]/[[...comment_reference]]/components/useContent';
import P from '@/components/typography/p';
import Header from '@/components/ui/header';
import { CONTENT_TYPE_LINKS, CONTENT_TYPES } from '@/utils/constants';


interface Props {
slug: string;
content_type: API.ContentType;
}

const ContentHeader: FC<Props> = ({ slug, content_type }) => {
const { data } = useContent({
content_type,
slug
})

const link = `${CONTENT_TYPE_LINKS[content_type]}/${slug}`;


return (
<div>
<Header href={link} title={data?.title} variant="h2" />
<P className="text-sm text-muted-foreground">{CONTENT_TYPES[content_type].title_ua}</P>
</div>
)
}

export default ContentHeader;
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use client';

import * as React from 'react';
import { FC } from 'react';

import Link from 'next/link';

import ContentCard from '@/components/content-card/content-card';
import Block from '@/components/ui/block';
import Header from '@/components/ui/header';
import { CONTENT_TYPE_LINKS } from '@/utils/constants';

import useContent from './useContent';


interface Props {
slug: string;
content_type: API.ContentType;
}

const Content: FC<Props> = ({ slug, content_type }) => {
const { data } = useContent({ content_type, slug });

const link = `${CONTENT_TYPE_LINKS[content_type]}/${slug}`;

return (
<Block>
<div className="hidden w-full items-center gap-4 px-16 md:px-48 lg:flex lg:px-0">
<ContentCard href={link} poster={data?.image} />
</div>
<div className="flex w-full gap-4 lg:hidden">
<div className="w-12">
<ContentCard href={link} poster={data?.image} />
</div>
<Link href={link}>{data?.title}</Link>
</div>
</Block>
);
};

export default Content;
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { useQuery } from '@tanstack/react-query';

import getAnimeInfo from '@/services/api/anime/getAnimeInfo';
import getCharacterInfo from '@/services/api/characters/getCharacterInfo';
import getCollection from '@/services/api/collections/getCollection';
import getEdit from '@/services/api/edit/getEdit';
import getPersonInfo from '@/services/api/people/getPersonInfo';
import { useSettingsContext } from '@/services/providers/settings-provider';

interface Props {
content_type: API.ContentType;
slug: string;
}

interface Response {
title: string;
image: string;
content_type: API.ContentType;
}

export const getContent = ({ content_type, slug }: Props) => {
switch (content_type) {
case 'anime':
return getAnimeInfo({ params: { slug } });
case 'character':
return getCharacterInfo({ params: { slug } });
case 'person':
return getPersonInfo({ params: { slug } });
case 'collection':
return getCollection({ params: { reference: slug } });
case 'edit':
return getEdit({ params: { edit_id: Number(slug) } });
default:
return getAnimeInfo({ params: { slug } });
}
};

const useContent = ({ slug, content_type }: Props) => {
const { titleLanguage } = useSettingsContext();

const query = useQuery({
queryKey: ['content', content_type, slug],
queryFn: async () => getContent({ content_type, slug }),
select: (data) => {
let content: Response | undefined;

if ('data_type' in data) {
if (data.data_type === 'anime') {
content = {
title:
data[titleLanguage!] ||
data.title_ua ||
data.title_en ||
data.title_ja,
image: data.poster,
content_type,
};
}

if (data.data_type === 'character') {
content = {
title: data.name_ua || data.name_en || data.name_ja,
image: data.image,
content_type,
};
}

if (data.data_type === 'person') {
content = {
title: data.name_ua || data.name_en || data.name_native,
image: data.image,
content_type,
};
}

if (data.data_type === 'collection') {
content = {
title: data.title,
image:
data.collection[0].content.data_type === 'anime'
? data.collection[0].content.poster
: data.collection[0].content.image,
content_type,
};
}
} else {
content = {
title: `Правка #${data.edit_id}`,
image:
data.content.data_type === 'anime'
? data.content.poster
: data.content.image,
content_type,
};
}

return content;
},
});

return query;
};

export default useContent;
Loading

0 comments on commit 2a7cd8c

Please sign in to comment.