FIX: correctly reset form before destroying it (#28516)

This change is preventing the "is dirty check" from happening when clicking delete on this form. This was not good UX and was also causing bugs by leaving the form in a unexpected state.
This commit is contained in:
Joffrey JAFFEUX 2024-08-23 14:58:31 +02:00 committed by GitHub
parent fe6c91daa3
commit fee8caf529
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 37 additions and 3 deletions

View File

@ -201,19 +201,24 @@ export default class AdminBadgesShowController extends Controller {
}
}
@action
registerApi(api) {
this.formApi = api;
}
@action
async handleDelete() {
if (!this.model?.id) {
return this.router.transitionTo("adminBadges.index");
}
const adminBadges = this.adminBadges.model;
return this.dialog.yesNoConfirm({
message: I18n.t("admin.badges.delete_confirm"),
didConfirm: async () => {
try {
await this.formApi.reset();
await this.model.destroy();
adminBadges.removeObject(this.model);
this.adminBadges.model.removeObject(this.model);
this.router.transitionTo("adminBadges.index");
} catch {
this.dialog.alert(I18n.t("generic_error"));

View File

@ -2,6 +2,7 @@
@data={{this.formData}}
@onSubmit={{this.handleSubmit}}
@validate={{this.validateForm}}
@onRegisterApi={{this.registerApi}}
class="badge-form current-badge content-body"
as |form data|
>
@ -295,7 +296,10 @@
<form.Submit />
{{#unless this.readOnly}}
<form.Button @action={{this.handleDelete}} class="btn-danger">
<form.Button
@action={{this.handleDelete}}
class="badge-form__delete-badge-btn btn-danger"
>
{{i18n "admin.badges.delete"}}
</form.Button>
{{/unless}}

View File

@ -93,4 +93,23 @@ describe "Admin Badges Page", type: :system do
expect(badges_page.form.field("target_posts")).to be_unchecked
end
end
context "when deleting a badge" do
let(:dialog) { PageObjects::Components::Dialog.new }
it "deletes a badge" do
badges_page.new_page
badges_page.form.field("enabled").accept
badges_page.form.field("name").fill_in("a name")
badges_page.form.field("badge_type_id").select(BadgeType::Bronze)
badges_page.form.field("icon").select("ambulance")
badges_page.submit_form
expect(badges_page).to have_saved_form
badges_page.form.field("name").fill_in("another name")
badges_page.delete_badge
dialog.click_yes
expect(page).to have_current_path("/admin/badges")
end
end
end

View File

@ -25,6 +25,12 @@ module PageObjects
def submit_form
form.submit
self
end
def delete_badge
page.find(".badge-form__delete-badge-btn").click
self
end
def choose_icon(name)