-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
140 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* ZDS ajax library | ||
* This library was created to reduce code duplication when we use ajax and ease up the JQuery removal | ||
* We use fetch API | ||
*/ | ||
class ZDSAjax { | ||
constructor() { | ||
this._crsf = document.querySelector("input[name='csrfmiddlewaretoken']").getAttribute('value') | ||
} | ||
|
||
get(url, dataCallback, errorCallback = (error) => console.error(error)) { | ||
const headers = new Headers() | ||
headers.append('Accept', 'application/json') | ||
headers.append('X-CSRFToken', this._crsf) | ||
headers.append('X-REQUESTED-WITH', 'XMLHttpRequest') | ||
const init = { | ||
method: 'GET', | ||
headers: headers, | ||
mode: 'cors', | ||
cache: 'default' | ||
} | ||
fetch(new Request(url, init), init).then(response => { | ||
if (response.ok) { | ||
return Promise.resolve(response.json()) | ||
} | ||
throw response.error() | ||
}).then(dataCallback).catch(errorCallback) | ||
} | ||
|
||
put(url, jsonOrFormData, dataCallback, errorCallback) { | ||
return this._sendRequestWithData(jsonOrFormData, 'PUT', url, dataCallback, errorCallback) | ||
} | ||
|
||
post(url, jsonOrFormData, dataCallback, errorCallback) { | ||
return this._sendRequestWithData(jsonOrFormData, 'POST', url, dataCallback, errorCallback) | ||
} | ||
|
||
_sendRequestWithData(jsonOrFormData, method, url, dataCallback, errorCallback) { | ||
const headers = new Headers() | ||
headers.append('Accept', 'application/json') | ||
headers.append('X-CSRFToken', this._crsf) | ||
headers.append('X-REQUESTED-WITH', 'XMLHttpRequest') | ||
const init = { | ||
method: method, | ||
headers: headers, | ||
mode: 'cors', | ||
cache: 'default', | ||
body: jsonOrFormData | ||
} | ||
return fetch(url, init).then(response => { | ||
return Promise.resolve(response.json()) | ||
}).then(dataCallback).catch(errorCallback) | ||
} | ||
} | ||
window.ajax = new ZDSAjax() |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,34 @@ | ||
(function($) { | ||
function changeHelpButtonState($helpButton, state) { | ||
const helpButtonClasses = $helpButton[0].classList | ||
(function(ajax) { | ||
function changeHelpButtonState(helpButton, state) { | ||
const helpButtonClasses = helpButton.classList | ||
|
||
helpButtonClasses.toggle('selected', state) | ||
helpButtonClasses.toggle('ico-after', state) | ||
helpButtonClasses.toggle('tick', state) | ||
helpButtonClasses.toggle('green', state) | ||
|
||
$helpButton.attr('data-activated', state.toString()) | ||
$helpButton.blur() | ||
helpButton.setAttribute('data-activated', state.toString()) | ||
|
||
$helpButton.parent().find('input[name="activated"]').val((!state).toString()) | ||
helpButton.parentNode.querySelector('input[name="activated"]') | ||
.setAttribute('value', (!state).toString()) | ||
} | ||
|
||
$('.help-toggle').click((e) => { | ||
e.preventDefault() | ||
document.querySelectorAll('.help-toggle') | ||
.forEach((element) => element.addEventListener('click', e => { | ||
const current = e.target | ||
|
||
const $current = $(e.target) | ||
const $form = $current.parent() | ||
const data = $form.serialize() | ||
const newActivation = $current.attr('data-activated') !== 'true' | ||
const form = current.parentElement | ||
const data = new FormData(form) | ||
const newActivation = current.getAttribute('data-activated') !== 'true' | ||
// Change status before request for instant feeling. | ||
// Will be changed back on error. | ||
changeHelpButtonState(current, newActivation) | ||
e.preventDefault() | ||
e.stopPropagation() | ||
|
||
// Change status before request for instant feeling. | ||
// Will be changed back on error. | ||
// This update the form so serialize must be called before/ | ||
changeHelpButtonState($current, newActivation) | ||
|
||
$.ajax($form.attr('action'), { | ||
method: 'POST', | ||
data, | ||
success: resultData => changeHelpButtonState($current, | ||
resultData.help_wanted), | ||
error: () => changeHelpButtonState($current, !newActivation) | ||
}) | ||
}) | ||
})(jQuery) | ||
ajax.post(form.getAttribute('action'), data, | ||
(result) => changeHelpButtonState(current, result.help_wanted), | ||
() => changeHelpButtonState(current, !newActivation) | ||
) | ||
})) | ||
})(window.ajax) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters