Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
haninekkoub committed Oct 13, 2024
1 parent 6fbaf41 commit 7ac7213
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
57 changes: 30 additions & 27 deletions storefront/app/(website)/faqs/_parts/faq-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {FAQS_PAGE_QUERYResult, FaqEntry} from "@/types/sanity.generated";

import Body from "@/components/shared/typography/body";
import Heading from "@/components/shared/typography/heading";
import {useEffect, useRef, useState} from "react";
import {useCallback, useEffect, useRef, useState} from "react";

import FaqContent from "./faq-content";
import SearchBar from "./search-bar";
Expand All @@ -31,33 +31,36 @@ export default function Faq({
initialCategory ?? "",
);

const onSearch = (query: string) => {
const search = query?.toLowerCase().trim();
queryRef.current = search;
if (search === "" || !data.category) {
setSearchResults([]);
return;
}
const onSearch = useCallback(
(query: string) => {
const search = query?.toLowerCase().trim();
queryRef.current = search;
if (search === "" || !data.category) {
setSearchResults([]);
return;
}

const uniqueQuestions = new Set<string>();
const entries = categories.flatMap(
(category) =>
category.questions?.map((entry) => ({
...entry,
categorySlug: category.slug?.current,
})) ?? [],
);

const uniqueQuestions = new Set<string>();
const entries = categories.flatMap(
(category) =>
category.questions?.map((entry) => ({
...entry,
categorySlug: category.slug?.current,
})) ?? [],
);

const results = entries.filter((entry) => {
if (!entry?.question) return false;
const question = entry.question.toLowerCase().trim();
if (uniqueQuestions.has(question)) return false;
uniqueQuestions.add(question);
return question.includes(search);
}) as FaqEntryWithCategory[];

setSearchResults(results);
};
const results = entries.filter((entry) => {
if (!entry?.question) return false;
const question = entry.question.toLowerCase().trim();
if (uniqueQuestions.has(question)) return false;
uniqueQuestions.add(question);
return question.includes(search);
}) as FaqEntryWithCategory[];

setSearchResults(results);
},
[categories, data.category, setSearchResults],
);

const scrollToQuestion = (id: string) => {
const top = document.getElementById(id)?.getBoundingClientRect()?.top;
Expand Down
2 changes: 1 addition & 1 deletion storefront/app/(website)/faqs/_parts/search-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function SearchBar({
return () => {
clearTimeout(handler);
};
}, [query]);
}, [query, onSearch]);

const handleSearch = (searchQuery: string) => {
onChange && onChange();
Expand Down
1 change: 1 addition & 0 deletions storefront/components/sections/collection-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function CollectionList(
));
return (
<EmblaCarousel
disableDesktopDrag
showButtons={false}
showProgress={true}
slides={slides}
Expand Down
3 changes: 3 additions & 0 deletions storefront/components/shared/carousel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type PropType = {
href: string | undefined;
text: string | undefined;
};
disableDesktopDrag?: boolean;
options?: EmblaOptionsType;
showButtons?: boolean;
showProgress?: boolean;
Expand All @@ -24,6 +25,7 @@ type PropType = {
export default function EmblaCarousel(props: PropType) {
const {
cta,
disableDesktopDrag = false,
options,
showButtons = true,
showProgress = false,
Expand All @@ -32,6 +34,7 @@ export default function EmblaCarousel(props: PropType) {
} = props;
const [emblaRef, emblaApi] = useEmblaCarousel({
...options,
breakpoints: {"(min-width: 1024px)": {watchDrag: !disableDesktopDrag}},
containScroll: "trimSnaps",
dragFree: true,
});
Expand Down

0 comments on commit 7ac7213

Please sign in to comment.