- Notifications
You must be signed in to change notification settings - Fork1k
fix: select default org in template form if only one exists#16639
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
a656cb8
bb93325
dd921e9
afa0a0a
67b477f
cb81f4b
c7ea9de
0330ba6
92e632a
cea580a
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { action } from "@storybook/addon-actions"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { userEvent, within } from "@storybook/test"; | ||
import { | ||
MockOrganization, | ||
MockOrganization2, | ||
MockUser, | ||
} from "testHelpers/entities"; | ||
import { OrganizationAutocomplete } from "./OrganizationAutocomplete"; | ||
const meta: Meta<typeof OrganizationAutocomplete> = { | ||
title: "components/OrganizationAutocomplete", | ||
component: OrganizationAutocomplete, | ||
args: { | ||
onChange: action("Selected organization"), | ||
}, | ||
}; | ||
export default meta; | ||
type Story = StoryObj<typeof OrganizationAutocomplete>; | ||
export const ManyOrgs: Story = { | ||
parameters: { | ||
showOrganizations: true, | ||
user: MockUser, | ||
features: ["multiple_organizations"], | ||
permissions: { viewDeploymentConfig: true }, | ||
queries: [ | ||
{ | ||
key: ["organizations"], | ||
data: [MockOrganization, MockOrganization2], | ||
}, | ||
], | ||
}, | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
const button = canvas.getByRole("button"); | ||
await userEvent.click(button); | ||
}, | ||
}; | ||
export const OneOrg: Story = { | ||
parameters: { | ||
showOrganizations: true, | ||
user: MockUser, | ||
features: ["multiple_organizations"], | ||
permissions: { viewDeploymentConfig: true }, | ||
queries: [ | ||
{ | ||
key: ["organizations"], | ||
data: [MockOrganization], | ||
}, | ||
], | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -7,7 +7,7 @@ import { organizations } from "api/queries/organizations"; | ||
import type { AuthorizationCheck, Organization } from "api/typesGenerated"; | ||
import { Avatar } from "components/Avatar/Avatar"; | ||
import { AvatarData } from "components/Avatar/AvatarData"; | ||
import { type ComponentProps, type FC,useEffect,useState } from "react"; | ||
import { useQuery } from "react-query"; | ||
export type OrganizationAutocompleteProps = { | ||
@@ -57,11 +57,26 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({ | ||
: []; | ||
} | ||
// Unfortunate: this useEffect sets a default org value | ||
// if only one is available and is necessary as the autocomplete loads | ||
// its own data. Until we refactor, proceed cautiously! | ||
useEffect(() => { | ||
const org = options[0]; | ||
if (options.length !== 1 || org === selected) { | ||
return; | ||
} | ||
setSelected(org); | ||
onChange(org); | ||
}, [options, selected, onChange]); | ||
return ( | ||
<Autocomplete | ||
noOptionsText="No organizations found" | ||
className={className} | ||
options={options} | ||
disabled={options.length === 1} | ||
value={selected} | ||
MemberAuthor
| ||
loading={organizationsQuery.isLoading} | ||
data-testid="organization-autocomplete" | ||
open={open} | ||
Uh oh!
There was an error while loading.Please reload this page.