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

Profile type select update to display informations on the related node object #4039

Merged
merged 12 commits into from
Aug 9, 2024
1 change: 1 addition & 0 deletions changelog/4001.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updates the profile type select when creating a profile, to display more relevant informations about the related nodes
56 changes: 51 additions & 5 deletions frontend/app/src/components/form/object-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import useFilters from "@/hooks/useFilters";
import useQuery from "@/hooks/useQuery";
import { getProfiles } from "@/graphql/queries/objects/getProfiles";
import { getObjectAttributes } from "@/utils/getSchemaObjectColumns";
import { PROFILE_KIND } from "@/config/constants";

type Profile = Record<string, Pick<AttributeType, "value" | "__typename">>;

Expand Down Expand Up @@ -66,6 +67,28 @@ const ObjectForm = ({ kind, isFilterForm, ...props }: ObjectFormProps) => {

if (!relatedSchema) return;

if (kind === PROFILE_KIND) {
const relationship = relatedSchema.relationships?.find(
(relationship) => relationship.name === "related_nodes"
);

const nodeSchema =
relationship?.peer &&
[...schemas, ...generics, ...profiles].find(
(schema) => schema.kind === relationship.peer
);

if (!nodeSchema) return;

const currentGeneric = {
value: relatedSchema.kind,
label: nodeSchema.label ?? nodeSchema.name,
badge: nodeSchema.namespace,
};

setKindToCreate(currentGeneric.value ?? "");
}

const currentGeneric = {
value: relatedSchema.kind,
label: relatedSchema.label ?? relatedSchema.name,
Expand All @@ -76,10 +99,33 @@ const ObjectForm = ({ kind, isFilterForm, ...props }: ObjectFormProps) => {
}

const items = generic.used_by
.map((kind) => {
const relatedSchema = [...schemas, ...profiles].find((schema) => schema.kind === kind);

if (!relatedSchema) return null;
.map((usedByKind) => {
const relatedSchema = [...schemas, ...profiles].find(
(schema) => schema.kind === usedByKind
);

if (!relatedSchema) return;

// When choosing a profile, display informations about the related node
if (kind === PROFILE_KIND) {
const relationship = relatedSchema.relationships?.find(
(relationship) => relationship.name === "related_nodes"
);

const nodeSchema =
relationship?.peer &&
[...schemas, ...generics, ...profiles].find(
(schema) => schema.kind === relationship.peer
);

if (!nodeSchema) return;

return {
value: relatedSchema.kind,
label: nodeSchema.label ?? nodeSchema.name,
badge: nodeSchema.namespace,
};
}

return {
value: relatedSchema.kind,
Expand Down Expand Up @@ -132,7 +178,7 @@ const NodeWithProfileForm = ({ kind, currentProfiles, ...props }: ObjectFormProp

return (
<>
{nodeSchema.generate_profile && (
{nodeSchema.generate_profile && !props.isFilterForm && (
<ProfilesSelector
schema={nodeSchema}
defaultValue={currentProfiles}
Expand Down
1 change: 0 additions & 1 deletion frontend/app/src/components/form/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ export const getFormFieldsFromSchema = ({
if (!schemas) return null;

const relatedSchema = schemas.find((schema) => schema.kind === kind);
console.log("relatedSchema: ", relatedSchema);

if (!relatedSchema) return null;

Expand Down
6 changes: 3 additions & 3 deletions frontend/app/src/components/ui/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ export const ComboboxList = ({ items, onReset }: ComboboxListProps) => {

{filteredOptions.length > 0 ? (
<ComboboxPrimitive.Options static className="h-full overflow-auto">
{filteredOptions.map((item) => {
{filteredOptions.map((item, index) => {
return typeof item === "string" ? (
<ComboboxItem key={item} item={item} />
<ComboboxItem key={index} item={item} />
) : (
<ComboboxItem key={item.label} item={item} />
<ComboboxItem key={index} item={item} />
);
})}
</ComboboxPrimitive.Options>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test.describe("/objects/CoreProfile - Profiles page", () => {
// Generic profile
await page.getByTestId("create-object-button").click();
await page.getByLabel("Select an object type").click();
await page.getByRole("option", { name: "ProfileInfraInterface Profile" }).click();
await page.getByRole("option", { name: "Interface Infra", exact: true }).click();
await page.getByLabel("Profile Name *").fill("Generic profile");
await page.getByLabel("Description").fill("Desc from generic profile");
await page.getByRole("button", { name: "Save" }).click();
Expand All @@ -33,7 +33,7 @@ test.describe("/objects/CoreProfile - Profiles page", () => {
// L2 profile v1
await page.getByTestId("create-object-button").click();
await page.getByLabel("Select an object type").click();
await page.getByRole("option", { name: "ProfileInfraInterfaceL2 Profile" }).click();
await page.getByRole("option", { name: "Interface L2 Infra", exact: true }).click();
await page.getByLabel("Profile Name *").fill("L2 profile v1");
await page.getByLabel("Description").fill("Desc from L2 profile v1");
await page.getByRole("button", { name: "Save" }).click();
Expand All @@ -44,7 +44,7 @@ test.describe("/objects/CoreProfile - Profiles page", () => {
// L2 profile v2
await page.getByTestId("create-object-button").click();
await page.getByLabel("Select an object type").click();
await page.getByRole("option", { name: "ProfileInfraInterfaceL2 Profile" }).click();
await page.getByRole("option", { name: "Interface L2 Infra", exact: true }).click();
await page.getByLabel("Profile Name *").fill("L2 profile v2");
await page.getByLabel("Description").fill("Desc from L2 profile v2");
await page.getByLabel("Profile Priority").fill("10");
Expand Down
10 changes: 4 additions & 6 deletions frontend/app/tests/e2e/objects/profiles/profiles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test.describe("/objects/CoreProfile - Profiles page", () => {
await test.step("Create a new profile", async () => {
await page.getByTestId("create-object-button").click();
await page.getByLabel("Select an object type").click();
await page.getByRole("option", { name: "ProfileBuiltinTag" }).click();
await page.getByRole("option", { name: "Tag Builtin", exact: true }).click();
await page.getByLabel("Profile Name *").fill("profile test tag");
await page.getByLabel("Description").fill("A profile for E2E test");
await page.getByRole("button", { name: "Save" }).click();
Expand Down Expand Up @@ -220,7 +220,7 @@ test.describe("/objects/CoreProfile - Profile for Interface L2 and fields verifi
.filter({ hasText: /^Clear$/ })
.getByRole("combobox")
.fill("l2");
await page.getByRole("option", { name: "ProfileInfraInterfaceL2" }).click();
await page.getByRole("option", { name: "Interface L2 Infra", exact: true }).click();
});

await test.step("verify Interface L2 optional attributes are all visible", async () => {
Expand Down Expand Up @@ -257,7 +257,7 @@ test.describe("/objects/CoreProfile - Profile for Interface L2 and fields verifi
]);
await page.getByTestId("create-object-button").click();
await page.getByLabel("Select an object type").click();
await page.getByRole("option", { name: "ProfileInfraInterfaceL2" }).click();
await page.getByRole("option", { name: "Interface L2 Infra", exact: true }).click();
});

await test.step("fill and submit form", async () => {
Expand Down Expand Up @@ -294,9 +294,7 @@ test.describe("/objects/CoreProfile - Profile for Interface L2 and fields verifi
]);
await page.getByTestId("create-object-button").click();
await page.getByLabel("Select an object type").click();
await page
.getByRole("option", { name: "ProfileInfraInterface Profile", exact: true })
.click();
await page.getByRole("option", { name: "Interface Infra", exact: true }).click();
});

await test.step("fill and submit form", async () => {
Expand Down
Loading