UX: Confirm before closing unsaved changes (#68)

This commit is contained in:
Keegan George 2023-11-03 13:47:56 -07:00 committed by GitHub
parent ca8aad0761
commit 0bca746a68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 3 deletions

View File

@ -1,6 +1,6 @@
<DModal <DModal
@title={{i18n this.modalAttributes.title}} @title={{i18n this.modalAttributes.title}}
@closeModal={{@closeModal}} @closeModal={{this.interceptCloseModal}}
class="insert-table-modal" class="insert-table-modal"
> >
<:body> <:body>
@ -24,7 +24,7 @@
<DButton <DButton
@class="btn-flat" @class="btn-flat"
@label={{theme-prefix "discourse_table_builder.edit.modal.cancel"}} @label={{theme-prefix "discourse_table_builder.edit.modal.cancel"}}
@action={{@closeModal}} @action={{this.interceptCloseModal}}
/> />
</div> </div>

View File

@ -12,10 +12,12 @@ import { popupAjaxError } from "discourse/lib/ajax-error";
import I18n from "I18n"; import I18n from "I18n";
import { schedule } from "@ember/runloop"; import { schedule } from "@ember/runloop";
import { tracked } from "@glimmer/tracking"; import { tracked } from "@glimmer/tracking";
import { inject as service } from "@ember/service";
export default class SpreadsheetEditor extends Component { export default class SpreadsheetEditor extends Component {
@service dialog;
@tracked showEditReason = false; @tracked showEditReason = false;
@tracked loading = null; @tracked loading = null;
spreadsheet = null; spreadsheet = null;
defaultColWidth = 150; defaultColWidth = 150;
isEditingTable = !!this.args.model.tableTokens; isEditingTable = !!this.args.model.tableTokens;
@ -62,6 +64,20 @@ export default class SpreadsheetEditor extends Component {
this.showEditReason = !this.showEditReason; this.showEditReason = !this.showEditReason;
} }
@action
interceptCloseModal() {
if (this._hasChanges()) {
this.dialog.yesNoConfirm({
message: I18n.t(
themePrefix("discourse_table_builder.modal.confirm_close")
),
didConfirm: () => this.args.closeModal(),
});
} else {
this.args.closeModal();
}
}
@action @action
insertTable() { insertTable() {
const updatedHeaders = this.spreadsheet.getHeaders().split(","); // keys const updatedHeaders = this.spreadsheet.getHeaders().split(","); // keys
@ -76,6 +92,27 @@ export default class SpreadsheetEditor extends Component {
} }
} }
_hasChanges() {
if (this.isEditingTable) {
const originalSpreadsheetData = this.extractTableContent(
tokenRange(this.args.model.tableTokens, "tr_open", "tr_close")
);
const currentHeaders = this.spreadsheet.getHeaders().split(",");
const currentRows = this.spreadsheet.getData();
const currentSpreadsheetData = currentHeaders.concat(currentRows.flat());
return (
JSON.stringify(currentSpreadsheetData) !==
JSON.stringify(originalSpreadsheetData)
);
} else {
return this.spreadsheet
.getData()
.flat()
.some((element) => element !== "");
}
}
// Helper Methods: // Helper Methods:
loadLibraries() { loadLibraries() {
this.loading = true; this.loading = true;

View File

@ -14,6 +14,7 @@ en:
new_row: "at the end of a row to insert a new row." new_row: "at the end of a row to insert a new row."
new_col: "at the end of a column to insert a new column." new_col: "at the end of a column to insert a new column."
options: "Right-click on cells to access more options in a dropdown menu." options: "Right-click on cells to access more options in a dropdown menu."
confirm_close: "Are you sure you want to close the table builder? Any unsaved changes will be lost."
edit: edit:
btn_edit: "Edit Table" btn_edit: "Edit Table"
modal: modal: