import { FormEvent, useState } from "react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { BriefcaseBusiness, Plus } from "lucide-react"; import { api, Project, Tenant } from "../api/client"; import { DataTable } from "../components/DataTable"; import { buttonClass, Field, inputClass, selectClass } from "../components/FormControls"; import { Modal } from "../components/Modal"; import { PageHeader } from "../components/PageHeader"; export function TenantsProjects() { const queryClient = useQueryClient(); const tenants = useQuery({ queryKey: ["tenants"], queryFn: () => api("/tenants") }); const projects = useQuery({ queryKey: ["projects"], queryFn: () => api("/projects") }); const [tenantForm, setTenantForm] = useState({ name: "Operations", description: "Operations tenant" }); const [projectForm, setProjectForm] = useState({ tenant_id: "", name: "Monitoring", description: "Monitoring workloads" }); const [tenantOpen, setTenantOpen] = useState(false); const [projectOpen, setProjectOpen] = useState(false); const createTenant = useMutation({ mutationFn: () => api("/tenants", { method: "POST", body: JSON.stringify(tenantForm) }), onSuccess: () => { setTenantOpen(false); queryClient.invalidateQueries({ queryKey: ["tenants"] }); }, }); const createProject = useMutation({ mutationFn: () => api("/projects", { method: "POST", body: JSON.stringify({ ...projectForm, tenant_id: projectForm.tenant_id || tenants.data?.[0]?.id }) }), onSuccess: () => { setProjectOpen(false); queryClient.invalidateQueries({ queryKey: ["projects"] }); }, }); async function submitTenant(event: FormEvent) { event.preventDefault(); await createTenant.mutateAsync(); } async function submitProject(event: FormEvent) { event.preventDefault(); await createProject.mutateAsync(); } return ( <>
setTenantOpen(false)}>
Add Tenant
setTenantForm({ ...tenantForm, name: event.target.value })} /> setTenantForm({ ...tenantForm, description: event.target.value })} />
setProjectOpen(false)}>
Add Project
setProjectForm({ ...projectForm, name: event.target.value })} /> setProjectForm({ ...projectForm, description: event.target.value })} />
[]} columns={[{ key: "name", label: "Tenant" }, { key: "description", label: "Description" }]} /> []} columns={[{ key: "name", label: "Project" }, { key: "description", label: "Description" }]} />
); }