Skip to content
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

Fleet UI: Surface duplicate label name error to users #22389

Merged
merged 1 commit into from
Sep 25, 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
1 change: 1 addition & 0 deletions changes/21875-duplicate-label-name
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fleet UI: Surface duplicate label name error to user
29 changes: 19 additions & 10 deletions frontend/pages/labels/NewLabelPage/DynamicLabel/DynamicLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React, { useContext } from "react";
import React, { useContext, useCallback } from "react";
import { RouteComponentProps } from "react-router";

import PATHS from "router/paths";
import labelsAPI from "services/entities/labels";
import { NotificationContext } from "context/notification";
import { IApiError } from "interfaces/errors";

import DynamicLabelForm from "pages/labels/components/DynamicLabelForm";
import { IDynamicLabelFormData } from "pages/labels/components/DynamicLabelForm/DynamicLabelForm";
import { DUPLICATE_ENTRY_ERROR } from "../ManualLabel/ManualLabel";

const baseClass = "dynamic-label";

Expand All @@ -26,15 +28,22 @@ const DynamicLabel = ({
}: IDynamicLabelProps) => {
const { renderFlash } = useContext(NotificationContext);

const onSaveNewLabel = async (formData: IDynamicLabelFormData) => {
try {
const res = await labelsAPI.create(formData);
router.push(PATHS.MANAGE_HOSTS_LABEL(res.label.id));
renderFlash("success", "Label added successfully.");
} catch {
renderFlash("error", "Couldn't add label. Please try again.");
}
};
const onSaveNewLabel = useCallback(
(formData: IDynamicLabelFormData) => {
labelsAPI
.create(formData)
.then((res) => {
router.push(PATHS.MANAGE_HOSTS_LABEL(res.label.id));
renderFlash("success", "Label added successfully.");
})
.catch((error: { data: IApiError }) => {
if (error.data.errors[0].reason.includes("Duplicate entry")) {
renderFlash("error", DUPLICATE_ENTRY_ERROR);
} else renderFlash("error", "Couldn't add label. Please try again.");
});
},
[renderFlash, router]
);

const onCancelLabel = () => {
router.goBack();
Expand Down
31 changes: 21 additions & 10 deletions frontend/pages/labels/NewLabelPage/ManualLabel/ManualLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
import React, { useContext } from "react";
import React, { useCallback, useContext } from "react";
import { RouteComponentProps } from "react-router";

import PATHS from "router/paths";
import labelsAPI from "services/entities/labels";
import { NotificationContext } from "context/notification";
import { IApiError } from "interfaces/errors";

import ManualLabelForm from "pages/labels/components/ManualLabelForm";
import { IManualLabelFormData } from "pages/labels/components/ManualLabelForm/ManualLabelForm";

const baseClass = "manual-label";

export const DUPLICATE_ENTRY_ERROR =
"Couldn't add. A label with this name already exists.";

type IManualLabelProps = RouteComponentProps<never, never>;

const ManualLabel = ({ router }: IManualLabelProps) => {
const { renderFlash } = useContext(NotificationContext);

const onSaveNewLabel = async (formData: IManualLabelFormData) => {
try {
const res = await labelsAPI.create(formData);
router.push(PATHS.MANAGE_HOSTS_LABEL(res.label.id));
renderFlash("success", "Label added successfully.");
} catch {
renderFlash("error", "Couldn't add label. Please try again.");
}
};
const onSaveNewLabel = useCallback(
(formData: IManualLabelFormData) => {
labelsAPI
.create(formData)
.then((res) => {
router.push(PATHS.MANAGE_HOSTS_LABEL(res.label.id));
renderFlash("success", "Label added successfully.");
})
.catch((error: { data: IApiError }) => {
if (error.data.errors[0].reason.includes("Duplicate entry")) {
renderFlash("error", DUPLICATE_ENTRY_ERROR);
} else renderFlash("error", "Couldn't add label. Please try again.");
});
},
[renderFlash, router]
);

const onCancelLabel = () => {
router.goBack();
Expand Down
Loading