Verified Commit 2792d0e2 authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻
Browse files

do the direct waive and iban update, instead of error prone cookie stuff

parent 745b2ddc
function lastSubmittedHoursAndFeedbacksForm() {
let $form = $('.last-submitted-hours-and-feedbacks form');
if ($form.length) {
restoreForm($form);
$('.last-submitted-hours-and-feedbacks .submit-button').on('click', ({target}) => {
clearFormData($form);
$form.submit();
return false
});
$(window).on('beforeunload', (event) => {
storeFormData($form);
});
}
}
const storeFormData = (form) => {
let arrayOfValues = form.serializeArray();
form.find('input:checkbox').each(function() {
arrayOfValues = arrayOfValues.filter(object => object.name != this.name);
arrayOfValues.push({ name: this.name, value: this.checked });
});
let stringValues = JSON.stringify(arrayOfValues);
let name = cookieNameFor(form);
Cookies.set(name, stringValues, { expires: 7, path: '' });
}
const restoreForm = (form) => {
let name = cookieNameFor(form);
let formValues = Cookies.get(name);
if (formValues) {
formValues = JSON.parse(formValues);
for (var i = 0; i < formValues.length; i++) {
let name = formValues[i].name;
let value = formValues[i].value;
if ($("input[name='" + name + "']").is(':checkbox')) {
$("input[name='" + name + "']").attr('checked', (value === true));
$("input[name='" + name + "']").val((value === true ? 1 : ''));
$("input[name='" + name + "']").trigger('change');
} else {
$("input[name='" + name + "'], select[name='" + name + "']").val(value);
}
}
}
const updateFieldXhr = ({ fieldName, fieldValue, volunteerId, authToken, targetElement }) => {
const volunteer = {};
volunteer[fieldName] = fieldValue;
$.ajax({
data: { volunteer },
method: 'PATCH',
dataType: 'json',
headers: { 'X-CSRF-Token': authToken },
url: `/volunteers/${volunteerId}/update_waive_and_iban`
}).done(() => {
targetElement.on('input', ({ target }) => inputHandler({ target, fieldName, volunteerId, authToken }))
}).fail(error => {
console.log(error)
targetElement.on('input', ({ target }) => inputHandler({ target, fieldName, volunteerId, authToken }))
})
}
const clearFormData = (form) => {
let today = new Date();
let expired = new Date(today.getTime() - 24 * 3600 * 1000);
let name = cookieNameFor(form);
const valueOrChecked = field => (field.prop('type') === 'checkbox') ? field.prop('checked') : field.val();
Cookies.remove(name);
const inputHandler = ({ target, fieldName, volunteerId, authToken }) => {
const targetElement = $(target);
targetElement.off('input')
updateFieldXhr({ fieldName, fieldValue: valueOrChecked(targetElement), volunteerId, authToken, targetElement })
}
const cookieNameFor = (form) => {
return form.attr('id') + '_form_values'
const lastSubmittedHoursAndFeedbacksForm = () => {
const authToken = $('input[name="authenticity_token"]').val()
const waiveIbanForm = $('#volunteer-update-waive-and-iban')
const volunteerId = waiveIbanForm.find('input[name$="assignment[volunteer_attributes][id]"]').val()
const formFields = ['waive', 'iban', 'bank'].map(fieldName => (
[fieldName, waiveIbanForm.find(`input[name$="assignment[volunteer_attributes][${fieldName}]"]`)]
))
formFields.map(([fieldName, inputElement]) => {
$(inputElement).on('input', ({ target }) => inputHandler({ target, fieldName, volunteerId, authToken }))
})
}
class VolunteersController < ApplicationController
before_action :set_volunteer, only: [:show, :edit, :update, :terminate, :account]
before_action :set_volunteer, only: [:show, :edit, :update, :terminate, :account, :update_waive_and_iban]
def index
authorize Volunteer
......@@ -79,6 +79,12 @@ class VolunteersController < ApplicationController
end
end
def update_waive_and_iban
authorize @volunteer
@volunteer.update(volunteer_params)
respond_to :json
end
def terminate
if @volunteer.terminatable?
@volunteer.terminate!
......
......@@ -53,11 +53,10 @@ module ApplicationHelper
}, class: html_class }
end
def checkbox_toggle_collapse(f, field, collapse_selector, check_shows: true, label_html: nil)
f.input(field,
input_html: { data: { collapse: collapse_selector, check_shows: check_shows },
class: 'checkbox-toggle-collapse' },
label_html: label_html)
def checkbox_toggle_collapse(f, field, collapse_selector, check_shows: true, label_html: nil, type: :boolean)
input_html = { data: { collapse: collapse_selector, check_shows: check_shows },
class: 'checkbox-toggle-collapse' }
f.input(field, type: type, input_html: input_html, label_html: label_html)
end
def single_field_fieldset(f, field, input_html: nil, fieldset_html: nil, legend_html: nil)
......
......@@ -45,6 +45,7 @@ class VolunteerPolicy < ApplicationPolicy
alias_method :show?, :volunteer_managing_or_volunteers_profile?
alias_method :edit?, :volunteer_managing_or_volunteers_profile?
alias_method :update?, :volunteer_managing_or_volunteers_profile?
alias_method :update_waive_and_iban?, :volunteer_managing_or_volunteers_profile?
alias_method :account?, :superadmin?
# supplementary policies
......
......@@ -9,15 +9,15 @@
h3 Spesen
= f.simple_fields_for :volunteer do |vf|
.row
.col-xs-12.text-left
.row.text-left#volunteer-update-waive-and-iban
.col-xs-12
= vf.input_field :id, type: :hidden
= checkbox_toggle_collapse(vf, :waive, '#not-waive-expenses', check_shows: false)
.col-xs-12.text-left#not-waive-expenses
.col-xs-12#not-waive-expenses
.row
.col-xs-4.text-left
= vf.input :iban
.col-xs-4.text-left
= vf.input :bank
.col-xs-4= vf.input :iban
.col-xs-4= vf.input :bank
h3 Stunden
= render 'last_submitted_hours', hours: hours, confirmable: confirmable
p.m-b-30= button_link t_title(:new, Hour), new_polymorphic_path([confirmable.volunteer, confirmable, Hour], redirect_to: request.fullpath)
......
json.volunteer do
json.id @volunteer.id
json.waive @volunteer.waive
json.iban @volunteer.iban
json.bank @volunteer.bank
json.errors @volunteer.errors&.messages
end
......@@ -103,6 +103,7 @@ Rails.application.routes.draw do
put :account, on: :member
get :find_client, on: :member, to: 'assignments#find_client'
get :seeking_clients, on: :collection
patch :update_waive_and_iban, on: :member
resources :assignments, except: [:destroy], concerns: [:assignment_feedbacks, :hours_resources]
resources :billing_expenses, only: [:index]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment