Skip to content

Commit

Permalink
Merge branch 'release-0.1.4' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiomarrocoli committed May 27, 2022
2 parents 0b39796 + 7f31745 commit 8c9fa24
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 189 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.1.4

- Form error styling
- Fixed form error handling and attachment submission


# 0.1.3

- updated Netherlands logo
Expand Down
2 changes: 1 addition & 1 deletion app/assets/stylesheets/base/_surveyjs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ $question-content-spacing-right: rem-calc(75);

.form__question--errors {
.sv-question__title {
&:not(.sv-question__title--answer) {
> div:not(.tooltip) {
color: $danger;
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/assets/stylesheets/components/_error.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@
.error {
&.animated-banner {
@include fade-in-out(10s);
z-index: 100;
width: 100vw;

display: flex;

position: fixed;
top: 0;
left: 0;
width: 100vw;
}

&.bg-danger {
Expand Down
68 changes: 42 additions & 26 deletions app/controllers/commitments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class CommitmentsController < ApplicationController
skip_before_action :authenticate_user!, only: %i[index list show]
before_action :set_commitment, only: %i[show edit update destroy]
before_action :purge_geospatial_file, only: %i[update create]
before_action :clean_progress_document_attachment_params, only: %i[update create]

def index
# make a copy of default params and modify those to prevent filters persisting over calls
Expand Down Expand Up @@ -64,18 +63,28 @@ def new
end

def create
@commitment = Commitment.new(commitment_params.merge(user: current_user, commitment_source: 'form', cfn_approved: true))
@commitment = Commitment.new(commitment_params.merge(
user: current_user,
commitment_source: 'form',
cfn_approved: true
))
criterium = Criterium.find(@commitment.criterium_id)
@commitment.manager_ids = [criterium.manager_id]

if @commitment.save
respond_to do |format|
format.json { json_response({ commitment: @commitment, redirect_path: dashboard_path }, :created) }
end
elsif attempting_to_publish? && save_draft
respond_to do |format|
format.json do
json_response({ commitment: @commitment, redirect_path: edit_commitment_path(@commitment) }, :created)
end
end
else
respond_to do |format|
format.json do
error_messages = @commitment.errors.messages.dup
error_messages = @commitment.errors_to_form_fields
json_response({ errors: error_messages }, :unprocessable_entity)
end
end
Expand All @@ -93,11 +102,20 @@ def update

if @commitment.update(commitment_params)
respond_to do |format|
format.json { json_response({ commitment: @commitment, redirect_path: dashboard_path }, 200) }
format.json { json_response({ commitment: @commitment, redirect_path: dashboard_path }, :ok) }
end
elsif attempting_to_publish? && update_draft
respond_to do |format|
format.json do
json_response({ commitment: @commitment, redirect_path: edit_commitment_path(@commitment) }, :ok)
end
end
else
respond_to do |format|
format.json { json_response({ errors: @commitment.errors }, :unprocessable_entity) }
format.json do
error_messages = @commitment.errors_to_form_fields
json_response({ errors: error_messages }, :unprocessable_entity)
end
end
end
end
Expand All @@ -118,31 +136,24 @@ def destroy

private

def purge_geospatial_file
if commitment_params[:geospatial_file].blank?
params[:commitment] = params[:commitment].except(:geospatial_file)
@commitment.geospatial_file.purge if @commitment && @commitment.geospatial_file.attached?
end
def save_draft
@commitment.state = 'draft'
@commitment.save
end

def clean_progress_document_attachment_params
return unless commitment_params[:progress_documents_attributes].present?

new_progress_document_attributes = []
invalid_progress_document_attributes = []
def update_draft
set_commitment
@commitment.update(commitment_params_as_draft)
end

commitment_params[:progress_documents_attributes].each do |progress_document_attributes|
if progress_document_attributes[:document].present?
new_progress_document_attributes << progress_document_attributes
elsif progress_document_attributes[:progress_notes].present?
invalid_progress_document_attributes << progress_document_attributes
end
end
def attempting_to_publish?
!@published && [@commitment.state, commitment_params[:state]].include?('live')
end

if invalid_progress_document_attributes.present?
raise MissingProgressDocumentAttachmentError
else
commitment_params[:progress_documents_attributes] = new_progress_document_attributes
def purge_geospatial_file
if commitment_params[:geospatial_file].blank?
params[:commitment] = params[:commitment].except(:geospatial_file)
@commitment.geospatial_file.purge if @commitment && @commitment.geospatial_file.attached?
end
end

Expand All @@ -153,6 +164,7 @@ def criterium_id_valid?

def set_commitment
@commitment = Commitment.find(params[:id])
@published = @commitment.state == 'live'
end

def commitment_params
Expand Down Expand Up @@ -181,4 +193,8 @@ def commitment_params
progress_documents_attributes: %i[id document progress_notes _destroy]
)
end

def commitment_params_as_draft
commitment_params.merge({ 'state': 'draft' })
end
end
6 changes: 0 additions & 6 deletions app/controllers/concerns/exception_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,5 @@ class MissingProgressDocumentAttachmentError < StandardError; end
format.html { redirect_back fallback_location: root_path, notice: I18n.t('errors.messages.forbidden_resource') }
end
end

rescue_from MissingProgressDocumentAttachmentError do |e|
respond_to do |format|
format.json { json_response({ errors: { message: [I18n.t('activerecord.errors.models.progress_document.attributes.document.document_missing')] }}, :unprocessable_entity) }
end
end
end
end
29 changes: 6 additions & 23 deletions app/javascript/components/banners/ErrorBanner.vue
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
.vue
<template>
<div>
<div
v-show="hasErrors"
class="error animated-banner bg-danger"
>
<span
v-for="(error, index) in errors"
:key="index"
class="error__message"
>
{{ index }}:
<br>
{{ error[0] }}
</span>
</div>
<div
class="error animated-banner bg-danger"
>
<span class="error__message" v-text="errorText" />
</div>
</template>

<script>
export default {
name: "ErrorBanner",
props: {
errors: {
type: Object,
errorText: {
type: String,
required: true
}
},
computed: {
hasErrors() {
return Object.keys(this.errors).length > 0
}
}
}
</script>
Loading

0 comments on commit 8c9fa24

Please sign in to comment.