Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
open-source
aoz-003
Commits
2792d0e2
Verified
Commit
2792d0e2
authored
Jul 03, 2018
by
Kaspar Vollenweider
👻
Browse files
do the direct waive and iban update, instead of error prone cookie stuff
parent
745b2ddc
Changes
7
Hide whitespace changes
Inline
Side-by-side
app/assets/javascripts/last_submitted_hours_and_feedbacks_form.es6
View file @
2792d0e2
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 }))
})
}
app/controllers/volunteers_controller.rb
View file @
2792d0e2
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!
...
...
app/helpers/application_helper.rb
View file @
2792d0e2
...
...
@@ -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
)
...
...
app/policies/volunteer_policy.rb
View file @
2792d0e2
...
...
@@ -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
...
...
app/views/application/_last_submitted_hours_and_feedbacks.html.slim
View file @
2792d0e2
...
...
@@ -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
)
...
...
app/views/volunteers/update_waive_and_iban.json.jbuilder
0 → 100644
View file @
2792d0e2
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
config/routes.rb
View file @
2792d0e2
...
...
@@ -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
]
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment