From 7316168794ef9874bad7b99a8d2b5ed24f3e11d1 Mon Sep 17 00:00:00 2001 From: Aditya Choudhari <48932219+adityachoudhari26@users.noreply.github.com> Date: Mon, 30 Sep 2024 00:02:49 -0700 Subject: [PATCH 1/3] fix: Update env on filter submit (#87) --- .../environments/SidebarEnvironmentPanel.tsx | 121 ++++++++---------- 1 file changed, 56 insertions(+), 65 deletions(-) diff --git a/apps/webservice/src/app/[workspaceSlug]/systems/[systemSlug]/environments/SidebarEnvironmentPanel.tsx b/apps/webservice/src/app/[workspaceSlug]/systems/[systemSlug]/environments/SidebarEnvironmentPanel.tsx index 74627f29a..fcae1d5bf 100644 --- a/apps/webservice/src/app/[workspaceSlug]/systems/[systemSlug]/environments/SidebarEnvironmentPanel.tsx +++ b/apps/webservice/src/app/[workspaceSlug]/systems/[systemSlug]/environments/SidebarEnvironmentPanel.tsx @@ -1,5 +1,6 @@ "use client"; +import type { TargetCondition } from "@ctrlplane/validators/targets"; import { useEffect } from "react"; import { useParams } from "next/navigation"; import { IconInfoCircle, IconPlant } from "@tabler/icons-react"; @@ -16,13 +17,9 @@ import { useForm, } from "@ctrlplane/ui/form"; import { Input } from "@ctrlplane/ui/input"; +import { Label } from "@ctrlplane/ui/label"; import { Separator } from "@ctrlplane/ui/separator"; import { Textarea } from "@ctrlplane/ui/textarea"; -import { - isDefaultCondition, - isValidTargetCondition, - targetCondition, -} from "@ctrlplane/validators/targets"; import { api } from "~/trpc/react"; import { TargetConditionBadge } from "../../../_components/target-condition/TargetConditionBadge"; @@ -32,7 +29,6 @@ import { usePanel } from "./SidepanelContext"; const environmentForm = z.object({ name: z.string(), description: z.string().default(""), - targetFilter: targetCondition.optional(), }); export const SidebarEnvironmentPanel: React.FC = () => { @@ -49,7 +45,6 @@ export const SidebarEnvironmentPanel: React.FC = () => { defaultValues: { name: node.data.label, description: node.data.description, - targetFilter: node.data.targetFilter, }, }); @@ -62,18 +57,15 @@ export const SidebarEnvironmentPanel: React.FC = () => { form.reset({ name: node.data.label, description: node.data.description, - targetFilter: node.data.targetFilter, }); }, [node, form]); - const { targetFilter } = form.watch(); - const targets = api.target.byWorkspaceId.list.useQuery( { workspaceId: workspace.data?.id ?? "", - filter: targetFilter, + filter: node.data.targetFilter, }, - { enabled: workspace.data != null && targetFilter != null }, + { enabled: workspace.data != null && node.data.targetFilter != null }, ); const utils = api.useUtils(); @@ -83,25 +75,39 @@ export const SidebarEnvironmentPanel: React.FC = () => { const node = nodes.find((n) => n.id === selectedNodeId); if (!node) return nodes; - const { targetFilter } = values; + update + .mutateAsync({ + id: node.id, + data: { ...values }, + }) + .then(() => + utils.environment.bySystemId.invalidate(node.data.systemId), + ); + return nodes.map((n) => + n.id === selectedNodeId + ? { + ...n, + data: { + ...n.data, + ...values, + label: values.name, + }, + } + : n, + ); + }); + }); - if (targetFilter != null && !isValidTargetCondition(targetFilter)) { - form.setError("targetFilter", { - message: - "Invalid target filter, ensure all fields are filled out correctly.", - }); - return nodes; - } + const onFilterDialogSubmit = (condition: TargetCondition | undefined) => + setNodes((nodes) => { + const node = nodes.find((n) => n.id === selectedNodeId); + if (!node) return nodes; update .mutateAsync({ id: node.id, data: { - ...values, - targetFilter: - targetFilter != null && isDefaultCondition(targetFilter) - ? undefined - : targetFilter, + targetFilter: condition ?? null, }, }) .then(() => @@ -113,15 +119,12 @@ export const SidebarEnvironmentPanel: React.FC = () => { ...n, data: { ...n.data, - ...values, - targetFilter, - label: values.name, + targetFilter: condition, }, } : n, ); }); - }); return (