Blame view

components/layout/Header.js 7.71 KB
jay committed
1 2 3 4
import { getSession, signOut } from "next-auth/react";
import Image from "next/image";
import Link from "next/link";
import React, { useEffect, useState } from "react";
jay committed
5
import { useDispatch, useSelector } from "react-redux";
jay committed
6
import { cleanImage } from "../../services/imageHandling";
Ravindra Kanojiya committed
7
import { Button, Container, Form, Nav, Navbar } from "react-bootstrap";
jaymehta committed
8
import { getCurrentEndUser, loadUser } from "../../redux/actions/userActions";
jaymehta committed
9
import { useRouter } from "next/router";
jaymehta committed
10
import { getWishlists } from "../../redux/actions/activityAction";
jaymehta committed
11 12
import { Typeahead } from "react-bootstrap-typeahead";
import axios from "axios";
Ravindra Kanojiya committed
13
import ActiveLink from "../common/ActiveLink";
jay committed
14 15

const Header = () => {
jaymehta committed
16
  const { loadedUser } = useSelector(state => state.loadedUser);
jaymehta committed
17
  const { endUser } = useSelector(state => state.endUser);
jaymehta committed
18 19
  const [open, setopen] = useState(false);
  const [options, setoptions] = useState([]);
.  
jaymehta committed
20
  // console.log("options", options);
jay committed
21
  const dispatch = useDispatch();
jaymehta committed
22
  // console.log("user", loadedUser);
Ravindra Kanojiya committed
23
  const [isSticky, setIsSticky] = useState(false);
jaymehta committed
24
  const router = useRouter();
Ravindra Kanojiya committed
25 26 27 28 29 30 31 32
  useEffect(() => {
    const handleScroll = () => {
      // Check if the scroll position is greater than a certain threshold
      const scrollY = window.scrollY || window.pageYOffset;
      setIsSticky(scrollY > 50);
    };

    // Attach the scroll event listener when the component mounts
jay committed
33
    window.addEventListener("scroll", handleScroll);
Ravindra Kanojiya committed
34 35 36

    // Clean up the event listener when the component unmounts
    return () => {
jay committed
37
      window.removeEventListener("scroll", handleScroll);
Ravindra Kanojiya committed
38
    };
jay committed
39
  }, []);
jaymehta committed
40 41 42 43 44 45 46
  useEffect(() => {
    dispatch(getCurrentEndUser());
  }, []);

  useEffect(() => {
    if (endUser) dispatch(getWishlists({ endUser: endUser.id }));
  }, [endUser]);
jay committed
47
  return (
jay committed
48
    <header className={`header_wrap ${isSticky ? "stick" : ""}`}>
49
      {isSticky && <div style={{ height: 76 }} className="bg-transparent"></div>}
jay committed
50 51
      <Navbar expand="lg" className="bg-body-tertiary">
        <Container fluid>
Ravindra Kanojiya committed
52
          <Navbar.Brand href="/">
jay committed
53
            <span className="image-container">
Chetan committed
54
              <Image loading="lazy" layout="fill" alt="" className="image img-fluid" src="/images/main-logo.svg" />
jay committed
55 56 57 58
            </span>
          </Navbar.Brand>
          <Navbar.Toggle aria-controls="navbarScroll" />
          <Navbar.Collapse id="navbarScroll">
Ravindra Kanojiya committed
59
            <Nav className=" my-2 my-lg-0" navbarScroll>
60 61 62
              <ActiveLink href="/user/wishlist" activeClassName="active">
                <a className="nav-link ">Wishlist</a>
              </ActiveLink>
Ravindra Kanojiya committed
63 64 65 66 67 68 69 70 71 72 73
              <ActiveLink href="/blog" activeClassName="active">
                <a className="nav-link ">Blogs</a>
              </ActiveLink>
              <ActiveLink href="/gift-card" activeClassName="active ">
                <a className="nav-link gift-card">
                  Gift Card{" "}
                  <span className="image-container">
                    <Image layout="fill" className="image img-fluid" src="/images/icons/gift-card-icon.svg" alt="" />
                  </span>
                </a>
              </ActiveLink>
Ravindra Kanojiya committed
74 75

              {/* <Nav.Link href="/gift-card" className="gift-card">
jay committed
76
                Gift Card
Ravindra Kanojiya committed
77
                <span className="image-container">
jay committed
78
                  <Image layout="fill" className="image img-fluid" src="/images/icons/gift-card-icon.svg" alt="" />
Ravindra Kanojiya committed
79
                </span>
Ravindra Kanojiya committed
80
              </Nav.Link> */}
jay committed
81 82 83
            </Nav>
            <Form className="d-flex me-3">
              <div className="header-search">
jaymehta committed
84 85
                <Typeahead
                  open={open}
.  
jaymehta committed
86 87 88
                  onBlur={() => {
                    setopen(false);
                  }}
jaymehta committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
                  id="select"
                  placeholder="Search for products, brands or categories"
                  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
117
                          return { id: item.id, label: `Category - ${item.name}` };
jaymehta committed
118 119 120 121
                        });
                        console.log("here", opts);
                        if (opts?.length > 0) {
                          const a = res.data.data.activities?.map(item => {
.  
jaymehta committed
122
                            return { id: item.id, label: `Experience - ${item.name}` };
jaymehta committed
123 124 125 126
                          });
                          opts = [...opts, ...a];
                        } else {
                          opts = res.data.data.activities?.map(item => {
.  
jaymehta committed
127
                            return { id: item.id, label: `Experience - ${item.name}` };
jaymehta committed
128 129
                          });
                        }
.  
jaymehta committed
130
                        // console.log("opts", opts);
jaymehta committed
131 132 133 134 135 136 137 138 139
                        setoptions(opts);
                        setopen(true);
                      }
                    } else {
                      setopen(false);
                    }
                  }}
                  options={options}
                />
jay committed
140 141 142 143 144 145 146 147
                <Button className="search-icon" variant="outline-success">
                  <span className="image-container">
                    <Image layout="fill" alt="" className="image img-fluid" src="/images/icons/search-icon.svg" />
                  </span>
                </Button>
              </div>
            </Form>
            {/* {console.log(user.id)} */}
jaymehta committed
148
            {loadedUser && loadedUser.id ? (
Ravindra Kanojiya committed
149
              <div className="top-btn">
Ravindra Kanojiya committed
150
                <div className="logout-bk">
jaymehta committed
151
                  <p>Brand Logo</p>
.  
jaymehta committed
152
                  <p>{loadedUser.phone}</p>
jaymehta committed
153 154
                </div>
                <Button
jaymehta committed
155
                  onClick={async () => {
jaymehta committed
156
                    signOut({ redirect: false });
jaymehta committed
157 158
                    await router.push("/");
                    window.location.reload();
jaymehta committed
159 160 161 162 163 164
                  }}
                  className="me-3"
                  variant="primary"
                >
                  Log out
                </Button>
jay committed
165 166
              </div>
            ) : (
Ravindra Kanojiya committed
167
              <div className="top-btn">
jaymehta committed
168 169 170 171 172 173 174
                <Button
                  onClick={() => {
                    router.push("/signup/user");
                  }}
                  className="me-3"
                  variant="primary"
                >
jay committed
175 176
                  Sign Up
                </Button>
jaymehta committed
177 178 179 180 181 182 183
                <Button
                  onClick={() => {
                    router.push("/login/user");
                  }}
                  className=""
                  variant="primary"
                >
jay committed
184 185 186 187 188 189 190
                  Log In
                </Button>
              </div>
            )}
          </Navbar.Collapse>
        </Container>
      </Navbar>
jay committed
191 192 193 194 195
    </header>
  );
};

export default Header;