From 632c19ab27d407e95edc363e6a612bcf54803dc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20Ara=C3=BAjo?= Date: Sun, 23 Oct 2022 22:12:32 -0300 Subject: [PATCH 01/25] Inserted turbo frames and streams --- app/controllers/kit/products_controller.rb | 24 +++++++++++++++---- app/views/kit/products/_list.html.erb | 17 +++++-------- app/views/kit/products/_list_item.html.erb | 15 ++++++++++++ app/views/kit/products/_product.html.erb | 12 ++++++---- .../kit/products/create.turbo_stream.erb | 1 + .../kit/products/destroy.turbo_stream.erb | 3 +++ app/views/kit/products/index.html.erb | 6 +++-- app/views/kit/products/show.turbo_stream.erb | 1 + .../kit/products/update.turbo_stream.erb | 2 ++ 9 files changed, 60 insertions(+), 21 deletions(-) create mode 100644 app/views/kit/products/_list_item.html.erb create mode 100644 app/views/kit/products/create.turbo_stream.erb create mode 100644 app/views/kit/products/destroy.turbo_stream.erb create mode 100644 app/views/kit/products/show.turbo_stream.erb create mode 100644 app/views/kit/products/update.turbo_stream.erb diff --git a/app/controllers/kit/products_controller.rb b/app/controllers/kit/products_controller.rb index b263dc9..9f19e8d 100644 --- a/app/controllers/kit/products_controller.rb +++ b/app/controllers/kit/products_controller.rb @@ -15,6 +15,7 @@ def show respond_to do |format| format.html { render(:index) } format.json { render(:show) } + format.turbo_stream end end @@ -22,7 +23,10 @@ def show def new @kit_product = Kit::Product.new - render(:index) + respond_to do |format| + format.html { render(:index) } + format.turbo_stream { render(:show) } + end end # POST /kit/products or /kit/products.json @@ -31,7 +35,9 @@ def create respond_to do |format| if @kit_product.save - format.html { redirect_to(kit_product_url(@kit_product), notice: 'Product was successfully created.') } + flash.now[:notice] = 'Product was successfully created.' + + format.html { redirect_to(kit_product_url(@kit_product)) } format.json { render(:show, status: :created, location: @kit_product) } else format.html do @@ -41,14 +47,19 @@ def create end format.json { render(json: @kit_product.errors, status: :unprocessable_entity) } end + + format.turbo_stream end end # PATCH/PUT /kit/products/1 or /kit/products/1.json def update + # binding.break respond_to do |format| if @kit_product.update(kit_product_params) - format.html { redirect_to(kit_product_url(@kit_product), notice: 'Product was successfully updated.') } + flash.now[:notice] = 'Product was successfully updated.' + + format.html { redirect_to(kit_product_url(@kit_product)) } format.json { render(:show, status: :ok, location: @kit_product) } else format.html do @@ -58,6 +69,8 @@ def update end format.json { render(json: @kit_product.errors, status: :unprocessable_entity) } end + + format.turbo_stream end end @@ -65,9 +78,12 @@ def update def destroy @kit_product.destroy + flash.now[:notice] = 'Product was successfully destroyed.' + respond_to do |format| - format.html { redirect_to(kit_products_url, notice: 'Product was successfully destroyed.') } + format.html { redirect_to(kit_products_url) } format.json { head(:no_content) } + format.turbo_stream end end diff --git a/app/views/kit/products/_list.html.erb b/app/views/kit/products/_list.html.erb index a5cd4fb..f3cd9f1 100644 --- a/app/views/kit/products/_list.html.erb +++ b/app/views/kit/products/_list.html.erb @@ -11,6 +11,7 @@ <%= link_to new_kit_product_path, aria: { label: "New product" }, + data: { turbo_frame: 'kit_product_form' }, class: "p-1.5 shrink-0 rounded border border-slate-200 hover:border-slate-300 shadow-sm ml-2" do %> @@ -35,18 +36,12 @@
diff --git a/app/views/kit/products/_list_item.html.erb b/app/views/kit/products/_list_item.html.erb new file mode 100644 index 0000000..e2e2636 --- /dev/null +++ b/app/views/kit/products/_list_item.html.erb @@ -0,0 +1,15 @@ +
  • + <%= link_to product, + aria: { label: "Edit this product" }, + data: { turbo_frame: 'kit_product_form' } , + class: class_names( + "flex items-center justify-between w-full p-2 rounded", + { "bg-indigo-100": current_page?(kit_product_path(product)) } + )do %> +
    +
    + <%= product.name %> +
    +
    + <% end %> +
  • diff --git a/app/views/kit/products/_product.html.erb b/app/views/kit/products/_product.html.erb index ef485f3..7a967b3 100644 --- a/app/views/kit/products/_product.html.erb +++ b/app/views/kit/products/_product.html.erb @@ -27,10 +27,14 @@