import MoreLink from "backpack-ui/dist/components/moreLink"; import { Heading } from "backpack-ui/dist/components/text"; import * as cn from "classnames"; import * as React from "react"; import cleanQuery from "../../helpers/cleanQuery"; import Template from "../../templates/template"; import Container from "../_backpack/container"; import Overlay from "../_backpack/overlay"; import { AutocompleteResults } from "../autocompleteResults"; import SearchInput from "../searchInput"; import SearchInputCancelButton from "../searchInputCancelButton"; import * as styles from "./styles.css"; export class GlobalAutocomplete extends React.PureComponent { public delaySearch = 150; public delayTimer; public scrollTimer; public autocomplete: React.RefObject; public state; constructor(props) { super(props); this.state = { keyword: "", isOpen: false, places: [], pois: [], news: [], }; this.autocomplete = React.createRef(); } public componentDidMount() { const $ = (window as any).$; const $search = $(".js-lp-global-header-search, .js-lp-search"); $search.on("click", e => { e.preventDefault(); this.setState({ isOpen: true, }); this.disableScrolling(); this.autocomplete.current.focus(); }); if (typeof document !== "undefined") { document.addEventListener("keydown", this.handleKeydown); } } public componentDidUpdate(prevProps, prevState) { clearTimeout(this.scrollTimer); if (this.state.keyword !== "" && this.state.keyword === prevState.keyword) { const $ = (window as any).$; const $results = $(".js-templateAreaResultsAutocomplete"); const $keywords = $(".js-templateAreaKeywordsAutocomplete"); $results.on("scroll", e => { e.preventDefault(); this.scrollTimer = setTimeout(() => { if ($results.scrollTop() > 10) { $keywords.addClass("lp-global-templateAreaKeywordsAutocompleteShadow"); } else { $keywords.removeClass("lp-global-templateAreaKeywordsAutocompleteShadow"); } }, 50); }); } } public componentWillUnmount() { if (typeof document !== "undefined") { document.removeEventListener("keydown", this.handleKeydown); } } public render() { const totalResults = this.state.places.length + this.state.pois.length + this.state.news.length; return ( {this.state.isOpen && (
0 && styles.autocompleteWithResults, ) } >
{ e.preventDefault(); window.location.href = `https://www.lonelyplanet.com/search?q=${encodeURI(this.state.keyword)}`; }} >
{totalResults > 0 && {this.state.places && this.state.places.length > 0 &&
Destinations
} {this.state.pois && this.state.pois.length > 0 &&
Points of interest
} {this.state.news && this.state.news.length > 0 &&
News and articles
}
See all results
}
)}
); } private clearData() { this.setState({ places: [], pois: [], news: [], }); } private resetInput() { this.setState({ keyword: "", isOpen: false, }); } private async fetchData() { const resultsMax = 4; const query = cleanQuery(this.state.keyword); if (query.length) { const [places, pois, news] = await Promise.all([ this.callFetch(query, "place"), this.callFetch(query, "poi"), this.callFetch(query, "video,article,news"), ]); this.setState({ places: places.data.slice(0, resultsMax), pois: pois.data.slice(0, resultsMax), news: news.data.slice(0, resultsMax), }); } else { this.clearData(); } } private async callFetch(query, type) { const response = await fetch(`https://www.lonelyplanet.com/search/api?q=${query}&type=${type}`); if (!response.ok) { return { total: 0, data: [], error: true }; } return await response.json(); } private handleSearch = e => { clearTimeout(this.delayTimer); if (e.target.value !== this.state.keyword) { this.setState({ keyword: e.target.value }, () => { this.delayTimer = setTimeout(() => { this.fetchData(); }, this.delaySearch); }); } } private handleClickAway = e => { e.preventDefault(); this.resetInput(); this.clearData(); this.enableScrolling(); this.autocomplete.current.blur(); } private handleKeydown = e => { if (e.keyCode === 27 && this.state.isOpen) { e.preventDefault(); this.resetInput(); this.clearData(); this.enableScrolling(); } } private disableScrolling() { if (typeof document !== "undefined") { document.documentElement.style.height = "100%"; document.documentElement.style.overflow = "hidden"; document.body.style.height = "100%"; document.body.style.overflow = "hidden"; } } private enableScrolling() { if (typeof document !== "undefined") { document.documentElement.removeAttribute("style"); document.body.removeAttribute("style"); } } } export default GlobalAutocomplete;