Blame view

components/gift-card/GiftCard.js 19.6 KB
Ravindra Kanojiya committed
1 2 3 4
import Image from "next/image";
import React, { useEffect, useState } from "react";
import { Formik, Form, Field, ErrorMessage } from "formik";
import * as Yup from "yup";
.  
jaymehta committed
5 6 7 8 9 10
import { useDispatch, useSelector } from "react-redux";
import { getSession } from "next-auth/react";
import axios from "axios";
import { toast } from "react-toastify";
import { Loader } from "react-bootstrap-typeahead";
import { finishVendorOtpVerification } from "../../redux/actions/vendorActions";
jaymehta committed
11
import { useRouter } from "next/router";
.  
jaymehta committed
12
// import { getCurrentEndUser } from "../../redux/actions/userActions";
Ravindra Kanojiya committed
13 14 15 16 17 18 19 20 21 22 23 24

const validationSchema = Yup.object().shape({
  customAmt: Yup.string().required("Full name is required"),
  code: Yup.string().required("Please Enter 4-Digit Code"),
  email: Yup.string().email("Invalid email").required("Email is required"),
  receiverEmail: Yup.string().email("Invalid email").required("Email is required"),
  message: Yup.string().required("Message is required")
});

let formik1;

const GiftCard = () => {
jaymehta committed
25
  const router = useRouter()
.  
jaymehta committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  const dispatch = useDispatch();
  // useEffect(() => {
  //   dispatch(getCurrentEndUser());
  // }, []);
  const [session, setSession] = useState(null);
  const [loading, setloading] = useState(null);

  useEffect(() => {
    const fetchSession = async () => {
      setSession(await getSession());
    };
    fetchSession();
    // dispatch(getLoggedInVendor());
  }, []);
  // const {loadedUser} = useSelector(state => state.loadedUser)
  // const { endUser } = useSelector(state => state.endUser);
  console.log("endUser", session);
Ravindra Kanojiya committed
43 44
  const [isStep1, setIsStep1] = useState(true);
  const [amount, setAmount] = useState(0);
Ravindra Kanojiya committed
45
  const [custom, setcustom] = useState();
Ravindra Kanojiya committed
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

  const [isStep2, setIsStep2] = useState(false);
  const [isResult, setIsResult] = useState(false);
  const [showTooltip1, setShowTooltip1] = useState(false); // State for first tooltip
  const [showTooltip2, setShowTooltip2] = useState(false); // State for second tooltip
  const tooltipText1 = "Enter your Email Id. We'll send you a 4-digit code to verify";
  const tooltipText2 = "Enter your Receiver’s Email Id whom you wish to send gift card.";
  const handleclose = () => {
    setIsResult(false);
    setIsStep1(true);
  };

  return (
    <section className="gift-card-session">
      <div className="container">
        <div className="row justify-content-center">
          <div className="col-md-10">
            {!isResult && (
              <div className="row">
                <div className="col-md-5">
                  <div className="gift-card-lt">
                    <div className="gift-box">
                      <img src="/images/zango-logo.svg" alt="Gift Card" />
                    </div>
                    <h3>Amount: ${amount}</h3>
                  </div>
                </div>
                <div className="col-md-7">
                  {isStep1 && (
                    <div className="gift-card-rt">
                      <div className="back-btn">
                        <a href="">
                          <span className="image-container">
                            <Image layout="fill" alt="" className="image img-fluid" src="/images/icons/arrow-left-02.svg" />
                          </span>
                        </a>
                      </div>
                      <Formik
                        initialValues={{
                          customAmt: "",
                          email: "",
                          receiverEmail: "",
                          message: ""
                        }}
                        enableReinitialize={true}
                        // validationSchema={validationSchema}
                        onSubmit={(values, { setSubmitting }) => {
                          // Handle form submission here
                        }}
                      >
                        {({ isSubmitting, values, handleChange, handleBlur, touched, errors }) => (
                          <Form action="" className="form-01">
                            <div className="title">Buy a gift card</div>
                            <div className="cl-gry">Please select an amount</div>
                            <div className="gift-card-amt">
                              <ul>
                                <li>
.  
jaymehta committed
103 104 105 106 107 108 109 110 111 112 113
                                  <input
                                    name="amt"
                                    id="amt-250"
                                    type="radio"
                                    value=""
                                    data-gtm-Form-interact-field-id="1"
                                    onChange={e => {
                                      setAmount(250);
                                      setcustom(false);
                                    }}
                                  />
Ravindra Kanojiya committed
114 115 116
                                  <label for="amt-250">$250</label>
                                </li>
                                <li>
.  
jaymehta committed
117 118 119 120 121 122 123 124 125 126 127
                                  <input
                                    name="amt"
                                    id="amt-500"
                                    type="radio"
                                    value=""
                                    data-gtm-form-interact-field-id="1"
                                    onChange={e => {
                                      setAmount(500);
                                      setcustom(false);
                                    }}
                                  />
Ravindra Kanojiya committed
128 129 130
                                  <label for="amt-500">$500</label>
                                </li>
                                <li>
.  
jaymehta committed
131 132 133 134 135 136 137 138 139 140 141
                                  <input
                                    name="amt"
                                    id="amt-750"
                                    type="radio"
                                    value=""
                                    data-gtm-form-interact-field-id="1"
                                    onChange={e => {
                                      setAmount(750);
                                      setcustom(false);
                                    }}
                                  />
Ravindra Kanojiya committed
142 143 144 145 146 147
                                  <label for="amt-750">
                                    <span>Most Popular</span>
                                    <br /> $750
                                  </label>
                                </li>
                                <li>
.  
jaymehta committed
148 149 150 151 152 153 154 155 156 157 158
                                  <input
                                    name="amt"
                                    id="amt-1000"
                                    type="radio"
                                    value=""
                                    data-gtm-form-interact-field-id="1"
                                    onChange={e => {
                                      setAmount(1000);
                                      setcustom(false);
                                    }}
                                  />
Ravindra Kanojiya committed
159 160 161
                                  <label for="amt-1000"> $1000</label>
                                </li>
                                <li>
.  
jaymehta committed
162 163 164 165 166 167 168 169 170 171 172
                                  <input
                                    name="amt"
                                    id="amt-1500"
                                    type="radio"
                                    value=""
                                    data-gtm-form-interact-field-id="1"
                                    onChange={e => {
                                      setAmount(1500);
                                      setcustom(false);
                                    }}
                                  />
Ravindra Kanojiya committed
173 174 175
                                  <label for="amt-1500"> $1500</label>
                                </li>
                                <li>
.  
jaymehta committed
176 177 178 179 180 181 182 183 184 185 186
                                  <input
                                    name="amt"
                                    id="amt-custom"
                                    type="radio"
                                    value=""
                                    data-gtm-form-interact-field-id="1"
                                    onChange={e => {
                                      setcustom(true);
                                      setAmount(null);
                                    }}
                                  />
Ravindra Kanojiya committed
187 188 189 190 191
                                  <label for="amt-custom"> $ Custom</label>
                                </li>
                              </ul>
                            </div>
                            <div className="row">
.  
jaymehta committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
                              {custom && (
                                <div className="col-md-12 mb-4">
                                  <label htmlFor="">Custom Amount</label>
                                  <Field
                                    className="form-control"
                                    type="number"
                                    name="customAmt"
                                    placeholder="Enter your amount"
                                    onChange={e => {
                                      setAmount(e.target.value);
                                    }}
                                    onBlur={handleBlur}
                                    value={amount}
                                  />
                                  {touched.customAmt && errors.customAmt && <div className="text-danger">{errors.customAmt}</div>}
                                </div>
                              )}
Ravindra Kanojiya committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
                              <div className="col-md-12 mb-4">
                                <label htmlFor="">Email Id</label>
                                <Field
                                  className="form-control"
                                  type="email"
                                  name="email"
                                  placeholder="yourname@example.com"
                                  onChange={handleChange}
                                  onBlur={handleBlur}
                                  value={values.email}
                                />
                                {touched.email && errors.email && <div className="text-danger">{errors.email}</div>}
                                <div className="tooltip-wrapper">
                                  <a className="tooltip-btn" onMouseEnter={() => setShowTooltip1(true)} onMouseLeave={() => setShowTooltip1(false)}>
                                    <span className="image-container">
                                      <Image layout="fill" alt="" className="image img-fluid" src="/images/icons/info.svg" />
                                    </span>
                                  </a>

                                  {/* Tooltip */}
                                  {showTooltip1 && <div className="tooltips">{tooltipText1}</div>}
                                </div>
                              </div>

                              <div className="col-md-12 mb-4">
                                <label htmlFor="">Receivers Email Id</label>
                                <Field
                                  className="form-control"
                                  type="receiverEmail"
                                  name="receiverEmail"
                                  placeholder="yourname@example.com"
                                  onChange={handleChange}
                                  onBlur={handleBlur}
                                  value={values.receiverEmail}
                                />
                                {touched.receiverEmail && errors.receiverEmail && <div className="text-danger">{errors.receiverEmail}</div>}
                                <div className="tooltip-wrapper">
                                  <a className="tooltip-btn" onMouseEnter={() => setShowTooltip2(true)} onMouseLeave={() => setShowTooltip2(false)}>
                                    <span className="image-container">
                                      <Image layout="fill" alt="" className="image img-fluid" src="/images/icons/info.svg" />
                                    </span>
                                  </a>

                                  {showTooltip2 && <div className="tooltips">{tooltipText2}</div>}
                                </div>
                              </div>

                              <div className="col-md-12 mb-4">
                                <Field
                                  className="form-control"
                                  as="textarea"
                                  rows={7}
                                  name="message"
                                  placeholder="Type your message..."
                                  onChange={handleChange}
                                  onBlur={handleBlur}
                                  value={values.message}
                                />
                                {touched.message && errors.message && <div className="text-danger">{errors.message}</div>}
                              </div>
                              <div className="col-md-12 mb-3">
.  
jaymehta committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
                                <button
                                  className="btn btn-primary w-100"
                                  onClick={async () => {
                                    setloading(true);
                                    if (!session) {
                                      toast.warning("Please log in to buy a gift card.");
                                      return;
                                    }
                                    const config = {
                                      headers: {
                                        "Content-Type": "application/json",
                                        Authorization: `Bearer ${session.jwt}`
                                      }
                                    };
                                    const data = {
                                      data: {
                                        userId: session.id
                                      }
                                    };
                                    const response = await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/end-user/verify-gift-card-otp`, data, config);
                                    console.log("response", response.data);
                                    setloading(false);
                                    console.log(values);
                                    setIsStep2(true);
                                    setIsStep1(false);
                                  }}
                                  disabled={!amount > 0 || !values.receiverEmail || !values.email}
                                >
                                  {loading ? <Loader /> : "Continue"}
Ravindra Kanojiya committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
                                </button>
                              </div>
                              <div className="col-12">
                                <p>
                                  By continuing you agree to our <a href="">Terms</a> and <a href="">Privacy Policy</a>
                                </p>
                              </div>
                            </div>
                          </Form>
                        )}
                      </Formik>
                    </div>
                  )}

                  {isStep2 && (
                    <div className="gift-card-rt">
                      <div className="back-btn">
                        <a href="">
                          <span className="image-container">
                            <Image layout="fill" alt="" className="image img-fluid" src="/images/icons/arrow-left-02.svg" />
                          </span>
                        </a>
                      </div>
                      <Formik
                        initialValues={{
                          code: ""
                        }}
                        // validationSchema={validationSchema}
                        onSubmit={(values, { setSubmitting }) => {
                          // Handle form submission here
                          console.log(values);
                          setSubmitting(false);
                          setIsStep2(false);
                          setIsStep1(false);
                          setIsResult(true);
                        }}
                      >
                        {({ isSubmitting, values, handleChange, handleBlur, touched, errors }) => (
                          <Form action="" className="form-01">
                            <div className="title">We emailed you a 4-digit code</div>
                            <div className="cl-gry">Please enter it below to create or login into your account:</div>

                            <div className="row mt-4">
                              <div className="col-md-12 mb-4">
                                <label htmlFor="">Enter 4-Digit Code</label>
                                <Field
                                  className="form-control"
                                  type="text"
                                  name="code"
                                  placeholder="Enter the code we emailed you"
                                  onChange={handleChange}
                                  onBlur={handleBlur}
                                  value={values.code}
                                />
                                {touched.code && errors.code && <div className="text-danger">{errors.code}</div>}
                                <div className="link-a">
                                  <a href="">Resend Code</a>
                                </div>
                              </div>
                            </div>
                            <div className="row">
                              <div className="col-md-12 mb-3">
.  
jaymehta committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374
                                <button
                                  className="btn btn-primary w-100"
                                  onClick={async () => {
                                    const res = await await finishVendorOtpVerification({ email: session.user.email, oneTimePassword: values.code });
                                    console.log("res", res.data);
                                    if (!res.data.ok) {
                                      toast.error(res.data.message);
                                    }
                                    if (res.data.ok) {
                                      toast.success("OTP verified!");
                                    }
                                  }}
                                  disabled={isSubmitting}
                                >
Ravindra Kanojiya committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
                                  Confirm
                                </button>
                              </div>
                            </div>
                          </Form>
                        )}
                      </Formik>
                    </div>
                  )}
                </div>
              </div>
            )}

            {isResult && (
              <div className="row">
                <div className="col-12">
                  <div className="result-box">
                    <p>
jaymehta committed
393 394
                      Gift card sent for approval, please wait till the vendor contacts you
                      <br /> Thank you.
Ravindra Kanojiya committed
395 396
                    </p>
                    <div>
jaymehta committed
397 398 399
                      <button className="btn btn-primary" type="button" onClick={()=>{
                        router.push("/listing")
                      }}>
Ravindra Kanojiya committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
                        Browse Experiences
                      </button>
                    </div>
                  </div>
                </div>
              </div>
            )}
          </div>
        </div>
      </div>
    </section>
  );
};

export default GiftCard;