|
| 1 | +import { useQuery } from '@apollo/client'; |
| 2 | +import { useParams } from 'react-router-dom'; |
| 3 | +import { Box, Flex, HStack, Text } from '@chakra-ui/react'; |
| 4 | + |
| 5 | +import { GET_COOKBOOK_RECIPES } from '../graphql/queries'; |
| 6 | +import { GET_USER_RECIPES_VARIABLES } from '../graphql/variables'; |
| 7 | +import ViewCookbookSnippetsError from '../components/ViewCookbookSnippets/ViewCookbookSnippetsError'; |
| 8 | +import ViewCookbookSnippetsLoading from '../components/ViewCookbookSnippets/ViewCookbookSnippetsLoading'; |
| 9 | +import ViewCookbookSnippetsEmpty from '../components/ViewCookbookSnippets/ViewCookbookSnippetsEmpty'; |
| 10 | +import BackButton from '../components/BackButton'; |
| 11 | +import FavoriteCookbook from '../components/Favorite/FavoriteCookbook'; |
| 12 | +import AvatarAndName from '../components/AvatarAndName'; |
| 13 | +import PrivacyAndVotes from '../components/PrivacyAndVotes'; |
| 14 | +import FormattedDate from '../components/FormattedDate'; |
| 15 | +import SnippetResults from '../components/SnippetResults/SnippetResults'; |
| 16 | + |
| 17 | +export default function ViewCookbookSnippets() { |
| 18 | + const params = useParams(); |
| 19 | + |
| 20 | + const { data, loading, error } = useQuery(GET_COOKBOOK_RECIPES, { |
| 21 | + variables: { |
| 22 | + cookbookId: Number(params.cookbookId), |
| 23 | + ...GET_USER_RECIPES_VARIABLES, |
| 24 | + }, |
| 25 | + }); |
| 26 | + |
| 27 | + const cookbook = data?.cookbook; |
| 28 | + |
| 29 | + if (loading) { |
| 30 | + return <ViewCookbookSnippetsLoading />; |
| 31 | + } |
| 32 | + |
| 33 | + if (error || !cookbook) { |
| 34 | + return <ViewCookbookSnippetsError />; |
| 35 | + } |
| 36 | + |
| 37 | + return ( |
| 38 | + <Box h="full"> |
| 39 | + {/* INFO SECTION */} |
| 40 | + <HStack |
| 41 | + alignItems="center" |
| 42 | + bg="neutral.25" |
| 43 | + _dark={{ bg: 'base.dark' }} |
| 44 | + h="74px" |
| 45 | + w="full" |
| 46 | + spacing="space_16" |
| 47 | + > |
| 48 | + <BackButton /> |
| 49 | + |
| 50 | + <Flex alignItems="center" gridGap="space_8"> |
| 51 | + <Text size="sm" fontWeight="bold" noOfLines={1}> |
| 52 | + {cookbook.name} |
| 53 | + </Text> |
| 54 | + <FavoriteCookbook |
| 55 | + isSubscribed={cookbook.isSubscribed} |
| 56 | + cookbookId={cookbook.id} |
| 57 | + /> |
| 58 | + </Flex> |
| 59 | + |
| 60 | + <AvatarAndName owner={cookbook.owner} /> |
| 61 | + |
| 62 | + <PrivacyAndVotes |
| 63 | + isPublic={cookbook.isPublic} |
| 64 | + upvotes={cookbook.upvotes} |
| 65 | + downvotes={cookbook.downvotes} |
| 66 | + /> |
| 67 | + |
| 68 | + <FormattedDate timestamp={cookbook.creationTimestampMs} /> |
| 69 | + </HStack> |
| 70 | + |
| 71 | + {!cookbook.recipes || cookbook.recipes.length === 0 ? ( |
| 72 | + <ViewCookbookSnippetsEmpty /> |
| 73 | + ) : ( |
| 74 | + <SnippetResults results={cookbook.recipes} /> |
| 75 | + )} |
| 76 | + </Box> |
| 77 | + ); |
| 78 | +} |
0 commit comments