api-getter
213 строк · 4.6 Кб
1'use client'
2
3import {
4Table,
5TableBody,
6TableCell,
7TableHead,
8TableHeader,
9TableRow,
10} from '@/components/ui/Table'
11import {
12flexRender,
13getCoreRowModel,
14useReactTable,
15} from '@tanstack/react-table'
16import axios from 'axios'
17import { useEffect, useState } from 'react'
18
19const columns = [
20{
21accessorKey: 'code',
22header: 'Код',
23},
24{
25accessorKey: 'createDate',
26header: 'Дата Создания',
27},
28{
29accessorKey: 'client',
30header: 'Клиент',
31},
32{
33accessorKey: 'name',
34header: 'Наименование',
35},
36{
37accessorKey: 'stage',
38header: 'Стадия',
39},
40{
41accessorKey: 'price',
42header: 'Сумма',
43},
44{
45accessorKey: 'currency',
46header: 'Валюта',
47},
48]
49
50export type data = {
51code: string
52createDate: string
53client: string
54name: string
55stage: string
56price: number
57currency: string
58}
59
60export default function Home() {
61const [data, setData] = useState<data[]>([
62{
63code: '',
64createDate: '',
65client: '',
66name: '',
67stage: '',
68price: 0,
69currency: '',
70},
71])
72const [messageSent, setMessageSent] = useState<boolean>(false)
73const [isPending, setIsPending] = useState<boolean>(false)
74const table = useReactTable({
75data: data,
76columns,
77getCoreRowModel: getCoreRowModel(),
78enableColumnPinning: true,
79
80state: {
81columnPinning: {
82right: ['status'],
83},
84},
85})
86
87const getData = async () => {
88const response = await axios.get(
89'http://localhost:3001/api/v1/deals/all-deals'
90)
91setData(convertDate(response.data))
92}
93
94const convertDate = (deals: data[]) => {
95let temp = deals
96for (let i = 0; i < temp.length; i++) {
97temp[i].createDate = temp[i].createDate.split('T').join(' ')
98}
99return temp
100}
101
102const sendEmail = async () => {
103setIsPending(true)
104const response = await axios.post(
105'http://localhost:3001/api/v1/deals/send-email'
106)
107if (response.status == 201) {
108setMessageSent(true)
109setIsPending(false)
110}
111}
112
113useEffect(() => {
114getData()
115}, [])
116
117return (
118<>
119<main className='flex min-h-screen flex-col items-center justify-between bg-slate-500 h-full'>
120{data.length <= 1 ? (
121<img src='./owl.gif' alt='' />
122) : (
123<>
124<Table className='bg-slate-500'>
125<TableHeader className='sticky top-0 bg-white'>
126{table.getHeaderGroups().map(headerGroup => (
127<TableRow key={headerGroup.id}>
128{headerGroup.headers.map(header => {
129return (
130<TableHead
131key={header.id}
132className={
133header.column.getIsPinned()
134? 'sticky z-20 right-0 bg-gradient-to-r from-transparent to-[10%] to-black-default'
135: ''
136}
137>
138{header.isPlaceholder
139? null
140: flexRender(
141header.column.columnDef.header,
142header.getContext()
143)}
144</TableHead>
145)
146})}
147</TableRow>
148))}
149</TableHeader>
150<TableBody className='max-h-[500px] overflow-hidden'>
151{table.getRowModel().rows?.length ? (
152table.getRowModel().rows.map(row => (
153<TableRow
154key={row.id}
155data-state={row.getIsSelected() && 'selected'}
156>
157{row.getVisibleCells().map(cell => (
158<TableCell
159key={cell.id}
160className={
161cell.column.getIsPinned()
162? 'sticky z-10 right-0'
163: ''
164}
165>
166<div
167className={`flex items-center justify-center text-white min-w-[70px] min-h-[28px] md:min-w-[80px] md:min-h-[35px] py-2 rounded-[6px]`}
168>
169{flexRender(
170cell.column.columnDef.cell,
171cell.getContext()
172)}
173</div>
174</TableCell>
175))}
176</TableRow>
177))
178) : (
179<TableRow>
180<TableCell
181colSpan={columns.length}
182className='h-24 text-center'
183>
184No results.
185</TableCell>
186</TableRow>
187)}
188</TableBody>
189</Table>
190<div className='m-24'>
191{messageSent ? (
192<button
193disabled
194className='rounded-[6px] p-4 bg-green-600 text-white transition-all'
195>
196Сообщение отправлено
197</button>
198) : (
199<button
200onClick={sendEmail}
201disabled={isPending}
202className='rounded-[6px] p-4 bg-[#9fa8da] text-white hover:bg-[#868db8] transition-all'
203>
204Отправить на почту
205</button>
206)}
207</div>
208</>
209)}
210</main>
211</>
212)
213}
214