summaryrefslogtreecommitdiff
path: root/src/lib/form/components/RequestForQuotation.jsx
blob: 31efb5d68c9ba49180e8da35bec742dbfe5607e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import odooApi from '@/core/api/odooApi'
import HookFormSelect from '@/core/components/elements/Select/HookFormSelect'
import cityApi from '@/lib/address/api/cityApi'
import { yupResolver } from '@hookform/resolvers/yup'
import React, { useEffect, useRef, useState } from 'react'
import ReCAPTCHA from 'react-google-recaptcha'
import { Controller, useForm } from 'react-hook-form'
import { toast } from 'react-hot-toast'
import * as Yup from 'yup'
import createLeadApi from '../api/createLeadApi'
import getFileBase64 from '@/core/utils/getFileBase64'

const RequestForQuotation = () => {
  const {
    register,
    handleSubmit,
    formState: { errors },
    control,
    reset
  } = useForm({
    resolver: yupResolver(validationSchema),
    defaultValues
  })
  const [cities, setCities] = useState([])

  const quotationFileRef = useRef(null)
  const recaptchaRef = useRef(null)

  useEffect(() => {
    const loadCities = async () => {
      let dataCities = await cityApi()
      dataCities = dataCities.map((obj) => ({ value: obj.name, label: obj.name }))
      setCities(dataCities)
    }
    loadCities()
  }, [])

  const onSubmitHandler = async (values) => {
    const recaptchaValue = recaptchaRef.current.getValue()
    if (!recaptchaValue) {
      toast.error('Recaptcha harus diisi')
      return
    }

    const file = quotationFileRef.current.files[0]
    let fileBase64 = null
    if (typeof file !== 'undefined') {
      if (file.size > 5000000) {
        toast.error('Maksimal ukuran file adalah 5MB')
        return
      }
      fileBase64 = await getFileBase64(file)
    }

    const data = {
      name: `Request For Quotation - ${values.company}`,
      email_from: values.email,
      phone: values.phone,
      description: [
        `Nama Perusahaan: ${values.company}`,
        `No. Telepon: ${values.phone}`,
        `Kota: ${values.city}`,
        `No. Handphone: ${values.mobile}`,
        `Alamat Email: ${values.email}`,
        `Keterangan: ${values.description}`
      ].join('\n')
    }

    if (fileBase64) data.file_quotation = fileBase64

    const createLead = await createLeadApi({ data })
    if (createLead) {
      toast.success('Berhasil mengirimkan formulir request for quotation')
      reset()
      recaptchaRef.current.reset()
    }
  }
  return (
    <div className='container mx-auto p-4 md:p-0 my-0 md:my-10'>
      <h1 className='text-h-sm md:text-title-sm font-semibold mb-6'>Request for Quotation</h1>

      <div className='w-full grid grid-cols-1 md:grid-cols-2'>
        <form onSubmit={handleSubmit(onSubmitHandler)} className='grid grid-cols-1 gap-y-6'>
          <div
            className='flex items-center bg-blue-100 border border-blue-300 text-blue-500 font-medium px-4 py-3 rounded leading-6'
            role='alert'
          >
            Halaman untuk pengajuan penawaran harga, lengkapi data di bawah ini dengan jelas untuk
            mempermudah tim support kami melayani kebutuhan Anda. Tim kami akan sesegera mungkin
            untuk membuatkan penawaran harga terbaik, hubungi kami melalui telpon jika ada
            keterlambatan pelayanan.
          </div>
          <div>
            <label className='form-label mb-2'>Nama Perusahan*</label>
            <input
              {...register('company')}
              placeholder='PT. Indoteknik Dotcom Gemilang'
              type='text'
              className='form-input'
              aria-invalid={errors.company?.message}
            />
            <div className='text-caption-2 text-danger-500 mt-1'>{errors.company?.message}</div>
          </div>

          <div>
            <label className='form-label mb-2'>No. Telepon*</label>
            <input
              {...register('phone')}
              placeholder='021XXXXXXX'
              type='text'
              className='form-input'
              aria-invalid={errors.phone?.message}
            />
            <div className='text-caption-2 text-danger-500 mt-1'>{errors.phone?.message}</div>
          </div>

          <div>
            <label className='form-label mb-2'>Kota*</label>
            <Controller
              name='city'
              control={control}
              render={(props) => <HookFormSelect {...props} options={cities} />}
            />
            <div className='text-caption-2 text-danger-500 mt-1'>{errors.city?.message}</div>
          </div>

          <div>
            <label className='form-label mb-2'>Contact Person*</label>
            <input
              {...register('contactPerson')}
              placeholder='John Doe'
              type='text'
              className='form-input'
              aria-invalid={errors.contactPerson?.message}
            />
            <div className='text-caption-2 text-danger-500 mt-1'>{errors.contactPerson?.message}</div>
          </div>

          <div>
            <label className='form-label mb-2'>No. Handphone*</label>
            <input
              {...register('mobile')}
              placeholder='628XXXXXXX'
              type='text'
              className='form-input'
              aria-invalid={errors.mobile?.message}
            />
            <div className='text-caption-2 text-danger-500 mt-1'>{errors.mobile?.message}</div>
          </div>

          <div>
            <label className='form-label mb-2'>Alamat Email*</label>
            <input
              {...register('email')}
              placeholder='contoh@email.com'
              type='email'
              className='form-input'
              aria-invalid={errors.email?.message}
            />
            <div className='text-caption-2 text-danger-500 mt-1'>{errors.email?.message}</div>
          </div>

          <div>
            <label className='form-label mb-2'>Keterangan</label>
            <textarea {...register('description')} type='text' className='form-input' />
          </div>

          <div>
            <label className='form-label mb-2'>File Daftar Produk</label>
            <input type="file" name="quotationFile" className='form-input' ref={quotationFileRef} />
          </div>

          <ReCAPTCHA ref={recaptchaRef} sitekey={process.env.NEXT_PUBLIC_RECAPTCHA_GOOGLE} />

          <button type='submit' className='btn-yellow w-full md:w-fit ml-0 md:ml-auto'>
            Simpan
          </button>
        </form>
      </div>
    </div>
  )
}

const validationSchema = Yup.object().shape({
  email: Yup.string().email('Format harus seperti contoh@email.com').required('Harus di-isi'),
  company: Yup.string().required('Harus di-isi'),
  phone: Yup.string().required('Harus di-isi'),
  mobile: Yup.string().required('Harus di-isi'),
  city: Yup.string().required('Harus dipilih'),
  contactPerson: Yup.string().required('Harus dipilih'),
})

const defaultValues = {
  email: '',
  company: '',
  phone: '',
  mobile: '',
  address: '',
  city: '',
  description: ''
}

export default RequestForQuotation