import { gql, useQuery } from '@apollo/client'; import { BookChapterCard } from '@app/client/components/BookChapterCard'; import { PaginatedListSkeleton } from '@app/client/components/PaginatedListSkeleton'; import { Pagination } from '@app/client/components/Pagination'; import { Button } from '@app/client/components/ui/button'; import { WorkingProgressStatusBadge } from '@app/client/components/WorkingProgressStatusBadge'; import { WorkingRatingBadge } from '@app/client/components/WorkingRatingBadge'; import { WorkingReviewsBadge } from '@app/client/components/WorkingReviewsTotalBadge'; import { bookChapterFragment, bookFragment } from '@app/client/fragments'; import { toFormattedDate } from '@app/client/helpers'; import { IdentityContext, useIdentity } from '@app/client/hooks/identity'; import { usePaginatedQuery } from '@app/client/hooks/usePaginatedQuery'; import { BookOne } from '@app/client/interfaces'; import { Box, Heading, HStack, Icon, Skeleton, SkeletonText, Stack, Text } from '@chakra-ui/react'; import { BookChapterListQuery, BookOverviewQuery } from '@generated/types'; import { createFileRoute, Link } from '@tanstack/react-router'; import { useState } from 'react'; export const Route = createFileRoute('/work/book/$bookId/overview')({ component: RouteComponent }); function RouteComponent() { const { bookId: id } = Route.useParams(); const { data: bookData } = useQuery(bookQuery, { variables: { id } }); const identity = useIdentity(); return ( {bookData != null ? ( ) : ( )} ); } function BookOverview({ identity, book }: { readonly identity: IdentityContext | null; readonly book: BookOne; }) { return ( {book.title} by: {book.author.username} created at: {toFormattedDate({ value: book.createdAt })} last updated: {toFormattedDate({ value: book.updatedAt })} chapters: {book.chaptersTotal} words: {book.wordsTotal} {book.description} ); } function BookChapterList({ id, identity, isAuthor }: { readonly id: string; readonly identity: IdentityContext | null; 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 && ( )} {identity != null && !isAuthor && ( <> )} ) : ( )} ); } function BookOverviewSkeleton() { return ( ); } const bookQuery = gql` query BookOverview($id: ID!) { bookOne(id: $id) { ...Book } } ${bookFragment} `; export const bookChapterListQuery = gql` query BookChapterList($input: BookChapterManyInput!) { bookChapterMany(input: $input) { items { ...BookChapter } total } } ${bookChapterFragment} `;