import React, { useState, useEffect } from ‘react’;
import { Container, Input, SearchIconSection } from ‘./styles’;
import { FontAwesomeIcon } from ‘@fortawesome/react-fontawesome’;
import { faSearch } from ‘@fortawesome/free-solid-svg-icons’;
import { builder } from ‘@builder.io/react’;
import { useRouter } from ‘next/router’;
// import axios from ‘axios’;
type Props = {
placeholder: string;
};
builder.init(process.env.NEXT_PUBLIC_BUILDER_API_KEY || ‘’);
function InputSearch(props: Props) {
const router = useRouter();
const [searchTerm, setSearchTerm] = useState(‘’);
const [searchResults, setSearchResults] = useState();
/* useEffect(() => {
const getArticles = async () => {
try {
const response = await axios.get(https://cdn.builder.io/api/v3/content/pible-page-article?apiKey=${process.env.NEXT_PUBLIC_BUILDER_API_KEY}&query.data.title.$options=i&&query.data.title.$regex=.*${searchTerm}.*&limit=20&omit=data.blocks
);
setSearchResults(response.data.results);
return response.data.results;
} catch (error) {
console.error('Error:', error);
throw error;
}
};
const delaySearch = setTimeout(() => {
if (searchTerm.trim() !== '') {
getArticles(searchTerm);
} else {
// setSuggestions([]);
}
}, 300);
return () => clearTimeout(delaySearch);
}, [searchTerm]); */
useEffect(() => {
if (searchTerm) {
builder
.get(‘page-article’, {
limit: 100,
query: {
data: {
$or: [
{
title: { $eq: ‘dev’ }, // just for test
},
],
},
},
})
.toPromise()
.then((response) => setSearchResults(response));
}
}, [searchTerm]);
const handleSearch = () => {
router.push(/tax-pible/${searchTerm}
);
};
return (
<Input
type=“text”
placeholder={props.placeholder}
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
/>
<SearchIconSection onClick={handleSearch}>
<FontAwesomeIcon icon={faSearch} />
</SearchIconSection>
{/* <ul>
{searchResults.length > 0 && searchResults.map(({ data }, index) => {
console.log('ields ', data.title)
return (
<li key={index}>{data.title}</li>
)
})}
</ul> */}
</Container>
);
}
export default InputSearch;