From ee69817b7ad9adfda42568bcbe8aacaf2c606e23 Mon Sep 17 00:00:00 2001 From: meglerhagen Date: Thu, 27 Jun 2024 08:53:31 +0200 Subject: [PATCH] two tables done in new kontrat --- apps/www/src/actions/get-tenant-details.ts | 18 +- .../(tenant)/tenant/[id]/contract/page.tsx | 2 - .../_components/BuildingFormContract.tsx | 245 +++++++++++++++ .../_components/ReusableFormTemplate.tsx | 295 ++++++++++++++++++ .../tenant/[id]/contract2/building/page.tsx | 58 ++++ .../(tenant)/tenant/[id]/contract2/layout.tsx | 65 ++++ .../(tenant)/tenant/[id]/contract2/page.tsx | 51 +++ .../tenant/_components/TenantDetailsForm.tsx | 197 ++++++++++++ .../tenant/[id]/contract2/tenant/page.tsx | 56 ++++ .../src/app/(tenant)/tenant/[id]/layout.tsx | 5 + 10 files changed, 988 insertions(+), 4 deletions(-) create mode 100644 apps/www/src/app/(tenant)/tenant/[id]/contract2/building/_components/BuildingFormContract.tsx create mode 100644 apps/www/src/app/(tenant)/tenant/[id]/contract2/building/_components/ReusableFormTemplate.tsx create mode 100644 apps/www/src/app/(tenant)/tenant/[id]/contract2/building/page.tsx create mode 100644 apps/www/src/app/(tenant)/tenant/[id]/contract2/layout.tsx create mode 100644 apps/www/src/app/(tenant)/tenant/[id]/contract2/page.tsx create mode 100644 apps/www/src/app/(tenant)/tenant/[id]/contract2/tenant/_components/TenantDetailsForm.tsx create mode 100644 apps/www/src/app/(tenant)/tenant/[id]/contract2/tenant/page.tsx diff --git a/apps/www/src/actions/get-tenant-details.ts b/apps/www/src/actions/get-tenant-details.ts index f983dcb..03f6455 100644 --- a/apps/www/src/actions/get-tenant-details.ts +++ b/apps/www/src/actions/get-tenant-details.ts @@ -3,7 +3,7 @@ import { prisma } from "@/lib/db" import { getCurrentUser } from "@/lib/session" -export async function getTenantDetails(tenantId: number) { +export async function getTenantDetails(tenantId) { const user = await getCurrentUser() const userId = user?.id @@ -71,7 +71,21 @@ export async function getTenantDetails(tenantId: number) { return null } - return tenant + // Fetch available floors and office spaces + const floors = await prisma.floor.findMany({ + where: { + buildingId: building.id, + }, + include: { + officeSpaces: { + where: { + isRented: false, + }, + }, + }, + }) + + return { ...tenant, floors } } catch (error) { console.error("Error fetching tenant details:", error) return null diff --git a/apps/www/src/app/(tenant)/tenant/[id]/contract/page.tsx b/apps/www/src/app/(tenant)/tenant/[id]/contract/page.tsx index 8b9249d..870b7c9 100644 --- a/apps/www/src/app/(tenant)/tenant/[id]/contract/page.tsx +++ b/apps/www/src/app/(tenant)/tenant/[id]/contract/page.tsx @@ -4,8 +4,6 @@ import { getTenantDetails } from "@/actions/get-tenant-details" import { DashboardHeader } from "@/components/dashboard/header" import { DashboardShell } from "@/components/dashboard/shell" import { generateContractContent } from "@/components/editor/contractTemplate" -import Editor from "@/components/editor/editor" -import { initialContent } from "@/components/editor/initialContent" import TenantEditor from "@/components/editor/TenantEditor" import { EmptyPlaceholder } from "@/components/shared/empty-placeholder" diff --git a/apps/www/src/app/(tenant)/tenant/[id]/contract2/building/_components/BuildingFormContract.tsx b/apps/www/src/app/(tenant)/tenant/[id]/contract2/building/_components/BuildingFormContract.tsx new file mode 100644 index 0000000..e4ee0a1 --- /dev/null +++ b/apps/www/src/app/(tenant)/tenant/[id]/contract2/building/_components/BuildingFormContract.tsx @@ -0,0 +1,245 @@ +"use client" + +import { useState } from "react" +import { zodResolver } from "@hookform/resolvers/zod" +import { useForm } from "react-hook-form" +import { toast } from "sonner" +import { z } from "zod" + +import { Button } from "@dingify/ui/components/button" +import { + Card, + CardContent, + CardDescription, + CardFooter, + CardHeader, + CardTitle, +} from "@dingify/ui/components/card" +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@dingify/ui/components/form" +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@dingify/ui/components/select" + +// Define the validation schema +const IssueSchema = z.object({ + building: z.string().min(1, "Bygning er påkrevd"), + property: z.string().min(1, "Eiendom er påkrevd"), + floor: z.string().min(1, "Etasje er påkrevd"), + officeSpace: z.string().min(1, "Kontorlokale er påkrevd"), +}) + +export function BuildingFormContract({ tenantDetails }) { + const [isLoading, setIsLoading] = useState(false) + const [selectedFloor, setSelectedFloor] = useState( + tenantDetails?.floor?.id?.toString() || "", + ) + + const form = useForm({ + resolver: zodResolver(IssueSchema), + defaultValues: { + building: tenantDetails?.building?.id.toString() || "", + property: tenantDetails?.property?.id.toString() || "", + floor: tenantDetails?.floor?.id?.toString() || "", + officeSpace: tenantDetails?.officeSpace?.id?.toString() || "", + }, + }) + + const onSubmit = async (data) => { + setIsLoading(true) + + try { + const result = await submitIssue(data) + + if (!result.success) { + throw new Error(result.error || "Failed to submit issue.") + } + + toast.success("Informasjonen er nå lagret") + form.reset() + } catch (error) { + toast.error(error.message) + console.error(error) + } finally { + setIsLoading(false) + } + } + + // Get office spaces for the selected floor + const officeSpaces = selectedFloor + ? tenantDetails.floors.find( + (floor) => floor.id.toString() === selectedFloor, + )?.officeSpaces || [] + : [] + + return ( + + + Lokaler + Hvordan lokale skal leies ut? + +
+ + +
+ ( + + Eiendom + + + + + + )} + /> + ( + + Byggning + + + + + + )} + /> + ( + + Etasje + + + + + + )} + /> + ( + + Kontorlokale + + + + + + )} + /> +
+
+ + + +
+ +
+ ) +} + +// Dummy function to simulate form submission +async function submitIssue(data) { + // Simulate a delay + return new Promise((resolve) => { + setTimeout(() => { + resolve({ success: true }) + }, 1000) + }) +} diff --git a/apps/www/src/app/(tenant)/tenant/[id]/contract2/building/_components/ReusableFormTemplate.tsx b/apps/www/src/app/(tenant)/tenant/[id]/contract2/building/_components/ReusableFormTemplate.tsx new file mode 100644 index 0000000..4c6ce5c --- /dev/null +++ b/apps/www/src/app/(tenant)/tenant/[id]/contract2/building/_components/ReusableFormTemplate.tsx @@ -0,0 +1,295 @@ +"use client" + +import { useState } from "react" +import Link from "next/link" +import { zodResolver } from "@hookform/resolvers/zod" +import { CalendarIcon } from "@radix-ui/react-icons" +import { format } from "date-fns" +import { useForm } from "react-hook-form" +import { z } from "zod" + +import { Button } from "@dingify/ui/components/button" +import { Calendar } from "@dingify/ui/components/calendar" +import { + Card, + CardContent, + CardDescription, + CardFooter, + CardHeader, + CardTitle, +} from "@dingify/ui/components/card" +import { Checkbox } from "@dingify/ui/components/checkbox" +import { + Form, + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@dingify/ui/components/form" +import { Input } from "@dingify/ui/components/input" +import { Label } from "@dingify/ui/components/label" +import { + Popover, + PopoverContent, + PopoverTrigger, +} from "@dingify/ui/components/popover" +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@dingify/ui/components/select" +import { Switch } from "@dingify/ui/components/switch" +import { Textarea } from "@dingify/ui/components/textarea" +import { toast } from "@dingify/ui/components/use-toast" + +import { cn } from "@/lib/utils" + +// Define the validation schema +const FormSchema = z.object({ + area: z.string().min(1, "Area is required"), + securityLevel: z.string().min(1, "Security Level is required"), + subject: z.string().min(1, "Subject is required"), + description: z.string().min(1, "Description is required"), + marketing_emails: z.boolean().default(false).optional(), + security_emails: z.boolean(), + mobile: z.boolean().default(false).optional(), + dob: z.date({ + required_error: "A date of birth is required.", + }), +}) + +export function ReusableFormTemplate() { + const [isLoading, setIsLoading] = useState(false) + + const form = useForm({ + resolver: zodResolver(FormSchema), + defaultValues: { + area: "", + securityLevel: "", + subject: "", + description: "", + marketing_emails: false, + security_emails: true, + mobile: true, + }, + }) + + const onSubmit = async (data) => { + setIsLoading(true) + + try { + // Simulate form submission + const result = await submitForm(data) + + if (!result.success) { + throw new Error(result.error || "Failed to submit form.") + } + + toast.success(`Form submitted successfully`) + form.reset() + } catch (error) { + toast.error(error.message) + console.error(error) + } finally { + setIsLoading(false) + } + } + + return ( + + + Reusable Form Template + + This form includes various input types for comprehensive data + collection. + + +
+ + +
+ ( + + Area + + + + + + )} + /> + ( + + Security Level + + + + + + )} + /> +
+ ( + + Subject + + + + + + )} + /> + ( + + Description + +