Skip to content

Commit

Permalink
add form and new views
Browse files Browse the repository at this point in the history
  • Loading branch information
ElviaBth committed Jul 11, 2024
1 parent addaee9 commit 0ec9895
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 3 deletions.
23 changes: 23 additions & 0 deletions decidim-admin/app/commands/decidim/admin/create_share_token.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Decidim
module Admin
class CreateShareToken < Decidim::Command
def initialize(form, share_token)
@form = form
@share_token = share_token
end

def call
return broadcast(:invalid) if form.invalid?

ShareToken.create!(component: share_token, token: form.token)
broadcast(:ok)
end

private

attr_reader :form, :share_token
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ class ShareTokensController < Decidim::Admin::ApplicationController

def index; end

def new
@form = form(ShareTokenForm).instance
end

def create
@form = form(ShareTokenForm).from_params(params)

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

on(:invalid) do
flash.now[:alert] = I18n.t("share_tokens.create.invalid", scope: "decidim.admin")
render action: "new"
end
end
end

def destroy
enforce_permission_to(:destroy, :share_token, share_token:)

Expand Down
11 changes: 8 additions & 3 deletions decidim-admin/app/forms/decidim/admin/share_token_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ module Admin
class ShareTokenForm < Decidim::Form
include TranslatableAttributes

attribute :token
attribute :expires_at
attribute :times_used
attribute :token, String
attribute :expires_at, Decidim::Attributes::TimeWithZone

validates :token, presence: true

def token
attributes[:token].to_s.upcase
end
end
end
end
10 changes: 10 additions & 0 deletions decidim-admin/app/views/decidim/admin/share_tokens/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="form__wrapper">
<div class="card pt-4">
<div class="row column">
<%= form.text_field :token, aria: { label: :token }, style: "text-transform: uppercase" %>
</div>
<div class="row column">
<%= form.datetime_field :expires_at, aria: { label: :expires_at } %>
</div>
</div>
</div>
21 changes: 21 additions & 0 deletions decidim-admin/app/views/decidim/admin/share_tokens/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<% add_decidim_page_title(t(".title")) %>
<div class="item_show__header">
<h1 class="item_show__header-title">
<%= t ".title", component_name: translated_attribute(component.name) %>
<a class="button button__sm button__secondary" href="<%= share_tokens_path(component) %>"><%= t("share_tokens.index.back_to_share_tokens", scope: "decidim.admin") %></a>
</h1>
</div>
<div class="item__edit-form">
<%= decidim_form_for(@form, url: share_tokens_path(component), html: { class: "form-defaults form ew_component_share_token" }) do |f| %>
<div class="card">
<div class="card-section">
<%= render partial: "form", object: f %>
</div>
</div>
<div class="item__edit-sticky">
<div class="item__edit-sticky-container">
<%= f.submit t(".create"), class: "button button__sm button__secondary" %>
</div>
</div>
<% end %>
</div>
4 changes: 4 additions & 0 deletions decidim-admin/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -965,8 +965,12 @@ en:
error: There was a problem destroying the token.
success: Token destroyed successfully.
index:
back_to_share_tokens: Back to share tokens
new_share_token_button: New token
title: 'Sharing tokens for component: %{component_name}'
new:
create: Create
title: 'New sharing tokens for component: %{component_name}'
share_tokens:
empty: There are no active tokens.
help: These tokens are used to publicly share this unpublished resource to any user. They will be hidden when the resource is published. Click on the token's share icon to visit the shareable URL.
Expand Down

0 comments on commit 0ec9895

Please sign in to comment.