-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: delete bulk operation modal COMPASS-7326 (#5005)
* chore: e2e flow * chore: add bulk state in a new substate * chore: add todo markers * chore: styling * chore: more styling 💅 * chore: linting * chore: add dependency to query-parser * chore: more styling * chore: fix height of preview * chore: linting * chore: fix package-lock.json * chore: fix package-lock * chore: fix tests * chore: test modal * chore: oops i did it again, remove the .only * chore: remove unused imports * chore: test bulk dialog * chore: last minute design changes * chore: add ts-ignore and clean up
- Loading branch information
Showing
7 changed files
with
405 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
75 changes: 75 additions & 0 deletions
75
packages/compass-crud/src/components/bulk-delete-modal.spec.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,75 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { render, screen, cleanup } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import BulkDeleteModal from './bulk-delete-modal'; | ||
|
||
function renderBulkDeleteModal( | ||
props?: Partial<React.ComponentProps<typeof BulkDeleteModal>> | ||
) { | ||
return render( | ||
<BulkDeleteModal | ||
open={true} | ||
documentCount={0} | ||
filterQuery="{ a: 1 }" | ||
namespace="mydb.mycoll" | ||
sampleDocuments={[]} | ||
onCancel={() => {}} | ||
onConfirmDeletion={() => {}} | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
describe('BulkDeleteModal Component', function () { | ||
afterEach(function () { | ||
cleanup(); | ||
}); | ||
|
||
it('does not render if closed', function () { | ||
renderBulkDeleteModal({ open: false }); | ||
expect(screen.queryByText(/Delete/)).to.not.exist; | ||
}); | ||
|
||
it('does render if open', function () { | ||
renderBulkDeleteModal(); | ||
expect(screen.queryAllByText(/Delete/)).to.not.be.empty; | ||
}); | ||
|
||
it('shows the number of documents that will be deleted', function () { | ||
renderBulkDeleteModal({ documentCount: 42 }); | ||
expect(screen.queryAllByText('Delete 42 documents')[0]).to.be.visible; | ||
}); | ||
|
||
it('shows the affected collection', function () { | ||
renderBulkDeleteModal({ namespace: 'mydb.mycoll' }); | ||
expect(screen.queryByText('mydb.mycoll')).to.be.visible; | ||
}); | ||
|
||
it('shows the provided query', function () { | ||
renderBulkDeleteModal({ filterQuery: '{ a: 1 }' }); | ||
expect(screen.queryByDisplayValue('{ a: 1 }')).to.be.visible; | ||
}); | ||
|
||
it('closes the modal when cancelled', function () { | ||
const onCloseSpy = sinon.spy(); | ||
renderBulkDeleteModal({ onCancel: onCloseSpy }); | ||
|
||
userEvent.click(screen.getByText('Close').closest('button')!); | ||
expect(onCloseSpy).to.have.been.calledOnce; | ||
}); | ||
|
||
it('confirms deletion when clicked on the Delete documents button', function () { | ||
const onConfirmDeletionSpy = sinon.spy(); | ||
renderBulkDeleteModal({ | ||
documentCount: 10, | ||
onConfirmDeletion: onConfirmDeletionSpy, | ||
}); | ||
|
||
userEvent.click( | ||
screen.getAllByText('Delete 10 documents')[1].closest('button')! | ||
); | ||
expect(onConfirmDeletionSpy).to.have.been.calledOnce; | ||
}); | ||
}); |
159 changes: 159 additions & 0 deletions
159
packages/compass-crud/src/components/bulk-delete-modal.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,159 @@ | ||
import React from 'react'; | ||
import { | ||
Modal, | ||
ModalHeader, | ||
ModalBody, | ||
ModalFooter, | ||
Button, | ||
TextInput, | ||
KeylineCard, | ||
css, | ||
cx, | ||
spacing, | ||
InfoSprinkle, | ||
Label, | ||
} from '@mongodb-js/compass-components'; | ||
import ReadonlyDocument from './readonly-document'; | ||
|
||
const modalFooterSpacingStyles = css({ | ||
gap: spacing[2], | ||
}); | ||
|
||
const documentHorizontalWrapper = css({ | ||
display: 'flex', | ||
flexDirection: 'row', | ||
flex: 'none', | ||
flexShrink: 0, | ||
overflow: 'auto', | ||
marginBottom: spacing[2], | ||
gap: spacing[2], | ||
maxWidth: '100%', | ||
}); | ||
|
||
const documentContainerStyles = css({ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
flex: 'none', | ||
flexShrink: 0, | ||
marginBottom: spacing[2], | ||
width: '100%', | ||
}); | ||
|
||
const documentStyles = css({ | ||
flexBasis: '164px', | ||
flexGrow: 1, | ||
flexShrink: 0, | ||
overflow: 'auto', | ||
padding: 0, | ||
width: '100%', | ||
}); | ||
|
||
const modalBodySpacingStyles = css({ | ||
marginTop: spacing[3], | ||
paddingLeft: spacing[5], | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: spacing[3], | ||
}); | ||
|
||
const previewStyles = css({ | ||
minHeight: '200px', | ||
}); | ||
|
||
type QueryLabelProps = { | ||
tooltip: string; | ||
label: string; | ||
}; | ||
|
||
const queryLabelStyles = css({ | ||
display: 'flex', | ||
gap: spacing[2], | ||
alignItems: 'center', | ||
}); | ||
|
||
const QueryLabel: React.FunctionComponent<QueryLabelProps> = ({ | ||
tooltip, | ||
label, | ||
}) => { | ||
return ( | ||
<div className={queryLabelStyles}> | ||
<Label htmlFor="template-dropdown">{label}</Label> | ||
<InfoSprinkle align="right">{tooltip}</InfoSprinkle> | ||
</div> | ||
); | ||
}; | ||
|
||
type BulkDeleteModalProps = { | ||
open: boolean; | ||
documentCount: number; | ||
filterQuery: string; | ||
namespace: string; | ||
sampleDocuments: Document[]; | ||
onCancel: () => void; | ||
onConfirmDeletion: () => void; | ||
}; | ||
|
||
const BulkDeleteModal: React.FunctionComponent<BulkDeleteModalProps> = ({ | ||
open, | ||
documentCount, | ||
filterQuery, | ||
namespace, | ||
sampleDocuments, | ||
onCancel, | ||
onConfirmDeletion, | ||
}) => { | ||
const preview = ( | ||
<div className={documentHorizontalWrapper}> | ||
{sampleDocuments.map((doc, i) => { | ||
return ( | ||
<KeylineCard | ||
key={i} | ||
className={cx(documentContainerStyles, previewStyles)} | ||
> | ||
<div className={documentStyles}> | ||
<ReadonlyDocument doc={doc as any} expandAll={false} /> | ||
</div> | ||
</KeylineCard> | ||
); | ||
})} | ||
</div> | ||
); | ||
|
||
return ( | ||
<Modal setOpen={onCancel} open={open}> | ||
<ModalHeader | ||
title={`Delete ${documentCount} documents`} | ||
subtitle={namespace} | ||
variant={'danger'} | ||
/> | ||
<ModalBody variant={'danger'} className={modalBodySpacingStyles}> | ||
<TextInput | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore the label can be any component, but it's weirdly typed to string | ||
label={ | ||
<QueryLabel | ||
label="Query" | ||
tooltip="Return to the Documents tab to edit this query." | ||
/> | ||
} | ||
disabled={true} | ||
value={filterQuery} | ||
/> | ||
<div> | ||
<b>Preview (sample of {sampleDocuments.length} documents)</b> | ||
{preview} | ||
</div> | ||
</ModalBody> | ||
<ModalFooter className={modalFooterSpacingStyles}> | ||
<Button variant="danger" onClick={onConfirmDeletion}> | ||
Delete {documentCount} documents | ||
</Button> | ||
<Button variant="default" onClick={onCancel}> | ||
Close | ||
</Button> | ||
</ModalFooter> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default BulkDeleteModal; |
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.