Skip to content

Commit

Permalink
Merge pull request #197 from solver-it-sro/GO-293/forms_and_templates
Browse files Browse the repository at this point in the history
GO-293 Forms and templates in UPVS domain
  • Loading branch information
luciajanikova authored Dec 8, 2023
2 parents 4113ddb + ee62044 commit 2d4a96e
Show file tree
Hide file tree
Showing 41 changed files with 1,737 additions and 139 deletions.
15 changes: 8 additions & 7 deletions app/components/message_draft_body_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@
<% if @message.not_yet_submitted? && @message.form&.signable? %>
<div class="gap-2 p-2 border-t-0 border-r-0 border-b-0 border-l-0 border-gray-200">
<%= content_tag(:div,
{
"data-controller": "autogram",
"data-autogram-object-id": @message.form.id,
"data-autogram-message-id": @message.id,
"data-autogram-object-path": message_message_object_path(@message.id, @message.form.id),
"data-autogram-authenticity-token": form_authenticity_token
}) do %>
{
"data-turbo-frame": "_top",
"data-controller": "autogram",
"data-autogram-object-id": @message.form.id,
"data-autogram-message-id": @message.id,
"data-autogram-object-path": message_message_object_path(@message.id, @message.form.id),
"data-autogram-authenticity-token": form_authenticity_token
}) do %>
<button data-action="click->autogram#signSingleFile" class="flex justify-strech items-start self-stretch flex-grow-0 flex-shrink-0 gap-2 p-6 text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-3.5 py-2.5 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">
Podpísať
</button>
Expand Down
9 changes: 9 additions & 0 deletions app/components/message_template_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
<% if @message.errors[:base].any? %>
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative w-full" role="alert">
<% @message.errors.full_messages_for(:base).each do |error| %>
<strong class="font-bold"><%= error %></strong>
<% end %>
</div>
<% end %>


<%
message_draft_body_form_id = "#{dom_id(@message, :body)}_form"
%>
Expand Down
6 changes: 0 additions & 6 deletions app/components/message_template_recipients.html.erb

This file was deleted.

33 changes: 33 additions & 0 deletions app/components/message_template_recipients_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%= render Common::ModalComponent.new do |modal| %>
<% modal.with_modal_content do %>
<div class="flex flex-col gap-2 mb-4">
<%= form_with url: message_template_search_recipients_list_path, class: "flex justify-start items-center self-stretch flex-grow-0 flex-shrink-0 mt-4" do |form| %>
<div class="flex w-full items-center rounded-md shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-inset focus-within:ring-indigo-600">
<div class="p-2">
<%= render Icons::MagnifyingGlassComponent.gray(size: "4") %>
</div>
<%= tag.turbo_frame id: "recipients-search-results" %>
<%= form.search_field :search, value: params[:search], placeholder: "Vyhľadajte prijímateľa",
oninput: "this.form.requestSubmit()",
onreset: "this.form.requestSubmit()",
autofocus: true,
class: "block w-full flex-1 border-0 bg-transparent py-1.5 pl-1 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6"
%>
</div>
<% end %>

<%= tag.turbo_frame id: "recipients-list", class: "flex-col self-stretch mt-2" do %>
<% @recipients_list.each do |recipient| %>
<%= button_to message_template_recipient_selected_path, method: 'post', params: { recipient_uri: recipient[:uri], recipient_name: recipient[:name] },
class: "flex justify-between items-center self-stretch flex-grow-0 flex-shrink-0 py-2", data: { turbo_frame: "_top" } do %>
<div class="flex justify-start items-center flex-grow-0 flex-shrink-0 relative gap-3">
<span class="flex-grow-0 flex-shrink-0 text-base font-medium text-left text-gray-900"><%= recipient[:name] %></span>
</div>
<% end %>
<% end %>
<% end %>
</div>

<%= render Common::ModalActionsComponent.new %>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class MessageTemplateRecipients < ViewComponent::Base
class MessageTemplateRecipientsComponent < ViewComponent::Base
def initialize(recipients_list:)
@recipients_list = recipients_list
end
Expand Down
89 changes: 69 additions & 20 deletions app/components/new_message_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,28 +1,77 @@
<div class="w-full p-4 flex-col justify-start items-start gap-4 inline-flex">
<div class="self-stretch bg-white rounded-md border border-gray-200 flex-col justify-start items-start flex">
<div class="self-stretch p-6 border-b border-gray-200 justify-start items-center gap-4 inline-flex">
<div class="grow shrink basis-0 text-gray-900 text-xl font-semibold leading-[35px]">Nová správa</div>
</div>
<div class="self-stretch p-6 flex-col justify-start items-start flex">
<%= form_with(url: message_drafts_path, method: :post, local: true) do %>
<% if @boxes.count > 1 %>
<label for="sender" class="block mb-2 text-base font-medium text-gray-900">Odosielateľ</label>
<%= select_tag(:sender, options_for_select(@boxes, [Current.box&.name, Current.box&.id]), required: true, prompt: "Zvoľte odosielateľa", class: "mb-4 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5") %>
<% else %>
<%= hidden_field :sender, @boxes.first %>
<% end %>
<div id="new_message" is="turbo-frame" class="w-full">
<div class="w-full p-4 flex-col justify-start items-start gap-4 inline-flex">
<div class="self-stretch bg-white rounded-md border border-gray-200 flex-col justify-start items-start flex">
<div class="self-stretch p-6 border-b border-gray-200 justify-start items-center gap-4 inline-flex">
<div class="grow shrink basis-0 text-gray-900 text-xl font-semibold leading-[35px]">Nová správa</div>
</div>
<%
form_id = "create_draft"
sender_element_id = "sender_id"
template_element_id = "message_template_id"
%>

<%= content_tag(:div, { "data-controller": "message-drafts", class: "w-full" }) do %>
<label for="message_template" class="block mb-2 text-base font-medium text-gray-900">Typ správy</label>
<%= select_tag(:message_template, options_for_select(@templates_list), required: true, prompt: "Zvoľte typ správy", "data-action": "change->message-drafts#loadTemplateRecipients", class: "mb-4 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5") %>
<% end %>
<%= form_with url: message_drafts_path, id: form_id, class: 'w-full' do |f| %>
<div class="self-stretch px-6 pt-6 flex-col justify-start items-start flex">
<div class="mb-4 w-full">
<% if @boxes.count > 1 %>
<label for="<%= sender_element_id %>" class="block mb-2 text-base font-medium text-gray-900">Odosielateľ</label>
<%= select_tag(sender_element_id, options_for_select(@boxes, ([Current.box&.name, Current.box&.id] if Current.box) || [@selected_box&.name, @selected_box&.id]), prompt: "Zvoľte odosielateľa", class: "text-sm rounded-lg block w-full p-2.5 #{@message.errors[:sender_name].any? && !@selected_box ? ERROR_CLASSES : DEFAULT_CLASSES}") %>
<% if @message.errors[:sender_name].any? && !@selected_box %>
<p class="mt-2 text-sm text-red-600 dark:text-red-500">
<span class="font-medium"><%= @message.errors.full_messages_for(:sender_name).to_sentence %></span>
</p>
<% end %>
<% else %>
<%= hidden_field_tag sender_element_id, @boxes.first %>
<% end %>
</div>

<div id="recipients_list" is="turbo-frame"></div>
<%= content_tag(:div, { "data-controller": "message-drafts", class: "mb-4 w-full" }) do %>
<label for="<%= template_element_id %>" class="block mb-2 text-base font-medium text-gray-900">Typ správy</label>
<%= select_tag(template_element_id, options_for_select(@templates_list, [@selected_message_template&.name, @selected_message_template&.id]), prompt: "Zvoľte typ správy", "data-action": "change->message-drafts#loadTemplateRecipients", class: "text-sm rounded-lg block w-full p-2.5 #{@message.errors[:metadata].any? ? ERROR_CLASSES : DEFAULT_CLASSES}") %>
<% if @message.errors[:metadata].any? %>
<p class="mt-2 text-sm text-red-600 dark:text-red-500">
<span class="font-medium"><%= @message.errors[:metadata].to_sentence %></span>
</p>
<% end %>
<% end %>

<div class="gap-2 border-t-0 border-r-0 border-b-0 border-l-0 border-gray-200">
<%= submit_tag "Vytvoriť správu", class: "flex justify-strech items-start self-stretch flex-grow-0 flex-shrink-0 gap-2 mt-6 text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-3.5 py-2.5 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800" %>
<div id="selected_recipient" is="turbo-frame">
<%= hidden_field_tag 'recipient_name', params[:recipient_name] %>
<%= hidden_field_tag 'recipient_uri', params[:recipient_uri] %>
</div>
</div>
<% end %>

<div class="self-stretch px-6 pb-6 flex-col justify-start items-start flex">
<div id="recipients_list" is="turbo-frame" class="w-full">
<% if @recipients_list && @recipients_list.count > 1 %>
<div>
<label for="recipient" class="block mb-2 text-base font-medium text-gray-900">Príjímateľ</label>
</div>
<%= button_to message_template_recipient_selector_path(@selected_message_template), method: 'get', data: { turbo_frame: "modal" }, class: "text-gray-500 hover:text-gray-900 w-full" do %>
<div id="recipient" class="text-sm rounded-lg block w-full p-2.5 <%= @message.errors[:recipient_name].any? ? ERROR_CLASSES : DEFAULT_CLASSES %>">
<p class="text-left"><%= params[:recipient_name].presence || 'Zvoľte príjemateľa' %></p>
</div>
<% if @message.errors[:recipient_name].any? %>
<p class="mt-2 text-left text-sm text-red-600 dark:text-red-500">
<span class="font-medium"><%= @message.errors.full_messages_for(:recipient_name).to_sentence %></span>
</p>
<% end %>
<% end %>
<% end %>
</div>

<%= content_tag(:div, {
"data-controller": "message-drafts",
"data-message-drafts-form-id": form_id,
class: "gap-2 border-t-0 border-r-0 border-b-0 border-l-0 border-gray-200 w-full"
}) do %>
<button data-action="click->message-drafts#create" class="flex justify-strech items-start self-stretch flex-grow-0 flex-shrink-0 gap-2 mt-6 text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-3.5 py-2.5 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">
Vytvoriť správu
</button>
<% end %>
</div>
</div>
</div>
</div>
9 changes: 8 additions & 1 deletion app/components/new_message_component.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
class NewMessageComponent < ViewComponent::Base
def initialize(templates_list:, boxes:)
DEFAULT_CLASSES = 'bg-gray-50 border border-gray-300 text-gray-900 focus:ring-blue-500 focus:border-blue-500'
ERROR_CLASSES = 'bg-red-50 border border-red-500 text-red-900 placeholder-red-700 focus:ring-red-500 focus:border-red-500'

def initialize(message:, templates_list:, boxes:, recipients_list: nil, selected_box: nil, selected_message_template: nil)
@message = message
@templates_list = templates_list
@boxes = boxes
@recipients_list = recipients_list
@selected_box = selected_box
@selected_message_template = selected_message_template
end
end
18 changes: 14 additions & 4 deletions app/controllers/message_drafts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,19 @@ def create
@message,
author: Current.user,
box: @box,
recipient_uri: new_message_draft_params[:recipient]
recipient_name: new_message_draft_params[:recipient_name],
recipient_uri: new_message_draft_params[:recipient_uri]
)

unless @message.valid?(:create)
@templates_list = MessageTemplate.tenant_templates_list(Current.tenant)
@boxes = Current.tenant.boxes.pluck(:name, :id)

@recipients_list = @message_template&.recipients&.pluck(:institution_name, :institution_uri)&.map { |name, uri| { uri: uri, name: name }}

render :update_new and return
end

redirect_to message_thread_path(@message.thread)
end

Expand Down Expand Up @@ -114,11 +124,11 @@ def load_original_message
end

def load_box
@box = Box.find(new_message_draft_params[:sender]) if new_message_draft_params[:sender].present?
@box = Box.find(new_message_draft_params[:sender_id]) if new_message_draft_params[:sender_id].present?
end

def load_message_template
@message_template = policy_scope(MessageTemplate).find(new_message_draft_params[:message_template]) if new_message_draft_params[:message_template].present?
@message_template = policy_scope(MessageTemplate).find(new_message_draft_params[:message_template_id]) if new_message_draft_params[:message_template_id].present?
end

def load_message_draft
Expand All @@ -131,6 +141,6 @@ def message_draft_params
end

def new_message_draft_params
params.permit(:message_template, :sender, :recipient)
params.permit(:message_template_id, :sender_id, :recipient_name, :recipient_uri)
end
end
9 changes: 5 additions & 4 deletions app/controllers/message_objects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class MessageObjectsController < ApplicationController
before_action :set_message_object, except: :create
before_action :set_message, only: [:create, :update, :destroy]
before_action :set_message, only: [:create, :update, :destroy, :signing_data]

def create
authorize @message

MessageObject.create_message_objects(@message, params[:attachments])

render partial: "list"
render partial: 'list'
end

def update
Expand All @@ -30,15 +30,16 @@ def download
def signing_data
authorize @message_object

head :no_content unless @message_object.content.present?
head :no_content and return unless @message_object.content.present?
render template: 'message_drafts/update_body' and return unless @message.valid?(:validate_data)
end

def destroy
authorize @message_object

@message_object.destroy

render partial: "list"
render partial: 'list'
end

private
Expand Down
26 changes: 26 additions & 0 deletions app/controllers/message_templates_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
class MessageTemplatesController < ApplicationController
before_action :load_message_template

def recipient_selector
authorize(@message_template, policy_class: MessageTemplatePolicy)

@recipients_list = @message_template.recipients
.pluck(:institution_name, :institution_uri)
.map { |name, uri| { uri: uri, name: name }}
end

def recipients_list
authorize(@message_template, policy_class: MessageTemplatePolicy)

@recipients_list = @message_template.recipients
.pluck(:institution_name, :institution_uri)
.map { |name, uri| { uri: uri, name: name }}
end

def search_recipients_list
authorize(@message_template, policy_class: MessageTemplatePolicy)

@recipients_list = @message_template.recipients
.where('unaccent(institution_name) ILIKE unaccent(?) OR institution_uri ILIKE ?', "%#{params[:search]}%", "%#{params[:search]}%")
.pluck(:institution_name, :institution_uri)
.map { |name, uri| { uri: uri, name: name }}
end

def recipient_selected
authorize(@message_template, policy_class: MessageTemplatePolicy)

@recipient_name = params[:recipient_name]
@recipient_uri = params[:recipient_uri]
end

private
Expand Down
9 changes: 4 additions & 5 deletions app/helpers/message_template_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def template_field(template_item, data:, errors: nil, editable:, is_last: false)

def text_field(name, value:, errors:, editable:, **args)
@template.content_tag(:div, class: 'mb-3') do
label(:label, name, class: 'font-semibold') +
super(name, {
placeholder: name,
value: value,
disabled: !editable,
'data-action': 'change->message-drafts#update',
Expand All @@ -36,8 +36,8 @@ def text_field(name, value:, errors:, editable:, **args)

def email_field(name, value:, errors:, editable:, **args)
@template.content_tag(:div, class: 'mb-3') do
label(:label, name, class: 'font-semibold') +
super(name, {
placeholder: name,
value: value,
disabled: !editable,
'data-action': 'change->message-drafts#update',
Expand All @@ -53,8 +53,8 @@ def email_field(name, value:, errors:, editable:, **args)

def text_area(name, value:, errors:, editable:, is_last:)
@template.content_tag(:div, class: 'mb-3') do
label(:label, name, class: 'font-semibold') +
super(name, {
placeholder: name,
value: value,
disabled: !editable,
autofocus: is_last,
Expand All @@ -72,9 +72,8 @@ def text_area(name, value:, errors:, editable:, is_last:)

def date_field(name, value:, errors:, editable:, **args)
@template.content_tag(:div, class: 'mb-3') do
label(:label, name) +
label(:label, name, class: 'font-semibold') +
super(name, {
placeholder: Date.today,
value: value,
disabled: !editable,
'data-action': 'change->message-drafts#update',
Expand Down
10 changes: 5 additions & 5 deletions app/javascript/controllers/autogram_controller.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Controller } from "@hotwired/stimulus"
import { patch } from '@rails/request.js'
import { get, patch } from '@rails/request.js'

export default class extends Controller {

async sign(messageObjectPath, that, batchId = null) {
return new Promise((resolve, reject) => {
fetch(`${messageObjectPath}/signing_data.json`)
get(`${messageObjectPath}/signing_data`, { responseKind: "*/*" })
.then(function (response) {
// TODO handle login if expired session

if (response.status === 204) {
alert("Vyplňte text správy");
alert("Vyplňte správu");
}
else {
return response.json();
else if (response.contentType === 'application/json') {
return response.json;
}
}).then(async function (messageObjectData) {
if (!messageObjectData) {
Expand Down
Loading

0 comments on commit 2d4a96e

Please sign in to comment.