-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ws): implement delete workspace action with a confirmation popup (…
…#178) * feat(ws): Notebooks 2.0 // Frontend // Delete workspace Signed-off-by: yelias <[email protected]> * Rename deleteModal.tsx Signed-off-by: yelias <[email protected]> --------- Signed-off-by: yelias <[email protected]> Co-authored-by: yelias <[email protected]>
- Loading branch information
Showing
3 changed files
with
206 additions
and
5 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
workspaces/frontend/src/__tests__/cypress/cypress/tests/e2e/Workspaces.cy.ts
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,65 @@ | ||
import { mockNamespaces } from '~/__mocks__/mockNamespaces'; | ||
import { mockBFFResponse } from '~/__mocks__/utils'; | ||
|
||
describe('Workspaces Component', () => { | ||
beforeEach(() => { | ||
// Mock the namespaces API response | ||
cy.intercept('GET', '/api/v1/namespaces', { | ||
body: mockBFFResponse(mockNamespaces), | ||
}).as('getNamespaces'); | ||
cy.visit('/'); | ||
cy.wait('@getNamespaces'); | ||
}); | ||
|
||
function openDeleteModal() { | ||
cy.findAllByTestId('table-body').first().findByTestId('action-column').click(); | ||
cy.findByTestId('action-delete').click(); | ||
cy.findByTestId('delete-modal-input').should('have.value', ''); | ||
} | ||
|
||
it('should test the close mechanisms of the delete modal', () => { | ||
const closeModalActions = [ | ||
() => cy.get('button').contains('Cancel').click(), | ||
() => cy.get('[aria-label="Close"]').click(), | ||
]; | ||
|
||
closeModalActions.forEach((closeAction) => { | ||
openDeleteModal(); | ||
cy.findByTestId('delete-modal-input').type('Some Text'); | ||
cy.findByTestId('delete-modal').should('be.visible'); | ||
closeAction(); | ||
cy.findByTestId('delete-modal').should('not.exist'); | ||
}); | ||
|
||
// Check that clicking outside the modal does not close it | ||
openDeleteModal(); | ||
cy.findByTestId('delete-modal').should('be.visible'); | ||
cy.get('body').click(0, 0); | ||
cy.findByTestId('delete-modal').should('be.visible'); | ||
}); | ||
|
||
it('should verify the delete modal verification mechanism', () => { | ||
openDeleteModal(); | ||
cy.findByTestId('delete-modal').within(() => { | ||
cy.get('strong') | ||
.first() | ||
.invoke('text') | ||
.then((resourceName) => { | ||
// Type incorrect resource name | ||
cy.findByTestId('delete-modal-input').type('Wrong Name'); | ||
cy.findByTestId('delete-modal-input').should('have.value', 'Wrong Name'); | ||
cy.findByTestId('delete-modal-helper-text').should('be.visible'); | ||
cy.get('button').contains('Delete').should('have.css', 'pointer-events', 'none'); | ||
|
||
// Clear and type correct resource name | ||
cy.findByTestId('delete-modal-input').clear(); | ||
cy.findByTestId('delete-modal-input').type(resourceName); | ||
cy.findByTestId('delete-modal-input').should('have.value', resourceName); | ||
cy.findByTestId('delete-modal-helper-text').should('not.be.exist'); | ||
cy.get('button').contains('Delete').should('not.have.css', 'pointer-events', 'none'); | ||
cy.get('button').contains('Delete').click(); | ||
cy.findByTestId('delete-modal').should('not.exist'); | ||
}); | ||
}); | ||
}); | ||
}); |
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
114 changes: 114 additions & 0 deletions
114
workspaces/frontend/src/shared/components/DeleteModal.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,114 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { | ||
Modal, | ||
ModalBody, | ||
ModalFooter, | ||
ModalHeader, | ||
ModalVariant, | ||
Button, | ||
TextInput, | ||
Stack, | ||
StackItem, | ||
FlexItem, | ||
HelperText, | ||
HelperTextItem, | ||
} from '@patternfly/react-core'; | ||
import { default as ExclamationCircleIcon } from '@patternfly/react-icons/dist/esm/icons/exclamation-circle-icon'; | ||
|
||
interface DeleteModalProps { | ||
isOpen: boolean; | ||
resourceName: string; | ||
namespace: string; | ||
onClose: () => void; | ||
onDelete: (resourceName: string) => void; | ||
title: string; | ||
} | ||
|
||
const DeleteModal: React.FC<DeleteModalProps> = ({ | ||
isOpen, | ||
resourceName, | ||
namespace, | ||
title, | ||
onClose, | ||
onDelete, | ||
}) => { | ||
const [inputValue, setInputValue] = useState(''); | ||
|
||
useEffect(() => { | ||
if (!isOpen) { | ||
setInputValue(''); | ||
} | ||
}, [isOpen]); | ||
|
||
const handleDelete = () => { | ||
if (inputValue === resourceName) { | ||
onDelete(resourceName); | ||
onClose(); | ||
} else { | ||
alert('Resource name does not match.'); | ||
} | ||
}; | ||
|
||
const handleInputChange = (event: React.FormEvent<HTMLInputElement>, value: string) => { | ||
setInputValue(value); | ||
}; | ||
|
||
const showWarning = inputValue !== '' && inputValue !== resourceName; | ||
|
||
return ( | ||
<Modal | ||
data-testid="delete-modal" | ||
variant={ModalVariant.small} | ||
title="Confirm Deletion" | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
> | ||
<ModalHeader title={title} titleIconVariant="warning" /> | ||
<ModalBody> | ||
<Stack hasGutter> | ||
<StackItem> | ||
<FlexItem> | ||
Are you sure you want to delete <strong>{resourceName}</strong> in namespace{' '} | ||
<strong>{namespace}</strong>? | ||
<br /> | ||
<br /> | ||
Please type the resource name to confirm: | ||
</FlexItem> | ||
<TextInput | ||
value={inputValue} | ||
type="text" | ||
onChange={handleInputChange} | ||
aria-label="Resource name confirmation" | ||
validated={showWarning ? 'error' : 'default'} | ||
data-testid="delete-modal-input" | ||
/> | ||
{showWarning && ( | ||
<HelperText data-testid="delete-modal-helper-text"> | ||
<HelperTextItem icon={<ExclamationCircleIcon />} variant="error"> | ||
The name doesn't match. Please enter exactly: {resourceName} | ||
</HelperTextItem> | ||
</HelperText> | ||
)} | ||
</StackItem> | ||
</Stack> | ||
</ModalBody> | ||
<ModalFooter> | ||
<div style={{ marginTop: '1rem' }}> | ||
<Button | ||
onClick={handleDelete} | ||
variant="danger" | ||
isDisabled={inputValue !== resourceName} | ||
aria-disabled={inputValue !== resourceName} | ||
> | ||
Delete | ||
</Button> | ||
<Button onClick={onClose} variant="link" style={{ marginLeft: '1rem' }}> | ||
Cancel | ||
</Button> | ||
</div> | ||
</ModalFooter> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default DeleteModal; |