summaryrefslogtreecommitdiff
path: root/src/lib/pengajuan-tempo/component/Referensi.jsx
blob: 8db321d16eca51cf9a5eb0f027c68b6988cb65e9 (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
204
205
206
207
208
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
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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
import React, { useState, useEffect, useMemo, useRef } from 'react';
import { useForm } from 'react-hook-form';
import {
  usePengajuanTempoStoreSupplier,
  usePengajuanTempoStore,
} from '../../../../src-migrate/modules/register/stores/usePengajuanTempoStore';
import * as Yup from 'yup';
import { yupResolver } from '@hookform/resolvers/yup';
import { PlusCircleIcon } from '@heroicons/react/24/outline';
import useDevice from '@/core/hooks/useDevice';
import { Trash2Icon } from 'lucide-react';
import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline';
import BottomPopup from '@/core/components/elements/Popup/BottomPopup';
const initialData = [];
const Referensi = ({ chekValid, buttonSubmitClick, data }) => {
  const [changeConfirmation, setChangeConfirmation] = useState(false);
  const [selectedIndex, setSelectedIndex] = useState(null);

  const { isDesktop, isMobile } = useDevice();
  const [openIndexes, setOpenIndexes] = useState([]);

  const {
    register,
    formState: { errors },
    handleSubmit,
    watch,
    setValue,
    control,
  } = useForm({
    resolver: yupResolver(validationSchema),
    defaultValues,
  });
  const { formSupplier, updateFormSupplier, updateHasSave, hasSavedata } =
    usePengajuanTempoStoreSupplier();
  const { form } = usePengajuanTempoStore();
  const [formData, setFormData] = useState([
    {
      supplier: '',
      pic: '',
      telepon: '',
      durasiTempo: '',
      creditLimit: '',
    },
  ]);

  const [buttonSubmit, setButtonSubmit] = useState(false);
  const [supplierData, setSupplierData] = useState(initialData);
  const [newSupplier, setNewSupplier] = useState({
    supplier: '',
    pic: '',
    telepon: '',
    durasiTempo: '',
    creditLimit: '',
  });
  const onChangeInput = (e, index) => {
    updateHasSave(false);
    const { name, value } = e.target;

    let formattedValue = value;

    if (name === 'durasiTempo') {
      formattedValue = value.replace(/\s*Hari\s*/g, '');
    } else if (name === 'creditLimit') {
      formattedValue = value.replace(/^Rp\s*/, '');
    }

    const editData = supplierData.map((item, i) =>
      i === index ? { ...item, [name]: formattedValue } : item
    );
    setSupplierData(editData);
    if (value == '' && supplierData.supplier) {
      updateHasSave(true);
    }
  };

  const handleNewSupplierChange = (e) => {
    updateHasSave(false);
    const { name, value } = e.target;

    let formattedValue = value;

    if (name === 'durasiTempo') {
      formattedValue = value.replace(/\s*Hari\s*/g, '');
    } else if (name === 'creditLimit') {
      formattedValue = value.replace(/^Rp\s*/, '');
    }

    const updatedSupplier = { ...newSupplier, [name]: formattedValue };
    setNewSupplier(updatedSupplier);
    if (value == '') {
      updateHasSave(true);
    }
  };

  useEffect(() => {
    const isAllFieldsEmpty = Object.values(newSupplier).every(
      (value) => value === ''
    );
    if (isAllFieldsEmpty) {
      updateHasSave(true);
    } else {
      updateHasSave(false);
    }
  }, [newSupplier]);

  const handleDeleteSupplier = () => {
    if (selectedIndex !== null) {
      // Logika untuk menghapus supplier
      const updatedSuppliers = supplierData.filter(
        (_, idx) => idx !== selectedIndex
      );
      setSupplierData(updatedSuppliers);
      setChangeConfirmation(false);
      updateHasSave(false);
    }
  };

  const handleAddNewSupplier = () => {
    // updateHasSave(false);
    if (Object.values(newSupplier).every((val) => val.trim() !== '')) {
      setSupplierData((prevData) => {
        const newData = [...prevData, newSupplier];
        return newData;
      });

      setNewSupplier({
        supplier: '',
        pic: '',
        telepon: '',
        durasiTempo: '',
        creditLimit: '',
      });
    }
  };

  useEffect(() => {
    handleAddNewSupplier();
    updateFormSupplier(supplierData);
    setButtonSubmit(!buttonSubmit);
  }, [buttonSubmitClick]);
  const simpanData = () => {
    setButtonSubmit(!buttonSubmit);
    if (Object.values(newSupplier).every((val) => val.trim() !== '')) {
      setSupplierData((prevData) => {
        const newData = [...prevData, newSupplier];
        return newData;
      });

      setNewSupplier({
        supplier: '',
        pic: '',
        telepon: '',
        durasiTempo: '',
        creditLimit: '',
      });
    }
    updateHasSave(true);
  };
  const formatRupiah = (value) => {
    if (!value) return '';
    const numberString = value.replace(/[^0-9]/g, '');
    return numberString
      ? 'Rp ' + new Intl.NumberFormat('id-ID').format(numberString)
      : '';
  };
  const formatHari = (value) => {
    if (!value) return '';

    const numberString = value.replace(/[^0-9]/g, '');

    return numberString ? numberString.replace(/Hari/g, '') + ' Hari' : '';
  };

  useEffect(() => {
    updateFormSupplier(supplierData);
  }, [buttonSubmit]);
  const getFromLocalStorage = (key) => {
    const itemStr = localStorage.getItem(key);
    if (!itemStr) return null;

    const item = JSON.parse(itemStr);
    return item;
  };
  useEffect(() => {
    // const cachedData = getFromLocalStorage('Referensi');
    if (data) {
      setSupplierData(data);
      updateFormSupplier(data);
    }
  }, [buttonSubmitClick]);

  useEffect(() => {
    setOpenIndexes(supplierData.map((_, index) => index));
  }, [supplierData]);

  const toggleOpen = (index) => {
    setOpenIndexes((prev) =>
      prev.includes(index) ? prev.filter((i) => i !== index) : [...prev, index]
    );
  };

  const handleOpenConfirmation = (index) => {
    setSelectedIndex(index);
    setChangeConfirmation(true);
  };
  return (
    <>
      <BottomPopup
        active={changeConfirmation}
        close={() => setChangeConfirmation(false)}
        title='Konfirmasi Hapus Data'
      >
        <div className='leading-7 text-gray_r-12/80'>
          Apakah anda yakin menghapus data?
        </div>
        <div className='flex mt-6 gap-x-4 md:justify-end'>
          <button
            className='btn-solid-red flex-1 md:flex-none'
            type='button'
            onClick={handleDeleteSupplier}
          >
            Ya, Hapus
          </button>
          <button
            className='btn-light flex-1 md:flex-none'
            type='button'
            onClick={() => setChangeConfirmation(false)}
          >
            Batal
          </button>
        </div>
      </BottomPopup>
      {isDesktop && (
        <div className='py-4'>
          <div className='flex flex-col justify-start'>
            <h1 className='font-bold text-2xl'>
              Referensi Supplier / Rekanan Bisnis Perusahaan{' '}
              <span className=' opacity-60 text-xl'>(Opsional)</span>
            </h1>
            <p className='opacity-60'>
              Data yang anda berikan hanya untuk bahan referensi internal kami
              untuk memberikan anda credit limit dan durasi tempo
            </p>
          </div>
          <form className='flex mt-4 flex-col w-full '>
            <table className='border' border='1' cellPadding='10'>
              <thead>
                <tr className='border '>
                  <th className='text-left px-5 py-2'>
                    Nama Supplier / Rekanan
                  </th>
                  <th className='text-left px-5 py-2'>PIC</th>
                  <th className='text-left px-5 py-2'>Telepon</th>
                  <th className='text-left px-5 py-2'>Durasi Tempo</th>
                  <th className='text-left px-5 py-2'>Credit Limit</th>
                </tr>
              </thead>
              <tbody>
                {supplierData.map((supplier, index) => (
                  <tr key={index}>
                    <td>
                      <input
                        name='supplier'
                        value={supplier.supplier}
                        type='text'
                        onChange={(e) => onChangeInput(e, index)}
                        className='form-input border px-4 py-2'
                        placeholder='Type Supplier'
                      />
                    </td>
                    <td>
                      <input
                        name='pic'
                        value={supplier.pic}
                        type='text'
                        className='form-input border px-4 py-2'
                        onChange={(e) => onChangeInput(e, index)}
                        placeholder='Type PIC'
                      />
                    </td>
                    <td>
                      <input
                        name='telepon'
                        type='text'
                        className='form-input border px-4 py-2'
                        value={supplier.telepon}
                        onChange={(e) => onChangeInput(e, index)}
                        placeholder='Type Telepon'
                      />
                    </td>
                    <td>
                      <input
                        name='durasiTempo'
                        type='text'
                        className='form-input border px-4 py-2'
                        value={formatHari(supplier.durasiTempo)}
                        onChange={(e) => onChangeInput(e, index)}
                        placeholder='Type Durasi Tempo'
                      />
                    </td>
                    <td className='flex flex-row gap-2 justify-center items-center'>
                      <input
                        name='creditLimit'
                        type='text'
                        value={formatRupiah(supplier.creditLimit)}
                        className='form-input border px-4 py-2'
                        onChange={(e) => onChangeInput(e, index)}
                        placeholder='Type Credit Limit'
                      />
                      <Trash2Icon
                        size={18}
                        onClick={() => handleOpenConfirmation(index)}
                        className='cursor-pointer'
                      />
                    </td>
                  </tr>
                ))}
                <tr>
                  <td>
                    <input
                      name='supplier'
                      value={newSupplier.supplier}
                      type='text'
                      className='form-input border px-4 py-2'
                      onChange={handleNewSupplierChange}
                      placeholder='Isi nama supplier anda'
                    />
                  </td>
                  <td>
                    <input
                      name='pic'
                      value={newSupplier.pic}
                      type='text'
                      className='form-input border px-4 py-2'
                      onChange={handleNewSupplierChange}
                      placeholder='Isi PIC supplier anda'
                    />
                  </td>
                  <td>
                    <input
                      name='telepon'
                      value={newSupplier.telepon}
                      type='text'
                      onChange={handleNewSupplierChange}
                      placeholder='Isi telepon supplier anda'
                      className='form-input border px-4 py-2'
                    />
                  </td>
                  <td>
                    <input
                      name='durasiTempo'
                      value={formatHari(newSupplier.durasiTempo)}
                      type='text'
                      onChange={handleNewSupplierChange}
                      className='form-input border px-4 py-2'
                      placeholder='Durasi jatuh tempo'
                    />
                  </td>
                  <td className='flex flex-row items-center gap-2 '>
                    <input
                      name='creditLimit'
                      value={formatRupiah(newSupplier.creditLimit)}
                      type='text'
                      className='form-input border px-4 py-2'
                      onChange={handleNewSupplierChange}
                      placeholder='limit kredit'
                    />
                    {/* <Trash2Icon
                      size={18}
                      onClick={() => handleDeleteSupplier(index)}
                    /> */}
                  </td>
                </tr>
              </tbody>
            </table>
          </form>
          <div className='flex items-center gap-4 mt-8'>
            {/* <button
              onClick={handleAddNewSupplier}
              className='bg-gray-200 border border-gray-500 rounded-md text-sm text-gray-500 p-2 h-11 mb-1 content-center flex flex-row justify-center items-center'
            >
              {<PlusCircleIcon className='w-5 mr-2' />}
              {''} Tambah Data Baru
            </button> */}
            {!hasSavedata && (
              <>
                <button
                  onClick={simpanData}
                  className={`bg-red-500 border border-red-500 rounded-md text-sm text-white p-2 h-11 mb-1 content-center flex flex-row justify-center items-center `}
                >
                  Simpan Data
                </button>
                <span className='text-sm opacity-60 text-red-500'>
                  *Klik simpan sebelum lanjut ke tahap selanjutnya
                </span>
              </>
            )}
          </div>
        </div>
      )}
      {isMobile && (
        <div className='text-sm'>
          <div className='flex flex-col py-4 mt-8 justify-start'>
            <h1 className='font-bold text-xl'>
              Referensi Supplier / Rekanan Bisnis Perusahaan{' '}
              <span className=' opacity-60 text-xl'>(Opsional)</span>
            </h1>
            <p className='opacity-60'>
              Data yang anda berikan hanya untuk bahan referensi internal kami
              untuk memberikan anda credit limit dan durasi tempo
            </p>
          </div>
          <div className='flex gap-4 flex-col'>
            <div className='h-[2px] bg-gray-300 w-[120%] inset-0 relative transform -translate-x-5'></div>
            <h2 className='py-2 font-semibold text-base'>
              Daftar Nama Supplier
            </h2>
            {/* <div className='h-[2px] bg-gray-300 w-[120%] inset-0 relative transform -translate-x-5'></div> */}
            <div className=''>
              {supplierData.map((supplier, index) => (
                <div key={index}>
                  <div
                    className='flex flex-row justify-center items-center py-4'
                    onClick={() => toggleOpen(index)}
                  >
                    <p className='font-semibold text-base w-4/5'>
                      {supplier.supplier}
                    </p>
                    <div className='w-1/5 flex justify-end items-center gap-2'>
                      <Trash2Icon
                        size={16}
                        color='red'
                        onClick={() => handleOpenConfirmation(index)}
                        className='cursor-pointer'
                      />
                      {openIndexes.includes(index) ? (
                        <ChevronUpIcon className='w-4' />
                      ) : (
                        <ChevronDownIcon className='w-4' />
                      )}
                    </div>
                  </div>
                  {openIndexes.includes(index) && (
                    <form className='flex flex-col w-full'>
                      <div className='w-full grid grid-row-2 gap-4'>
                        <div className='flex flex-row justify-start items-start'>
                          <div className='w-2/5'>
                            <label className='form-label text-nowrap'>
                              Nama Supplier
                            </label>
                          </div>
                          <div className='w-3/5 opacity-70'>
                            {supplier.supplier}
                          </div>
                        </div>

                        <div className='flex flex-row justify-start items-start'>
                          <div className='w-2/5'>
                            <label className='form-label text-nowrap'>
                              PIC
                            </label>
                          </div>
                          <div className='w-3/5 opacity-70'>{supplier.pic}</div>
                        </div>

                        <div className='flex flex-row justify-start items-start'>
                          <div className='w-2/5'>
                            <label className='form-label text-nowrap'>
                              Telepon
                            </label>
                          </div>
                          <div className='w-3/5 opacity-70'>
                            {supplier.telepon}
                          </div>
                        </div>

                        <div className='flex flex-row justify-start items-start'>
                          <div className='w-2/5'>
                            <label className='form-label text-nowrap'>
                              Durasi Tempo
                            </label>
                          </div>
                          <div className='w-3/5 opacity-70'>
                            {formatHari(supplier.durasiTempo)}
                          </div>
                        </div>

                        <div className='flex flex-row justify-start items-start'>
                          <div className='w-2/5'>
                            <label className='form-label text-nowrap'>
                              Kredit Limit
                            </label>
                          </div>
                          <div className='w-3/5 opacity-70'>
                            {formatRupiah(supplier.creditLimit)}
                          </div>
                        </div>
                      </div>
                    </form>
                  )}
                </div>
              ))}
            </div>
            {/* <div className='h-[2px] bg-gray-300 w-[120%] inset-0 relative transform -translate-x-5'></div> */}
            {/* <h2 className='py-2 font-semibold text-base text-red-500 flex flex-row'>
              <PlusCircleIcon className='w-5 mr-2' />
              Tambah Data Baru
            </h2> */}
            <div className='h-[2px] bg-gray-300 w-[120%] inset-0 relative transform -translate-x-5'></div>
            <form className='flex flex-col w-full'>
              <div className='w-full grid grid-row-2 gap-2'>
                <div className='flex flex-row justify-start items-start'>
                  <div className='w-2/5'>
                    <label className='form-label text-nowrap'>
                      Nama Supplier
                    </label>
                  </div>
                  <div className='w-3/5 opacity-70'>
                    <input
                      name='supplier'
                      value={newSupplier.supplier}
                      type='text'
                      className='form-input border px-4 py-2'
                      onChange={handleNewSupplierChange}
                      placeholder='Format: PT. ABC TESTING INDONESIA'
                    />
                  </div>
                </div>

                <div className='flex flex-row justify-start items-start'>
                  <div className='w-2/5'>
                    <label className='form-label text-nowrap'>PIC</label>
                  </div>
                  <div className='w-3/5 opacity-70'>
                    <input
                      name='pic'
                      value={newSupplier.pic}
                      type='text'
                      className='form-input border px-4 py-2'
                      onChange={handleNewSupplierChange}
                      placeholder='John Doe'
                    />
                  </div>
                </div>

                <div className='flex flex-row justify-start items-start'>
                  <div className='w-2/5'>
                    <label className='form-label text-nowrap'>Telepon</label>
                  </div>
                  <div className='w-3/5 opacity-70'>
                    <input
                      name='telepon'
                      value={newSupplier.telepon}
                      type='text'
                      onChange={handleNewSupplierChange}
                      placeholder='Format: 08123456789'
                      className='form-input border px-4 py-2'
                    />
                  </div>
                </div>

                <div className='flex flex-row justify-start items-start'>
                  <div className='w-2/5'>
                    <label className='form-label text-nowrap'>
                      Durasi Tempo
                    </label>
                  </div>
                  <div className='w-3/5 opacity-70'>
                    <input
                      name='durasiTempo'
                      value={formatHari(newSupplier.durasiTempo)}
                      type='text'
                      onChange={handleNewSupplierChange}
                      className='form-input border px-4 py-2'
                      placeholder='Isi durasi tempo supplier anda (hari)'
                    />
                  </div>
                </div>

                <div className='flex flex-row justify-start items-start'>
                  <div className='w-2/5'>
                    <label className='form-label text-nowrap'>
                      Kredit Limit
                    </label>
                  </div>
                  <div className='w-3/5 opacity-70'>
                    <input
                      name='creditLimit'
                      value={formatRupiah(newSupplier.creditLimit)}
                      type='text'
                      className='form-input border px-4 py-2'
                      onChange={handleNewSupplierChange}
                      placeholder='Rp 999.999.999'
                    />
                  </div>
                </div>
              </div>
            </form>
          </div>
          <div className='flex flex-col justify-start items-start gap-4 mt-8'>
            {!hasSavedata && (
              <>
                <span className='text-xs opacity-60 text-red-500'>
                  *Klik simpan sebelum lanjut ke tahap selanjutnya
                </span>
                <button
                  onClick={simpanData}
                  className={`bg-red-500 border border-red-500 rounded-md w-full text-sm text-white p-2 h-11 mb-1 content-center flex flex-row justify-center items-center`}
                >
                  Simpan Data
                </button>
              </>
            )}
          </div>
        </div>
      )}
    </>
  );
};

const validationSchema = Yup.object().shape({
  supplier: Yup.string().required('Harus di-isi'),
  pic: Yup.string().required('Harus di-isi'),
  telepon: Yup.string().required('Harus di-isi'),
  durasiTempo: Yup.string().required('Harus di-isi'),
  creditLimit: Yup.string().required('Harus di-isi'),
});

const defaultValues = {
  supplier: '',
  pic: '',
  telepon: '',
  durasiTempo: '',
  creditLimit: '',
};
export default Referensi;