-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from TheSecretOrganization/form
form system
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 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,53 @@ | ||
|
||
function submitForm(id, oncomplete, onerror) { | ||
let form = document.querySelector(`form#${id}`); | ||
let inputs = document.querySelectorAll(`form#${id} input`); | ||
let values = {}; | ||
|
||
if (form == undefined || inputs == undefined) | ||
throw new Error(`Unknown form with id ${id}`); | ||
|
||
if (!('url' in form.dataset)) | ||
throw new Error(`Missing data url in form id ${id}`); | ||
let url = form.dataset.url; | ||
|
||
for (let input of inputs) | ||
values[input.id] = input.value | ||
|
||
postFetch(url, getCookie('csrftoken'), values) | ||
.then((response) => { | ||
if (response.status == 200) | ||
oncomplete(); | ||
else | ||
response.json() | ||
.then(json => onerror(json)) | ||
.catch(error => console.error(error)); | ||
}).catch(error => console.error(error)); | ||
} | ||
|
||
function getCookie(name) { | ||
let cookieValue = null; | ||
if (document.cookie && document.cookie !== '') { | ||
const cookies = document.cookie.split(';'); | ||
for (let i = 0; i < cookies.length; i++) { | ||
const cookie = cookies[i].trim(); | ||
if (cookie.substring(0, name.length + 1) === (name + '=')) { | ||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); | ||
break; | ||
} | ||
} | ||
} | ||
return cookieValue; | ||
} | ||
|
||
function postFetch(url, csrf, body) { | ||
return fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'X-CSRFToken': csrf, | ||
'Content-Type': 'application/json', | ||
}, | ||
mode: 'same-origin', | ||
body: JSON.stringify(body), | ||
}); | ||
} |