Skip to content

Commit

Permalink
🐛 Fix tag source override when updating application (#837)
Browse files Browse the repository at this point in the history
The `ApplicationForm` now handles directly tags (TagRef[]) allowing to
preserve value of existing tags "source" field and to avoid automated
tags (source = "Analysis") to be overridden with source = "".

Resolves #836
  • Loading branch information
gildub authored Apr 26, 2023
1 parent 8f4b4e5 commit 6a0433e
Showing 1 changed file with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface FormValues {
description: string;
comments: string;
businessServiceName: string;
tagNames: string[];
tags: TagRef[];
owner: string | null;
contributors: string[];
kind: string;
Expand Down Expand Up @@ -258,7 +258,7 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
id: application?.id || 0,
comments: application?.comments || "",
businessServiceName: application?.businessService?.name || "",
tagNames: application?.tags?.map((tag) => tag?.name) || [],
tags: application?.tags || [],
owner: application?.owner?.name || undefined,
contributors:
application?.contributors?.map((contributor) => contributor.name) || [],
Expand Down Expand Up @@ -312,10 +312,6 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
formValues?.businessServiceName === businessService.name
);

const matchingTagRefs = tags?.filter((tagRef) =>
formValues.tagNames.includes(tagRef.name)
);

const matchingOwner = stakeholders.find(
(stakeholder) => formValues?.owner === stakeholder.name
);
Expand All @@ -334,7 +330,7 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
name: matchingBusinessService.name,
}
: undefined,
tags: matchingTagRefs,
tags: formValues.tags,
owner: matchingOwner
? { id: matchingOwner.id, name: matchingOwner.name }
: undefined,
Expand Down Expand Up @@ -362,10 +358,7 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
};

if (application) {
updateApplication({
...application,
...payload,
});
updateApplication(payload);
} else {
createApplication(payload);
}
Expand All @@ -386,6 +379,9 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
},
];

const getTagRef = (tagName: string) =>
tags?.find((tag) => tag.name === tagName);

return (
<Form onSubmit={handleSubmit(onSubmit)}>
{axiosError && (
Expand Down Expand Up @@ -448,7 +444,7 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({

<HookFormPFGroupController
control={control}
name="tagNames"
name="tags"
label={t("terms.tags")}
fieldId="tags"
renderInput={({ field: { value, name, onChange } }) => (
Expand All @@ -463,8 +459,8 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
toggleAriaLabel="tags dropdown toggle"
aria-label={name}
value={value
.map((formTagName) =>
tags?.find((tagRef) => tagRef.name === formTagName)
.map((formTag) =>
tags?.find((tagRef) => tagRef.name === formTag.name)
)
.map((matchingTag) =>
matchingTag
Expand All @@ -482,14 +478,19 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({

const currentValue = value || [];
const e = currentValue.find(
(f) => f === selectionWithValue.value
(f) => f.name === selectionWithValue.value
);
if (e) {
onChange(
currentValue.filter((f) => f !== selectionWithValue.value)
currentValue.filter(
(f) => f.name !== selectionWithValue.value
)
);
} else {
onChange([...currentValue, selectionWithValue.value]);
onChange([
...currentValue,
getTagRef(selectionWithValue.value),
]);
}
}}
onClear={() => onChange([])}
Expand Down

0 comments on commit 6a0433e

Please sign in to comment.