Blame view

components/home/SearchBar.js 3.81 KB
jaymehta committed
1 2 3 4 5 6 7 8 9
import axios from "axios";
import Image from "next/image";
import { useRouter } from "next/router";
import React, { useState } from "react";
import { Button } from "react-bootstrap";
import { Typeahead } from "react-bootstrap-typeahead";
import "react-bootstrap-typeahead/css/Typeahead.css";
import { useDispatch } from "react-redux";
import { setActivityFilters } from "../../redux/actions/activityAction";
Ravindra Kanojiya committed
10 11

const SearchBar = () => {
jaymehta committed
12 13 14 15
  const [open, setopen] = useState(false);
  const [options, setoptions] = useState([]);
  const dispatch = useDispatch();
  const router = useRouter();
.  
jaymehta committed
16
  // console.log("options", options);
Ravindra Kanojiya committed
17 18
  return (
    <>
jaymehta committed
19 20 21 22 23 24 25 26 27
      <section className="searchbar-session">
        <div className="container">
          <div className="row">
            <div className="col-12">
              <div className="searchbar-h">
                <Typeahead
                  open={open}
                  id="select"
                  placeholder="Search for products, brands or categories"
.  
jaymehta committed
28 29
                  onBlur={() => {
                    setopen(false);
.  
jaymehta committed
30
                  }}
jaymehta committed
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
                  onChange={selected => {
                    console.log("selected", selected);
                    // router.push("/listing");
                    if (selected[0]?.label.includes("experience")) {
                      // console.log("jjja");
                      router.push(`/activities/${selected[0].id}`);
                    }
                    if (selected[0]?.label.includes("category")) {
                      router.push(`/listing?category=${selected[0].id}`);
                      // dispatch(setActivityFilters({ filters: { category: selected[0].id, subCategories: [] } }));
                    }
                    // Handle selections...
                  }}
                  onInputChange={async e => {
                    console.log(e);
                    if (e.length > 1) {
                      const config = {
                        headers: {
                          "Content-Type": "application/json"
                        }
                      };

                      const res = await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/experience/get-search-response`, { string: e }, config);
                      console.log("res", res.data);
                      if (res.data.success) {
                        let opts = res.data.data.categories?.map(item => {
.  
jaymehta committed
57
                          return { id: item.id, label: `Category - ${item.name}` };
jaymehta committed
58 59 60 61
                        });
                        console.log("here", opts);
                        if (opts?.length > 0) {
                          const a = res.data.data.activities?.map(item => {
.  
jaymehta committed
62
                            return { id: item.id, label: `Experience - ${item.name}` };
jaymehta committed
63 64 65 66
                          });
                          opts = [...opts, ...a];
                        } else {
                          opts = res.data.data.activities?.map(item => {
.  
jaymehta committed
67
                            return { id: item.id, label: `Experience - ${item.name}` };
jaymehta committed
68 69
                          });
                        }
.  
jaymehta committed
70
                        // console.log("opts", opts);
jaymehta committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
                        setoptions(opts);
                        setopen(true);
                      }
                    } else {
                      setopen(false);
                    }
                  }}
                  options={options}
                />
                {/* <input type="text"  /> */}
                <Button className="search-icon">
                  <span className="image-container">
                    <Image layout="fill" alt="" className="image img-fluid" src="/images/icons/search-icon.svg" />
                  </span>
                </Button>
              </div>
Ravindra Kanojiya committed
87
            </div>
jaymehta committed
88 89
          </div>
        </div>
Ravindra Kanojiya committed
90 91
      </section>
    </>
jaymehta committed
92 93
  );
};
Ravindra Kanojiya committed
94

jaymehta committed
95
export default SearchBar;