Skip to content

Commit

Permalink
openfoodfoundation#11068: add delete variant and product reflexes
Browse files Browse the repository at this point in the history
  • Loading branch information
chahmedejaz authored and Flavien LE BLOND committed Mar 13, 2024
1 parent 17364b3 commit 2ddf495
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
18 changes: 18 additions & 0 deletions app/reflexes/products_reflex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ def bulk_update
render_products_form_with_flash
end

def delete_product(product_id)
if ProductDeleter.delete(product_id)
puts "Deleted Successfully"
else
puts "Failure"
end
fetch_and_render_products
end

def delete_variant(variant_id)
if VariantDeleter.new.delete(variant_id)
puts "Deleted Successfully"
else
puts "Failure"
end
fetch_and_render_products
end

private

def init_filters_params
Expand Down
6 changes: 4 additions & 2 deletions app/services/product_deleter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

# This soft deletes the product
class ProductDeleter
def self.delete(product)
product.destroy
# @param id [int] ID of the product to be deleted
def self.delete(id)
product = Spree::Product.find_by(id:)
product&.destroy
end
end
4 changes: 2 additions & 2 deletions app/views/admin/products_v3/_delete_modals.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
confirm_button_text: t(".delete_#{object_type}_modal.confirmation_text"),
cancel_button_text: t(".delete_#{object_type}_modal.cancellation_text"),
confirm_button_color: :red,
controller: "product-actions",
confirm_actions: "click->product-actions#delete#{object_type.titleize}",
confirm_reflexes: nil,
controller: "products",
confirm_actions: "click->products#delete#{object_type.titleize}",
controller_data_values: {"current-id": object_id},
)
= render delete_modal do
Expand Down
19 changes: 18 additions & 1 deletion app/webpacker/controllers/products_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ import ApplicationController from "./application_controller";

export default class extends ApplicationController {
static targets = ["loading"];
static values = {currentId: Number};

connect() {
super.connect();
// Fetch the products on page load
this.stimulate("Products#fetch");
}

deleteProduct() {
window.dispatchEvent(new Event('modal:close'));
this.stimulate('Products#delete_product', this.currentIdValue)
}

deleteVariant() {
this.stimulate('Products#delete_variant', this.currentIdValue)
}

beforeReflex() {
this.showLoading();
this.scrollToElement();
Expand All @@ -35,8 +45,15 @@ export default class extends ApplicationController {
};

getLoadingController = () => {
let loadingSpinner = null;
try {
loadingSpinner = this.loadingTarget; // throws missing loading target error
} catch (error) {
loadingSpinner = document.getElementById('loading-spinner');
}

return (this.loadingController ||= this.application.getControllerForElementAndIdentifier(
this.loadingTarget,
loadingSpinner,
"loading"
));
};
Expand Down

0 comments on commit 2ddf495

Please sign in to comment.