From 766396b979bfcfcfe5d4bd5635e9b6e4ee69ace5 Mon Sep 17 00:00:00 2001 From: wandji20 Date: Sun, 13 Oct 2024 01:18:21 +0100 Subject: [PATCH] Handle enterprise edit response with turbo_stream --- app/controllers/admin/enterprises_controller.rb | 7 ++++--- app/views/admin/enterprises/edit.turbo_stream.haml | 4 ++++ .../controllers/primary_details_controller.js | 14 +++++++------- 3 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 app/views/admin/enterprises/edit.turbo_stream.haml diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index e5b35473ac..8a881aa840 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -7,7 +7,6 @@ require 'open_food_network/order_cycle_permissions' module Admin class EnterprisesController < Admin::ResourceController include GeocodeEnterpriseAddress - include CablecarResponses include Pagy::Backend # These need to run before #load_resource so that @object is initialised with sanitised values @@ -51,8 +50,10 @@ module Admin @enterprise.is_primary_producer = params[:is_primary_producer] @enterprise.sells = params[:enterprise_sells] - render cable_ready: cable_car.morph("#side_menu", partial("admin/shared/side_menu")) - .morph("#permalink", partial("admin/enterprises/form/permalink")) + respond_to do |format| + format.html { head :ok } + format.turbo_stream { render :edit } + end end def welcome diff --git a/app/views/admin/enterprises/edit.turbo_stream.haml b/app/views/admin/enterprises/edit.turbo_stream.haml new file mode 100644 index 0000000000..921b635e59 --- /dev/null +++ b/app/views/admin/enterprises/edit.turbo_stream.haml @@ -0,0 +1,4 @@ += turbo_stream.replace 'side_menu' do + = render partial: 'admin/shared/side_menu' += turbo_stream.replace 'permalink' do + = render partial: 'admin/enterprises/form/permalink' diff --git a/app/webpacker/controllers/primary_details_controller.js b/app/webpacker/controllers/primary_details_controller.js index b4e72be4cf..eb73480bc6 100644 --- a/app/webpacker/controllers/primary_details_controller.js +++ b/app/webpacker/controllers/primary_details_controller.js @@ -22,13 +22,13 @@ export default class extends Controller { `?stimulus=true&enterprise_sells=${this.enterpriseSellsValue}&is_primary_producer=${this.primaryProducerValue}`, { method: "GET", - headers: { "Content-type": "application/json; charset=UTF-8" }, + headers: { + 'Content-Type': 'application/json; charset=UTF-8', + 'Accept': 'text/vnd.turbo-stream.html' + } } ) - .then((data) => data.json()) - .then((operation) => { - CableReady.perform(operation); - this.spinnerTarget.classList.add("hidden"); - }); - } + .then(response => response.text()) + .then(html => Turbo.renderStreamMessage(html)) + .then(() => this.spinnerTarget.classList.add("hidden"))} }