Skip to content

Commit

Permalink
Checkpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdyoung3 committed Apr 30, 2024
1 parent a94414a commit bc3c7f7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
4 changes: 2 additions & 2 deletions algolia-search/dist/islands/algolia-search.island.js

Large diffs are not rendered by default.

47 changes: 33 additions & 14 deletions algolia-search/src/search-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ const SearchForm = (props) => {
const windowSearchParams = new URLSearchParams(window.location.search)
const {query, refine} = useSearchBox(props);
const {items: pageTypeRefinements, refine: refineNewsType} = useRefinementList({attribute: "basic_page_type"});
const [chosenNewsTypes, setChosenNewsTypes] = useState<string[]>(windowSearchParams.get('news-types')?.split(',') || []);
const [chosenBasicPageTypes, setChosenNewsTypes] = useState<string[]>(windowSearchParams.get('basic-page-type')?.split(',') || []);
const {status} = useInstantSearch();
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (ref.current) return
ref.current = true;
chosenNewsTypes.map(newsType => refineNewsType(newsType));
}, [ref, chosenNewsTypes])
chosenBasicPageTypes.map(basicPageType => refineNewsType(basicPageType));
}, [ref, chosenBasicPageTypes])

return (
<form
Expand All @@ -31,14 +31,14 @@ const SearchForm = (props) => {
e.stopPropagation();
refine(inputRef.current?.value);

const addRefinements = chosenNewsTypes.filter(newsType => !pageTypeRefinements.find(item => item.value === newsType).isRefined)
const removeRefinements = pageTypeRefinements.filter(refinement => refinement.isRefined && !chosenNewsTypes.includes(refinement.value)).map(refinement => refinement.value);
const addRefinements = chosenBasicPageTypes.filter(basicPageType => !pageTypeRefinements.find(item => item.value === basicPageType).isRefined)
const removeRefinements = pageTypeRefinements.filter(refinement => refinement.isRefined && !chosenBasicPageTypes.includes(refinement.value)).map(refinement => refinement.value);

[...addRefinements, ...removeRefinements].map(newsType => refineNewsType(newsType))
[...addRefinements, ...removeRefinements].map(basicPageType => refineNewsType(basicPageType))

const searchParams = new URLSearchParams(window.location.search)
inputRef.current?.value.length > 0 ? searchParams.set('key', inputRef.current?.value): searchParams.delete('key');
chosenNewsTypes.length > 0 ? searchParams.set('news-types', chosenNewsTypes.join(',')) : searchParams.delete('news-types')
chosenBasicPageTypes.length > 0 ? searchParams.set('basic-page-type', chosenBasicPageTypes.join(',')) : searchParams.delete('basic-page-type')

window.history.replaceState(null, '', `?${searchParams.toString()}`)
}}
Expand All @@ -53,7 +53,7 @@ const SearchForm = (props) => {
setChosenNewsTypes([]);
const searchParams = new URLSearchParams(window.location.search)
searchParams.delete('key')
searchParams.delete('news-types');
searchParams.delete('basic-page-type');
window.history.replaceState(null, '', `?${searchParams.toString()}`)
}}
style={{marginBottom: "20px"}}
Expand Down Expand Up @@ -95,17 +95,36 @@ const SearchForm = (props) => {
}}>
<input
type="checkbox"
checked={chosenNewsTypes.findIndex(value => value === item.value) >= 0}
checked={chosenBasicPageTypes.findIndex(value => value === item.value) >= 0}
onChange={(e) => {
setChosenNewsTypes(prevTypes => {
const newTypes = [...prevTypes];
const pageTypes = [...prevTypes];
if (e.currentTarget.checked) {
newTypes.push(item.value);
pageTypes.push(item.value);
} else {
newTypes.splice(prevTypes.findIndex(value => value === item.value), 1)
pageTypes.splice(prevTypes.findIndex(value => value === item.value), 1)
}
return newTypes;
return pageTypes;
});
((e) => {
console.log(i);
console.log(item.value);
e.preventDefault();
e.stopPropagation();
refine(inputRef.current?.value);

const addRefinements = chosenBasicPageTypes.filter(basicPageType => !pageTypeRefinements.find(item => item.value === basicPageType).isRefined)
const removeRefinements = pageTypeRefinements.filter(refinement => refinement.isRefined && !chosenBasicPageTypes.includes(refinement.value)).map(refinement => refinement.value);

[...addRefinements, ...removeRefinements].map(basicPageType => refineNewsType(basicPageType))

const searchParams = new URLSearchParams(window.location.search)
inputRef.current?.value.length > 0 ? searchParams.set('key', inputRef.current?.value): searchParams.delete('key');
chosenBasicPageTypes.length > 0 ? searchParams.set('basic-page-type', chosenBasicPageTypes.join(',')) : searchParams.delete('basic-page-type')

window.history.replaceState(null, '', `?${searchParams.toString()}`)

})(e)
}}
style={{
border: 'black',
Expand All @@ -131,7 +150,7 @@ const SearchForm = (props) => {
</ul>
</fieldset>

<StatusMessage status={status} query={query}/>
Status: <StatusMessage status={status} query={query}/>
</form>
);
}
Expand Down

0 comments on commit bc3c7f7

Please sign in to comment.