Skip to content

Commit

Permalink
Merge pull request #225 from solver-it-sro/GO-392/signed_draft_UX_unsign
Browse files Browse the repository at this point in the history
Locked draft behavior hotfix
  • Loading branch information
stage-rl authored Dec 5, 2023
2 parents 97c2852 + a78bac7 commit dded5ca
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
],
"[erb]": {
"editor.defaultFormatter": "Jota0222.multi-formatter"
},
"[markdown]": {
"editor.defaultFormatter": null
}
}
49 changes: 26 additions & 23 deletions app/components/message_draft_body_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,26 @@

<div class="flex flex-col justify-stretch items-start relative gap-4 p-6 border-t-0 border-r-0 border-b border-l-0 border-gray-200">
<% if @message.custom_visualization? %>
<%
message_title_id = dom_id(@message, :title)
message_text_id = dom_id(@message, :text)
%>
<% message_title_id = dom_id(@message, :title)
message_text_id = dom_id(@message, :text) %>
<%= content_tag(:div,
{
"data-controller": "message-drafts",
"data-message-drafts-message-draft-path": message_draft_path(@message.id),
"data-message-drafts-authenticity-token": form_authenticity_token,
"data-message-drafts-title-id": message_title_id,
"data-message-drafts-text-id": message_text_id,
"class": "mb-3 pt-0 w-full"
}
) do %>
<%= text_field_tag message_title_id, @message.title, placeholder: "Predmet", "data-action": "change->message-drafts#update", disabled: !@message.editable?, class: "mb-3 px-3 py-4 placeholder-slate-300 text-slate-900 relative bg-white bg-white rounded-lg text-base border-0 shadow outline-none focus:outline-none focus:ring w-full" %>
<%= text_area_tag message_text_id, @message.metadata["message_body"], autofocus: @is_last, placeholder: "Text", "data-action": "change->message-drafts#update", rows: 10, disabled: !@message.editable?, class: "px-3 py-4 placeholder-slate-300 text-slate-900 relative bg-white bg-white rounded-lg text-base border-0 shadow outline-none focus:outline-none focus:ring w-full h-full" %>
{
"data-controller": "message-drafts",
"data-message-drafts-message-draft-path": message_draft_path(@message.id),
"data-message-drafts-authenticity-token": form_authenticity_token,
"data-message-drafts-title-id": message_title_id,
"data-message-drafts-text-id": message_text_id,
"class": "mb-3 pt-0 w-full"
}) do %>
<% if @message.editable? %>
<%= text_field_tag message_title_id, @message.title, placeholder: "Predmet", "data-action": "change->message-drafts#update", class: "mb-3 px-3 py-4 placeholder-slate-300 text-slate-900 relative bg-white bg-white rounded-lg text-base border-0 shadow outline-none focus:outline-none focus:ring w-full" %>
<%= text_area_tag message_text_id, @message.metadata["message_body"], autofocus: @is_last, placeholder: "Text", "data-action": "change->message-drafts#update", rows: 10, class: "px-3 py-4 placeholder-slate-300 text-slate-900 relative bg-white bg-white rounded-lg text-base border-0 shadow outline-none focus:outline-none focus:ring w-full h-full" %>
<% else %>
<%= button_to confirm_unlock_message_draft_path(@message), class: "w-full", data: { turbo_frame: "modal" } do %>
<%= text_field_tag message_title_id, @message.title, placeholder: "Predmet", readonly: true, class: "mb-3 px-3 py-4 placeholder-slate-300 text-slate-900 relative bg-white bg-white rounded-lg text-base border-0 shadow outline-none focus:outline-none focus:ring w-full" %>
<%= text_area_tag message_text_id, @message.metadata["message_body"], autofocus: @is_last, readonly: true, placeholder: "Text", rows: 10, class: "px-3 py-4 placeholder-slate-300 text-slate-900 relative bg-white bg-white rounded-lg text-base border-0 shadow outline-none focus:outline-none focus:ring w-full h-full" %>
<% end %>
<% end %>
<% end %>
<% else %>
<div class="w-full">
Expand All @@ -61,14 +65,13 @@
<div class="gap-2 p-2 border-t-0 border-r-0 border-b-0 border-l-0 border-gray-200">
<% if @message.not_yet_submitted? && @message.form&.signable? %>
<%= 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-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
27 changes: 27 additions & 0 deletions app/components/message_draft_confirm_unlock_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= render Common::ModalComponent.new do |modal| %>
<% modal.with_header do %>
Draft nie je možné upravovať
<% end %>
<% modal.with_modal_content do %>
<div class="mt-2 pb-4">
<p class="text-sm text-gray-500">
<% if @message.reason_for_readonly == :form_signed %>
<%= t "message_draft.form_signed" %>
<% elsif @message.reason_for_readonly == :form_submitted %>
<%= t "message_draft.form_submitted" %>
<% else %>
<%= t "message_draft.read_only_agenda" %>
<% end %>
</p>
</div>
<%= render Common::ModalActionsComponent.new do |actions| %>
<% if @message.reason_for_readonly == :form_signed %>
<% actions.with_submit_button do %>
<%= button_to unlock_message_draft_path(@message), class: "inline-flex w-full justify-center rounded-md bg-red-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-red-500 sm:ml-3 sm:w-auto", data: { turbo_frame: "_top" } do %>
Odstrániť podpisy
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
5 changes: 5 additions & 0 deletions app/components/message_draft_confirm_unlock_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MessageDraftConfirmUnlockComponent < ViewComponent::Base
def initialize(message)
@message = message
end
end
13 changes: 13 additions & 0 deletions app/controllers/message_drafts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ def destroy
redirect_to redirect_path, notice: "Draft bol zahodený"
end

def unlock
authorize @message
if @message.remove_form_signature
redirect_to message_thread_path(@message.thread), notice: "Podpisy boli úspešne odstránené, správu je možné upravovať"
else
redirect_to message_thread_path(@message.thread), alert: "Nastala neočakávaná chyba, nepodarilo sa odstrániť podpisy"
end
end

def confirm_unlock
authorize @message
end

private

def load_message_drafts
Expand Down
67 changes: 44 additions & 23 deletions app/models/message_draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,7 @@ def self.create_message_reply(original_message: , author:)
"status": "created"
}
)

# TODO clean the domain (no UPVS stuff)
message_draft.objects.create!(
name: "form.xml",
mimetype: "application/x-eform-xml",
object_type: "FORM",
is_signed: false
)
create_form_object

message_draft
end
Expand All @@ -86,22 +79,10 @@ def update_content(title:, body:)
self.title = title
metadata["message_body"] = body
save!

return unless title.present? && body.present?

# TODO clean the domain (no UPVS stuff)
if form.message_object_datum
form.message_object_datum.update(
blob: Upvs::FormBuilder.build_general_agenda_xml(subject: title, body: body)
)
else
form.message_object_datum = MessageObjectDatum.create(
message_object: form,
blob: Upvs::FormBuilder.build_general_agenda_xml(subject: title, body: body)
)
end

self.reload
update_form_object
reload
end

def draft?
Expand All @@ -116,6 +97,12 @@ def editable?
metadata["posp_id"] == GENERAL_AGENDA_POSP_ID && !form&.is_signed? && not_yet_submitted?
end

def reason_for_readonly
return :read_only_agenda unless metadata["posp_id"] == GENERAL_AGENDA_POSP_ID
return :form_submitted if submitted? || being_submitted?
return :form_signed if form.is_signed?
end

def custom_visualization?
metadata["posp_id"] == GENERAL_AGENDA_POSP_ID
end
Expand Down Expand Up @@ -147,7 +134,7 @@ def submitted!
save!
EventBus.publish(:message_draft_submitted, self)
end

def invalid?
metadata["status"] == "invalid"
end
Expand All @@ -156,6 +143,16 @@ def original_message
Message.find(metadata["original_message_id"]) if metadata["original_message_id"]
end

def remove_form_signature
return false unless form
return false unless form.is_signed?

form.destroy
create_form_object
reload
update_form_object
end

private

def validate_metadata
Expand Down Expand Up @@ -183,4 +180,28 @@ def validate_objects
errors.merge!(object.errors)
end
end

def create_form_object
# TODO: clean the domain (no UPVS stuff)
objects.create!(
name: "form.xml",
mimetype: "application/x-eform-xml",
object_type: "FORM",
is_signed: false
)
end

def update_form_object
# TODO: clean the domain (no UPVS stuff)
if form.message_object_datum
form.message_object_datum.update(
blob: Upvs::FormBuilder.build_general_agenda_xml(subject: title, body: metadata["message_body"])
)
else
form.message_object_datum = MessageObjectDatum.create(
message_object: form,
blob: Upvs::FormBuilder.build_general_agenda_xml(subject: title, body: metadata["message_body"])
)
end
end
end
10 changes: 1 addition & 9 deletions app/models/message_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class MessageObject < ApplicationRecord
belongs_to :message, inverse_of: :objects
has_one :message_object_datum, dependent: :destroy
has_many :nested_message_objects, inverse_of: :message_object
has_many :nested_message_objects, inverse_of: :message_object, dependent: :destroy

scope :unsigned, -> { where(is_signed: false) }
scope :to_be_signed, -> { where(to_be_signed: true) }
Expand Down Expand Up @@ -68,14 +68,6 @@ def destroyable?
message.draft? && message.not_yet_submitted? && !form?
end

def asice?
mimetype == 'application/vnd.etsi.asic-e+zip'
end

def destroyable?
message.draft? && message.not_yet_submitted? && !form?
end

private

def allowed_mime_type?
Expand Down
8 changes: 8 additions & 0 deletions app/policies/message_draft_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ def submit_all?
def destroy?
create?
end

def confirm_unlock?
unlock?
end

def unlock?
create?
end
end
3 changes: 3 additions & 0 deletions app/views/message_drafts/confirm_unlock.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= tag.turbo_frame id: "modal" do %>
<%= render MessageDraftConfirmUnlockComponent.new(@message) %>
<% end %>
4 changes: 4 additions & 0 deletions config/locales/sk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,7 @@ sk:
tag_editing_in_message_threads:
one: "Úprava štítkov v jednom vlákne"
other: "Úprava štítkov v %{count} vláknach"
message_draft:
form_signed: "Správa už bola podpísaná. Podpísanú správu nie je možné ďalej upravovať. V prípade potreby zmien kliknite na tlačidlo Odstrániť podpisy. Všetky podpisy budú odstránené, správu bude ďalej možné upravovať. Následne ju bude potrebné znova podpísať."
form_submitted: "Správa už bola odoslaná. Odoslanú správu nie je možné ďalej upravovať. V prípade potreby pripravte novú správu a odošlite ju."
read_only_agenda: "Správu nie je možné ďalej upravovať. V prípade potreby pripravte novú správu a odošlite ju."
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@

resources :message_drafts do
member do
post 'submit'
post :confirm_unlock
post :unlock
post :submit
end

post 'submit_all', on: :collection
Expand Down

0 comments on commit dded5ca

Please sign in to comment.