add: news search bar, application history
This commit is contained in:
@@ -10,8 +10,8 @@ import { Textarea } from "@/components/ui/textarea";
|
||||
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
||||
import { approveApplication, rejectApplication } from "@/lib/citizenship/actions";
|
||||
import { CitizenshipApplicationWithWarning } from "@/lib/citizenship/types";
|
||||
import { formatDate } from "@/lib/utils";
|
||||
import { TriangleAlertIcon } from "lucide-react";
|
||||
import { cn, formatDate } from "@/lib/utils";
|
||||
import { CheckIcon, ClockFadingIcon, PlusIcon, TriangleAlertIcon, XIcon } from "lucide-react";
|
||||
import { useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
@@ -36,7 +36,6 @@ export default function ApplicationCard({ application, onClose }: { application:
|
||||
setIsLoading(false)
|
||||
if (result.success) {
|
||||
toast.success("Application approved")
|
||||
onClose()
|
||||
} else {
|
||||
toast.error(result.error ?? "Failed to approve application")
|
||||
}
|
||||
@@ -50,7 +49,6 @@ export default function ApplicationCard({ application, onClose }: { application:
|
||||
if (result.success) {
|
||||
toast.success("Application rejected")
|
||||
setRejectOpen(false)
|
||||
onClose()
|
||||
} else {
|
||||
toast.error(result.error ?? "Failed to reject application")
|
||||
}
|
||||
@@ -64,14 +62,14 @@ export default function ApplicationCard({ application, onClose }: { application:
|
||||
<CardDescription>{application.applicationType === "PREVIOUS_CITIZENSHIP" ? "Previous citizenship application" : "Naturalization application"}</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="max-w-sm">
|
||||
<div className="flex flex-row gap-3 mb-4">
|
||||
{application.status !== "REJECTED" && (<div className="flex flex-row gap-3 mb-4">
|
||||
<div className="w-20 aspect-4/5 rounded-md overflow-hidden ring-1 ring-foreground/10 shrink-0">
|
||||
<img src={application.profileUrl} alt="Profile photo" className="w-full h-full object-cover" />
|
||||
</div>
|
||||
<div className="w-40 px-2 aspect-5/2 rounded-md overflow-hidden ring-1 ring-foreground/10 shrink-0 bg-white">
|
||||
<img src={application.signatureUrl} alt="Signature" className="w-full h-full object-contain" />
|
||||
</div>
|
||||
</div>
|
||||
</div>)}
|
||||
<div className="flex flex-col gap-1">
|
||||
<h4 className="mb-1">Personal information</h4>
|
||||
{ application.nameMismatch && (
|
||||
@@ -147,17 +145,51 @@ export default function ApplicationCard({ application, onClose }: { application:
|
||||
</div>
|
||||
)
|
||||
}
|
||||
<Separator className="my-2" />
|
||||
<div className="flex flex-col gap-1">
|
||||
<h4 className="mb-1">Application history</h4>
|
||||
</div>
|
||||
<div className="flex flex-col gap-1">
|
||||
<div className="flex flex-row gap-2 items-center text-muted-foreground">
|
||||
<PlusIcon size={16} /><p>Created on {formatDate(application.createdAt)}</p>
|
||||
</div>
|
||||
{application.status !== "PENDING" && (<div className="flex flex-row gap-2 items-center text-muted-foreground">
|
||||
<ClockFadingIcon size={16} /><p>{application.reviewedAt ? `Reviewed on ${formatDate(application.reviewedAt)} by ${application.reviewer?.firstName} ${application.reviewer?.secondName}` : "Reviewed"}</p>
|
||||
</div>)}
|
||||
{application.status === "REJECTED" && (
|
||||
<div className="flex flex-col gap-0.5">
|
||||
<div className="flex flex-row gap-2 items-center text-destructive">
|
||||
<XIcon size={16} /><p>Rejected</p>
|
||||
</div>
|
||||
<div className="flex flex-col">
|
||||
<p>Rejection reason</p>
|
||||
<p className="text-muted-foreground">{application.rejectionReason}</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{application.status === "APPROVED" && (
|
||||
<div className="flex flex-row gap-2 items-center">
|
||||
<CheckIcon size={16} /><p>Approved</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
<CardFooter className="gap-2">
|
||||
<Button onClick={handleApprove} disabled={isloading}>
|
||||
{isloading && <Spinner />}Approve
|
||||
</Button>
|
||||
<Button variant="outline" onClick={() => setRejectOpen(true)} disabled={isloading}>
|
||||
{isloading && <Spinner />}Reject
|
||||
</Button>
|
||||
<Button variant="outline" onClick={onClose}>Close</Button>
|
||||
</CardFooter>
|
||||
{application.status === "PENDING" ? (
|
||||
<CardFooter className="gap-2">
|
||||
<Button onClick={handleApprove} disabled={isloading}>
|
||||
{isloading && <Spinner />}Approve
|
||||
</Button>
|
||||
<Button variant="outline" onClick={() => setRejectOpen(true)} disabled={isloading}>
|
||||
{isloading && <Spinner />}Reject
|
||||
</Button>
|
||||
<Button variant="outline" onClick={onClose}>Close</Button>
|
||||
</CardFooter>
|
||||
) : (
|
||||
<CardFooter>
|
||||
<Button variant="outline" onClick={onClose}>Close</Button>
|
||||
</CardFooter>
|
||||
)}
|
||||
</Card>
|
||||
<Dialog open={rejectOpen} onOpenChange={setRejectOpen}>
|
||||
<DialogContent>
|
||||
|
||||
@@ -5,6 +5,14 @@ import { CitizenshipApplication } from "@/lib/citizenship/types";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { formatDate } from "@/lib/utils";
|
||||
|
||||
type BadgeVariant = "default" | "outline" | "destructive";
|
||||
|
||||
const variants: Record<string, BadgeVariant> = {
|
||||
PENDING: "outline",
|
||||
REJECTED: "destructive",
|
||||
APPROVED: "default",
|
||||
}
|
||||
|
||||
export const columns: ColumnDef<CitizenshipApplication>[] = [
|
||||
{
|
||||
accessorKey: "surname",
|
||||
@@ -31,7 +39,7 @@ export const columns: ColumnDef<CitizenshipApplication>[] = [
|
||||
header: "Status",
|
||||
cell: ({ row }) => {
|
||||
const status = row.getValue("status") as string
|
||||
return <Badge variant={status === "PENDING" ? "outline" : "default"}>{status}</Badge>
|
||||
return <Badge variant={variants[status] ?? "default"}>{status}</Badge>
|
||||
},
|
||||
},
|
||||
]
|
||||
@@ -2,9 +2,10 @@ import ReviewPageClient from "./client";
|
||||
import { getCurrentUser } from "@/lib/user";
|
||||
import { redirect } from "next/navigation";
|
||||
import { AccountMenu } from "../../account-menu";
|
||||
import { getPendingApplications } from "@/lib/citizenship/repository";
|
||||
import { getAllApplications } from "@/lib/citizenship/repository";
|
||||
import { getImageUrl } from "@/lib/image";
|
||||
import { Metadata } from "next";
|
||||
import { findUserById } from "@/lib/user/repository";
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "Review citizenship applications",
|
||||
@@ -18,21 +19,31 @@ export default async function ReviewPage() {
|
||||
const user = await getCurrentUser();
|
||||
if (!user?.isAdmin) redirect("/account/citizenship");
|
||||
|
||||
const applications = await getPendingApplications();
|
||||
const applications = await getAllApplications();
|
||||
|
||||
const applicationsWithWarning = await Promise.all(
|
||||
applications.map(async (application) => ({
|
||||
...application,
|
||||
profileUrl: await getImageUrl(application.profileKey),
|
||||
signatureUrl: await getImageUrl(application.signatureKey),
|
||||
applications.map(async (application) => {
|
||||
const reviewerUser = application.reviewedBy
|
||||
? await findUserById(application.reviewedBy)
|
||||
: null;
|
||||
|
||||
nameMismatch:
|
||||
application.givenNames !== application.user.firstName ||
|
||||
application.surname !== application.user.secondName,
|
||||
return {
|
||||
...application,
|
||||
profileUrl: await getImageUrl(application.profileKey),
|
||||
signatureUrl: await getImageUrl(application.signatureKey),
|
||||
|
||||
previousCitizenship:
|
||||
application.user._count.citizenships > 0,
|
||||
}))
|
||||
nameMismatch:
|
||||
application.givenNames !== application.user.firstName ||
|
||||
application.surname !== application.user.secondName,
|
||||
|
||||
previousCitizenship:
|
||||
application.user._count.citizenships > 0,
|
||||
|
||||
reviewer: reviewerUser
|
||||
? { firstName: reviewerUser.firstName, secondName: reviewerUser.secondName }
|
||||
: null,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return (
|
||||
|
||||
+18
-7
@@ -5,13 +5,23 @@ import { getPages } from "@/lib/pages/repository";
|
||||
import { getCurrentUser } from "@/lib/user";
|
||||
import { Metadata } from "next";
|
||||
import Link from "next/link";
|
||||
import SearchBar from "./searchBar";
|
||||
|
||||
export const metadata: Metadata = { title: "News" };
|
||||
|
||||
export default async function News() {
|
||||
export default async function News({ searchParams }: { searchParams: Promise<{ q?: string }> }) {
|
||||
const { q } = await searchParams;
|
||||
const user = await getCurrentUser();
|
||||
const news = await getPages("NEWS", "PUBLISHED");
|
||||
|
||||
const query = q?.trim().toLowerCase() ?? "";
|
||||
|
||||
const filteredNews = query
|
||||
? news.filter((n) =>
|
||||
`${n.title.toLowerCase()} ${n.meta.tags?.join(" ").toLowerCase() ?? ""}`.includes(query)
|
||||
)
|
||||
: news;
|
||||
|
||||
const newsDrafts = await getPages("NEWS", "DRAFT");
|
||||
const pageDrafts = await getPages("PAGE", "DRAFT");
|
||||
|
||||
@@ -21,6 +31,7 @@ export default async function News() {
|
||||
<div className="flex flex-col p-4 md:p-16 gap-8 md:gap-16 mb-4 md:mb-0">
|
||||
<div className="flex flex-col gap-4 md:flex-row md:items-center justify-between">
|
||||
<h1>News</h1>
|
||||
<SearchBar />
|
||||
<div className="flex flex-row items-center gap-4">
|
||||
{user?.isAdmin && (
|
||||
<div className="flex flex-row items-center gap-4">
|
||||
@@ -31,12 +42,12 @@ export default async function News() {
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-col gap-6">
|
||||
{
|
||||
news.length > 0 ? news.map((n) => (
|
||||
<NewsCard news={n} key={n.id} />
|
||||
)) : <h2 className="text-muted-foreground">There are no news yet...</h2>
|
||||
}
|
||||
{
|
||||
filteredNews.length > 0 ? filteredNews.map((n) => (
|
||||
<NewsCard news={n} key={n.id} />
|
||||
)) : <h2 className="text-muted-foreground">There are no news yet...</h2>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
"use client"
|
||||
|
||||
import { SearchIcon } from "lucide-react";
|
||||
import { InputGroup, InputGroupAddon, InputGroupInput } from "@/components/ui/input-group";
|
||||
import { Field } from "@/components/ui/field";
|
||||
import { usePathname, useRouter, useSearchParams } from "next/navigation";
|
||||
import { useState, useTransition } from "react";
|
||||
|
||||
export default function SearchBar() {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const searchParams = useSearchParams();
|
||||
const [value, setValue] = useState(searchParams.get("q") ?? "");
|
||||
const [, startTransition] = useTransition();
|
||||
|
||||
function handleChange(next: string) {
|
||||
setValue(next);
|
||||
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
if (next) {
|
||||
params.set("q", next);
|
||||
} else {
|
||||
params.delete("q");
|
||||
}
|
||||
|
||||
startTransition(() => {
|
||||
router.replace(`${pathname}?${params.toString()}`, { scroll: false });
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<Field className="max-w-sm">
|
||||
<InputGroup>
|
||||
<InputGroupInput
|
||||
placeholder="Search..."
|
||||
value={value}
|
||||
onChange={(e) => handleChange(e.target.value)}
|
||||
/>
|
||||
<InputGroupAddon align="inline-start">
|
||||
<SearchIcon className="text-muted-foreground" />
|
||||
</InputGroupAddon>
|
||||
</InputGroup>
|
||||
</Field>
|
||||
)
|
||||
}
|
||||
@@ -77,9 +77,8 @@ export async function getUserApplications(userId: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function getPendingApplications() {
|
||||
export async function getAllApplications() {
|
||||
return prisma.citizenshipApplication.findMany({
|
||||
where: { status: "PENDING" },
|
||||
orderBy: { createdAt: "desc" },
|
||||
include: {
|
||||
user: {
|
||||
|
||||
@@ -43,4 +43,5 @@ export type CitizenshipApplicationWithWarning = CitizenshipApplication & {
|
||||
previousCitizenship: boolean;
|
||||
profileUrl: string;
|
||||
signatureUrl: string;
|
||||
};
|
||||
reviewer: { firstName: string; secondName: string } | null;
|
||||
};
|
||||
Reference in New Issue
Block a user