Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit b28ed55

Browse files
feat: added search and improved loading structure
1 parent e219d04 commit b28ed55

File tree

2 files changed

+99
-52
lines changed

2 files changed

+99
-52
lines changed

src/renderer/graphql/queries.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -437,15 +437,8 @@ export const GET_RECIPE = gql`
437437
}
438438
`;
439439

440-
export const GET_COOKBOOK_RECIPES = gql`
441-
query getCookbookRecipes(
442-
$cookbookId: Long!
443-
$howmany: Long!
444-
$skip: Long!
445-
$name: String
446-
$orderBy: AssistantRecipeQueryOrderBy
447-
$desc: Boolean
448-
) {
440+
export const GET_COOKBOOK_INFO = gql`
441+
query getCookbookRecipes($cookbookId: Long!) {
449442
cookbook: assistantCookbook(id: $cookbookId) {
450443
id
451444
name
@@ -466,6 +459,20 @@ export const GET_COOKBOOK_RECIPES = gql`
466459
upvotes
467460
downvotes
468461
languages
462+
}
463+
}
464+
`;
465+
466+
export const GET_COOKBOOK_RECIPES = gql`
467+
query getCookbookRecipes(
468+
$cookbookId: Long!
469+
$howmany: Long!
470+
$skip: Long!
471+
$name: String
472+
$orderBy: AssistantRecipeQueryOrderBy
473+
$desc: Boolean
474+
) {
475+
cookbook: assistantCookbook(id: $cookbookId) {
469476
recipes(
470477
howmany: $howmany
471478
skip: $skip

src/renderer/pages/ViewCookbookSnippets.tsx

Lines changed: 83 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { useState } from 'react';
12
import { useQuery } from '@apollo/client';
23
import { useParams } from 'react-router-dom';
3-
import { Box, Flex, HStack, Link, Text } from '@chakra-ui/react';
4+
import { Box, Flex, HStack, Input, Link, Text } from '@chakra-ui/react';
45

56
import { getCookbookUrl } from '../utils/urlUtils';
6-
import { GET_COOKBOOK_RECIPES } from '../graphql/queries';
7+
import { GET_COOKBOOK_INFO, GET_COOKBOOK_RECIPES } from '../graphql/queries';
78
import { GET_USER_RECIPES_VARIABLES } from '../graphql/variables';
9+
import { PAGE_QUERY_POLL_INTERVAL_IN_MS } from '../lib/constants';
810
import ViewCookbookSnippetsError from '../components/ViewCookbookSnippets/ViewCookbookSnippetsError';
911
import ViewCookbookSnippetsLoading from '../components/ViewCookbookSnippets/ViewCookbookSnippetsLoading';
1012
import ViewCookbookSnippetsEmpty from '../components/ViewCookbookSnippets/ViewCookbookSnippetsEmpty';
@@ -14,72 +16,110 @@ import AvatarAndName from '../components/AvatarAndName';
1416
import PrivacyAndVotes from '../components/PrivacyAndVotes';
1517
import FormattedDate from '../components/FormattedDate';
1618
import SnippetResults from '../components/SnippetResults/SnippetResults';
19+
import SnippetResultsLoading from '../components/SnippetResults/SnippetResultsLoading';
1720

1821
export default function ViewCookbookSnippets() {
1922
const params = useParams();
23+
const [searchTerm, setSearchTerm] = useState('');
2024

21-
const { data, loading, error } = useQuery(GET_COOKBOOK_RECIPES, {
25+
const {
26+
data: cookbookInfoData,
27+
loading: cookbookInfoLoading,
28+
error: cookbookInfoError,
29+
} = useQuery(GET_COOKBOOK_INFO, {
30+
pollInterval: PAGE_QUERY_POLL_INTERVAL_IN_MS,
31+
variables: {
32+
cookbookId: Number(params.cookbookId),
33+
},
34+
});
35+
36+
const {
37+
data: cookbookSnippetData,
38+
loading: cookbookSnippetLoading,
39+
error: cookbookSnippetError,
40+
} = useQuery(GET_COOKBOOK_RECIPES, {
41+
pollInterval: PAGE_QUERY_POLL_INTERVAL_IN_MS,
2242
variables: {
2343
cookbookId: Number(params.cookbookId),
2444
...GET_USER_RECIPES_VARIABLES,
45+
name: searchTerm || null,
46+
},
47+
context: {
48+
debounceKey: 'view-cookbook-snippets',
2549
},
2650
});
2751

28-
const cookbook = data?.cookbook;
52+
const cookbook = cookbookInfoData?.cookbook;
53+
const snippets = cookbookSnippetData?.cookbook?.recipes || [];
2954

30-
if (loading) {
31-
return <ViewCookbookSnippetsLoading />;
32-
}
33-
34-
if (error || !cookbook) {
55+
if (cookbookInfoError || cookbook === null || cookbookSnippetError) {
3556
return <ViewCookbookSnippetsError />;
3657
}
3758

3859
return (
3960
<Box h="full">
4061
{/* INFO SECTION */}
41-
<HStack
42-
alignItems="center"
43-
bg="neutral.25"
44-
_dark={{ bg: 'base.dark' }}
45-
h="74px"
46-
w="full"
47-
spacing="space_16"
48-
>
49-
<BackButton />
62+
{cookbookInfoLoading ? (
63+
<ViewCookbookSnippetsLoading />
64+
) : (
65+
<HStack
66+
alignItems="center"
67+
bg="neutral.25"
68+
_dark={{ bg: 'base.dark' }}
69+
h="74px"
70+
w="full"
71+
spacing="space_16"
72+
>
73+
<BackButton />
5074

51-
<Flex alignItems="center" gridGap="space_8">
52-
<Text size="sm" fontWeight="bold" noOfLines={1}>
53-
<Link
54-
isExternal
55-
variant="subtle"
75+
<Flex alignItems="center" gridGap="space_8">
76+
<Text size="sm" fontWeight="bold" noOfLines={1}>
77+
<Link
78+
isExternal
79+
variant="subtle"
5680
_focus={{ boxShadow: 'none' }}
57-
href={getCookbookUrl(cookbook.id)}
58-
>
59-
{cookbook.name}
60-
</Link>
61-
</Text>
62-
<FavoriteCookbook
63-
isSubscribed={cookbook.isSubscribed}
64-
cookbookId={cookbook.id}
65-
/>
66-
</Flex>
81+
href={getCookbookUrl(cookbook.id)}
82+
>
83+
{cookbook.name}
84+
</Link>
85+
</Text>
86+
<FavoriteCookbook
87+
isSubscribed={cookbook.isSubscribed}
88+
cookbookId={cookbook.id}
89+
/>
90+
</Flex>
6791

68-
<AvatarAndName owner={cookbook.owner} />
92+
<AvatarAndName owner={cookbook.owner} />
6993

70-
<PrivacyAndVotes
71-
isPublic={cookbook.isPublic}
72-
upvotes={cookbook.upvotes}
73-
downvotes={cookbook.downvotes}
74-
/>
94+
<PrivacyAndVotes
95+
isPublic={cookbook.isPublic}
96+
upvotes={cookbook.upvotes}
97+
downvotes={cookbook.downvotes}
98+
/>
99+
100+
<FormattedDate timestamp={cookbook.creationTimestampMs} />
75101

76-
<FormattedDate timestamp={cookbook.creationTimestampMs} />
77-
</HStack>
102+
<Flex w="full" flex={1} justifyContent="flex-end">
103+
<Input
104+
minWidth="200px"
105+
maxWidth="500px"
106+
justifySelf="flex-end"
107+
mr="space_16"
108+
placeholder="Search on this cookbook"
109+
value={searchTerm}
110+
onChange={(e) => setSearchTerm(e.target.value)}
111+
/>
112+
</Flex>
113+
</HStack>
114+
)}
78115

79-
{!cookbook.recipes || cookbook.recipes.length === 0 ? (
116+
{/* eslint-disable-next-line no-nested-ternary */}
117+
{cookbookSnippetLoading ? (
118+
<SnippetResultsLoading />
119+
) : snippets.length === 0 ? (
80120
<ViewCookbookSnippetsEmpty />
81121
) : (
82-
<SnippetResults results={cookbook.recipes} />
122+
<SnippetResults results={snippets} />
83123
)}
84124
</Box>
85125
);

0 commit comments

Comments
 (0)