From e234407a17ead33756628b6a4be7eeb06859bbec Mon Sep 17 00:00:00 2001 From: Ivan Rezchikov Date: Tue, 28 Jul 2026 17:05:45 +0300 Subject: [PATCH] temp --- app/account/account-menu.tsx | 7 +- .../positions/create-position-dialog.tsx | 92 ++++ app/account/positions/page.tsx | 58 +++ app/acts/[id]/approve-button.tsx | 20 + app/acts/[id]/page.tsx | 220 +++++++++ app/acts/[id]/veto-button.tsx | 20 + app/acts/create/client.tsx | 59 +++ app/acts/create/editor.tsx | 422 ++++++++++++++++++ app/acts/create/page.tsx | 30 ++ app/acts/page.tsx | 55 +++ app/news/page.tsx | 2 +- components/footer.tsx | 2 +- components/header.tsx | 2 +- components/identity-card.tsx | 2 +- components/mobile-nav.tsx | 2 +- {app/news => components}/searchBar.tsx | 0 lib/acts/actions.ts | 136 ++++++ lib/acts/flows.ts | 48 ++ lib/acts/repository.ts | 187 ++++++++ lib/acts/types.ts | 83 ++++ lib/acts/validation.ts | 48 ++ lib/citizenship/actions.ts | 13 +- lib/citizenship/repository.ts | 24 + lib/positions/actions.ts | 28 ++ lib/positions/repository.ts | 46 ++ lib/positions/types.ts | 14 + lib/prisma.ts | 7 +- prisma/schema.prisma | 320 ++++++++++--- public/emblems/DU.png | Bin 0 -> 87504 bytes public/{emblem.png => emblems/europe.png} | Bin 30 files changed, 1873 insertions(+), 74 deletions(-) create mode 100644 app/account/positions/create-position-dialog.tsx create mode 100644 app/account/positions/page.tsx create mode 100644 app/acts/[id]/approve-button.tsx create mode 100644 app/acts/[id]/page.tsx create mode 100644 app/acts/[id]/veto-button.tsx create mode 100644 app/acts/create/client.tsx create mode 100644 app/acts/create/editor.tsx create mode 100644 app/acts/create/page.tsx create mode 100644 app/acts/page.tsx rename {app/news => components}/searchBar.tsx (100%) create mode 100644 lib/acts/actions.ts create mode 100644 lib/acts/flows.ts create mode 100644 lib/acts/repository.ts create mode 100644 lib/acts/types.ts create mode 100644 lib/acts/validation.ts create mode 100644 lib/positions/actions.ts create mode 100644 lib/positions/repository.ts create mode 100644 lib/positions/types.ts create mode 100755 public/emblems/DU.png rename public/{emblem.png => emblems/europe.png} (100%) diff --git a/app/account/account-menu.tsx b/app/account/account-menu.tsx index 026fbaf..5c4f9bc 100644 --- a/app/account/account-menu.tsx +++ b/app/account/account-menu.tsx @@ -1,7 +1,7 @@ "use client"; import { cn } from "@/lib/utils"; -import { BookOpenTextIcon, UserIcon } from "lucide-react"; +import { BookOpenTextIcon, BriefcaseBusinessIcon, UserIcon } from "lucide-react"; import Link from "next/link"; import { usePathname } from "next/navigation"; @@ -15,6 +15,11 @@ const menuItems = [ href: "/account/citizenship", label: "Citizenship", icon: BookOpenTextIcon, + }, + { + href: "/account/positions", + label: "Positions", + icon: BriefcaseBusinessIcon, } ]; diff --git a/app/account/positions/create-position-dialog.tsx b/app/account/positions/create-position-dialog.tsx new file mode 100644 index 0000000..b8a34f9 --- /dev/null +++ b/app/account/positions/create-position-dialog.tsx @@ -0,0 +1,92 @@ +"use client"; + +import { + Dialog, DialogClose, DialogContent, + DialogFooter, DialogHeader, DialogTitle, DialogTrigger, +} from "@/components/ui/dialog"; +import { Button } from "@/components/ui/button"; +import { Spinner } from "@/components/ui/spinner"; +import { createPosition } from "@/lib/positions/actions"; +import { Input } from "@/components/ui/input"; +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; +import { startTransition, useActionState, useEffect, useState } from "react"; +import { initialFormState } from "@/lib/form-action"; +import { Field, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field"; +import { Institution } from "@/lib/acts/types"; +import { toast } from "sonner"; + +export function CreatePositionDialog({ institutions }: { institutions: Institution[] }) { + const [state, formAction, isPending] = useActionState(createPosition, initialFormState); + const [isOpen, setIsOpen] = useState(false); + const [name, setName] = useState(""); + const [institutionId, setInstitutionId] = useState(""); + + const handleSubmit = () => { + const formData = new FormData(); + formData.set("name", name); + formData.set("institutionId", institutionId); + + startTransition(() => formAction(formData)); + }; + + useEffect(() => { + if (state.success) { + setIsOpen(false); + setName(""); + setInstitutionId(""); + toast("Position successfully created") + } + }, [state.success]); + + return ( + + + + + + +

Create a new position

+
+ + + Position name + setName(e.target.value)} + /> + {state.fieldErrors?.name && {state.fieldErrors.name}} + + + Institution + + {state.fieldErrors?.institutionId && {state.fieldErrors.institutionId}} + + + {state.error && {state.error}} + + + + + + +
+
+ ); +} \ No newline at end of file diff --git a/app/account/positions/page.tsx b/app/account/positions/page.tsx new file mode 100644 index 0000000..d99198e --- /dev/null +++ b/app/account/positions/page.tsx @@ -0,0 +1,58 @@ +import { getCurrentUser, getUsers } from "@/lib/user"; +import { redirect } from "next/navigation"; +import { AccountMenu } from "../account-menu"; +import { Metadata } from "next"; +import { getUserPositions } from "@/lib/positions/repository"; +import { Button } from "@/components/ui/button"; +import { CreatePositionDialog } from "./create-position-dialog"; +import { getInstitutions } from "@/lib/acts/repository"; +import { formatDate } from "@/lib/utils"; +import Link from "next/link"; + +export const metadata: Metadata = { + title: "Positions", + robots: { + index: false, + follow: false, + }, +}; + +export default async function Account() { + const user = await getCurrentUser(); + if (!user) redirect("/"); + + const userPositions = await getUserPositions(user.id); + const institutions = await getInstitutions(); + + return ( +
+ +
+
+
+

Positions

+

Here are your positions in the European Federation

+
+ {userPositions.length === 0 ?

You do not have any positions

: +
+ {userPositions.map((position) => ( +
+

{position.position.name}

+

Assigned on {formatDate(position.assignedAt)} by {position.act?.actType.name} {position.act?.numberInSeries && `№${position.act.numberInSeries}/${position.act.numberYear}`}

+
+ ))} +
+ } + {user.isAdmin && ( +
+ +

+ To assign or dismiss someone from a position, create an appointment act. +

+
+ )} +
+
+
+ ); +} \ No newline at end of file diff --git a/app/acts/[id]/approve-button.tsx b/app/acts/[id]/approve-button.tsx new file mode 100644 index 0000000..9e6af5b --- /dev/null +++ b/app/acts/[id]/approve-button.tsx @@ -0,0 +1,20 @@ +"use client"; + +import { Button } from "@/components/ui/button"; +import { Spinner } from "@/components/ui/spinner"; +import { useAction } from "@/hooks/useAction"; +import { approveActAction } from "@/lib/acts/actions"; +import { CheckIcon } from "lucide-react"; + +export function ApproveButton({ actId }: { actId: string }) { + const { run, isPending } = useAction(approveActAction, { + successMessage: "Approved", + }); + + return ( + + ); +} \ No newline at end of file diff --git a/app/acts/[id]/page.tsx b/app/acts/[id]/page.tsx new file mode 100644 index 0000000..5bf42ff --- /dev/null +++ b/app/acts/[id]/page.tsx @@ -0,0 +1,220 @@ +import { Badge } from "@/components/ui/badge"; +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; +import { + ArticleContent, + PreambleContent, + LEGAL_STATUS_LABELS, +} from "@/lib/acts/types"; +import { + getActById, + getActTypeById, + getInstitutionById, + getActVersions, + getCurrentActVersion, + getActBlocks, + getActApprovals, +} from "@/lib/acts/repository"; +import { getPositions, getCitizenshipPositions } from "@/lib/positions/repository"; +import { getActiveCitizenship } from "@/lib/citizenship/repository"; +import { getActFlow, getNextPhase } from "@/lib/acts/flows"; +import { ApproveButton } from "./approve-button"; +import { VetoButton } from "./veto-button"; +import { getCurrentUser, getUsers } from "@/lib/user"; +import { cn, formatDate } from "@/lib/utils"; +import { Metadata } from "next"; +import { Merriweather } from "next/font/google"; +import Image from "next/image"; +import Link from "next/link"; +import { notFound } from "next/navigation"; + +const documentText = Merriweather({ weight: "400" }); +const documentHeading = Merriweather({ weight: "700" }); + +export async function generateMetadata({ params }: { params: Promise<{ id: string }> }): Promise { + const { id } = await params; + + const act = await getActById(id); + if (!act) return {}; + + const type = await getActTypeById(act.actTypeId); + + return { + title: `${type?.name ?? ""}${act.title ? ` “${act.title}”` : ""}`, + }; +} + +export default async function Act({ params }: { params: Promise<{ id: string }> }) { + const { id } = await params; + const user = await getCurrentUser(); + + const act = await getActById(id); + if (!act) notFound(); + + const type = await getActTypeById(act.actTypeId); + const institution = await getInstitutionById(act.issuingInstitutionId); + const [author] = await getUsers(act.authorId); + const versions = await getActVersions(id); + const version = await getCurrentActVersion(id); + if (!version) notFound(); + + const blocks = await getActBlocks(version.id); + const needsRegion = institution?.kind === "REGION"; + const preamble = blocks.find((b) => b.type === "PREAMBLE"); + const preambleContent = preamble?.content as PreambleContent | undefined; + + const approvals = await getActApprovals(version.id); + const positions = await getPositions(); + const positionNames = new Map(positions.map((p) => [p.id, p.name])); + + const flow = getActFlow(type?.code ?? ""); + const phase = getNextPhase(flow, approvals); + const stageLabel = phase?.label ?? "Completed"; + + let canApprove = false; + let canVeto = false; + + if (user && act.legalStatus === "DRAFT" && phase) { + const citizenship = await getActiveCitizenship(user.id); + if (citizenship) { + const citizenshipPositions = await getCitizenshipPositions(citizenship.id); + const holds = citizenshipPositions.some( + (p) => !p.resignedAt && p.position.capabilities.some((c) => c.capability === phase.capability) + ); + canApprove = holds; + canVeto = holds && !!phase.allowVeto; + } + } + + return ( +
+
+
+ + + +

+ {type?.name} {act.title && `“${act.title}”`}{" "} + {act.numberInSeries ? `№${act.numberInSeries}/${act.numberYear}` : without a number} +

+
+
+ +
+

Issuing institution

+

{institution?.name}

+
+
+

Author

+

{author?.firstName} {author?.secondName}

+
+
+

Created

+

{formatDate(act.createdAt)}

+
+
+

Version from

+

{formatDate(version.createdAt)}

+
+
+

Process stage

+

{stageLabel}

+
+
+

Legal status

+ {act.legalStatus === "NOT_ADOPTED" ? {LEGAL_STATUS_LABELS[act.legalStatus]} :

{LEGAL_STATUS_LABELS[act.legalStatus]}

} +
+
+
+ + +

History

+
+ +
+ {versions.map((v) => + v.id === version.id ? ( +
+

Version from {formatDate(v.createdAt)}

+ Current +
+ ) : ( + +

Version from {formatDate(v.createdAt)}

+ Outdated + + ) + )} +
+
+
+ { (canApprove || canVeto) && ( + + +

Signature

+

You are able to sign or reject this act

+
+ + {canApprove && } + {canVeto && } + +
+ )} +
+
+
+ Emblem of the European Federation +
+

{!needsRegion ? "European Federation" : "Region"}

+

{institution?.name}

+
+

+ {type?.name} {act.numberInSeries && `№${act.numberInSeries}/${act.numberYear}`} {act.title && `“${act.title}”`} +

+
+
+ {preambleContent && ( +

{preambleContent.text}

+ )} + {blocks + .filter((b) => b.type === "ARTICLE") + .map((block, index) => { + const article = block.content as ArticleContent; + return ( +
+
+

+ Article {index + 1}{article.title && `. ${article.title}.`} +

+
+ {article.intro &&

{article.intro}

} +
+ {article.clauses.map((clause, clauseIndex) => ( +

+ {article.markerStyle === "NUMBERS" && `${clauseIndex + 1}. `} + {article.markerStyle === "LETTERS" && `${String.fromCharCode(97 + clauseIndex)}) `} + {clause.kind === "APPOINTMENT" + ? `${clause.action === "ASSIGN" ? "Appoint" : "Dismiss"} ${clause.citizenshipName} ${clause.action === "ASSIGN" ? "to" : "from"} ${positionNames.get(clause.positionId) ?? "an unknown position"}.` + : clause.text} +

+ ))} +
+
+ ); + })} +
+
+ {approvals.map((a) => ( +
+

{a.name}

+

{a.role}

+

+ {a.decision === "APPROVE" ? "Approved" : "Vetoed"} at {formatDate(a.decidedAt)} +

+
+ ))} +
+
+
+
+ ); +} \ No newline at end of file diff --git a/app/acts/[id]/veto-button.tsx b/app/acts/[id]/veto-button.tsx new file mode 100644 index 0000000..35af5f2 --- /dev/null +++ b/app/acts/[id]/veto-button.tsx @@ -0,0 +1,20 @@ +"use client"; + +import { Button } from "@/components/ui/button"; +import { Spinner } from "@/components/ui/spinner"; +import { useAction } from "@/hooks/useAction"; +import { vetoActAction } from "@/lib/acts/actions"; +import { XIcon } from "lucide-react"; + +export function VetoButton({ actId, vetoLabel }: { actId: string, vetoLabel: string }) { + const { run, isPending } = useAction(vetoActAction, { + successMessage: "Vetoed", + }); + + return ( + + ); +} \ No newline at end of file diff --git a/app/acts/create/client.tsx b/app/acts/create/client.tsx new file mode 100644 index 0000000..f2900c2 --- /dev/null +++ b/app/acts/create/client.tsx @@ -0,0 +1,59 @@ +"use client"; + +import { useRef } from "react"; +import { useRouter } from "next/navigation"; +import { Button } from "@/components/ui/button"; +import ActEditor from "./editor"; +import { saveActAction } from "@/lib/acts/actions"; +import { Spinner } from "@/components/ui/spinner"; +import { useAction } from "@/hooks/useAction"; +import { ActEditorData } from "@/lib/acts/types"; + +export default function CreateActClient({ actTypes, institutions, regions, positions }: { + actTypes: { id: string; name: string; allowedIssuerKind: string }[]; + institutions: { id: string; name: string; kind: string; appointedByInstitutionId: string | null }[]; + regions: { id: string; name: string }[]; + positions: { id: string; name: string; institutionId: string }[]; +}) { + const router = useRouter(); + + const { run, isPending } = useAction(saveActAction, { + successMessage: "Act saved", + onSuccess: () => router.push("/acts"), + }); + + const dataRef = useRef({ + actTypeId: "", + issuingInstitutionId: "", + title: "", + preambleText: "", + regionId: null, + articles: [], + }); + + function handleSave() { + const data = dataRef.current; + run(data); + } + + return ( +
+
+

Create act

+
+ +
+
+ { dataRef.current = data; }} + /> +
+ ); +} \ No newline at end of file diff --git a/app/acts/create/editor.tsx b/app/acts/create/editor.tsx new file mode 100644 index 0000000..6d9fd0d --- /dev/null +++ b/app/acts/create/editor.tsx @@ -0,0 +1,422 @@ +"use client"; + +import { useState, useEffect, useRef } from "react"; +import { v4 as uuidv4 } from "uuid"; +import { Button } from "@/components/ui/button"; +import { Field, FieldError, FieldGroup, FieldLabel, FieldSet } from "@/components/ui/field"; +import { InputGroup, InputGroupInput } from "@/components/ui/input-group"; +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; +import { PlusIcon, XIcon, ChevronDownIcon, ChevronUpIcon, Trash2Icon, ListOrderedIcon, CaseLowerIcon, UserCogIcon } from "lucide-react"; +import { cn } from "@/lib/utils"; +import Image from "next/image"; +import { Merriweather } from "next/font/google"; +import { ActEditorData, ActType, ArticleContent, Clause, ClauseMarkerStyle, Institution, PositionOption, Region } from "@/lib/acts/types"; +import { searchCitizensAction } from "@/lib/citizenship/actions"; + +const documentText = Merriweather({ weight: "400" }); +const documentHeading = Merriweather({ weight: "700" }); + +type ActEditorProps = { + actTypes: ActType[]; + institutions: Institution[]; + regions: Region[]; + positions: PositionOption[]; + initial?: Partial; + onChange: (data: ActEditorData) => void; + errors?: { title?: string[]; issuingInstitutionId?: string[] }; +}; + +const LETTER_MARKERS = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"]; + +function computeMarker(style: ClauseMarkerStyle, index: number): string | null { + if (style === "NONE") return null; + if (style === "LETTERS") return `${LETTER_MARKERS[index] ?? index + 1})`; + return `${index + 1}.`; +} + +function makeEmptyTextClause(): Clause { + return { id: uuidv4(), kind: "TEXT", text: "" }; +} + +function makeEmptyAppointmentClause(): Clause { + return { id: uuidv4(), kind: "APPOINTMENT", action: "ASSIGN", positionId: "", citizenshipId: "", citizenshipName: "" }; +} + +function makeEmptyArticle(): ArticleContent { + return { id: uuidv4(), title: "", markerStyle: "NONE", clauses: [makeEmptyTextClause()] }; +} + +export default function ActEditor({ actTypes, institutions, regions, positions, initial, onChange, errors }: ActEditorProps) { + const [issuingInstitutionId, setIssuingInstitutionId] = useState(initial?.issuingInstitutionId ?? ""); + const [actTypeId, setActTypeId] = useState(initial?.actTypeId ?? ""); + const [regionId, setRegionId] = useState(initial?.regionId ?? ""); + const [title, setTitle] = useState(initial?.title ?? ""); + const [preambleText, setPreambleText] = useState(initial?.preambleText ?? ""); + const [articles, setArticles] = useState(initial?.articles ?? [makeEmptyArticle()]); + + const onChangeRef = useRef(onChange); + useEffect(() => { onChangeRef.current = onChange; }); + + useEffect(() => { + onChangeRef.current({ actTypeId, issuingInstitutionId, regionId, title, preambleText, articles }); + }, [actTypeId, issuingInstitutionId, regionId, title, preambleText, articles]); + + const appointablePositions = positions.filter((p) => { + const owner = institutions.find((i) => i.id === p.institutionId); + const requiredIssuerId = owner?.appointedByInstitutionId ?? owner?.id; + return requiredIssuerId === issuingInstitutionId; + }); + + const selectedInstitution = institutions.find((i) => i.id === issuingInstitutionId); + const availableActTypes = selectedInstitution + ? actTypes.filter((t) => t.allowedIssuerKind === selectedInstitution.kind) + : []; + const needsRegion = selectedInstitution?.kind === "REGION"; + + function updateArticle(id: string, patch: Partial) { + setArticles(articles.map((a) => (a.id === id ? { ...a, ...patch } : a))); + } + + function addArticle() { + setArticles([...articles, makeEmptyArticle()]); + } + + function removeArticle(id: string) { + setArticles(articles.filter((a) => a.id !== id)); + } + + function moveArticle(from: number, to: number) { + const updated = [...articles]; + const [moved] = updated.splice(from, 1); + updated.splice(to, 0, moved); + setArticles(updated); + } + + return ( +
+
+

Meta

+ + + Issuing institution + + {errors?.issuingInstitutionId && ({ message: m }))} />} + + + + Act type + + + + {needsRegion && ( + + Region + + + )} + + + Title + + setTitle(e.target.value)} + /> + + {errors?.title && ({ message: m }))} />} + + +
+ +
+
+ Emblem of the European Federation +
+

{!needsRegion ? "European Federation" : regions.find((region) => region.id === regionId)?.name}

+

{institutions.find((i) => i.id === issuingInstitutionId)?.name}

+
+

+ Draft of a {actTypes.find((a) => a.id === actTypeId)?.name} {title && `“${title}”`} +

+
+ + setPreambleText(e.target.value)} + /> +
+ {articles.map((article, index) => ( +
+
+ updateArticle(article.id, patch)} + /> +
+
+ + + {articles.length > 1 && ( + + )} +
+
+ ))} + +
+
+
+ ); +} + +function ArticleEditor({ + content, + articleIndex, + appointablePositions, + onChange, +}: { + content: ArticleContent; + articleIndex: number; + appointablePositions: PositionOption[]; + onChange: (patch: Partial) => void; +}) { + const clauses = content.clauses ?? [makeEmptyTextClause()]; + + function updateClause(id: string, patch: Partial) { + onChange({ clauses: clauses.map((c) => (c.id === id ? { ...c, ...patch } as Clause : c)) }); + } + + function addClause(afterIndex: number, clause: Clause) { + const updated = [...clauses]; + updated.splice(afterIndex + 1, 0, clause); + onChange({ clauses: updated }); + } + + function removeClause(id: string) { + const updated = clauses.filter((c) => c.id !== id); + onChange({ clauses: updated.length ? updated : [makeEmptyTextClause()] }); + } + + return ( +
+
+

Article {articleIndex + 1}

+ onChange({ title: e.target.value })} + /> + +
+ + {content.markerStyle !== "NONE" && ( + onChange({ intro: e.target.value })} + /> + )} + +
+ {clauses.map((clause, i) => { + const marker = computeMarker(content.markerStyle, i); + return ( +
+ {marker && {marker}} + {clause.kind === "APPOINTMENT" ? ( + updateClause(clause.id, patch)} + /> + ) : ( + updateClause(clause.id, { text: e.target.value })} + /> + )} +
+ + + {clauses.length > 1 && ( + + )} +
+
+ ); + })} +
+
+ ); +} + +// Renders an appointment clause inline as a fill-in-the-blank sentence: +// "[Appoint|Dismiss] [citizen] [to|from] [position]." +function AppointmentClauseEditor({ + clause, + positions, + onChange, +}: { + clause: Extract; + positions: PositionOption[]; + onChange: (patch: Partial) => void; +}) { + return ( +
+ + onChange({ citizenshipId: c?.id ?? "", citizenshipName: c?.name ?? "" })} + /> + {clause.action === "ASSIGN" ? "to" : "from"} + + . +
+ ); +} + +function CitizenshipPicker({ + value, + onSelect, +}: { + value: { id: string; name: string } | null; + onSelect: (value: { id: string; name: string } | null) => void; +}) { + const [query, setQuery] = useState(""); + const [results, setResults] = useState<{ id: string; firstName: string; secondName: string }[]>([]); + + useEffect(() => { + if (!query.trim()) { setResults([]); return; } + const timeout = setTimeout(async () => { + const result = await searchCitizensAction(query); + if (result.success) setResults(result.data); + }, 250); + return () => clearTimeout(timeout); + }, [query]); + + if (value) { + return ( + + {value.name} + + + ); + } + + return ( + + setQuery(e.target.value)} + /> + {results.length > 0 && ( +
+ {results.map((c) => ( + + ))} +
+ )} +
+ ); +} + +function AutoResizeTextarea({ value, onChange, className, placeholder }: { value: string; onChange: (e: React.ChangeEvent) => void; className?: string; placeholder?: string }) { + const ref = useRef(null); + + useEffect(() => { + if (ref.current) { + ref.current.style.height = "auto"; + ref.current.style.height = `${ref.current.scrollHeight}px`; + } + }, [value]); + + return