2022-03-08 14:43:12 +01:00
|
|
|
import type { NextPage } from "next"
|
|
|
|
import styles from "../styles/home.module.css"
|
2022-03-08 21:17:07 +01:00
|
|
|
import { useState, useEffect } from "react"
|
2022-03-08 14:43:12 +01:00
|
|
|
|
|
|
|
const Home: NextPage = () => {
|
2022-03-08 21:17:07 +01:00
|
|
|
const [sales, setSales] = useState([])
|
|
|
|
const [currentPage, setCurrentPage] = useState(1)
|
|
|
|
|
|
|
|
const getSales = async (page: Number = 1) => {
|
|
|
|
let url = "/api/sales"
|
|
|
|
if (page) {
|
|
|
|
url += `?page=${page}`
|
|
|
|
}
|
|
|
|
const response = await fetch(url)
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(await response.text())
|
|
|
|
}
|
|
|
|
const sales = await response.json()
|
|
|
|
// @ts-ignore
|
|
|
|
setCurrentPage(page)
|
|
|
|
return setSales(sales.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
const saveSale = async () => {
|
|
|
|
const response = await fetch("/api/sales", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({}),
|
|
|
|
})
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(await response.text())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const goToNextPage = async () => {
|
|
|
|
await getSales(currentPage + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
const goToPrevPage = async () => {
|
|
|
|
if (currentPage > 1) {
|
|
|
|
await getSales(currentPage - 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getSales().catch(() => {
|
|
|
|
setSales([])
|
|
|
|
})
|
|
|
|
}, [])
|
|
|
|
|
2022-03-08 14:43:12 +01:00
|
|
|
return (
|
|
|
|
<div className={styles.container}>
|
2022-03-08 21:18:11 +01:00
|
|
|
<h1 className="subtitle">Sales</h1>
|
2022-03-08 21:17:07 +01:00
|
|
|
<div>{sales.map((sale: any) => <p key={sale.sale_id}>{sale.sale_id}</p>)}</div>
|
|
|
|
<button onClick={goToPrevPage}>Prev Page</button>
|
|
|
|
<button onClick={goToNextPage}>Next Page</button>
|
2022-03-08 14:43:12 +01:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Home
|