|
1 |
| -import { useEffect, useState } from "react"; |
| 1 | +import { useState } from "react"; |
2 | 2 | import { Popover, Transition } from "@headlessui/react";
|
3 | 3 | import { Search } from "react-feather";
|
4 |
| -import Input from './Input'; |
| 4 | +import Input from "./Input"; |
5 | 5 | import { useRouter } from "next/router";
|
| 6 | +import { useQuery } from "react-query"; |
| 7 | +import { structurePolls } from "./Utils"; |
| 8 | +import useVoting from "../hooks/useVoting"; |
6 | 9 |
|
7 | 10 | function SearchBar() {
|
8 |
| - const [polls, setPolls] = useState([]) |
9 |
| - const [pollID, setPollID] = useState("") |
10 |
| - const router = useRouter(); |
11 |
| - useEffect(() => { |
12 |
| - const polls = JSON.parse(localStorage.getItem("polls")) |
13 |
| - if (polls !== null) { |
14 |
| - setPolls(polls) |
15 |
| - } |
16 |
| - }, []) |
| 11 | + const voting = useVoting({}); |
| 12 | + const [pollID, setPollID] = useState(""); |
| 13 | + const router = useRouter(); |
| 14 | + const { data: pollData } = useQuery("polls", async () => { |
| 15 | + const { contract } = await voting; |
| 16 | + const polls = await contract.getPolls(); |
| 17 | + return structurePolls(polls); |
| 18 | + }); |
| 19 | + const polls = pollData ?? []; |
17 | 20 |
|
18 |
| - // useEffect(() => { |
19 |
| - // const listener = event => { |
20 |
| - // if (event.code === "Enter" || event.code === "NumpadEnter") { |
21 |
| - // console.log("Enter key was pressed. Run your function."); |
22 |
| - // event.preventDefault(); |
23 |
| - // // callMyFunction(); |
24 |
| - // } |
25 |
| - // }; |
26 |
| - // document.addEventListener("keydown", listener); |
27 |
| - // return () => { |
28 |
| - // document.removeEventListener("keydown", listener); |
29 |
| - // }; |
30 |
| - // }, []); |
| 21 | + const handleChange = (e) => { |
| 22 | + setPollID(e.target.value); |
| 23 | + }; |
31 | 24 |
|
32 |
| - const handleChange = e => { |
33 |
| - setPollID(e.target.value) |
34 |
| - } |
35 |
| - |
36 |
| - const handleSubmit = e => { |
37 |
| - e.preventDefault() |
38 |
| - console.log(pollID) |
39 |
| - router.push(`/pollDetails/${pollID}`) |
40 |
| - } |
41 |
| - return ( |
42 |
| - <Popover className="relative"> |
43 |
| - {({ open }) => ( |
44 |
| - <> |
45 |
| - <Popover.Button className="focus:outline-none pt-1"> |
46 |
| - <Search className={`cursor-pointerz-50 ${open && "text-primary"}`} /> |
47 |
| - </Popover.Button> |
48 |
| - <Transition |
49 |
| - enter="transition ease-out duration-200" |
50 |
| - enterFrom="opacity-0 translate-x-1" |
51 |
| - enterTo="opacity-100 translate-x-0" |
52 |
| - leave="transition ease-in duration-150" |
53 |
| - leaveFrom="opacity-100 translate-x-0" |
54 |
| - leaveTo="opacity-0 translate-x-1" |
55 |
| - > |
56 |
| - <Popover.Panel className="absolute z-10 lg:-left-72 lg:-ml-10 -ml-48 -top-3"> |
57 |
| - <form onSubmit={handleSubmit}> |
58 |
| - <Input onChange={handleChange} list="polls" placeholder="Search for past and present polls" width="lg:w-80" /> |
59 |
| - <datalist id="polls" width="lg:w-80"> |
60 |
| - {polls.length > 0 && polls.map(poll => ( |
61 |
| - <option value={poll.name} key={poll.id} /> |
62 |
| - ))} |
63 |
| - </datalist> |
64 |
| - </form> |
65 |
| - </Popover.Panel> |
66 |
| - </Transition> |
67 |
| - </> |
68 |
| - )} |
69 |
| - </Popover> |
70 |
| - ); |
| 25 | + const handleSubmit = (e) => { |
| 26 | + e.preventDefault(); |
| 27 | + router.push(`/pollDetails/${pollID}`); |
| 28 | + }; |
| 29 | + return ( |
| 30 | + <Popover className="relative"> |
| 31 | + {({ open }) => ( |
| 32 | + <> |
| 33 | + <Popover.Button className="focus:outline-none pt-1"> |
| 34 | + <Search |
| 35 | + className={`cursor-pointerz-50 ${open && "text-primary"}`} |
| 36 | + /> |
| 37 | + </Popover.Button> |
| 38 | + <Transition |
| 39 | + enter="transition ease-out duration-200" |
| 40 | + enterFrom="opacity-0 translate-x-1" |
| 41 | + enterTo="opacity-100 translate-x-0" |
| 42 | + leave="transition ease-in duration-150" |
| 43 | + leaveFrom="opacity-100 translate-x-0" |
| 44 | + leaveTo="opacity-0 translate-x-1" |
| 45 | + > |
| 46 | + <Popover.Panel className="absolute z-10 lg:-left-72 lg:-ml-10 -ml-48 -top-3"> |
| 47 | + <form onSubmit={handleSubmit}> |
| 48 | + <Input |
| 49 | + onChange={handleChange} |
| 50 | + list="polls" |
| 51 | + placeholder="Search for past and present polls" |
| 52 | + width="lg:w-80" |
| 53 | + /> |
| 54 | + <datalist id="polls" width="lg:w-80"> |
| 55 | + {polls.length > 0 && |
| 56 | + polls.map((poll) => ( |
| 57 | + <option value={poll.name} key={poll.id} /> |
| 58 | + ))} |
| 59 | + </datalist> |
| 60 | + </form> |
| 61 | + </Popover.Panel> |
| 62 | + </Transition> |
| 63 | + </> |
| 64 | + )} |
| 65 | + </Popover> |
| 66 | + ); |
71 | 67 | }
|
72 | 68 |
|
73 | 69 | export default SearchBar;
|
0 commit comments