Skip to content

Commit

Permalink
refactor share_token form
Browse files Browse the repository at this point in the history
  • Loading branch information
ElviaBth committed Jul 11, 2024
1 parent c9284ee commit 81fe2c1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def new
def create
@form = form(ShareTokenForm).from_params(params)

CreateShareToken.call(@form, share) do
CreateShareToken.call(@form, share_token) do
on(:ok) do
flash[:notice] = I18n.t("share_tokens.create.success", scope: "decidim.admin")
redirect_to code_group_codes_path
redirect_to share_tokens_path(component)
end

on(:invalid) do
Expand Down
8 changes: 8 additions & 0 deletions decidim-admin/app/forms/decidim/admin/share_token_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ class ShareTokenForm < Decidim::Form

attribute :token, String
attribute :expires_at, Decidim::Attributes::TimeWithZone
attribute :no_expiration, Boolean, default: true

validates :token, presence: true
validates :expires_at, presence: true, if: ->(form) { form.no_expiration.blank? }

def token
attributes[:token].to_s.upcase
end

def expires_at
return nil if no_expiration

super
end
end
end
end
40 changes: 38 additions & 2 deletions decidim-admin/app/views/decidim/admin/share_tokens/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
<div class="form__wrapper">
<div class="card pt-4">
<div class="row column">
<%= form.text_field :token, aria: { label: :token }, style: "text-transform: uppercase" %>
<%= form.text_field :token, aria: { label: :token }, class: "form-input" %>
</div>
<div class="row column">
<%= form.datetime_field :expires_at, aria: { label: :expires_at } %>
<div class="flex items-center space-x-4">
<div class="flex items-center">
<%= form.radio_button :no_expiration, true, id: "expires_never", checked: form.object.no_expiration %>
<%= form.label :no_expiration, "Never expire", for: "expires_never" %>
</div>

<div class="flex items-center">
<%= form.radio_button :no_expiration, false, id: "expires_custom", checked: !form.object.no_expiration %>
<%= form.label :no_expiration, "Custom", for: "expires_custom" %>
</div>
</div>

<div id="expires_at_field_wrapper" class="<%= form.object.no_expiration ? "hidden" : "" %> mt-4">
<%= form.datetime_field :expires_at, aria: { label: :expires_at } %>
</div>
</div>
</div>
</div>

<script>
document.addEventListener("DOMContentLoaded", () => {
const expiresNever = document.getElementById("expires_never");
const expiresCustom = document.getElementById("expires_custom");
const expiresAtFieldWrapper = document.getElementById("expires_at_field_wrapper");

const toggleExpiresAtField = () => {
if (expiresCustom.checked) {
expiresAtFieldWrapper.classList.remove("hidden");
} else {
expiresAtFieldWrapper.classList.add("hidden");
expiresAtFieldWrapper.querySelector("input[name='share_token[expires_at]']").value = "";
}
};

expiresNever.addEventListener("change", toggleExpiresAtField);
expiresCustom.addEventListener("change", toggleExpiresAtField);

toggleExpiresAtField();
});
</script>

0 comments on commit 81fe2c1

Please sign in to comment.