-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vincent T <vtaylor@microsoft.com>
- Loading branch information
Showing
13 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
frontend/src/components/namespace/CreateNamespaceButton.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,28 @@ | ||
import { Meta, StoryFn } from '@storybook/react'; | ||
import React from 'react'; | ||
import { TestContext } from '../../test'; | ||
import CreateNamespaceButton from './CreateNamespaceButton'; | ||
|
||
export default { | ||
title: 'Namespace/CreateNamespaceButton', | ||
component: CreateNamespaceButton, | ||
decorators: [ | ||
Story => { | ||
return ( | ||
<div style={{ margin: '4em' }}> | ||
<Story /> | ||
</div> | ||
); | ||
}, | ||
], | ||
} as Meta; | ||
|
||
const Template: StoryFn = () => { | ||
return ( | ||
<TestContext> | ||
<CreateNamespaceButton /> | ||
</TestContext> | ||
); | ||
}; | ||
|
||
export const Regular = Template.bind({}); |
166 changes: 166 additions & 0 deletions
166
frontend/src/components/namespace/CreateNamespaceButton.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,166 @@ | ||
import { | ||
Box, | ||
Button, | ||
Dialog, | ||
DialogActions, | ||
DialogContent, | ||
DialogTitle, | ||
TextField, | ||
} from '@mui/material'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useDispatch } from 'react-redux'; | ||
import { getCluster } from '../../lib/cluster'; | ||
import { post } from '../../lib/k8s/apiProxy'; | ||
import Namespace from '../../lib/k8s/namespace'; | ||
import { clusterAction } from '../../redux/clusterActionSlice'; | ||
import { EventStatus, HeadlampEventType, useEventCallback } from '../../redux/headlampEventSlice'; | ||
import { ActionButton, AuthVisible } from '../common'; | ||
|
||
export default function CreateNamespaceButton() { | ||
const { t } = useTranslation(['glossary', 'translation']); | ||
const [namespaceName, setNamespaceName] = useState(''); | ||
const [namespaceNameValid, setNamespaceNameValid] = useState(false); | ||
const [nameHelperMessage, setNameHelperMessage] = useState(''); | ||
const [namespaceDialogOpen, setNamespaceDialogOpen] = useState(false); | ||
const dispatchCreateEvent = useEventCallback(HeadlampEventType.CREATE_RESOURCE); | ||
const dispatch = useDispatch(); | ||
|
||
function createNewNamespace() { | ||
const clusterData = getCluster(); | ||
const newNamespaceData = { | ||
apiVersion: 'v1', | ||
kind: 'Namespace', | ||
metadata: { | ||
name: namespaceName, | ||
}, | ||
}; | ||
|
||
const newNamespaceName = newNamespaceData.metadata.name; | ||
|
||
async function namespaceRequest() { | ||
try { | ||
return await post('/api/v1/namespaces', newNamespaceData, true, { | ||
cluster: clusterData || '', | ||
}); | ||
} catch (error) { | ||
console.error('Error creating namespace', error); | ||
const errorMessage = error instanceof Error ? error.message : String(error); | ||
if (errorMessage.includes('already exists')) { | ||
setNamespaceDialogOpen(true); | ||
setNamespaceNameValid(false); | ||
setNameHelperMessage(t('translation|Namespace name already exists.')); | ||
} | ||
throw error; | ||
} | ||
} | ||
|
||
setNamespaceDialogOpen(false); | ||
|
||
dispatch( | ||
clusterAction(() => namespaceRequest(), { | ||
startMessage: t('translation|Applying {{ newItemName }}…', { | ||
newItemName: newNamespaceName, | ||
}), | ||
cancelledMessage: t('translation|Cancelled applying {{ newItemName }}.', { | ||
newItemName: newNamespaceName, | ||
}), | ||
successMessage: t('translation|Applied {{ newItemName }}.', { | ||
newItemName: newNamespaceName, | ||
}), | ||
errorMessage: t('translation|Failed to create {{ kind }} {{ name }}.', { | ||
kind: 'namespace', | ||
name: newNamespaceName, | ||
}), | ||
cancelCallback: () => { | ||
setNamespaceDialogOpen(true); | ||
}, | ||
}) | ||
); | ||
} | ||
|
||
useEffect(() => { | ||
setNamespaceNameValid(Namespace.isValidNamespaceFormat(namespaceName)); | ||
|
||
if (!Namespace.isValidNamespaceFormat(namespaceName)) { | ||
if (namespaceName.length > 63) { | ||
setNameHelperMessage(t('translation|Namespaces must be under 64 characters.')); | ||
} else { | ||
setNameHelperMessage( | ||
t( | ||
"translation|Namespaces must contain only lowercase alphanumeric characters or '-', and must start and end with an alphanumeric character." | ||
) | ||
); | ||
} | ||
} | ||
}, [namespaceName]); | ||
|
||
return ( | ||
<AuthVisible item={Namespace} authVerb="create"> | ||
<ActionButton | ||
color="primary" | ||
description={t('translation|Create')} | ||
icon={'mdi:plus-circle'} | ||
onClick={() => { | ||
setNamespaceDialogOpen(true); | ||
}} | ||
/> | ||
|
||
{namespaceDialogOpen && ( | ||
<Dialog open={namespaceDialogOpen} onClose={() => setNamespaceDialogOpen(false)}> | ||
<DialogTitle>{t('translation|Create Namespace')}</DialogTitle> | ||
<DialogContent> | ||
<Box component="form" style={{ width: '20vw', maxWidth: '20vw' }}> | ||
<TextField | ||
margin="dense" | ||
id="name" | ||
label={t('translation|Name')} | ||
type="text" | ||
error={!namespaceNameValid && namespaceName.length > 0} | ||
helperText={ | ||
!namespaceNameValid && namespaceName.length > 0 ? nameHelperMessage : '' | ||
} | ||
fullWidth | ||
value={namespaceName} | ||
onChange={event => setNamespaceName(event.target.value)} | ||
onKeyDown={e => { | ||
if (e.key === 'Enter') { | ||
e.preventDefault(); | ||
if (namespaceNameValid) { | ||
createNewNamespace(); | ||
dispatchCreateEvent({ | ||
status: EventStatus.CONFIRMED, | ||
}); | ||
} | ||
} | ||
}} | ||
/> | ||
</Box> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button | ||
aria-label="Cancel" | ||
onClick={() => { | ||
setNamespaceDialogOpen(false); | ||
}} | ||
> | ||
{t('translation|Cancel')} | ||
</Button> | ||
<Button | ||
aria-label="Create" | ||
disabled={!namespaceNameValid} | ||
onClick={() => { | ||
createNewNamespace(); | ||
dispatchCreateEvent({ | ||
status: EventStatus.CONFIRMED, | ||
}); | ||
}} | ||
> | ||
{t('translation|Create')} | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
)} | ||
</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
1 change: 1 addition & 0 deletions
1
...mponents/namespace/__snapshots__/CreateNamespaceButton.badNamespaceData.stories.storyshot
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 @@ | ||
<DocumentFragment /> |
1 change: 1 addition & 0 deletions
1
...ponents/namespace/__snapshots__/CreateNamespaceButton.longNamespaceData.stories.storyshot
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 @@ | ||
<DocumentFragment /> |
1 change: 1 addition & 0 deletions
1
...mponents/namespace/__snapshots__/CreateNamespaceButton.newNamespaceData.stories.storyshot
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 @@ | ||
<DocumentFragment /> |
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
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
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
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