summaryrefslogtreecommitdiff
path: root/src-migrate/modules/product-detail/components/Information.tsx
blob: 5d70e5345ae4603388afe591a74e0d44bfa84a31 (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
import style from '../styles/information.module.css';
import {
  AutoComplete,
  AutoCompleteInput,
  AutoCompleteItem,
  AutoCompleteList,
} from '@choc-ui/chakra-autocomplete';

import React, { useEffect, useState } from 'react';
import dynamic from 'next/dynamic';
import Link from 'next/link';
import { useQuery } from 'react-query';

import { IProductDetail } from '~/types/product';
import { IProductVariantSLA } from '~/types/productVariant';
import { createSlug } from '~/libs/slug';
import { getVariantSLA } from '~/services/productVariant';
import { formatToShortText } from '~/libs/formatNumber';
import currencyFormat from '@/core/utils/currencyFormat';
import { Icon, InputGroup, InputRightElement } from '@chakra-ui/react';
import { CheckIcon, ChevronDownIcon, FingerPrintIcon } from '@heroicons/react/24/outline';
import { useProductDetail } from '../stores/useProductDetail';

const Skeleton = dynamic(() =>
  import('@chakra-ui/react').then((mod) => mod.Skeleton)
);

type Props = {
  product: IProductDetail;
};

const Information = ({ product }: Props) => {
  const { selectedVariant, setSelectedVariant, setSla, setActive, } = useProductDetail()
  const variantOptions = product?.variants;

  const querySLA = useQuery<IProductVariantSLA>({
    queryKey: ['variant-sla', selectedVariant?.id],
    queryFn: () => getVariantSLA(selectedVariant?.id),
    enabled: selectedVariant?.id === 1,
  });
  const sla = querySLA?.data;

  useEffect(() => {
    setSla(querySLA?.data);
  }, [selectedVariant]);

  const handleOnChange = (vals: any) => {
    let code = vals.split(" ")[0];
    let variant = variantOptions.find((item) => item.code === code);
    setSelectedVariant(variant);
  }

  return (
    <div className={style['wrapper']}>
      <div className='realtive mb-5'>
        <label className='form-label mb-2 text-lg'>Pilih Variant * : <span className='text-gray_r-9 text-sm'>{product?.variant_total} Variants</span>  </label>
        <AutoComplete openOnFocus className='form-input' onChange={vals => handleOnChange(vals)}>
          <InputGroup>
            <AutoCompleteInput value={selectedVariant?.code + ' - ' + selectedVariant?.attributes[0]} />
            <InputRightElement>
              <ChevronDownIcon className='h-6 w-6 text-gray-500' />
            </InputRightElement>
          </InputGroup>

          <AutoCompleteList>
            {variantOptions.map((option, cid) => (
              <AutoCompleteItem
                key={`option-${cid}`}
                value={option.code + ' - ' + option.attributes[0]}
                textTransform='capitalize'
              >
                <div className='flex gap-x-2 justify-between w-full'>
                  <div className='text-small'>
                    {option.code + ' - ' + option.attributes[0]}
                  </div>
                  <div className='grid grid-cols-3 items-start'>
                    <div className='badge-solid-red text-xs'>
                      {Math.floor(option?.price?.discount_percentage)}%
                    </div>
                    <div className='text-gray_r-11 line-through text-[11px] sm:text-caption-2'>
                      {currencyFormat(option?.price?.price)}
                    </div>
                    <div className='text-danger-500 font-semibold mb-2'>
                      {currencyFormat(option?.price?.price_discount)}
                    </div>
                  </div>
                </div>
              </AutoCompleteItem>
            ))}
          </AutoCompleteList>
        </AutoComplete>
      </div>
      <div className={style['row']}>
        <div className={style['label']}>Item Code</div>
        <div className={style['value']}>{selectedVariant?.code}</div>
      </div>
      <div className={style['row']}>
        <div className={style['label']}>Manufacture</div>
        <div className={style['value']}>
          {!!product.manufacture.name ? (
            <Link
              href={createSlug(
                '/shop/brands/',
                product.manufacture.name,
                product.manufacture.id.toString()
              )}
              className='text-danger-500 hover:underline'
            >
              {product.manufacture.name}
            </Link>
          ) : (
            '-'
          )}
        </div>
      </div>
      <div className={style['row']}>
        <div className={style['label']}>Terjual</div>
        <div className={style['value']}>
          {product.qty_sold > 0 ? formatToShortText(product.qty_sold) : '-'}
        </div>
      </div>
      <div className={style['row']}>
        <div className={style['label']}>Persiapan Barang</div>
        <div className={style['value']}>{sla?.sla_date}</div>
      </div>
    </div>
  );
};

export default Information;