Skip to content

A platform for community discussion. Free, open, simple.

FIX: Add delete button to themes grid (#34606)

Krzysztof Kotlareke774b0c46a5b
diff --git a/app/assets/javascripts/admin/addon/components/themes-grid-card.gjs b/app/assets/javascripts/admin/addon/components/themes-grid-card.gjs
index 4d79e02d06b..f4d3ccd8035 100644
--- a/app/assets/javascripts/admin/addon/components/themes-grid-card.gjs
+++ b/app/assets/javascripts/admin/addon/components/themes-grid-card.gjs
@@ -20,6 +20,7 @@ import ThemesGridPlaceholder from "./themes-grid-placeholder";
// to change as we improve this incrementally.
export default class ThemeCard extends Component {
@service toasts;
+ @service dialog;
@tracked isUpdating = false;
@@ -40,6 +41,10 @@ export default class ThemeCard extends Component {
return `/admin/themes/${this.args.theme.id}/preview`;
}
+ get destroyDisabled() {
+ return this.args.theme.default || this.args.theme.system;
+ }
+
@action
onRegisterApi(api) {
this.dMenu = api;
@@ -79,7 +84,6 @@ export default class ThemeCard extends Component {
theme: this.args.theme.name,
}),
},
- duration: "short",
});
window.location.reload();
@@ -102,7 +106,6 @@ export default class ThemeCard extends Component {
data: {
message: i18n("admin.customize.theme.setting_was_saved"),
},
- duration: "short",
});
}
@@ -122,7 +125,6 @@ export default class ThemeCard extends Component {
theme: this.args.theme.name,
}),
},
- duration: "short",
});
})
.catch(popupAjaxError)
@@ -131,6 +133,31 @@ export default class ThemeCard extends Component {
});
}
+ @action
+ destroyTheme() {
+ return this.dialog.deleteConfirm({
+ title: i18n("admin.customize.delete_confirm", {
+ theme_name: this.args.theme.name,
+ }),
+ didConfirm: async () => {
+ try {
+ await this.args.theme.destroyRecord();
+ this.args.allThemes.removeObject(this.args.theme);
+
+ this.toasts.success({
+ data: {
+ message: i18n("admin.customize.theme.delete_success", {
+ theme: this.args.theme.name,
+ }),
+ },
+ });
+ } catch (error) {
+ popupAjaxError(error);
+ }
+ },
+ });
+ }
+
<template>
<AdminConfigAreaCard class={{this.themeCardClasses}}>
<:content>
@@ -260,6 +287,15 @@ export default class ThemeCard extends Component {
>{{icon "eye"}}
{{i18n "admin.customize.theme.preview"}}</a>
</dropdown.item>
+ <dropdown.item>
+ <DButton
+ @action={{this.destroyTheme}}
+ @label="admin.customize.delete"
+ @icon="trash-can"
+ @disabled={{this.destroyDisabled}}
+ class="theme-card__button btn-danger btn-transparent delete"
+ />
+ </dropdown.item>
</DropdownMenu>
</:content>
</DMenu>
diff --git a/app/assets/javascripts/admin/addon/templates/customize-themes-show-index.gjs b/app/assets/javascripts/admin/addon/templates/customize-themes-show-index.gjs
index 6e27ca77df6..bcf6cf01385 100644
--- a/app/assets/javascripts/admin/addon/templates/customize-themes-show-index.gjs
+++ b/app/assets/javascripts/admin/addon/templates/customize-themes-show-index.gjs
@@ -487,7 +487,7 @@ export default RouteTemplate(
class="btn-default btn-normal"
/>
{{/if}}
- {{#unless @controller.model.system}}
+ {{#unless (or @controller.model.system @controller.model.default)}}
<DButton
@action={{@controller.destroyTheme}}
@label="admin.customize.delete"
diff --git a/app/serializers/theme_index_serializer.rb b/app/serializers/theme_index_serializer.rb
index 483964f59df..c1b714b48b9 100644
--- a/app/serializers/theme_index_serializer.rb
+++ b/app/serializers/theme_index_serializer.rb
@@ -1,8 +1,12 @@
# frozen_string_literal: true
class ThemeIndexSerializer < BasicThemeSerializer
- attributes :user_selectable, :screenshot_url, :remote_theme_id, :enabled?, :supported?
+ attributes :user_selectable, :screenshot_url, :remote_theme_id, :enabled?, :supported?, :system
has_one :color_scheme, serializer: ColorSchemeSerializer, embed: :object
has_one :remote_theme, serializer: RemoteThemeSerializer, embed: :objects
+
+ def system
+ object.system?
+ end
end
diff --git a/app/services/themes/destroy.rb b/app/services/themes/destroy.rb
index 95256838b19..59d8eae9843 100644
--- a/app/services/themes/destroy.rb
+++ b/app/services/themes/destroy.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
# Destroys a theme and logs the staff action. Related records are destroyed
-# by ActiveRecord dependent: :destroy. Cannot be used to destroy system themes.
+# by ActiveRecord dependent: :destroy. Cannot be used to destroy system or default themes.
#
# @example
# Themes::Destroy.call(
@@ -28,6 +28,7 @@ class Themes::Destroy
end
model :theme
+ policy :not_default
transaction do
step :destroy_theme
@@ -40,6 +41,10 @@ class Themes::Destroy
Theme.find_by(id: params.id)
end
+ def not_default(theme:)
+ !theme.default?
+ end
+
def destroy_theme(theme:)
theme.destroy
end
diff --git a/spec/services/themes/destroy_spec.rb b/spec/services/themes/destroy_spec.rb
index 0f6539e7264..6e03cc91cd0 100644
--- a/spec/services/themes/destroy_spec.rb
+++ b/spec/services/themes/destroy_spec.rb
@@ -28,6 +28,12 @@ RSpec.describe Themes::Destroy do
it { is_expected.to fail_to_find_a_model(:theme) }
end
+ context "for a default theme" do
+ before { theme.set_default! }
+
+ it { is_expected.to fail_a_policy(:not_default) }
+ end
+
context "when everything is ok" do
it { is_expected.to run_successfully }
diff --git a/spec/system/admin_customize_themes_config_area_spec.rb b/spec/system/admin_customize_themes_config_area_spec.rb
index edd1fce2cab..b7e2111281b 100644
--- a/spec/system/admin_customize_themes_config_area_spec.rb
+++ b/spec/system/admin_customize_themes_config_area_spec.rb
@@ -36,6 +36,19 @@ describe "Admin Customize Themes Config Area Page", type: :system do
expect(page).to have_current_path("/admin/config/customize/themes")
end
+ it "allows to delete not system and not default theme" do
+ theme.set_default!
+ config_area.visit
+
+ expect(config_area).to have_disabled_delete_button(theme)
+
+ expect(config_area).to have_disabled_delete_button(horizon_theme)
+
+ expect(config_area).to have_themes(["First theme", "Horizon", "Foundation", "Second theme"])
+ config_area.delete_theme(theme_2)
+ expect(config_area).to have_no_theme("Second theme")
+ end
+
it "allows to mark theme as default" do
config_area.visit
expect(config_area).to have_default_badge(foundation_theme)
diff --git a/spec/system/page_objects/pages/admin_customize_themes_config_area.rb b/spec/system/page_objects/pages/admin_customize_themes_config_area.rb
index a75f12c87f8..50464bc9bc8 100644
--- a/spec/system/page_objects/pages/admin_customize_themes_config_area.rb
+++ b/spec/system/page_objects/pages/admin_customize_themes_config_area.rb
@@ -25,6 +25,14 @@ module PageObjects
find(".set-default").click
end
+ def delete_theme(theme)
+ open_theme_menu(theme)
+ find(".delete").click
+ confirmation_dialog = PageObjects::Components::Dialog.new
+ confirmation_dialog.click_danger
+ expect(confirmation_dialog).to be_closed
+ end
+
def has_default_badge?(theme)
has_badge?(theme, "--default", text: I18n.t("admin_js.admin.customize.theme.default"))
end
@@ -41,6 +49,11 @@ module PageObjects
find_theme_card(theme).has_no_css?(".theme-card__badge.#{badge}")
end
+ def has_disabled_delete_button?(theme)
+ open_theme_menu(theme)
+ has_css?(".btn-danger.delete[disabled]")
+ end
+
def has_themes?(names)
expect(all(".theme-card__title").map(&:text)).to eq(names)
end