import type { NextPage } from "next" import styles from "../styles/home.module.css" import { useState, useEffect, useCallback } from "react" import Notifications from "../components/notifications" const Home: NextPage = () => { const [sales, setSales] = useState([]) const [currentPage, setCurrentPage] = useState(1) const [loaded, setLoaded] = useState(false) const getSales = useCallback(async (page: Number = 1) => { let url = "/api/sales" if (page) { url += `?page=${page}` } const response = await fetch(url) if (!response.ok) { const error = await response.text() Notifications.error(error, "Failed to get sales") return } const sales = await response.json() // @ts-ignore setCurrentPage(page) return setSales(sales.data) }, []) const goToNextPage = useCallback(async () => { await getSales(currentPage + 1) }, [currentPage, getSales]) const goToPrevPage = useCallback(async () => { if (currentPage > 1) { await getSales(currentPage - 1) } }, [currentPage, getSales]) useEffect(() => { getSales().then(() => { setLoaded(true) }).catch(() => { setSales([]) }) }, []) if (!loaded) { return null } return (
Sale ID | name | Sold by |
---|---|---|
{sale.sale_id} | {sale.sale_name} | {sale.sales_person?.map((person: any) => person.primaryDisplay)[0]} |