diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index fa2d331..507a19a 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -39,10 +39,15 @@ def new def create @product = current_user.products.new(product_params) + if params[:changed_form] + render "create" and return + end + if @product.save redirect_to @product, notice: 'Product was successfully created.' else - render :new, status: 422 + render "create" + # render :new, status: 422 end end @@ -50,11 +55,19 @@ def edit end def update - if @product.update(product_params) + + @product.assign_attributes(product_params) + + if params[:changed_form] + render "update" and return + end + + if @product.save redirect_to @product, notice: 'Product was successfully updated.' else Rails.logger.error("AAA #{@product.errors.full_messages}") - render :edit, status: 422 + # render :edit, status: 422 + render "update" end end diff --git a/app/javascript/controllers/simple_editor_controller.jsx b/app/javascript/controllers/simple_editor_controller.jsx index e0bc444..34d7007 100644 --- a/app/javascript/controllers/simple_editor_controller.jsx +++ b/app/javascript/controllers/simple_editor_controller.jsx @@ -119,9 +119,12 @@ export default class extends Controller { this.editor.commands.toggleItalic() } - toggleHeading(e){ - e.preventDefault() - this.editor.commands.toggleHeading({ level: parseInt(e.currentTarget.dataset.level) }) + toggleHeading(e) { + // Check if the event is from a click or a specific key press on the button + if (e.type === 'click') { // || (e.type === 'keydown' && e.key === 'Enter' && e.target.matches('[data-action="toggleHeading"]'))) { + e.preventDefault(); + this.editor.commands.toggleHeading({ level: parseInt(e.currentTarget.dataset.level) }); + } } toggleOrderedList(e){ diff --git a/app/javascript/controllers/submit_on_change_controller.js b/app/javascript/controllers/submit_on_change_controller.js index bddd61e..4774c6d 100644 --- a/app/javascript/controllers/submit_on_change_controller.js +++ b/app/javascript/controllers/submit_on_change_controller.js @@ -1,7 +1,21 @@ -import { Controller } from "@hotwired/stimulus" +import { Controller } from "@hotwired/stimulus"; export default class extends Controller { - handleChange(event) { + connect() { + console.log("SubmitOnChangeController connected"); + } + + submitForm(event) { + // Prevent the default behavior if needed + // event.preventDefault(); + + let hiddenInput = document.createElement("input"); + hiddenInput.type = "hidden"; + hiddenInput.name = "changed_form"; + hiddenInput.value = "true"; + this.element.appendChild(hiddenInput); + + // Submit the form this.element.requestSubmit(); } } \ No newline at end of file diff --git a/app/views/products/_form.erb b/app/views/products/_form.erb index 0da4857..65724f9 100644 --- a/app/views/products/_form.erb +++ b/app/views/products/_form.erb @@ -9,14 +9,26 @@ <% end %> - <%= form_with(model: product, local: true) do |form| %> - - <%= form.object.errors.full_messages.join(" ") %> + <%= form_with(model: product, local: true, data: { + controller: "submit-on-change" + }) do |form| %> + + <% if form.object.errors.any? %> +
+

Errors Found

+ <% # form.object.errors.full_messages.join(" ") %> +
+ <% end %>
- <%= form.select :category, Product.categories.keys %> + <%= form.select :category, Product.categories.keys , {}, { + #label: false, + data: { action: "change->submit-on-change#submitForm" } + } + %> + <% if ['vinyl', 'cassette'].include?(product.category) %> <%= form.select :playlist_id, current_user.playlists.where(playlist_type: ['album', 'ep']).map{|o| [o.title, o.id] diff --git a/app/views/products/create.turbo_stream.erb b/app/views/products/create.turbo_stream.erb new file mode 100644 index 0000000..7f972c5 --- /dev/null +++ b/app/views/products/create.turbo_stream.erb @@ -0,0 +1,5 @@ +<%= turbo_stream.update("product-form") do %> + + <%= render "form", product: @product %> + +<% end %> \ No newline at end of file diff --git a/app/views/products/edit.html.erb b/app/views/products/edit.html.erb index cb5b9b9..568e5f1 100644 --- a/app/views/products/edit.html.erb +++ b/app/views/products/edit.html.erb @@ -1 +1,3 @@ -<%= render "form", product: @product %> \ No newline at end of file +
+ <%= render "form", product: @product %> +
\ No newline at end of file diff --git a/app/views/products/new.html.erb b/app/views/products/new.html.erb index cb5b9b9..568e5f1 100644 --- a/app/views/products/new.html.erb +++ b/app/views/products/new.html.erb @@ -1 +1,3 @@ -<%= render "form", product: @product %> \ No newline at end of file +
+ <%= render "form", product: @product %> +
\ No newline at end of file diff --git a/app/views/products/update.turbo_stream.erb b/app/views/products/update.turbo_stream.erb new file mode 100644 index 0000000..7f972c5 --- /dev/null +++ b/app/views/products/update.turbo_stream.erb @@ -0,0 +1,5 @@ +<%= turbo_stream.update("product-form") do %> + + <%= render "form", product: @product %> + +<% end %> \ No newline at end of file diff --git a/app/views/shared/_simple_editor.erb b/app/views/shared/_simple_editor.erb index 6e54445..fade6c6 100644 --- a/app/views/shared/_simple_editor.erb +++ b/app/views/shared/_simple_editor.erb @@ -3,19 +3,66 @@
- - + + + + + + + + + - - + + - - - + + + + +
diff --git a/app/views/shared/_user_menu.html.erb b/app/views/shared/_user_menu.html.erb index e7dd13c..9dcd9e9 100644 --- a/app/views/shared/_user_menu.html.erb +++ b/app/views/shared/_user_menu.html.erb @@ -243,6 +243,13 @@ <% end %> <% #end %> + <% if current_user&.can_sell_products? %> + <%= link_to user_products_path(current_user.username), + class: "block px-4 py-2 text-sm text-gray-700 dark:text-gray-300" do %> + <%= t("menu.my_purchases") %> + <% end %> + <% end %> + <%= link_to "/sales", class: "block px-4 py-2 text-sm text-gray-700 dark:text-gray-300" do %> <%= t("menu.my_sales") %>