Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 18 additions & 33 deletions app/(pages)/admin/donations/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,13 @@ place holder list
*/

const headCells = [
{
id: "type",
numeric: false,
label: "Donation Type",
},
{
id: "amount",
numeric: true,
label: "Donation Amount / Item(s) Value",
},
{
id: "item",
numeric: false,
label: "Item",
},
{
id: "paymentMethod",
numeric: false,
label: "Method",
},
{
id: "date",
numeric: false,
label: "Date",
},
{ id: "donorType", numeric: false, label: "Donor Type" },
{ id: "donor", numeric: false, label: "Donor" },
{ id: "amount", numeric: true, label: "Amount" },
{ id: "date", numeric: false, label: "Date" },
{ id: "campaign", numeric: false, label: "Campaign" },
{ id: "paymentMethod", numeric: false, label: "Method" },
{ id: "type", numeric: false, label: "Type" },
];
export const TableHeader = () => {
return (
Expand Down Expand Up @@ -93,21 +75,24 @@ export default function DonationsList() {
<TableHeader />
<TableBody>
{data.map((donation) => {
const donorName = donation?.donor?.person
? `${donation.donor.person.firstName} ${donation.donor.person.lastName}`
: donation?.donor?.organization?.name || "—";
const amount = typeof donation.amount === "number" ? donation.amount : Number(donation.amount ?? 0);
const donorType = donation?.donor?.type || "";
return (
<TableRow hover key={donation.id}>
<TableCell sx={styles.tableCell}>
<Link className="text-blue-500" href={`/admin/donations/detail/${donation.id}`}>
{donation.type}
{donorType}
</Link>
</TableCell>
<TableCell sx={styles.tableCell} align="right">
${donation.amount}
</TableCell>
<TableCell sx={styles.tableCell}>{donation.type !== "In-Kind" ? "" : donation.item}</TableCell>
<TableCell sx={styles.tableCell}>
{donation.type !== "In-Kind" ? donation.paymentMethod : ""}
</TableCell>
<TableCell sx={styles.tableCell}>{donorName}</TableCell>
<TableCell sx={styles.tableCell} align="right">${amount.toFixed(2)}</TableCell>
<TableCell sx={styles.tableCell}>{new Date(donation.date).toLocaleDateString()}</TableCell>
<TableCell sx={styles.tableCell}>{donation.campaign || ""}</TableCell>
<TableCell sx={styles.tableCell}>{donation.type !== "In-Kind" ? (donation.paymentMethod || "") : ""}</TableCell>
<TableCell sx={styles.tableCell}>{donation.type}</TableCell>
</TableRow>
);
})}
Expand Down
47 changes: 29 additions & 18 deletions app/(pages)/admin/donors/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import React, { useState, useEffect } from "react";
import { Box, Table, TableBody, TableCell, TableContainer, TableHead, TableRow } from "@mui/material";
import Link from "next/link";
import { Donor } from "@prisma/client";
import type { Donor as PrismaDonor } from "@prisma/client";
import { useRouter } from "next/navigation";
import Loading from "@/app/loading";

Expand All @@ -11,21 +11,13 @@ place holder list
*/

const headCells = [
{
id: "type",
numeric: false,
label: "Donor Type",
},
{
id: "commPref",
numeric: false,
label: "Communication Preference",
},
{
id: "status",
numeric: false,
label: "Status",
},
{ id: "type", numeric: false, label: "Donor Type" },
{ id: "name", numeric: false, label: "Name" },
{ id: "email", numeric: false, label: "Email" },
{ id: "phone", numeric: false, label: "Phone" },
{ id: "total", numeric: true, label: "Total Donated" },
{ id: "last", numeric: false, label: "Last Donation" },
{ id: "status", numeric: false, label: "Status" },
];
export const TableHeader = () => {
return (
Expand All @@ -41,8 +33,14 @@ export const TableHeader = () => {
);
};

type DonorWithRelations = PrismaDonor & {
person?: { firstName: string; lastName: string; emailAddress: string; phoneNumber: string | null } | null;
organization?: { name: string; emailAddress: string | null } | null;
donation?: Array<{ amount: number; date: string }>;
};

export default function DonorsList() {
const [data, setData] = useState<Donor[]>([]);
const [data, setData] = useState<DonorWithRelations[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(true);

const router = useRouter();
Expand Down Expand Up @@ -83,14 +81,27 @@ export default function DonorsList() {
<TableHeader />
<TableBody>
{data.map((donor) => {
const name = donor.person
? `${donor.person.firstName} ${donor.person.lastName}`
: donor.organization?.name || "—";
const email = donor.person?.emailAddress || donor.organization?.emailAddress || "";
const phone = donor.person?.phoneNumber || "";
const total = (donor.donation || []).reduce((sum, d) => sum + (Number(d.amount) || 0), 0);
const lastDateIso = donor.donation && donor.donation[0]?.date;
const last = lastDateIso ? new Date(lastDateIso).toLocaleDateString() : "";

return (
<TableRow hover key={donor.id}>
<TableCell sx={styles.tableCell}>
<Link className="text-blue-500" href={`/admin/donors/detail/${donor.id}`}>
{donor.type}
</Link>
</TableCell>
<TableCell sx={styles.tableCell}>{donor.communicationPreference}</TableCell>
<TableCell sx={styles.tableCell}>{name}</TableCell>
<TableCell sx={styles.tableCell}>{email}</TableCell>
<TableCell sx={styles.tableCell}>{phone}</TableCell>
<TableCell sx={styles.tableCell} align="right">${total.toFixed(2)}</TableCell>
<TableCell sx={styles.tableCell}>{last}</TableCell>
<TableCell sx={styles.tableCell}>{donor.status}</TableCell>
</TableRow>
);
Expand Down
12 changes: 11 additions & 1 deletion app/api/admin/donations/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,17 @@ export async function POST(req: NextRequest) {
// Read
export async function GET() {
try {
const data = await prisma.donation.findMany();
const data = await prisma.donation.findMany({
include: {
donor: {
include: {
person: { select: { firstName: true, lastName: true } },
organization: { select: { name: true } },
},
},
},
orderBy: { date: "desc" },
});

return NextResponse.json({ message: "Successful fetch", data: data }, { status: 200 });
} catch (error) {
Expand Down
26 changes: 25 additions & 1 deletion app/api/admin/donors/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,31 @@ export async function POST(req: NextRequest) {
// Read
export async function GET() {
try {
const data = await prisma.donor.findMany();
const data = await prisma.donor.findMany({
include: {
person: {
select: {
firstName: true,
lastName: true,
emailAddress: true,
phoneNumber: true,
},
},
organization: {
select: {
name: true,
emailAddress: true,
},
},
donation: {
select: {
amount: true,
date: true,
},
orderBy: { date: "desc" },
},
},
});

return NextResponse.json({ message: "Successful fetch", data: data }, { status: 200 });
} catch (error) {
Expand Down
Loading