5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-26 12:36:22 +00:00

feat: poder crear un post relacionado in situ

This commit is contained in:
f 2024-05-20 14:12:53 -03:00
parent 47655c3975
commit 56003508a7
No known key found for this signature in database
5 changed files with 77 additions and 8 deletions

View file

@ -44,6 +44,7 @@ class PostsController < ApplicationController
# estamos creando. # estamos creando.
def form def form
uuid = params.permit(:uuid).try(:[], :uuid) uuid = params.permit(:uuid).try(:[], :uuid)
locale
@post = @post =
if uuid.present? if uuid.present?
@ -53,12 +54,7 @@ class PostsController < ApplicationController
site.posts(lang: locale).build(layout: params.require(:layout)) site.posts(lang: locale).build(layout: params.require(:layout))
end end
triggers = {} swap_modals
params.permit(:show, :hide).each_pair do |key, value|
triggers["modal:#{key}"] = { id: value }
end
headers['HX-Trigger-After-Swap'] = triggers.to_json if triggers.present?
render layout: false render layout: false
end end
@ -118,7 +114,23 @@ class PostsController < ApplicationController
if @post.persisted? if @post.persisted?
site.touch site.touch
forget_content forget_content
end
# @todo Enviar la creación a otro endpoint para evitar tantas
# condiciones.
if htmx?
if @post.persisted?
swap_modals
@value = @post.title.value
@uuid = @post.uuid.value
@name = params.require(:name)
render 'posts/new_has_many_value', layout: false
else
# @todo Mostrar errores
end
elsif @post.persisted?
redirect_to site_post_path(@site, @post) redirect_to site_post_path(@site, @post)
else else
render 'posts/new' render 'posts/new'
@ -216,4 +228,13 @@ class PostsController < ApplicationController
def service_for_direct_upload def service_for_direct_upload
session[:service_name] = site.name.to_sym session[:service_name] = site.name.to_sym
end end
# @param triggers [Hash] Otros disparadores
def swap_modals(triggers = {})
params.permit(:show, :hide).each_pair do |key, value|
triggers["modal:#{key}"] = { id: value }
end
headers['HX-Trigger'] = triggers.to_json if triggers.present?
end
end end

View file

@ -0,0 +1,41 @@
-#
El formulario del artículo, con HTMX activado.
@param :site [Site]
@param :post [Post]
@param :locale [Symbol, String]
@param :dir [Symbol, String]
:ruby
options = {
multipart: true,
class: 'form post ',
'hx-swap': params.require(:swap),
'hx-target': "##{params.require(:target)}"
}
if post.new?
url = options[:'hx-post'] = site_posts_path(site, locale: locale)
options[:class] += 'new'
else
url = options[:'hx-patch'] = site_post_path(site, post.id, locale: locale)
options[:method] = :patch
options[:class] += 'edit'
end
= form_tag url, **options do
-# Parámetros para HTMX
%input{ type: 'hidden', name: 'hide', value: params.require(:show) }
%input{ type: 'hidden', name: 'show', value: params.require(:hide) }
%input{ type: 'hidden', name: 'name', value: params.require(:name) }
%input{ type: 'hidden', name: 'base', value: params.require(:base) }
-# Botones de guardado
= render 'posts/submit', site: site, post: post
= hidden_field_tag "#{base}[layout]", post.layout.name
-# Dibuja cada atributo
= render 'posts/attributes', site: site, post: post, dir: dir, base: base, locale: locale
-# Botones de guardado
= render 'posts/submit', site: site, post: post

View file

@ -16,6 +16,7 @@
post_form_id = "post-form-#{Nanoid.generate}" post_form_id = "post-form-#{Nanoid.generate}"
post_modal_id = "post-modal-#{Nanoid.generate}" post_modal_id = "post-modal-#{Nanoid.generate}"
post_form_loaded_id = "post-loaded-#{Nanoid.generate}" post_form_loaded_id = "post-loaded-#{Nanoid.generate}"
value_list_id = "#{id}_body"
%div{ id: modal_id, data: { controller: 'modal array', 'array-original-value': metadata.value.to_json, 'array-new-array-value': site_posts_new_related_post_path(site) } } %div{ id: modal_id, data: { controller: 'modal array', 'array-original-value': metadata.value.to_json, 'array-new-array-value': site_posts_new_related_post_path(site) } }
.form-group .form-group
@ -45,7 +46,7 @@
%input.form-control{ data: { target: 'array.search', action: 'input->array#search' }, type: 'search', placeholder: t('.filter') } %input.form-control{ data: { target: 'array.search', action: 'input->array#search' }, type: 'search', placeholder: t('.filter') }
- content_for :"#{id}_body" do - content_for :"#{id}_body" do
.form-group.mb-0{ id: "#{id}_body" } .form-group.mb-0{ id: value_list_id }
- metadata.values.each_pair do |value, uuid| - metadata.values.each_pair do |value, uuid|
.mb-2{ data: { target: 'array.item', 'searchable-value': value.remove_diacritics.downcase, value: uuid } } .mb-2{ data: { target: 'array.item', 'searchable-value': value.remove_diacritics.downcase, value: uuid } }
= render 'bootstrap/custom_checkbox', name: name, id: "value-#{Nanoid.generate}", value: uuid, checked: metadata.value.include?(uuid), content: value = render 'bootstrap/custom_checkbox', name: name, id: "value-#{Nanoid.generate}", value: uuid, checked: metadata.value.include?(uuid), content: value
@ -81,6 +82,10 @@
%form{ id: post_form_id, 'hx-get': site_posts_form_path(site), 'hx-target': "##{post_form_loaded_id}" } %form{ id: post_form_id, 'hx-get': site_posts_form_path(site), 'hx-target': "##{post_form_loaded_id}" }
%input{ type: 'hidden', name: 'show', value: post_modal_id } %input{ type: 'hidden', name: 'show', value: post_modal_id }
%input{ type: 'hidden', name: 'hide', value: modal_id } %input{ type: 'hidden', name: 'hide', value: modal_id }
%input{ type: 'hidden', name: 'target', value: value_list_id }
%input{ type: 'hidden', name: 'swap', value: 'beforeend' }
%input{ type: 'hidden', name: 'base', value: id }
%input{ type: 'hidden', name: 'name', value: name }
%div{ id: post_modal_id, data: { controller: 'modal' } } %div{ id: post_modal_id, data: { controller: 'modal' } }
= render 'bootstrap/modal', id: post_id, modal_content_attributes: { class: 'h-100' } do = render 'bootstrap/modal', id: post_id, modal_content_attributes: { class: 'h-100' } do
- content_for :"#{post_id}_body" do - content_for :"#{post_id}_body" do

View file

@ -4,4 +4,4 @@
@param :site [Site] @param :site [Site]
@param :post [Post] @param :post [Post]
= render 'posts/form', site: @site, post: @post = render 'posts/htmx_form', site: @site, post: @post, locale: @locale, dir: t("locales.#{@locale}.dir"), base: params.require(:base)

View file

@ -0,0 +1,2 @@
.mb-2{ data: { target: 'array.item', 'searchable-value': @value.remove_diacritics.downcase, value: @uuid } }
= render 'bootstrap/custom_checkbox', name: @name, id: "value-#{Nanoid.generate}", value: @uuid, checked: true, content: @value