Skip to content

Commit eeb6a96

Browse files
committed
Fix editing error
1 parent ab6edfb commit eeb6a96

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

src/CodeSnippetEditor.tsx

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ export class CodeSnippetEditor extends ReactWidget {
256256
Dialog.okButton({ label: 'Save' }),
257257
],
258258
}).then((response: any): void => {
259-
console.log(response.button);
260259
if (response.button.accept) {
261260
if (response.button.label === 'Discard') {
262261
this.dispose();
@@ -418,54 +417,58 @@ export class CodeSnippetEditor extends ReactWidget {
418417
tags: this._codeSnippetEditorMetaData.selectedTags,
419418
};
420419

421-
if (newName !== oldName) {
422-
try {
423-
this.contentsService.duplicateNameExists(newName);
424-
} catch (e) {
425-
await showDialog({
426-
title: e.message,
427-
body: <p> {`"${newName}" already exists.`} </p>,
428-
buttons: [Dialog.okButton({ label: 'Dismiss' })],
429-
});
430-
return false;
431-
}
432-
}
420+
const isDuplicatName = this.contentsService.duplicateNameExists(newName);
433421

434422
// set new name as an old name
435423
this.oldCodeSnippetName = this._codeSnippetEditorMetaData.name;
436424

437425
// add new snippet
438426
if (this._codeSnippetEditorMetaData.fromScratch) {
439-
this.contentsService.addSnippet(newSnippet).then((res: boolean) => {
440-
if (!res) {
441-
console.log('Error in adding snippet');
442-
return false;
443-
}
444-
});
427+
if (isDuplicatName) {
428+
const oldSnippet = this.contentsService.getSnippet(newName)[0];
429+
await saveOverWriteFile(this.contentsService, oldSnippet, newSnippet);
430+
} else {
431+
this.contentsService.addSnippet(newSnippet).then((res: boolean) => {
432+
if (!res) {
433+
console.log('Error in adding snippet');
434+
return false;
435+
}
436+
});
437+
}
445438
}
446439
// modify existing snippet
447440
else {
448-
console.log('modify existing snippet');
449-
console.log(oldName);
450-
console.log(newSnippet);
451-
452-
if (oldName.toLowerCase() === newSnippet.name.toLowerCase()) {
453-
const oldSnippet = this.contentsService.getSnippet(oldName)[0];
454-
saveOverWriteFile(this.contentsService, oldSnippet, newSnippet).then(
455-
(res: boolean) => {
441+
if (newName !== oldName) {
442+
if (isDuplicatName) {
443+
// overwrite
444+
const oldSnippet = this.contentsService.getSnippet(newName)[0];
445+
await saveOverWriteFile(
446+
this.contentsService,
447+
oldSnippet,
448+
newSnippet
449+
).then((res: boolean) => {
456450
if (res) {
457-
this.contentsService
458-
.modifyExistingSnippet(oldName, newSnippet)
459-
.then((res: boolean) => {
460-
if (!res) {
461-
console.log('Error in modifying snippet');
462-
return false;
463-
}
464-
});
451+
// get the id of snippet you are editting
452+
const removedSnippet = this.contentsService.getSnippet(
453+
oldName
454+
)[0];
455+
456+
// delete the one you are editting
457+
this.contentsService.deleteSnippet(removedSnippet.id);
458+
} else {
459+
return false;
465460
}
466-
}
467-
);
461+
});
462+
}
468463
}
464+
this.contentsService
465+
.modifyExistingSnippet(oldName, newSnippet)
466+
.then((res: boolean) => {
467+
if (!res) {
468+
console.log('Error in modifying snippet');
469+
return false;
470+
}
471+
});
469472
}
470473

471474
this.saved = true;

0 commit comments

Comments
 (0)