-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.tsx
107 lines (104 loc) · 3.08 KB
/
search.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"use client";
import { Input, Form, List, Spin } from "antd";
import { SearchOutlined } from "@ant-design/icons";
import Link from "next/link";
import getSearchedBlogs from "@/actions/get-searched-blog";
import axios from "axios";
import { useState } from "react";
import Image from "next/image";
type Listtype = {
id: string;
title: string;
description: string | null;
imageUrl?: string;
};
const Search = () => {
const [listData, setListData] = useState<Listtype[]>([]);
const [loadingSpin, setLoadingSpin] = useState(false);
const [IsSearch, SetIsSearch] = useState(false);
const handleSearch = async (e: any) => {
e.preventDefault();
// console.log("search params", e.target.value);
try {
SetIsSearch(true);
setLoadingSpin(true);
if (e.target.value) {
const blogs = await axios(`/api/blogs/search/${e.target.value}`);
setListData(blogs.data);
console.log("blogs", blogs.data);
} else {
SetIsSearch(false);
const emptyBlogs = await axios(`/api/blogs/search/${undefined}`);
setListData(emptyBlogs.data);
}
} catch (error) {
} finally {
setLoadingSpin(false);
}
};
return (
<>
<Input
placeholder="search..."
onKeyUp={handleSearch}
prefix={<SearchOutlined />}
/>
<div
className={`absolute top-16 left-0 md:max-w-screen-lg w-full bg-gray-50 p-3 rounded-md overflow-y-scroll max-h-72 z-50 ${
IsSearch ? "visible" : "hidden"
}`}
>
{/* <Spin tip="loading..." spinning={loadingSpin}> */}
<List
loading={loadingSpin}
className="max-w-screen-sm mx-auto"
itemLayout="vertical"
dataSource={listData}
renderItem={(item, index) => (
<List.Item
extra={
<Image
src={item.imageUrl || ""}
width={300}
height={200}
className="aspect-video"
alt="blog image"
/>
}
>
<List.Item.Meta
// avatar={
// <Avatar
// src={`https://api.dicebear.com/7.x/miniavs/svg?seed=${index}`}
// />
// }
title={
<Link
onClick={() => SetIsSearch(false)}
className="hover:text-red-600"
href={`/blogs/details/${item.id}`}
>
{item.title}
</Link>
}
description={
<div className="ql-snow">
<div
className=" line-clamp-3 text-lg "
dangerouslySetInnerHTML={{
__html: item.description || "",
}}
/>
</div>
}
/>
{/* {item.description} */}
</List.Item>
)}
/>
{/* </Spin> */}
</div>
</>
);
};
export default Search;