Files
old-kitabcitab-frontend/kitabcitab/pages/search.js

67 lines
2.4 KiB
JavaScript
Raw Normal View History

import React, { useState } from 'react'
2023-01-03 13:31:47 +01:00
import Results from '../components/Results'
import { AiOutlineClose, AiOutlineSearch } from 'react-icons/ai'
2023-01-03 13:31:47 +01:00
import Link from 'next/link'
import { useRouter } from 'next/router'
import { ENV_VAR } from '../envconfig'
2022-12-30 04:02:16 +01:00
const SearchPage = ({data}) => {
const router = useRouter()
const [searchInput, setSearchInput] = useState(router.query.term)
2023-03-08 12:08:29 +01:00
const term = searchInput
const search = () => {
if(!term) return
router.push(`/search?term=${term}`)
}
2023-03-08 12:08:29 +01:00
const enterHandler = (e) => {
if(e.key === "Enter" && term !== "") router.push(`/search?term=${term}`)
}
2022-12-30 04:02:16 +01:00
return (
2023-01-03 13:31:47 +01:00
<div className='flex flex-col justify-center'>
<div className="flex flex-col laptop:flex-row flex-wrap items-start w-[100vw] laptop:w-[80vw] laptop:h-[20vh] laptop:max-w-[90vw]">
<div className="p-10 w-[100vw] laptop:w-[25rem]">
<Link href='/'><p className="text-5xl tablet:text-6xl laptop:text-3xl text-white text-center font-serif ">KitabCitab</p></Link>
2022-12-30 04:02:16 +01:00
</div>
<div className="group
my-3
mx-auto
py-5
2022-12-30 04:02:16 +01:00
h-[4vh]
2023-01-03 13:31:47 +01:00
min-w-[90vw]
2022-12-30 04:02:16 +01:00
bg-[#303134]
2023-01-03 13:31:47 +01:00
border-[1px] w-[90vw] border-[#bdc1c6] rounded-3xl
2022-12-30 04:02:16 +01:00
flex flex-row justify-around items-center
2023-01-03 13:31:47 +01:00
tablet:w-[70vw]
2022-12-30 04:02:16 +01:00
2023-01-03 13:31:47 +01:00
laptop:min-w-[40vw]
2022-12-30 04:02:16 +01:00
laptop:w-[40vw]
laptop:mx-0
laptop:my-10
laptop:justify-between
"
>
<i className='rounded-3xl hover:border-black text-[#9aa0a6] px-3' onClick={() => search()}><AiOutlineSearch size={25}/></i>
2023-03-08 12:08:29 +01:00
<input onKeyDown={(e) => enterHandler(e)} value={searchInput} onChange={(e) => setSearchInput(e.target.value)} className="bg-[#303134] ml-5 w-[70%] active:border-none text-white outline-none text-lg "/>
<i className='rounded-3xl text-[#9aa0a6] px-3' onClick={() => setSearchInput("")}><AiOutlineClose size={25} /></i>
2022-12-30 04:02:16 +01:00
</div>
</div>
2023-02-16 00:05:13 +01:00
<Results data={data.hits.hits} term={searchInput}/>
2023-01-03 13:31:47 +01:00
</div>
2022-12-30 04:02:16 +01:00
)
}
export async function getServerSideProps(context) {
// Fetch data from external API
const res = await fetch(`${ENV_VAR}_search?q=${context.query.term}`)
const data = await res.json()
// Pass data to the page via props
return { props: { data } }
}
2022-12-30 04:02:16 +01:00
export default SearchPage