Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small fixes #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 19 additions & 5 deletions src/pages/search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,20 @@ import {

const DEFAULT_PRODUCTS_PER_PAGE = 24

function isEmpty(obj) {
return Object.keys(obj).length === 0;
}

export async function getServerData({ query, ...rest }) {
if(isEmpty(query)) {
return {
props: {
query: { q: "" },
products: [],
}
};
}

const { getSearchResults } = require("../utils/search")
const products = await getSearchResults({
query,
Expand Down Expand Up @@ -75,7 +88,7 @@ function SearchPage({
location,
}) {
// These default values come from the page query string
const queryParams = getValuesFromQuery(location.search || serverData.query)
const queryParams = getValuesFromQuery(location.search || serverData?.query || "");

const [filters, setFilters] = React.useState(queryParams)
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -105,7 +118,7 @@ function SearchPage({
sortKey,
false,
DEFAULT_PRODUCTS_PER_PAGE,
serverData.products,
serverData && serverData.products.length ? serverData.products : [],
initialFilters
)

Expand Down Expand Up @@ -146,9 +159,10 @@ function SearchPage({
}
}, [location.hash, hasNextPage, fetchNextPage])

const currencyCode = getCurrencySymbol(
serverData.products?.[0]?.node?.priceRangeV2?.minVariantPrice?.currencyCode
)
const currencySymbol = serverData && serverData.products.length
? serverData.products?.[0]?.node?.priceRangeV2?.minVariantPrice?.currencyCode
: "GBP"; // set in .env?
const currencyCode = getCurrencySymbol(currencySymbol);

return (
<Layout>
Expand Down