2022-03-08 14:43:12 +01:00
|
|
|
import type { NextPage } from "next"
|
|
|
|
import styles from "../styles/home.module.css"
|
2022-03-09 16:00:36 +01:00
|
|
|
import { useState, useEffect, useCallback } from "react"
|
|
|
|
import Notifications from "../components/notifications"
|
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)
|
2022-03-09 16:00:36 +01:00
|
|
|
const [loaded, setLoaded] = useState(false)
|
2022-03-08 21:17:07 +01:00
|
|
|
|
2022-03-09 16:00:36 +01:00
|
|
|
const getSales = useCallback(async (page: Number = 1) => {
|
2022-03-08 21:17:07 +01:00
|
|
|
let url = "/api/sales"
|
|
|
|
if (page) {
|
|
|
|
url += `?page=${page}`
|
|
|
|
}
|
|
|
|
const response = await fetch(url)
|
|
|
|
if (!response.ok) {
|
2022-03-09 16:00:36 +01:00
|
|
|
const error = await response.text()
|
|
|
|
Notifications.error(error, "Failed to get sales")
|
|
|
|
return
|
2022-03-08 21:17:07 +01:00
|
|
|
}
|
|
|
|
const sales = await response.json()
|
|
|
|
// @ts-ignore
|
|
|
|
setCurrentPage(page)
|
|
|
|
return setSales(sales.data)
|
2022-03-09 16:00:36 +01:00
|
|
|
}, [])
|
2022-03-08 21:17:07 +01:00
|
|
|
|
2022-03-09 16:00:36 +01:00
|
|
|
const goToNextPage = useCallback(async () => {
|
2022-03-08 21:17:07 +01:00
|
|
|
await getSales(currentPage + 1)
|
2022-03-09 16:00:36 +01:00
|
|
|
}, [currentPage, getSales])
|
2022-03-08 21:17:07 +01:00
|
|
|
|
2022-03-09 16:00:36 +01:00
|
|
|
const goToPrevPage = useCallback(async () => {
|
2022-03-08 21:17:07 +01:00
|
|
|
if (currentPage > 1) {
|
|
|
|
await getSales(currentPage - 1)
|
|
|
|
}
|
2022-03-09 16:00:36 +01:00
|
|
|
}, [currentPage, getSales])
|
2022-03-08 21:17:07 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-03-09 16:00:36 +01:00
|
|
|
getSales().then(() => {
|
|
|
|
setLoaded(true)
|
|
|
|
}).catch(() => {
|
2022-03-08 21:17:07 +01:00
|
|
|
setSales([])
|
|
|
|
})
|
|
|
|
}, [])
|
|
|
|
|
2022-03-09 16:00:36 +01:00
|
|
|
if (!loaded) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:43:12 +01:00
|
|
|
return (
|
|
|
|
<div className={styles.container}>
|
2022-03-09 16:00:36 +01:00
|
|
|
<div className={styles.tableSection}>
|
|
|
|
<h1 className="subtitle">Sales</h1>
|
|
|
|
<div className={styles.table}>
|
|
|
|
<table className="table">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Sale ID</th>
|
|
|
|
<th>name</th>
|
|
|
|
<th>Sold by</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{sales.map((sale: any) =>
|
|
|
|
<tr key={sale.sale_id}>
|
|
|
|
<th>{sale.sale_id}</th>
|
|
|
|
<th>{sale.sale_name}</th>
|
2022-03-09 16:19:18 +01:00
|
|
|
<th>{sale.sales_person?.map((person: any) => person.primaryDisplay)[0]}</th>
|
2022-03-09 16:00:36 +01:00
|
|
|
</tr>
|
|
|
|
)}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
<div className={styles.buttons}>
|
|
|
|
<button className="button" onClick={goToPrevPage}>Prev Page</button>
|
|
|
|
<button className="button" onClick={goToNextPage}>Next Page</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-03-08 14:43:12 +01:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Home
|