Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"query-string": "^6.13.8",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-query": "^2.26.3",
"react-query-devtools": "^2.6.3",
"react-query": "^3.9.6",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"web-vitals": "^0.2.4"
Expand Down
17 changes: 10 additions & 7 deletions src/components/CardItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Text, Image, Box, Flex } from '@chakra-ui/react';
import { Link, useLocation } from 'react-router-dom';

function CardItem({ item, galleryArray }) {
function CardItem({ item, pagesArray }) {
const background = useLocation();
return (
<>
Expand All @@ -12,7 +12,7 @@ function CardItem({ item, galleryArray }) {
state: {
modal: true,
background,
galleryArray,
pagesArray,
item,
},
}}
Expand All @@ -22,21 +22,23 @@ function CardItem({ item, galleryArray }) {
borderRadius="lg"
background="#b62a07"
overflow="hidden"
h="400px"
padding="5"
padding="4"
maxW="sm"
height="100%"
direction="column"
justifyContent="space-between"
margin="auto"
boxShadow="1px 1px 4px 1px black"
>
<Image
src={item.image}
alt={item.title}
fit="cover"
p="10px"
borderRadius="20px"
mb="10px"
p="0px"
borderRadius="lg"
height="100%"
/>

<Box>
<Text
color="white"
Expand All @@ -47,6 +49,7 @@ function CardItem({ item, galleryArray }) {
p="10px"
borderTop="1px solid white"
borderBottom="1px solid white"
isTruncated
>
{item.title}
</Text>
Expand Down
17 changes: 0 additions & 17 deletions src/components/CardList.jsx

This file was deleted.

8 changes: 1 addition & 7 deletions src/components/ItemRecipe.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ function ItemRecipe() {
return response.json();
}
throw Error(`code ${response.status}`);
},
{
refetchOnWindowFocus: false,
refetchOnMount: false,
staleTime: 3600000,
cacheTime: 3600000,
}
);

Expand Down Expand Up @@ -89,7 +83,7 @@ function ItemRecipe() {
/>
) : (
<Text>
Read the detailed instructions on
Read the detailed instructions on{' '}
<Link href={`${data.sourceUrl}`}>{`${data.sourceName}`}</Link>
</Text>
)}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/components/NutritionalTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function NutritionalTable({ data }) {
<Tr key={nutrient.name}>
<Td>{nutrient.name}</Td>
<Td isNumeric>{`${nutrient.amount} ${nutrient.unit}`}</Td>
<Td isNumeric>{nutrient.percentOfDailyNeeds}</Td>
<Td isNumeric>{Math.round(nutrient.percentOfDailyNeeds)}</Td>
</Tr>
))}
</Tbody>
Expand Down
22 changes: 12 additions & 10 deletions src/components/SearchFieldCollapse.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
useDisclosure,
Collapse,
Button,
HStack,
ButtonGroup,
} from '@chakra-ui/react';
import { SearchIcon, ChevronDownIcon, ChevronUpIcon } from '@chakra-ui/icons';
import { useHistory } from 'react-router-dom';
Expand Down Expand Up @@ -65,7 +65,8 @@ function SearchFieldCollapse({ userQueryData }) {
});
};

const SearchQuery = () => {
const SearchQuery = (e) => {
e.preventDefault();
const query = queryString.stringify(userQuery, {
arrayFormat: 'comma',
skipEmptyString: true,
Expand All @@ -75,11 +76,11 @@ function SearchFieldCollapse({ userQueryData }) {
};

return (
<Box>
<Box>
<Box as="form">
<Box m="1rem">
<InputGroup size="md">
<Input
m="1rem"
m="0rem"
pr="4.5rem"
fontWeight="bold"
color="#b62a07"
Expand All @@ -93,20 +94,21 @@ function SearchFieldCollapse({ userQueryData }) {
onChange={handleStringChange}
/>

<InputRightElement m="1rem 2rem 0 0" width="">
<HStack>
<InputRightElement w="" p="0 1%">
<ButtonGroup isAttached>
<IconButton
aria-label="Search"
icon={<SearchIcon />}
onClick={() => SearchQuery()}
onClick={(e) => SearchQuery(e)}
size="sm"
mr="1rem"
bg="#b62a07"
mr="1px"
color="white"
_hover={{
bg: '#6e1a05',
}}
_focus={{ border: 'none' }}
type="submit"
/>
<Button
size="sm"
Expand All @@ -120,7 +122,7 @@ function SearchFieldCollapse({ userQueryData }) {
>
{isOpen ? <ChevronUpIcon /> : <ChevronDownIcon />}
</Button>
</HStack>
</ButtonGroup>
</InputRightElement>
</InputGroup>
</Box>
Expand Down
92 changes: 76 additions & 16 deletions src/components/SearchResults.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,60 @@
import React from 'react';
import { useQuery } from 'react-query';
import { Text, Flex, Box, Spinner } from '@chakra-ui/react';
import React, { useCallback, useState } from 'react';
import {
Box,
SimpleGrid,
Button,
Text,
Flex,
Spinner,
Center,
} from '@chakra-ui/react';
import { useInfiniteQuery } from 'react-query';

import CardList from './CardList';
import CardItem from './CardItem';
import useIntersectionObserver from '../hooks/useIntersectionObsever';

function SearchResults({ query }) {
const queryURL = `${process.env.REACT_APP_SEARCH_URL + query}&apiKey=${
process.env.REACT_APP_KEY
}`;
const itemsPerQuery = 50;
const queryURL = (offset) =>
`${process.env.REACT_APP_SEARCH_URL + query}&apiKey=${
process.env.REACT_APP_KEY
}&number=${itemsPerQuery}&offset=${offset}`;

const { isLoading, error, data } = useQuery(
['foodData', queryURL],
async () => {
const response = await fetch(queryURL);
const getParams = (lastPage) =>
lastPage.totalResults - lastPage.offset > itemsPerQuery
? lastPage.offset + itemsPerQuery
: false;

const {
isLoading,
error,
data,
isFetchingNextPage,
fetchNextPage,
hasNextPage,
} = useInfiniteQuery(
['foodData', queryURL(0)],
async ({ pageParam = 0 }) => {
const response = await fetch(queryURL(pageParam));
if (response.ok) {
return response.json();
}
throw Error(`code ${response.status}`);
},
{
refetchOnWindowFocus: false,
refetchOnMount: false,
staleTime: 3600000,
cacheTime: 3600000,
getNextPageParam: (lastPage) => getParams(lastPage),
}
);

// could have used useRef, but the ref won't get updated with the DOM element until a rerender
const [buttonRef, setButtonRef] = useState();

useIntersectionObserver({
target: buttonRef,
onIntersect: useCallback(fetchNextPage, [getParams, fetchNextPage]),
enabled: hasNextPage,
});

if (isLoading)
return (
<Flex alignItems="center" justifyContent="center" flexDirection="column">
Expand Down Expand Up @@ -62,7 +91,38 @@ function SearchResults({ query }) {
}
return (
<Box>
<CardList items={data.results} />
<SimpleGrid minChildWidth="280px" spacing="5" alignContent="center">
{data &&
data.pages.map((page) =>
page.results.map((item) => {
return (
<CardItem pagesArray={data.pages} item={item} key={item.id} />
);
})
)}
</SimpleGrid>

<Center p="5">
<Button
ref={(node) => setButtonRef(node)}
onClick={() => fetchNextPage()}
isLoading={isFetchingNextPage}
loadingText="Loading more..."
bg="#b62a07"
color="white"
_hover={{
bg: '#6e1a05',
boxShadow: '1px 1px 10px black',
transitionDuration: '0.5s',
}}
>
{hasNextPage ? (
<Text>Load Newer</Text>
) : (
<Text>Nothing more to Load</Text>
)}
</Button>
</Center>
</Box>
);
}
Expand Down
8 changes: 5 additions & 3 deletions src/components/ViewRecipe.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ function ViewRecipe() {
useEffect(onOpen, [onOpen]);

const { state = {} } = location;
const { modal, background = {}, galleryArray = [], item = {} } = state;
const { modal, background = {}, pagesArray = [], item = {} } = state;

const { pathname, search } = background;

const galleryArray = pagesArray.map((page) => page.results).flat();

const currentItemIndex = galleryArray.indexOf(item);
const maxIndex = galleryArray.length - 1;

Expand Down Expand Up @@ -75,7 +77,7 @@ function ViewRecipe() {
state: {
modal,
background,
galleryArray,
pagesArray,
item: prevItem,
},
}}
Expand All @@ -100,7 +102,7 @@ function ViewRecipe() {
state: {
modal,
background,
galleryArray,
pagesArray,
item: nextItem,
},
}}
Expand Down
Loading