cuponModule

 avatar
bruteCoder
jsx
16 days ago
3.6 kB
2
Indexable
Never
import { z } from 'zod'
import React, { useState } from 'react'
import { useForm } from 'react-hook-form'
import { Input } from '@/components/form'
import { zodResolver } from '@hookform/resolvers/zod'
import { GetServerSidePropsContext } from 'next'
import SelectCoupon from "@/components/coupons/SelectCoupon";
import axios from 'axios'

export async function getServerSideProps(context: GetServerSidePropsContext) {

  const { id } = context.params as { id: string }

  try {
    const couponData = await axios.get(`${process.env.NEXT_PUBLIC_BASE_API}coupons/masterclass/${id}/paid-mc`, {
      headers: {
        "Accept-Encoding": "gzip",
      },
    });
    const masterClassData = await axios.get(`${process.env.NEXT_PUBLIC_BASE_API}masterclasses/${id}`, {
      headers: {
        "Accept-Encoding": "gzip",
      },
    }
    );

    return {
      props: {
        id: id,
        couponData: couponData?.data?.data,
        masterClassData: masterClassData?.data?.data
      }
    }
  } catch (err) {
    return {
      props: {
        masterclass: {},
      },
    };
  }

}
const leadSchema = z.object({
  email: z.string().email(),
  name: z.string().min(1, { message: 'Name is Required' }).max(255, { message: 'Invalid Name' }),
  phone: z.string().min(10, { message: 'Invalid Phone Number' }).max(10, { message: 'Invalid Phone Number' }),
})

export default function CuponPages(
  { id, couponData, masterClassData }:
    { id: string, couponData: any, masterClassData: any }
) {

  console.log("couponData", couponData)
  console.log("masterClassData", masterClassData)
  console.log("id", id)
  const {
    register,
    handleSubmit,
    reset,
    watch,
    formState: { errors }
  } = useForm<z.infer<typeof leadSchema>>({
    resolver: zodResolver(leadSchema)
  })

  const [couponDiscount, setCouponDiscount] = useState<number>(0);
  const [finalCouponSelected, setFinalCouponSelected] =
    useState<any>(undefined);

  const submitForm = async (data: z.infer<typeof leadSchema>) => {
    console.log(data)
  }

  const phoneNumber = watch('phone')

  return (
    <main className='w-full min-h-screen flex flex-col items-center justify-center bg-slate-300'>
      <div className='p-4 flex flex-col justify-center items-center border border-black'>
        <h1 className='text-center text-2xl'>Please Enter Your Details</h1>
        <form onSubmit={handleSubmit(submitForm)}>
          <Input
            type='text'
            register={register}
            name='name'
            label='Name'
            placeholder='Your Name'
            errors={errors}
            rules={{ required: 'Name is required' }}
            className='mb-3' />
          <Input
            type='email'
            register={register}
            name='email'
            label='Email'
            placeholder='Your Email'
            errors={errors}
            rules={{ required: 'Email is required' }}
            className='mb-3' />
          <Input
            type='number'
            register={register}
            name='phone'
            label='Phone Number'
            placeholder='Your Phone Number'
            errors={errors}
            rules={{ required: 'Phone Number is required' }}
            className='mb-3' />

          <SelectCoupon
            phoneNumber={phoneNumber}
            coupons={couponData}
            originalOrderAmount={99}
            isSelectCouponEnabled={true}
            setFinalCouponSelected={setFinalCouponSelected}
            setCouponDiscount={setCouponDiscount}
          />
        </form>
      </div>
    </main>
  )
}

Leave a Comment