import React, { useState, useEffect } from 'react'
import { usePrevious } from 'react-use'
import { Head, router } from '@inertiajs/react'
import { formatIDDate } from '@/utils'
import CustomerLayout from '@/Layouts/CustomerLayout'
import HeaderTrx from '../Components/HeaderTrx'
import { isEmpty } from 'lodash'
const EmptyHere = () => {
return (
Transaksi kosong
Yuk, checkout keranjang mu sekarang!
)
}
export default function Index(props) {
const {
query: { data, next_page_url },
_start_date,
_end_date,
} = props
const [sales, setSales] = useState(data)
const [dates, setDates] = useState({
startDate: _start_date,
endDate: _end_date,
})
const preValue = usePrevious(`${dates}`)
const handleNextPage = () => {
router.get(
next_page_url,
{
startDate: dates.startDate,
endDate: dates.endDate,
},
{
replace: true,
preserveState: true,
only: ['query'],
onSuccess: (res) => {
setSales(sales.concat(res.props.query.data))
},
}
)
}
useEffect(() => {
if (preValue) {
if (isEmpty(dates.endDate)) {
return
}
router.get(
route(route().current()),
{
startDate: dates.startDate,
endDate: dates.endDate,
},
{
replace: true,
preserveState: true,
only: ['query'],
onSuccess: (res) => {
setSales(res.props.query.data)
},
}
)
}
}, [dates])
return (
{sales.length <= 0 &&
}
{sales.length > 0 && (
{formatIDDate(dates.startDate)} s/d{' '}
{formatIDDate(dates.endDate)}
)}
{sales.map((sale) => (
router.get(
route('transactions.sale.show', sale.id)
)
}
>
{sale.format_human_created_at}
{sale.code}
))}
{next_page_url !== null && (
Load more
)}
)
}