-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1996 from headlamp-k8s/create-resource-ui
frontend: Add create resource UI
- Loading branch information
Showing
67 changed files
with
1,123 additions
and
194 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
frontend/src/components/common/CreateResourceButton.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
import { expect, userEvent, waitFor } from '@storybook/test'; | ||
import { screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { Provider } from 'react-redux'; | ||
import { KubeObjectClass } from '../../lib/k8s/cluster'; | ||
import ConfigMap from '../../lib/k8s/configMap'; | ||
import store from '../../redux/stores/store'; | ||
import { TestContext } from '../../test'; | ||
import { CreateResourceButton, CreateResourceButtonProps } from './CreateResourceButton'; | ||
|
||
export default { | ||
title: 'CreateResourceButton', | ||
component: CreateResourceButton, | ||
parameters: { | ||
storyshots: { | ||
disable: true, | ||
}, | ||
}, | ||
decorators: [ | ||
Story => { | ||
return ( | ||
<Provider store={store}> | ||
<TestContext> | ||
<Story /> | ||
</TestContext> | ||
</Provider> | ||
); | ||
}, | ||
], | ||
} as Meta; | ||
|
||
type Story = StoryObj<CreateResourceButtonProps>; | ||
|
||
export const ValidResource: Story = { | ||
args: { resourceClass: ConfigMap as unknown as KubeObjectClass }, | ||
|
||
play: async ({ args }) => { | ||
await userEvent.click( | ||
screen.getByRole('button', { | ||
name: `Create ${args.resourceClass.getBaseObject().kind}`, | ||
}) | ||
); | ||
|
||
await waitFor(() => expect(screen.getByRole('textbox')).toBeVisible()); | ||
|
||
await userEvent.click(screen.getByRole('textbox')); | ||
|
||
await userEvent.keyboard('{Control>}a{/Control} {Backspace}'); | ||
await userEvent.keyboard(`apiVersion: v1{Enter}`); | ||
await userEvent.keyboard(`kind: ConfigMap{Enter}`); | ||
await userEvent.keyboard(`metadata:{Enter}`); | ||
await userEvent.keyboard(` name: base-configmap`); | ||
|
||
const button = await screen.findByRole('button', { name: 'Apply' }); | ||
expect(button).toBeVisible(); | ||
}, | ||
}; | ||
|
||
export const InvalidResource: Story = { | ||
args: { resourceClass: ConfigMap as unknown as KubeObjectClass }, | ||
|
||
play: async ({ args }) => { | ||
await userEvent.click( | ||
screen.getByRole('button', { | ||
name: `Create ${args.resourceClass.getBaseObject().kind}`, | ||
}) | ||
); | ||
|
||
await waitFor(() => expect(screen.getByRole('textbox')).toBeVisible()); | ||
|
||
await userEvent.click(screen.getByRole('textbox')); | ||
|
||
await userEvent.keyboard('{Control>}a{/Control}'); | ||
await userEvent.keyboard(`apiVersion: v1{Enter}`); | ||
await userEvent.keyboard(`kind: ConfigMap{Enter}`); | ||
await userEvent.keyboard(`metadata:{Enter}`); | ||
await userEvent.keyboard(` name: base-configmap{Enter}`); | ||
await userEvent.keyboard(`creationTimestamp: ''`); | ||
|
||
const button = await screen.findByRole('button', { name: 'Apply' }); | ||
expect(button).toBeVisible(); | ||
|
||
await userEvent.click(button); | ||
|
||
await waitFor(() => | ||
userEvent.click( | ||
screen.getByRole('button', { | ||
name: `Create ${args.resourceClass.getBaseObject().kind}`, | ||
}) | ||
) | ||
); | ||
|
||
await waitFor(() => expect(screen.getByText(/Failed/)).toBeVisible(), { | ||
timeout: 15000, | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { KubeObjectClass } from '../../lib/k8s/cluster'; | ||
import { ActionButton, AuthVisible, EditorDialog } from '../common'; | ||
|
||
export interface CreateResourceButtonProps { | ||
resourceClass: KubeObjectClass; | ||
resourceName?: string; | ||
} | ||
|
||
export function CreateResourceButton(props: CreateResourceButtonProps) { | ||
const { resourceClass, resourceName } = props; | ||
const { t } = useTranslation(['glossary', 'translation']); | ||
const [openDialog, setOpenDialog] = React.useState(false); | ||
const [errorMessage, setErrorMessage] = React.useState(''); | ||
|
||
const baseObject = resourceClass.getBaseObject(); | ||
const name = resourceName ?? baseObject.kind; | ||
|
||
return ( | ||
<AuthVisible item={resourceClass} authVerb="create"> | ||
<ActionButton | ||
color="primary" | ||
description={t('translation|Create {{ name }}', { name })} | ||
icon={'mdi:plus-circle'} | ||
onClick={() => { | ||
setOpenDialog(true); | ||
}} | ||
/> | ||
<EditorDialog | ||
item={baseObject} | ||
open={openDialog} | ||
setOpen={setOpenDialog} | ||
onClose={() => setOpenDialog(false)} | ||
saveLabel={t('translation|Apply')} | ||
errorMessage={errorMessage} | ||
onEditorChanged={() => setErrorMessage('')} | ||
title={t('translation|Create {{ name }}', { name })} | ||
/> | ||
</AuthVisible> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.