-
Notifications
You must be signed in to change notification settings - Fork 149
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 <[email protected]>
- Loading branch information
Showing
12 changed files
with
148 additions
and
7 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
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,112 @@ | ||
import { | ||
Box, | ||
Button, | ||
Dialog, | ||
DialogActions, | ||
DialogContent, | ||
DialogTitle, | ||
TextField, | ||
} from '@mui/material'; | ||
import React, { 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 function CreateNamespaceButton() { | ||
const { t } = useTranslation(['glossary', 'translation']); | ||
const [namespaceName, setNamespaceName] = useState(''); | ||
const [namespaceDialogOpen, setNamespaceDialogOpen] = useState(false); | ||
const dispatchCreateEvent = useEventCallback(HeadlampEventType.CREATE_RESOURCE); | ||
const dispatch = useDispatch(); | ||
|
||
function createNewNamespace() { | ||
const cluster = getCluster(); | ||
const newNamespaceData = { | ||
apiVersion: 'v1', | ||
kind: 'Namespace', | ||
metadata: { | ||
name: namespaceName, | ||
}, | ||
}; | ||
const newNamespaceName = newNamespaceData.metadata.name; | ||
|
||
async function namespaceRequest() { | ||
try { | ||
const response = await post('/api/v1/namespaces', newNamespaceData, true, { | ||
cluster: `${cluster}`, | ||
}); | ||
return response; | ||
} catch (error) { | ||
console.error('Error creating namespace', error); | ||
throw error; | ||
} | ||
} | ||
|
||
setNamespaceDialogOpen(false); | ||
dispatch( | ||
clusterAction(() => namespaceRequest(), { | ||
startMessage: t(`Creating namespace ${newNamespaceName}…`), | ||
cancelledMessage: t(`Cancelled creation of ${newNamespaceName}.`), | ||
successMessage: t(`Created namespace ${newNamespaceName}.`), | ||
errorMessage: t(`Error creating namespace ${newNamespaceName}.`), | ||
cancelCallback: () => { | ||
setNamespaceDialogOpen(true); | ||
}, | ||
}) | ||
); | ||
} | ||
|
||
return ( | ||
<AuthVisible item={Namespace} authVerb="create"> | ||
<ActionButton | ||
color="primary" | ||
description={t('translation|Create')} | ||
icon={'mdi:plus-circle'} | ||
onClick={() => { | ||
setNamespaceDialogOpen(true); | ||
}} | ||
/> | ||
|
||
<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" | ||
fullWidth | ||
value={namespaceName} | ||
onChange={event => setNamespaceName(event.target.value)} | ||
/> | ||
</Box> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button | ||
onClick={() => { | ||
setNamespaceDialogOpen(false); | ||
}} | ||
> | ||
{t('translation|Cancel')} | ||
</Button> | ||
<Button | ||
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
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
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