Skip to content

two tables done in new kontrat #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2024
Merged
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
18 changes: 16 additions & 2 deletions apps/www/src/actions/get-tenant-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions apps/www/src/app/(tenant)/tenant/[id]/contract/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
<Card>
<CardHeader>
<CardTitle>Lokaler</CardTitle>
<CardDescription>Hvordan lokale skal leies ut?</CardDescription>
</CardHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<CardContent className="grid gap-6">
<div className="grid grid-cols-2 gap-4">
<FormField
control={form.control}
name="property"
render={({ field }) => (
<FormItem>
<FormLabel>Eiendom</FormLabel>
<FormControl>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<SelectTrigger>
<SelectValue placeholder="Select property" />
</SelectTrigger>
<SelectContent>
<SelectItem
value={tenantDetails.property.id.toString()}
>
{tenantDetails.property.name}
</SelectItem>
</SelectContent>
</Select>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="building"
render={({ field }) => (
<FormItem>
<FormLabel>Byggning</FormLabel>
<FormControl>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<SelectTrigger>
<SelectValue placeholder="Select building" />
</SelectTrigger>
<SelectContent>
<SelectItem
value={tenantDetails.building.id.toString()}
>
{tenantDetails.building.name}
</SelectItem>
</SelectContent>
</Select>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="floor"
render={({ field }) => (
<FormItem>
<FormLabel>Etasje</FormLabel>
<FormControl>
<Select
onValueChange={(value) => {
field.onChange(value)
setSelectedFloor(value)
}}
defaultValue={field.value}
>
<SelectTrigger>
<SelectValue placeholder="Velg Etasje" />
</SelectTrigger>
<SelectContent>
{tenantDetails.floors.length > 0 ? (
tenantDetails.floors.map((floor) => (
<SelectItem
key={floor.id}
value={floor.id.toString()}
>
{`Etasje ${floor.number}`}
</SelectItem>
))
) : (
<SelectItem disabled value="no-floors">
Du mΓ₯ legge til etasjer
</SelectItem>
)}
</SelectContent>
</Select>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="officeSpace"
render={({ field }) => (
<FormItem>
<FormLabel>Kontorlokale</FormLabel>
<FormControl>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<SelectTrigger>
<SelectValue placeholder="Velg kontor" />
</SelectTrigger>
<SelectContent>
{officeSpaces.length > 0 ? (
officeSpaces.map((officeSpace) => (
<SelectItem
key={officeSpace.id}
value={officeSpace.id.toString()}
>
{officeSpace.name}
</SelectItem>
))
) : (
<SelectItem disabled value="no-office-spaces">
Du har ingen kontorlokale til etasjen
</SelectItem>
)}
</SelectContent>
</Select>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
</CardContent>
<CardFooter className="justify-between space-x-2">
<Button type="submit" disabled={isLoading}>
{isLoading ? "Lagrer..." : "Lagre"}
</Button>
</CardFooter>
</form>
</Form>
</Card>
)
}

// Dummy function to simulate form submission
async function submitIssue(data) {
// Simulate a delay
return new Promise((resolve) => {
setTimeout(() => {
resolve({ success: true })
}, 1000)
})
}
Loading