import { gql, useMutation, useQuery } from '@apollo/client'; import { BookChapterCard } from '@app/client/components/BookChapterCard'; import { BookOverviewTabs } from '@app/client/components/BookOverviewTabs'; import { PaginatedListSkeleton } from '@app/client/components/PaginatedListSkeleton'; import { Pagination } from '@app/client/components/Pagination'; import { RatingPanel } from '@app/client/components/RatingPanel'; import { Button } from '@app/client/components/ui/button'; import { bookChapterFragment } from '@app/client/fragments'; import { useIdentity } from '@app/client/hooks/identity'; import { usePaginatedQuery } from '@app/client/hooks/usePaginatedQuery'; import { Box, Skeleton, Stack, Text } from '@chakra-ui/react'; import { BookChapterListQuery, BookOverviewRatingQuery } from '@generated/types'; import { createFileRoute, Link } from '@tanstack/react-router'; import { useState } from 'react'; export const Route = createFileRoute('/work/book/$bookId/overview/contents')({ component: RouteComponent }); function RouteComponent() { const { bookId: id } = Route.useParams(); const identity = useIdentity(); const { data } = useQuery(bookRatingQuery, { variables: { id } }); const [ratingUpdate] = useMutation(bookRatingMutation, { refetchQueries: [bookRatingQuery] }); return ( <> {data != null ? ( <> {identity != null && identity?.data.id !== data.bookOne.author.id && data.bookOne.chaptersTotal > 0 && ( { await ratingUpdate({ variables: { input: { bookId: data.bookOne.id, ratingType } } }); }} /> )} ) : ( )} ); } function BookChapterList({ id, isAuthor }: { readonly id: string; readonly isAuthor: boolean }) { const [chaptersListPage, setChaptersListPage] = useState(1); const [chaptersListPageSize, setChaptersListPageSize] = useState(10); const startIndex = (chaptersListPage - 1) * chaptersListPageSize; const { data } = usePaginatedQuery({ query: bookChapterListQuery, input: { bookId: id, filter: { offset: startIndex, size: chaptersListPageSize } } }); return ( {data != null ? ( <> {data.bookChapterMany.items.length > 0 ? ( <> {data.bookChapterMany.items.map(item => ( ))} ) : ( No chapters here yet! {isAuthor && ( )} )} {data.bookChapterMany.total > chaptersListPageSize && ( )} <> {isAuthor && data.bookChapterMany.items.length !== 0 && ( )} ) : ( )} ); } export const bookChapterListQuery = gql` query BookChapterList($input: BookChapterManyInput!) { bookChapterMany(input: $input) { items { ...BookChapter } total } } ${bookChapterFragment} `; const bookRatingQuery = gql` query BookOverviewRating($id: ID!) { bookOne(id: $id) { id ratingTotal personalRating chaptersTotal author { id } } } `; const bookRatingMutation = gql` mutation BookOverviewRatingUpdate($input: RatingSetInput!) { ratingSet(input: $input) } `;