Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 191b0ef

Browse files
Kira-Pilotaslilac
andauthored
fix: select default org in template form if only one exists (coder#16639)
resolves coder#16849 coder/internal#147 ![Screenshot 2025-02-19 at 9 06 16 PM](https://github.com/user-attachments/assets/2973d81d-7a74-4c82-aa6b-16d4a41eeb9a) --------- Co-authored-by: ケイラ <[email protected]>
1 parent 4b1da9b commit 191b0ef

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

site/e2e/helpers.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,13 @@ export const createTemplate = async (
267267
);
268268
}
269269

270-
await orgPicker.click();
271-
await page.getByText(orgName, { exact: true }).click();
270+
// picker is disabled if only one org is available
271+
const pickerIsDisabled = await orgPicker.isDisabled();
272+
273+
if (!pickerIsDisabled) {
274+
await orgPicker.click();
275+
await page.getByText(orgName, { exact: true }).click();
276+
}
272277
}
273278

274279
const name = randomName();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { action } from "@storybook/addon-actions";
2+
import type { Meta, StoryObj } from "@storybook/react";
3+
import { userEvent, within } from "@storybook/test";
4+
import {
5+
MockOrganization,
6+
MockOrganization2,
7+
MockUser,
8+
} from "testHelpers/entities";
9+
import { OrganizationAutocomplete } from "./OrganizationAutocomplete";
10+
11+
const meta: Meta<typeof OrganizationAutocomplete> = {
12+
title: "components/OrganizationAutocomplete",
13+
component: OrganizationAutocomplete,
14+
args: {
15+
onChange: action("Selected organization"),
16+
},
17+
};
18+
19+
export default meta;
20+
type Story = StoryObj<typeof OrganizationAutocomplete>;
21+
22+
export const ManyOrgs: Story = {
23+
parameters: {
24+
showOrganizations: true,
25+
user: MockUser,
26+
features: ["multiple_organizations"],
27+
permissions: { viewDeploymentConfig: true },
28+
queries: [
29+
{
30+
key: ["organizations"],
31+
data: [MockOrganization, MockOrganization2],
32+
},
33+
],
34+
},
35+
play: async ({ canvasElement }) => {
36+
const canvas = within(canvasElement);
37+
const button = canvas.getByRole("button");
38+
await userEvent.click(button);
39+
},
40+
};
41+
42+
export const OneOrg: Story = {
43+
parameters: {
44+
showOrganizations: true,
45+
user: MockUser,
46+
features: ["multiple_organizations"],
47+
permissions: { viewDeploymentConfig: true },
48+
queries: [
49+
{
50+
key: ["organizations"],
51+
data: [MockOrganization],
52+
},
53+
],
54+
},
55+
};

site/src/components/OrganizationAutocomplete/OrganizationAutocomplete.tsx

+16-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { organizations } from "api/queries/organizations";
77
import type { AuthorizationCheck, Organization } from "api/typesGenerated";
88
import { Avatar } from "components/Avatar/Avatar";
99
import { AvatarData } from "components/Avatar/AvatarData";
10-
import { type ComponentProps, type FC, useState } from "react";
10+
import { type ComponentProps, type FC, useEffect, useState } from "react";
1111
import { useQuery } from "react-query";
1212

1313
export type OrganizationAutocompleteProps = {
@@ -57,11 +57,26 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
5757
: [];
5858
}
5959

60+
// Unfortunate: this useEffect sets a default org value
61+
// if only one is available and is necessary as the autocomplete loads
62+
// its own data. Until we refactor, proceed cautiously!
63+
useEffect(() => {
64+
const org = options[0];
65+
if (options.length !== 1 || org === selected) {
66+
return;
67+
}
68+
69+
setSelected(org);
70+
onChange(org);
71+
}, [options, selected, onChange]);
72+
6073
return (
6174
<Autocomplete
6275
noOptionsText="No organizations found"
6376
className={className}
6477
options={options}
78+
disabled={options.length === 1}
79+
value={selected}
6580
loading={organizationsQuery.isLoading}
6681
data-testid="organization-autocomplete"
6782
open={open}

0 commit comments

Comments
 (0)