-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Converted scripts to modules Miscellaneous: * fix country subtag * make styles href relative to root
- Loading branch information
1 parent
32a7a38
commit ac28475
Showing
6 changed files
with
125 additions
and
113 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
import * as Send from "/modules/send.js"; | ||
import * as HandleDependencies from "/modules/dependent-input.js"; | ||
|
||
const form = document.querySelector("form"); | ||
const output = document.querySelector("output"); | ||
|
||
Send.initialize(form, output); | ||
HandleDependencies.initialize(form); |
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,37 @@ | ||
let form; | ||
let dependencyTriggers, dependencyFollowers; | ||
|
||
function initialize(f) { | ||
form = f; | ||
|
||
dependencyTriggers = form.querySelectorAll("input.js-dependency"); | ||
dependencyFollowers = form.querySelectorAll(".js-dependency:not(input)"); | ||
|
||
dependencyTriggers.forEach((trigger) => { | ||
// consider initial states | ||
if (trigger.checked) { | ||
updateDependencyFollowers(trigger.name, trigger.value); | ||
} | ||
|
||
// update on change | ||
trigger.addEventListener("change", () => { | ||
updateDependencyFollowers( | ||
trigger.name, | ||
trigger.value, | ||
trigger.checked | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
function updateDependencyFollowers(key, value, checked = true) { | ||
dependencyFollowers.forEach((follower) => { | ||
if (follower.dataset[key]) { | ||
if (checked && follower.dataset[key] == value) | ||
follower.removeAttribute("hidden"); | ||
else follower.setAttribute("hidden", ""); | ||
} | ||
}); | ||
} | ||
|
||
export { initialize }; |
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,77 @@ | ||
let form, output; | ||
|
||
function initialize(f, o) { | ||
form = f; | ||
output = o; | ||
|
||
form.addEventListener("submit", handleSubmit); | ||
} | ||
|
||
function handleSubmit(event) { | ||
event.preventDefault(); | ||
|
||
let submitButton = event.submitter; | ||
let url = constructUrl(form.elements); | ||
let body = new FormData(); | ||
|
||
output.value = ""; | ||
submitButton.setAttribute("disabled", ""); | ||
|
||
// TODO: Need to teach this how to check dependent fields | ||
if (form.elements.buildFrom.value === "json") { | ||
body.append("payload_json", form.elements.payload_json.value); | ||
} else { | ||
body.append("content", form.elements.content.value); | ||
} | ||
|
||
fetch(url, { | ||
body: body, | ||
method: "POST", | ||
}).then((response) => { | ||
console.log(response); | ||
|
||
displayResponse(response); | ||
}); | ||
|
||
submitButton.removeAttribute("disabled"); | ||
} | ||
|
||
function constructUrl(elements) { | ||
let baseUrl = elements.action.value; | ||
let threadId = elements.thread_id.value.trim(); | ||
|
||
if (threadId) { | ||
return `${baseUrl}?thread_id=${threadId}`; | ||
} | ||
|
||
return baseUrl; | ||
} | ||
|
||
function displayResponse(response) { | ||
if (response.ok) { | ||
insertLinebreak(output); | ||
output.value = "Message sent successfully"; | ||
} else { | ||
output.value = `HTTP Status Code ${response.status}: ${response.statusText}`; | ||
|
||
response.text().then((text) => { | ||
try { | ||
let json = JSON.parse(text); | ||
|
||
for (const [key, value] of Object.entries(json)) { | ||
insertLinebreak(output); | ||
output.append(`${key}: ${value}`); | ||
} | ||
} catch { | ||
insertLinebreak(output); | ||
output.append(text); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
function insertLinebreak(element) { | ||
element.append(document.createElement("br")); | ||
} | ||
|
||
export { initialize }; |