forked from Aaron/Kino-Website
Anmeldefenster überarbeitet ; Accountdaten können über den Stift bearbeitet werden ; Texte bei Zahlungsmethoden aktualisiert
This commit is contained in:
358
account.js
358
account.js
@@ -19,15 +19,39 @@ function normalizeUser(user) {
|
||||
}
|
||||
|
||||
return {
|
||||
salutation: user.salutation || "",
|
||||
firstName: user.firstName || "",
|
||||
lastName: user.lastName || "",
|
||||
birthday: user.birthday || "",
|
||||
street: user.street || "",
|
||||
houseNumber: user.houseNumber || "",
|
||||
zip: user.zip || "",
|
||||
city: user.city || "",
|
||||
country: user.country || "Deutschland",
|
||||
email: user.email || "",
|
||||
password: user.password || "",
|
||||
passwordHash: user.passwordHash || (user.password ? hashPassword(user.password) : ""),
|
||||
orders: Array.isArray(user.orders) ? user.orders : [],
|
||||
paymentMethods: Array.isArray(user.paymentMethods) ? user.paymentMethods : []
|
||||
};
|
||||
}
|
||||
|
||||
function hashPassword(password) {
|
||||
let hashA = 0xdeadbeef;
|
||||
let hashB = 0x41c6ce57;
|
||||
const text = String(password || "");
|
||||
|
||||
for (let index = 0; index < text.length; index += 1) {
|
||||
const charCode = text.charCodeAt(index);
|
||||
hashA = Math.imul(hashA ^ charCode, 2654435761);
|
||||
hashB = Math.imul(hashB ^ charCode, 1597334677);
|
||||
}
|
||||
|
||||
hashA = Math.imul(hashA ^ (hashA >>> 16), 2246822507) ^ Math.imul(hashB ^ (hashB >>> 13), 3266489909);
|
||||
hashB = Math.imul(hashB ^ (hashB >>> 16), 2246822507) ^ Math.imul(hashA ^ (hashA >>> 13), 3266489909);
|
||||
|
||||
return `hash$${(hashB >>> 0).toString(16).padStart(8, "0")}${(hashA >>> 0).toString(16).padStart(8, "0")}`;
|
||||
}
|
||||
|
||||
function escapeHtml(value) {
|
||||
return String(value || "")
|
||||
.replaceAll("&", "&")
|
||||
@@ -58,45 +82,56 @@ if (!Array.isArray(users)) {
|
||||
users = [];
|
||||
}
|
||||
users = users.map(normalizeUser).filter(Boolean);
|
||||
persistUsers();
|
||||
|
||||
let currentUser = normalizeUser(readStorageJson("currentUser", null));
|
||||
if (currentUser && currentUser.email) {
|
||||
const storedMatch = users.find((user) => user.email === currentUser.email);
|
||||
if (storedMatch) {
|
||||
currentUser = storedMatch;
|
||||
persistCurrentUser();
|
||||
} else {
|
||||
users.push(currentUser);
|
||||
persistUsers();
|
||||
persistCurrentUser();
|
||||
}
|
||||
}
|
||||
|
||||
function registerUser() {
|
||||
if (!validateRegisterForm()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const salutation = document.getElementById("reg-salutation")?.value.trim() || "";
|
||||
const firstName = document.getElementById("reg-firstname")?.value.trim() || "";
|
||||
const lastName = document.getElementById("reg-lastname")?.value.trim() || "";
|
||||
const birthday = document.getElementById("reg-birthday")?.value.trim() || "";
|
||||
const street = document.getElementById("reg-street")?.value.trim() || "";
|
||||
const houseNumber = document.getElementById("reg-house-number")?.value.trim() || "";
|
||||
const zip = document.getElementById("reg-zip")?.value.trim() || "";
|
||||
const city = document.getElementById("reg-city")?.value.trim() || "";
|
||||
const country = document.getElementById("reg-country")?.value.trim() || "";
|
||||
const email = (document.getElementById("reg-email")?.value.trim() || "").toLowerCase();
|
||||
const password = document.getElementById("reg-password")?.value || "";
|
||||
|
||||
if (!firstName || !lastName || !email || !password) {
|
||||
alert("Bitte fuelle alle Felder aus.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!email.includes("@")) {
|
||||
alert("Bitte gib eine gueltige E-Mail-Adresse ein.");
|
||||
return;
|
||||
}
|
||||
|
||||
const existingUser = users.find((user) => user.email.toLowerCase() === email);
|
||||
if (existingUser) {
|
||||
alert("E-Mail bereits registriert");
|
||||
setFieldError(document.getElementById("reg-email"), "Diese E-Mail ist bereits registriert.");
|
||||
return;
|
||||
}
|
||||
|
||||
const newUser = {
|
||||
salutation,
|
||||
firstName,
|
||||
lastName,
|
||||
birthday,
|
||||
street,
|
||||
houseNumber,
|
||||
zip,
|
||||
city,
|
||||
country,
|
||||
email,
|
||||
password,
|
||||
passwordHash: hashPassword(password),
|
||||
orders: [],
|
||||
paymentMethods: []
|
||||
};
|
||||
@@ -107,22 +142,28 @@ function registerUser() {
|
||||
persistUsers();
|
||||
persistCurrentUser();
|
||||
|
||||
alert("Registrierung erfolgreich");
|
||||
document.getElementById("register-modal")?.classList.add("hidden");
|
||||
|
||||
openAccountDashboard();
|
||||
}
|
||||
|
||||
function loginUser() {
|
||||
if (!validateLoginForm()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const email = (document.getElementById("login-email")?.value.trim() || "").toLowerCase();
|
||||
const password = document.getElementById("login-password")?.value || "";
|
||||
const passwordHash = hashPassword(password);
|
||||
|
||||
const user = users.find(
|
||||
(entry) => entry.email.toLowerCase() === email && entry.password === password
|
||||
(entry) => entry.email.toLowerCase() === email && entry.passwordHash === passwordHash
|
||||
);
|
||||
|
||||
if (!user) {
|
||||
document.getElementById("login-error")?.classList.remove("hidden");
|
||||
setFieldError(document.getElementById("login-email"), "E-Mail oder Passwort stimmen nicht.");
|
||||
setFieldError(document.getElementById("login-password"), "E-Mail oder Passwort stimmen nicht.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -146,8 +187,16 @@ function openAccountDashboard() {
|
||||
<div class="account-panel">
|
||||
<div class="account-panel-header">
|
||||
<h2>Mein Konto</h2>
|
||||
<div class="account-header-actions">
|
||||
<button class="account-edit-btn" type="button" onclick="renderPersonalInfo(true)" aria-label="Persönliche Daten bearbeiten" title="Daten bearbeiten">
|
||||
<svg viewBox="0 0 24 24" aria-hidden="true">
|
||||
<path d="M12 20h9"></path>
|
||||
<path d="M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4Z"></path>
|
||||
</svg>
|
||||
</button>
|
||||
<button class="account-logout-btn" onclick="logoutUser()">Abmelden</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="account-tabs">
|
||||
<button class="account-tab-btn" onclick="renderPersonalInfo()">Persönliche Daten</button>
|
||||
@@ -162,19 +211,280 @@ function openAccountDashboard() {
|
||||
renderPersonalInfo();
|
||||
}
|
||||
|
||||
function renderPersonalInfo() {
|
||||
function renderPersonalInfo(isEditing = false) {
|
||||
const target = document.getElementById("account-tab-content");
|
||||
if (!target || !currentUser) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isEditing) {
|
||||
target.innerHTML = `
|
||||
<div class="account-card">
|
||||
<p><strong>Vorname:</strong> ${currentUser.firstName || "-"}</p>
|
||||
<p><strong>Nachname:</strong> ${currentUser.lastName || "-"}</p>
|
||||
<p><strong>E-Mail:</strong> ${currentUser.email || "-"}</p>
|
||||
<div class="account-card account-personal-card">
|
||||
<div class="account-card-title-row">
|
||||
<h3>Persönliche Daten bearbeiten</h3>
|
||||
<span>E-Mail bleibt unverändert</span>
|
||||
</div>
|
||||
<div class="account-edit-form">
|
||||
<label class="auth-field">
|
||||
<span>Anrede</span>
|
||||
<select id="account-edit-salutation">
|
||||
<option value="">Bitte wählen</option>
|
||||
<option value="Frau" ${currentUser.salutation === "Frau" ? "selected" : ""}>Frau</option>
|
||||
<option value="Herr" ${currentUser.salutation === "Herr" ? "selected" : ""}>Herr</option>
|
||||
<option value="Divers" ${currentUser.salutation === "Divers" ? "selected" : ""}>Divers</option>
|
||||
</select>
|
||||
<span class="field-error"></span>
|
||||
</label>
|
||||
<label class="auth-field">
|
||||
<span>Vorname</span>
|
||||
<input type="text" id="account-edit-firstname" value="${escapeHtml(currentUser.firstName)}" required data-required-message="Vorname ist ein Pflichtfeld.">
|
||||
<span class="field-error"></span>
|
||||
</label>
|
||||
<label class="auth-field">
|
||||
<span>Nachname</span>
|
||||
<input type="text" id="account-edit-lastname" value="${escapeHtml(currentUser.lastName)}" required data-required-message="Nachname ist ein Pflichtfeld.">
|
||||
<span class="field-error"></span>
|
||||
</label>
|
||||
<label class="auth-field">
|
||||
<span>Geburtstag</span>
|
||||
<input type="date" id="account-edit-birthday" value="${escapeHtml(currentUser.birthday)}">
|
||||
<span class="field-error"></span>
|
||||
</label>
|
||||
<label class="auth-field">
|
||||
<span>Straße</span>
|
||||
<input type="text" id="account-edit-street" value="${escapeHtml(currentUser.street)}">
|
||||
<span class="field-error"></span>
|
||||
</label>
|
||||
<label class="auth-field">
|
||||
<span>Nr.</span>
|
||||
<input type="text" id="account-edit-house-number" value="${escapeHtml(currentUser.houseNumber)}">
|
||||
<span class="field-error"></span>
|
||||
</label>
|
||||
<label class="auth-field">
|
||||
<span>PLZ</span>
|
||||
<input type="text" id="account-edit-zip" value="${escapeHtml(currentUser.zip)}">
|
||||
<span class="field-error"></span>
|
||||
</label>
|
||||
<label class="auth-field">
|
||||
<span>Ort</span>
|
||||
<input type="text" id="account-edit-city" value="${escapeHtml(currentUser.city)}">
|
||||
<span class="field-error"></span>
|
||||
</label>
|
||||
<label class="auth-field">
|
||||
<span>Land</span>
|
||||
<select id="account-edit-country">
|
||||
<option value="Deutschland" ${currentUser.country === "Deutschland" ? "selected" : ""}>Deutschland</option>
|
||||
<option value="Österreich" ${currentUser.country === "Österreich" ? "selected" : ""}>Österreich</option>
|
||||
<option value="Schweiz" ${currentUser.country === "Schweiz" ? "selected" : ""}>Schweiz</option>
|
||||
<option value="Luxemburg" ${currentUser.country === "Luxemburg" ? "selected" : ""}>Luxemburg</option>
|
||||
</select>
|
||||
<span class="field-error"></span>
|
||||
</label>
|
||||
<label class="auth-field account-email-locked">
|
||||
<span>E-Mail</span>
|
||||
<input type="email" value="${escapeHtml(currentUser.email)}" disabled>
|
||||
<small>Die E-Mail-Adresse ist fest mit deinem Konto verbunden.</small>
|
||||
</label>
|
||||
</div>
|
||||
<div class="account-edit-actions">
|
||||
<button type="button" class="account-secondary-btn" onclick="renderPersonalInfo()">Abbrechen</button>
|
||||
<button type="button" class="account-save-btn" onclick="savePersonalInfo()">Speichern</button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
target.innerHTML = `
|
||||
<div class="account-card account-personal-card">
|
||||
<h3>Persönliche Daten</h3>
|
||||
<div class="account-personal-grid">
|
||||
<p><span>Anrede</span><strong>${escapeHtml(currentUser.salutation || "-")}</strong></p>
|
||||
<p><span>Vorname</span><strong>${escapeHtml(currentUser.firstName || "-")}</strong></p>
|
||||
<p><span>Nachname</span><strong>${escapeHtml(currentUser.lastName || "-")}</strong></p>
|
||||
<p><span>Geburtstag</span><strong>${escapeHtml(currentUser.birthday || "-")}</strong></p>
|
||||
<p><span>Straße</span><strong>${escapeHtml(currentUser.street || "-")}</strong></p>
|
||||
<p><span>Nr.</span><strong>${escapeHtml(currentUser.houseNumber || "-")}</strong></p>
|
||||
<p><span>PLZ</span><strong>${escapeHtml(currentUser.zip || "-")}</strong></p>
|
||||
<p><span>Ort</span><strong>${escapeHtml(currentUser.city || "-")}</strong></p>
|
||||
<p><span>Land</span><strong>${escapeHtml(currentUser.country || "-")}</strong></p>
|
||||
<p><span>E-Mail</span><strong>${escapeHtml(currentUser.email || "-")}</strong></p>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
function savePersonalInfo() {
|
||||
if (!currentUser) {
|
||||
return;
|
||||
}
|
||||
|
||||
const requiredFields = ["account-edit-firstname", "account-edit-lastname"];
|
||||
const isValid = requiredFields
|
||||
.map((id) => validateAccountField(document.getElementById(id)))
|
||||
.every(Boolean);
|
||||
|
||||
if (!isValid) {
|
||||
return;
|
||||
}
|
||||
|
||||
currentUser.salutation = document.getElementById("account-edit-salutation")?.value || "";
|
||||
currentUser.firstName = document.getElementById("account-edit-firstname")?.value.trim() || "";
|
||||
currentUser.lastName = document.getElementById("account-edit-lastname")?.value.trim() || "";
|
||||
currentUser.birthday = document.getElementById("account-edit-birthday")?.value || "";
|
||||
currentUser.street = document.getElementById("account-edit-street")?.value.trim() || "";
|
||||
currentUser.houseNumber = document.getElementById("account-edit-house-number")?.value.trim() || "";
|
||||
currentUser.zip = document.getElementById("account-edit-zip")?.value.trim() || "";
|
||||
currentUser.city = document.getElementById("account-edit-city")?.value.trim() || "";
|
||||
currentUser.country = document.getElementById("account-edit-country")?.value || "Deutschland";
|
||||
|
||||
const userIndex = users.findIndex((user) => user.email === currentUser.email);
|
||||
if (userIndex >= 0) {
|
||||
users[userIndex] = currentUser;
|
||||
}
|
||||
|
||||
persistUsers();
|
||||
persistCurrentUser();
|
||||
renderPersonalInfo();
|
||||
}
|
||||
|
||||
function getFieldWrapper(input) {
|
||||
if (!input) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return input.closest(".auth-field") || document.querySelector(`[data-field-for="${input.id}"]`);
|
||||
}
|
||||
|
||||
function setFieldError(input, message) {
|
||||
const wrapper = getFieldWrapper(input);
|
||||
if (!wrapper) {
|
||||
return;
|
||||
}
|
||||
|
||||
input.dataset.touched = "true";
|
||||
wrapper.classList.add("is-invalid");
|
||||
const error = wrapper.querySelector(".field-error");
|
||||
if (error) {
|
||||
error.textContent = message;
|
||||
}
|
||||
}
|
||||
|
||||
function clearFieldError(input) {
|
||||
const wrapper = getFieldWrapper(input);
|
||||
if (!wrapper) {
|
||||
return;
|
||||
}
|
||||
|
||||
wrapper.classList.remove("is-invalid");
|
||||
const error = wrapper.querySelector(".field-error");
|
||||
if (error) {
|
||||
error.textContent = "";
|
||||
}
|
||||
}
|
||||
|
||||
function validateAccountField(input) {
|
||||
if (!input) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const value = (input.value || "").trim();
|
||||
if (input.required && !value) {
|
||||
setFieldError(input, input.dataset.requiredMessage || "Dieses Feld ist ein Pflichtfeld.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (input.type === "email" && value && !value.includes("@")) {
|
||||
setFieldError(input, input.dataset.emailMessage || "Bitte gib eine gültige E-Mail-Adresse ein.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (input.type === "password" && value && (value.length < 8 || value.length > 32)) {
|
||||
setFieldError(input, input.dataset.passwordMessage || "Passwort muss 8 bis 32 Zeichen haben.");
|
||||
return false;
|
||||
}
|
||||
|
||||
clearFieldError(input);
|
||||
return true;
|
||||
}
|
||||
|
||||
function validateRegisterForm() {
|
||||
const fieldIds = [
|
||||
"reg-salutation",
|
||||
"reg-firstname",
|
||||
"reg-lastname",
|
||||
"reg-birthday",
|
||||
"reg-street",
|
||||
"reg-house-number",
|
||||
"reg-zip",
|
||||
"reg-city",
|
||||
"reg-country",
|
||||
"reg-email",
|
||||
"reg-password"
|
||||
];
|
||||
|
||||
return fieldIds
|
||||
.map((id) => validateAccountField(document.getElementById(id)))
|
||||
.every(Boolean);
|
||||
}
|
||||
|
||||
function validateLoginForm() {
|
||||
return ["login-email", "login-password"]
|
||||
.map((id) => validateAccountField(document.getElementById(id)))
|
||||
.every(Boolean);
|
||||
}
|
||||
|
||||
function initAccountFieldValidation() {
|
||||
const fieldSelector = [
|
||||
"#login-email",
|
||||
"#login-password",
|
||||
"#reg-salutation",
|
||||
"#reg-firstname",
|
||||
"#reg-lastname",
|
||||
"#reg-birthday",
|
||||
"#reg-street",
|
||||
"#reg-house-number",
|
||||
"#reg-zip",
|
||||
"#reg-city",
|
||||
"#reg-country",
|
||||
"#reg-email",
|
||||
"#reg-password"
|
||||
].join(",");
|
||||
|
||||
document.querySelectorAll(fieldSelector).forEach((input) => {
|
||||
input.addEventListener("blur", () => {
|
||||
input.dataset.touched = "true";
|
||||
validateAccountField(input);
|
||||
});
|
||||
|
||||
input.addEventListener("input", () => {
|
||||
input.dataset.touched = "true";
|
||||
if (input.type !== "hidden") {
|
||||
validateAccountField(input);
|
||||
}
|
||||
});
|
||||
|
||||
input.addEventListener("change", () => {
|
||||
input.dataset.touched = "true";
|
||||
validateAccountField(input);
|
||||
});
|
||||
});
|
||||
|
||||
document.querySelectorAll(".gender-selector button").forEach((button) => {
|
||||
button.addEventListener("click", () => {
|
||||
const salutationInput = document.getElementById("reg-salutation");
|
||||
if (!salutationInput) {
|
||||
return;
|
||||
}
|
||||
|
||||
salutationInput.value = button.dataset.gender || "";
|
||||
salutationInput.dataset.touched = "true";
|
||||
button.closest(".gender-selector")?.querySelectorAll("button").forEach((item) => {
|
||||
item.classList.toggle("active", item === button);
|
||||
});
|
||||
validateAccountField(salutationInput);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function renderOrders() {
|
||||
@@ -220,7 +530,7 @@ function renderOrders() {
|
||||
target.innerHTML = `
|
||||
<div class="account-orders-shell">
|
||||
<h3>Meine Bestellungen</h3>
|
||||
<p class="account-payments-note">Klicke auf eine Bestellung, um dein Ticket-Detail zu sehen.</p>
|
||||
<p class="account-payments-note">Klicke auf eine Bestellung, um genauere Informationen zu sehen.</p>
|
||||
<div class="account-orders-grid">${orderHtml}</div>
|
||||
<div id="order-ticket-details" class="order-ticket-details hidden"></div>
|
||||
</div>
|
||||
@@ -293,7 +603,7 @@ function renderPayments() {
|
||||
target.innerHTML = `
|
||||
<div class="account-card">
|
||||
<h3>Zahlungsmethoden</h3>
|
||||
<p class="account-payments-note">Platzhalter zum Hinterlegen deiner Logos oder Anbieter-Informationen.</p>
|
||||
<p class="account-payments-note">Hier kannst du deine bevorzugten Zahlungsmethoden verwalten.</p>
|
||||
<div class="account-payment-grid">
|
||||
<button type="button" class="account-payment-card account-pay-trigger" data-pay-modal="pay-modal-card">
|
||||
<div class="payment-logo-slot">
|
||||
@@ -314,14 +624,14 @@ function renderPayments() {
|
||||
<img src="img/applepay.png" alt="Apple Pay">
|
||||
</div>
|
||||
<h4>Apple Pay</h4>
|
||||
<p>Geraet freischalten</p>
|
||||
<p>Gerät freischalten</p>
|
||||
</button>
|
||||
<button type="button" class="account-payment-card account-pay-trigger" data-pay-modal="pay-modal-google">
|
||||
<div class="payment-logo-slot">
|
||||
<img src="img/googlepay.png" alt="Google Pay">
|
||||
</div>
|
||||
<h4>Google Pay</h4>
|
||||
<p>Wallet verknuepfen</p>
|
||||
<p>Wallet verknüpfen</p>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user