-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Niklas Mohrin <dev@niklasmohrin.de>
- Loading branch information
1 parent
70e1746
commit 6aa3a5f
Showing
12 changed files
with
204 additions
and
67 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
declare const bootstrap: typeof import("bootstrap"); | ||
|
||
import { selectOrError, sleep, assert } from "./utils.js"; | ||
import { CSRF_HEADERS } from "./csrf-utils.js"; | ||
|
||
const SUCCESS_MESSAGE_TIMEOUT = 3000; | ||
|
||
export class ContactModalLogic { | ||
private readonly modal: bootstrap.Modal; | ||
private readonly successMessageModal: bootstrap.Modal; | ||
private readonly actionButtonElement: HTMLButtonElement; | ||
private readonly messageTextElement: HTMLInputElement; | ||
private readonly showButtonElement: HTMLElement; | ||
private readonly title: string; | ||
|
||
// may be null if anonymous feedback is not enabled | ||
private readonly anonymousRadioElement: HTMLInputElement | null; | ||
|
||
constructor(modalId: string, title: string) { | ||
this.title = title; | ||
this.modal = new bootstrap.Modal(selectOrError("#" + modalId)); | ||
this.successMessageModal = new bootstrap.Modal(selectOrError("#successMessageModal_" + modalId)); | ||
this.actionButtonElement = selectOrError("#" + modalId + "ActionButton"); | ||
this.messageTextElement = selectOrError("#" + modalId + "MessageText"); | ||
this.anonymousRadioElement = document.querySelector<HTMLInputElement>("#" + modalId + "AnonymousName"); | ||
this.showButtonElement = selectOrError("#" + modalId + "ShowButton"); | ||
} | ||
|
||
public attach = (): void => { | ||
this.actionButtonElement.addEventListener("click", async event => { | ||
this.actionButtonElement.disabled = true; | ||
event.preventDefault(); | ||
const message = this.messageTextElement.value; | ||
if (message.trim() === "") { | ||
this.modal.hide(); | ||
this.actionButtonElement.disabled = false; | ||
return; | ||
} | ||
try { | ||
const response = await fetch("/contact", { | ||
body: new URLSearchParams({ | ||
anonymous: String(this.anonymousRadioElement !== null && this.anonymousRadioElement.checked), | ||
message, | ||
title: this.title, | ||
}), | ||
headers: CSRF_HEADERS, | ||
method: "POST", | ||
}); | ||
assert(response.ok); | ||
} catch (_) { | ||
window.alert("Sending failed, sorry!"); | ||
return; | ||
} | ||
this.modal.hide(); | ||
this.successMessageModal.show(); | ||
this.messageTextElement.value = ""; | ||
|
||
await sleep(SUCCESS_MESSAGE_TIMEOUT); | ||
this.successMessageModal.hide(); | ||
this.actionButtonElement.disabled = false; | ||
}); | ||
|
||
this.showButtonElement.addEventListener("click", () => { | ||
this.modal.show(); | ||
}); | ||
}; | ||
} |
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,13 @@ | ||
export const selectOrError = <T extends Element>(selectors: string): T => { | ||
const elem = document.querySelector<T>(selectors); | ||
assert(elem, `Element with id ${selectors} not found`); | ||
return elem; | ||
}; | ||
|
||
export function assert(condition: unknown, message: string = "Assertion Failed"): asserts condition { | ||
if (!condition) throw new Error(message); | ||
} | ||
|
||
export const sleep = (ms: number): Promise<number> => { | ||
return new Promise(resolve => window.setTimeout(resolve, ms)); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.