-
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
8 changed files
with
156 additions
and
8 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
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,126 @@ | ||
import { | ||
Box, | ||
Button, | ||
Dialog, | ||
DialogActions, | ||
DialogContent, | ||
DialogTitle, | ||
TextField, | ||
Typography, | ||
} from '@mui/material'; | ||
import React, { useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useDispatch } from 'react-redux'; | ||
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(); | ||
const kubeNamespace = Namespace; | ||
|
||
function toggleCreateNamespaceVisibility() { | ||
setNamespaceDialogOpen(namespaceDialogOpen => !namespaceDialogOpen); | ||
} | ||
|
||
console.log('kubeNamespace', kubeNamespace); | ||
|
||
function createNewNamespace() { | ||
const newNamespaceData = { | ||
apiVersion: 'v1', | ||
kind: 'Namespace', | ||
metadata: { | ||
name: namespaceName, | ||
}, | ||
}; | ||
|
||
// need this for the item name? | ||
const itemName = newNamespaceData.metadata.name; | ||
|
||
// need a callback so just made one to use | ||
const callback = (itemName: string) => { | ||
console.log(`Callback called with ${itemName}`); | ||
}; | ||
|
||
async function createNamesapce(newNamespaceData: any) { | ||
try { | ||
const response = await post('/api/v1/namespaces', newNamespaceData); | ||
console.log('response', response); | ||
|
||
toggleCreateNamespaceVisibility(); | ||
dispatch( | ||
clusterAction(callback.bind(itemName), { | ||
startMessage: t('Creating namespace {{ itemName }}…', { itemName }), | ||
cancelledMessage: t('Cancelled creation of {{ itemName }}.', { itemName }), | ||
successMessage: t('Created namesapce {{ itemName }}.', { itemName }), | ||
errorMessage: t('Error creating namespace {{ itemName }}.', { itemName }), | ||
cancelUrl: location.pathname, | ||
// startUrl: item!.getListLink(), | ||
// errorUrl: item!.getListLink(), | ||
// ...options, | ||
}) | ||
); | ||
|
||
return response; | ||
} catch (error) { | ||
console.error('Error creating namespace', error); | ||
|
||
toggleCreateNamespaceVisibility(); | ||
return error; | ||
} | ||
} | ||
|
||
createNamesapce(newNamespaceData); | ||
} | ||
|
||
return ( | ||
<AuthVisible item={kubeNamespace} authVerb="create" namespace="default"> | ||
<ActionButton | ||
color="primary" | ||
description={t('translation|Create')} | ||
icon={'mdi:plus-circle'} | ||
onClick={() => { | ||
toggleCreateNamespaceVisibility(); | ||
}} | ||
/> | ||
|
||
<Dialog open={namespaceDialogOpen} onClose={() => toggleCreateNamespaceVisibility()}> | ||
<DialogTitle>{t('translation|Create Namespace')}</DialogTitle> | ||
<DialogContent> | ||
<Box component="form"> | ||
<Typography>Please enter the name of the new namespace</Typography> | ||
<TextField | ||
margin="dense" | ||
id="name" | ||
label={t('glossary|Namespace')} | ||
type="text" | ||
fullWidth | ||
value={namespaceName} | ||
onChange={event => setNamespaceName(event.target.value)} | ||
/> | ||
</Box> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button | ||
onClick={() => { | ||
toggleCreateNamespaceVisibility(); | ||
dispatchCreateEvent({ | ||
status: EventStatus.CONFIRMED, | ||
}); | ||
}} | ||
> | ||
{t('translation|Cancel')} | ||
</Button> | ||
<Button onClick={() => createNewNamespace()}>{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