'use client'; import { CheckCircleIcon, Cog8ToothIcon, PlusCircleIcon, ShareIcon, BanknotesIcon, EyeIcon, TicketIcon, ShoppingCartIcon, EnvelopeIcon, ExclamationTriangleIcon, ClockIcon } from "@heroicons/react/24/outline"; import { FC } from "react"; import { BillBadge } from "./BillBadge"; import { BillingLocation, EmailStatus } from "../lib/db-types"; import { formatYearMonth } from "../lib/format"; import { formatCurrency } from "../lib/formatStrings"; import Link from "next/link"; import { useLocale, useTranslations } from "next-intl"; import { toast } from "react-toastify"; import { generateShareLink } from "../lib/actions/locationActions"; export interface LocationCardProps { location: BillingLocation; currency?: string | null; } export const LocationCard: FC = ({ location, currency }) => { const { _id, name, yearMonth, bills, seenByTenantAt, // NOTE: only the fileName is projected from the DB to reduce data transfer utilBillsProofOfPayment, tenantEmail, tenantEmailStatus, } = location; const t = useTranslations("home-page.location-card"); // sum all the unpaid and paid bill amounts (regardless of who pays) const totalUnpaid = bills.reduce((acc, bill) => !bill.paid ? acc + (bill.payedAmount ?? 0) : acc, 0); const totalPayed = bills.reduce((acc, bill) => bill.paid ? acc + (bill.payedAmount ?? 0) : acc, 0); const handleCopyLinkClick = async () => { // copy URL to clipboard const shareLink = await generateShareLink(_id); if(shareLink.error) { toast.error(shareLink.error, { theme: "dark" }); } else { navigator.clipboard.writeText(shareLink.shareUrl as string); toast.success(t("link-copy-message"), { theme: "dark" }); } } return (

{formatYearMonth(yearMonth)} {name}

{ bills.length > 0 ? (
{ bills.map(bill => ) }
) : null }
{t("add-bill-button-tooltip")}
{ totalUnpaid > 0 || totalPayed > 0 || seenByTenantAt || utilBillsProofOfPayment?.uploadedAt ? <>
{ totalUnpaid > 0 ?
{t("total-due-label")} {formatCurrency(totalUnpaid, currency ?? "EUR")}
: null } { totalPayed > 0 ?
{t("total-payed-label")} {formatCurrency(totalPayed, currency ?? "EUR")}
: null } {tenantEmail && tenantEmailStatus && tenantEmailStatus !== EmailStatus.Verified && (
{tenantEmailStatus === EmailStatus.Unverified && } {tenantEmailStatus === EmailStatus.VerificationPending && } {tenantEmailStatus === EmailStatus.Unsubscribed && } {tenantEmailStatus === EmailStatus.Unverified && `${t("email-status.unverified")} ⚠️`} {tenantEmailStatus === EmailStatus.VerificationPending && `${t("email-status.verification-pending")} ⏳`} {tenantEmailStatus === EmailStatus.Unsubscribed && `${t("email-status.unsubscribed")} ✉️`}
)} {seenByTenantAt && (
{t("seen-by-tenant-label")} at {seenByTenantAt.toLocaleString()}
)} {utilBillsProofOfPayment?.uploadedAt && ( {t("download-proof-of-payment-label")} )}
: null }
); };