Skip to content

Commit

Permalink
Dedupe tags when selecting in app form
Browse files Browse the repository at this point in the history
  • Loading branch information
ibolton336 committed Oct 10, 2023
1 parent 82de730 commit f0452b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import { yupResolver } from "@hookform/resolvers/yup";

import { SimpleSelect, OptionWithValue } from "@app/components/SimpleSelect";
import { DEFAULT_SELECT_MAX_HEIGHT } from "@app/Constants";
import { Application, Tag } from "@app/api/models";
import { Application, Tag, TagRef } from "@app/api/models";
import {
customURLValidation,
dedupeArrayOfObjects,
duplicateNameCheck,
getAxiosErrorMessage,
} from "@app/utils/utils";
Expand Down Expand Up @@ -103,8 +104,11 @@ export const ApplicationForm: React.FC<ApplicationFormProps> = ({
};
});

const manualTags =
application?.tags?.filter((t) => t?.source ?? "" === "") ?? [];
const manualTags: TagRef[] = useMemo(() => {
const rawManualTags: TagRef[] =
application?.tags?.filter((t) => t?.source ?? "" === "") ?? [];
return dedupeArrayOfObjects<TagRef>(rawManualTags, "name");
}, [application?.tags]);

const nonManualTags =
application?.tags?.filter((t) => t?.source ?? "" !== "") ?? [];
Expand Down
12 changes: 12 additions & 0 deletions client/src/app/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ export const dedupeFunction = (arr: any[]) =>
index === self.findIndex((t) => t.value === value.value)
);

export const dedupeArrayOfObjects = <T>(arr: Array<T>, key: keyof T) => {
const seen = new Set();
return arr.filter((item) => {
const value = item[key];

Check warning on line 81 in client/src/app/utils/utils.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/utils/utils.ts#L81

Added line #L81 was not covered by tests
if (seen.has(value)) {
return false;

Check warning on line 83 in client/src/app/utils/utils.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/utils/utils.ts#L83

Added line #L83 was not covered by tests
}
seen.add(value);
return true;

Check warning on line 86 in client/src/app/utils/utils.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/utils/utils.ts#L85-L86

Added lines #L85 - L86 were not covered by tests
});
};

export const numStr = (num: number | undefined): string => {
if (num === undefined) return "";
return String(num);
Expand Down

0 comments on commit f0452b7

Please sign in to comment.