Files
web/components/MyPages/Blocks/Points/EarnAndBurn/index.tsx
2024-05-23 15:25:08 +02:00

183 lines
5.4 KiB
TypeScript

"use client"
import { dt } from "@/lib/dt"
import { _ } from "@/lib/translation"
import { trpc } from "@/lib/trpc/client"
import Header from "@/components/MyPages/Blocks/Header"
import Button from "@/components/TempDesignSystem/Button"
import styles from "./earnAndBurn.module.css"
import { AccountPageComponentProps } from "@/types/components/myPages/myPage/accountPage"
import { Page, RowProps } from "@/types/components/myPages/myPage/earnAndBurn"
const tableHeadings = [
_("Arrival date"),
_("Description"),
_("Booking number"),
_("Transaction date"),
_("Points"),
]
function EarnAndBurn({
lang,
title,
subtitle,
link,
}: AccountPageComponentProps) {
const { data, hasNextPage, isFetching, fetchNextPage } =
trpc.user.transaction.friendTransactions.useInfiniteQuery(
{ limit: 5 },
{
getNextPageParam: (lastPage: Page) => lastPage.nextCursor,
}
)
function loadMoreData() {
if (hasNextPage) {
fetchNextPage()
}
}
const transactions = data?.pages.flatMap((page) => page.data) ?? []
return (
<div className={styles.container}>
<Header title={title} link={link} subtitle={subtitle} />
<div className={styles.mobileTableContainer}>
<table className={styles.mobileTable}>
<thead className={styles.mobileThead}>
<tr>
<th className={styles.mobileTh}>{_("Transactions")}</th>
<th className={styles.mobileTh}>{_("Points")}</th>
</tr>
</thead>
<tbody>
{transactions.length ? (
transactions.map((transaction) => (
<tr
className={styles.mobileTr}
key={transaction.confirmationNumber}
>
<td
className={`${styles.mobileTd} ${styles.mobileTransactionDetails}`}
>
<span className={styles.mobileTransactionDate}>
{dt(transaction.checkinDate)
.locale(lang)
.format("DD MMM YYYY")}
</span>
{transaction.hotelName && transaction.city ? (
<span>{`${transaction.hotelName}, ${transaction.city}`}</span>
) : null}
<span>
{`${transaction.nights} ${_(transaction.nights === 1 ? "night" : "nights")}`}
</span>
</td>
<td
className={`${styles.mobileTd} ${styles.mobileTransactionPoints}`}
>
{`${transaction.awardPoints} P`}
</td>
</tr>
))
) : (
<tr>
<td className={styles.mobilePlaceholder} colSpan={2}>
Empty
</td>
</tr>
)}
</tbody>
</table>
</div>
<div className={styles.tableContainer}>
{transactions.length ? (
<table className={styles.table}>
<thead className={styles.thead}>
<tr>
{tableHeadings.map((heading) => (
<th key={heading} className={styles.th}>
{heading}
</th>
))}
</tr>
</thead>
<tbody>
{transactions.map((transaction) => (
<Row
lang={lang}
key={transaction.confirmationNumber}
transaction={transaction}
/>
))}
</tbody>
</table>
) : (
// TODO: add once pagination is available through API
// <Button
// disabled={isFetching}
// intent="primary"
// bgcolor="white"
// type="button"
// onClick={loadMoreData}
// >
// {_("See more transactions")}
// </Button>
<table className={styles.table}>
<thead className={styles.thead}>
<tr>
{tableHeadings.map((heading) => (
<th key={heading} className={styles.th}>
{heading}
</th>
))}
</tr>
</thead>
<tbody>
<tr>
<td
colSpan={tableHeadings.length}
className={styles.placeholder}
>
{_("No transactions available")}
</td>
</tr>
</tbody>
</table>
)}
</div>
</div>
)
}
function Row({ transaction, lang }: RowProps) {
const description =
transaction.hotelName && transaction.city
? `${_(transaction.hotelName)}, ${transaction.city} ${transaction.nights} ${_("nights")}`
: `${transaction.nights} ${_("nights")}`
const arrival = dt(transaction.checkinDate).locale(lang).format("DD MMM YYYY")
const departure = dt(transaction.checkoutDate)
.locale(lang)
.format("DD MMM YYYY")
const values = [
arrival,
description,
transaction.confirmationNumber,
departure,
transaction.awardPoints,
]
return (
<tr className={styles.tr}>
{values.map((value, idx) => (
<td key={`value-${idx}`} className={styles.td}>
{value}
</td>
))}
</tr>
)
}
export default EarnAndBurn