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
|
"use client";
import { useResultStore } from "@/common/stores/useResultStore";
import { StockOpnameRes } from "@/common/types/stockOpname";
import { Button, Dropdown, DropdownItem, DropdownMenu, DropdownTrigger, Pagination, Skeleton, Spacer, Spinner } from "@nextui-org/react"
import { keepPreviousData, useQuery } from "@tanstack/react-query";
import styles from "./table.module.css"
import clsxm from "@/common/libs/clsxm";
import DetailRow from "./DetailRow";
import { useDebounce } from "usehooks-ts";
import getClientCredential from "@/common/libs/getClientCredential";
import { SHOWING_SELECTIONS } from "@/common/constants/result";
import { useEffect, useState } from "react";
import moment from "moment";
import { MoreVerticalIcon } from "lucide-react";
import toast from "@/common/libs/toast";
import { Product } from "prisma/generated/client";
const Table = () => {
const credential = getClientCredential()
const { filter: { company, search, show, page }, updateFilter } = useResultStore()
const debouncedSearch = useDebounce(search, 500)
useEffect(() => {
updateFilter('page', 1)
}, [company, debouncedSearch, show, updateFilter])
const stockOpnames = useQuery({
queryKey: ['stockOpnames', company, debouncedSearch, page, show],
queryFn: async () => {
const searchParams = new URLSearchParams()
if (!company) return null
searchParams.set('companyId', company)
const showValue = SHOWING_SELECTIONS.find((item) => item.key === show)?.value || ''
searchParams.set('show', showValue);
if (debouncedSearch) searchParams.set('search', debouncedSearch)
searchParams.set('page', page.toString());
return await fetch(`/api/stock-opname?${searchParams}`)
.then(res => {
window.scrollTo({ top: 0, 'behavior': 'smooth' })
return res.json()
})
},
placeholderData: keepPreviousData
})
const { filter } = useResultStore()
const [exportLoad, setExportLoad] = useState<boolean>(false)
const exportResult = async () => {
setExportLoad(true)
const response = await fetch(`/api/stock-opname/export?companyId=${filter.company}`)
const buffer = await response.arrayBuffer()
console.log({ type: response.headers.get('type') });
const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
const url = window.URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `export_${moment().format('MM_DD_HH_ss')}.xlsx`
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
setExportLoad(false)
}
const toggleDifferent = async (id: number) => {
const response = await fetch(`/api/product/${id}/toggle-different`, { method: 'POST' })
const product: Product = await response.json()
toast(`Berhasil mengubah status barang ${product.itemCode} ${product.name} menjadi ${product.isDifferent ? 'selisih' : 'aman'}`, { duration: 10000 })
stockOpnames.refetch()
}
const isLoading = stockOpnames.isLoading || stockOpnames.isRefetching
const COL_LENGTH = 9
return (
<>
<div className="flex">
<Button type="button" onPress={exportResult} disabled={exportLoad} variant="flat" className="ml-auto mb-4">
{exportLoad ? <><Spinner size="sm" />Exporting...</> : 'Export'}
</Button>
</div>
<div className="w-full flex-1 overflow-auto pb-4">
<table className="w-full">
<thead className={styles.thead}>
<th className={styles.th}>STATUS</th>
<th className={clsxm(styles.th, '!text-left')}>NAMA PRODUK</th>
<th className={styles.th}>TIM HITUNG 1</th>
<th className={styles.th}>TIM HITUNG 2</th>
<th className={styles.th}>TIM HITUNG 3</th>
<th className={styles.th}>TIM VERIFIKASI</th>
<th className={styles.th}>ON-HAND QTY</th>
<th className={styles.th}>GUDANG SELISIH</th>
<th className={styles.th}></th>
</thead>
<tbody className={styles.tbody}>
{!isLoading && stockOpnames.data?.result.map((stockOpname: StockOpnameRes['result']) => (
<>
<tr key={stockOpname.id} className={clsxm("border-t border-neutral-200", {
"text-danger-600": stockOpname.isDifferent,
"text-success-600": !stockOpname.isDifferent
})}
>
<td className={styles.td}>
<div className={clsxm("w-full rounded-lg mr-1 p-1 text-xs text-white whitespace-nowrap", {
"bg-danger-600": stockOpname.isDifferent,
"bg-success-600": !stockOpname.isDifferent,
})}>
{stockOpname.isDifferent ? 'Selisih' : 'Aman'}
</div>
</td>
<td className={clsxm(styles.td, '!text-left flex min-w-[250px]')} aria-label="name">
{stockOpname.itemCode ? `[${stockOpname.itemCode}] ` : ''}
{stockOpname.name}
{stockOpname.barcode ? ` [${stockOpname.barcode}]` : ''}
</td>
<td className={styles.td}>
{credential?.team == 'VERIFICATION' && typeof stockOpname.quantity.COUNT1 === 'number' ? stockOpname.quantity.COUNT1 : '-'}
</td>
<td className={styles.td}>
{credential?.team == 'VERIFICATION' && typeof stockOpname.quantity.COUNT2 === 'number' ? stockOpname.quantity.COUNT2 : '-'}
</td>
<td className={styles.td}>
{credential?.team == 'VERIFICATION' && typeof stockOpname.quantity.COUNT3 === 'number' ? stockOpname.quantity.COUNT3 : '-'}
</td>
<td className={styles.td}>
{credential?.team == 'VERIFICATION' && typeof stockOpname.quantity.VERIFICATION === 'number' ? stockOpname.quantity.VERIFICATION : '-'}
</td>
<td className={styles.td}>
{credential?.team == 'VERIFICATION' ? stockOpname.onhandQty : '-'}
</td>
<td className={styles.td}>
{credential?.team == 'VERIFICATION' ? stockOpname.differenceQty : '-'}
</td>
<td>
{credential?.team == 'VERIFICATION' && (
<Dropdown>
<DropdownTrigger>
<Button variant="light" className="p-1 min-w-fit">
<MoreVerticalIcon size={16} />
</Button>
</DropdownTrigger>
<DropdownMenu>
<DropdownItem key="toggleDifferent" onPress={() => toggleDifferent(stockOpname.id)}>
Tandai {stockOpname.isDifferent ? 'aman' : 'selisih'}
</DropdownItem>
</DropdownMenu>
</Dropdown>
)}
</td>
</tr>
<DetailRow productId={stockOpname.id} />
</>
))}
{!isLoading && stockOpnames.data?.result.length === 0 && (
<tr>
<td colSpan={COL_LENGTH} className="text-center text-neutral-600 py-4">Belum ada data untuk ditampilkan</td>
</tr>
)}
{isLoading && (
<tr>
<td colSpan={COL_LENGTH}>
<div className="flex flex-col gap-y-2.5">
{Array.from({ length: 10 }, (_, i) => (
<Skeleton className="h-16" key={i} />
))}
</div>
</td>
</tr>
)}
</tbody>
</table>
</div>
<Spacer y={4} />
<Pagination
page={stockOpnames.data?.page || 1}
total={stockOpnames.data?.totalPage || 1}
onChange={(page) => updateFilter('page', page)}
className="min-h-[36px] m-0 p-0"
/>
</>
)
}
export default Table
|