From 7728061e5e47ea89970920e727b5b1a799a2b930 Mon Sep 17 00:00:00 2001 From: d-g-town <66391417+d-g-town@users.noreply.github.com> Date: Thu, 22 Feb 2024 12:50:45 -0500 Subject: [PATCH] properly delete old ingress key when edited (#4314) --- .../tabs/IngressCustomAnnotations.tsx | 47 ++++++++++++++----- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/dashboard/src/main/home/app-dashboard/validate-apply/services-settings/tabs/IngressCustomAnnotations.tsx b/dashboard/src/main/home/app-dashboard/validate-apply/services-settings/tabs/IngressCustomAnnotations.tsx index 0873a1c908..f465106f16 100644 --- a/dashboard/src/main/home/app-dashboard/validate-apply/services-settings/tabs/IngressCustomAnnotations.tsx +++ b/dashboard/src/main/home/app-dashboard/validate-apply/services-settings/tabs/IngressCustomAnnotations.tsx @@ -1,27 +1,29 @@ -import Button from "components/porter/Button"; -import { ControlledInput } from "components/porter/ControlledInput"; -import Spacer from "components/porter/Spacer"; -import { PorterAppFormData } from "lib/porter-apps"; import React from "react"; import { useFieldArray, useFormContext } from "react-hook-form"; import styled from "styled-components"; +import Button from "components/porter/Button"; +import { ControlledInput } from "components/porter/ControlledInput"; +import Spacer from "components/porter/Spacer"; +import { type PorterAppFormData } from "lib/porter-apps"; + type Props = { index: number; }; const IngressCustomAnnotations: React.FC = ({ index }) => { - const { control, register } = useFormContext(); + const { control, register, setValue } = useFormContext(); const { remove, append, fields } = useFieldArray({ control, name: `app.services.${index}.config.ingressAnnotations`, }); - const { append: appendAnnotationDeletion } = useFieldArray({ - control, - name: `app.services.${index}.ingressAnnotationDeletions`, - }); + const { append: appendAnnotationDeletion, fields: fieldsAnnotationDeletion } = + useFieldArray({ + control, + name: `app.services.${index}.ingressAnnotationDeletions`, + }); - const onRemove = (i: number, key: string) => { + const onRemove = (i: number, key: string): void => { remove(i); appendAnnotationDeletion({ key, @@ -44,7 +46,30 @@ const IngressCustomAnnotations: React.FC = ({ index }) => { "You may only edit this field in your porter.yaml." } {...register( - `app.services.${index}.config.ingressAnnotations.${i}.key` + `app.services.${index}.config.ingressAnnotations.${i}.key`, + { + // If the user edits an existing key, we need to mark the old key as deleted. + // Otherwise, the backend will merge the old set of keys with the new set, and the original key + // will still exist. + onChange: (e) => { + if ( + e.target.value && + e.target.value !== annotation.key + ) { + setValue( + `app.services.${index}.config.ingressAnnotations.${i}.key`, + e.target.value + ); + if ( + !fieldsAnnotationDeletion.find( + (d) => d.key === annotation.key + ) + ) { + appendAnnotationDeletion({ key: annotation.key }); + } + } + }, + } )} />