Skip to content

Commit e7dc505

Browse files
committed
fix: rename to stop editing and properly handle editing finished
1 parent fe04ed8 commit e7dc505

File tree

3 files changed

+61
-51
lines changed

3 files changed

+61
-51
lines changed

packages/compass-components/src/components/document-list/document-edit-actions-footer.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ function useHadronDocumentStatus(
166166
updateStatus('DeleteError', err, errorDetails);
167167
};
168168

169+
const onEditingFinished = () => {
170+
updateStatus('Initial');
171+
};
172+
169173
doc.on(ElementEvents.Added, onUpdate);
170174
doc.on(ElementEvents.Edited, onUpdate);
171175
doc.on(ElementEvents.Removed, onUpdate);
@@ -179,6 +183,7 @@ function useHadronDocumentStatus(
179183
doc.on(DocumentEvents.RemoveStarted, onRemoveStart);
180184
doc.on(DocumentEvents.RemoveSuccess, onRemoveSuccess);
181185
doc.on(DocumentEvents.RemoveError, onRemoveError);
186+
doc.on(DocumentEvents.EditingFinished, onEditingFinished);
182187

183188
return () => {
184189
doc.on(ElementEvents.Added, onUpdate);
@@ -194,6 +199,7 @@ function useHadronDocumentStatus(
194199
doc.off(DocumentEvents.RemoveStarted, onRemoveStart);
195200
doc.off(DocumentEvents.RemoveSuccess, onRemoveSuccess);
196201
doc.off(DocumentEvents.RemoveError, onRemoveError);
202+
doc.off(DocumentEvents.EditingFinished, onEditingFinished);
197203
};
198204
}, [doc, updateStatus]);
199205

packages/compass-crud/src/components/use-document-item-context-menu.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('useDocumentItemContextMenu', function () {
103103
userEvent.click(screen.getByTestId('test-container'), { button: 2 });
104104

105105
// Should show "Stop editing" when editing
106-
expect(screen.getByText('Stop editing')).to.exist;
106+
expect(screen.getByText('Cancel editing')).to.exist;
107107
expect(screen.queryByText('Edit document')).to.not.exist;
108108
// But show other operations
109109
expect(screen.getByText('Expand all fields')).to.exist;
@@ -182,7 +182,7 @@ describe('useDocumentItemContextMenu', function () {
182182

183183
// Should show "Edit document" when not editing
184184
expect(screen.getByText('Edit document')).to.exist;
185-
expect(screen.queryByText('Stop editing')).to.not.exist;
185+
expect(screen.queryByText('Cancel editing')).to.not.exist;
186186

187187
// Click edit
188188
userEvent.click(screen.getByText('Edit document'), undefined, {
@@ -207,11 +207,11 @@ describe('useDocumentItemContextMenu', function () {
207207
userEvent.click(screen.getByTestId('test-container'), { button: 2 });
208208

209209
// Should show "Stop editing" when editing
210-
expect(screen.getByText('Stop editing')).to.exist;
210+
expect(screen.getByText('Cancel editing')).to.exist;
211211
expect(screen.queryByText('Edit document')).to.not.exist;
212212

213213
// Click stop editing
214-
userEvent.click(screen.getByText('Stop editing'), undefined, {
214+
userEvent.click(screen.getByText('Cancel editing'), undefined, {
215215
skipPointerEventsCheck: true,
216216
});
217217

packages/compass-crud/src/components/use-document-item-context-menu.tsx

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type HadronDocument from 'hadron-document';
2-
import { useContextMenuItems } from '@mongodb-js/compass-components';
2+
import { useContextMenuGroups } from '@mongodb-js/compass-components';
33

44
import type { DocumentProps } from './document';
55

@@ -15,57 +15,61 @@ export function useDocumentItemContextMenu({
1515
openInsertDocumentDialog,
1616
}: UseDocumentItemContextMenuProps) {
1717
const { expanded: isExpanded, editing: isEditing } = doc;
18-
return useContextMenuItems(
18+
return useContextMenuGroups(
1919
() => [
20-
{
21-
label: isExpanded ? 'Collapse all fields' : 'Expand all fields',
22-
onAction: () => {
23-
if (isExpanded) {
24-
doc.collapse();
25-
} else {
26-
doc.expand();
27-
}
28-
},
29-
},
30-
...(isEditable
31-
? [
32-
{
33-
label: isEditing ? 'Stop editing' : 'Edit document',
34-
onAction: () => {
35-
if (isEditing) {
36-
doc.finishEditing();
37-
} else {
38-
doc.startEditing();
39-
}
20+
[
21+
...(isEditable
22+
? [
23+
{
24+
label: isEditing ? 'Cancel editing' : 'Edit document',
25+
onAction: () => {
26+
if (isEditing) {
27+
doc.finishEditing();
28+
} else {
29+
doc.startEditing();
30+
}
31+
},
4032
},
41-
},
42-
]
43-
: []),
44-
{
45-
label: 'Copy document',
46-
onAction: () => {
47-
copyToClipboard?.(doc);
33+
]
34+
: []),
35+
],
36+
[
37+
{
38+
label: isExpanded ? 'Collapse all fields' : 'Expand all fields',
39+
onAction: () => {
40+
if (isExpanded) {
41+
doc.collapse();
42+
} else {
43+
doc.expand();
44+
}
45+
},
46+
},
47+
{
48+
label: 'Copy document',
49+
onAction: () => {
50+
copyToClipboard?.(doc);
51+
},
4852
},
49-
},
50-
...(isEditable
51-
? [
52-
{
53-
label: 'Clone document...',
54-
onAction: () => {
55-
const clonedDoc = doc.generateObject({
56-
excludeInternalFields: true,
57-
});
58-
void openInsertDocumentDialog?.(clonedDoc, true);
53+
...(isEditable
54+
? [
55+
{
56+
label: 'Clone document...',
57+
onAction: () => {
58+
const clonedDoc = doc.generateObject({
59+
excludeInternalFields: true,
60+
});
61+
void openInsertDocumentDialog?.(clonedDoc, true);
62+
},
5963
},
60-
},
61-
{
62-
label: 'Delete document',
63-
onAction: () => {
64-
doc.markForDeletion();
64+
{
65+
label: 'Delete document',
66+
onAction: () => {
67+
doc.markForDeletion();
68+
},
6569
},
66-
},
67-
]
68-
: []),
70+
]
71+
: []),
72+
],
6973
],
7074
[
7175
doc,

0 commit comments

Comments
 (0)