Skip to content

Commit

Permalink
Fleet UI: Surface duplicate label name error to users (#22389)
Browse files Browse the repository at this point in the history
  • Loading branch information
RachelElysia authored Sep 25, 2024
1 parent 345d35c commit 692e0fc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
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

0 comments on commit 692e0fc

Please sign in to comment.