feat: Add new Alpine.js component TypeScript files
Extracted inline JavaScript from templates into proper TypeScript modules: - api-explorer-docs.ts: API explorer page functionality - collection-rules.ts: Collection rules management page - index.ts: Homepage theme and auth redirect - login.ts: Login page theme initialization - profile-modal.ts: Profile modal close and escape key - profile.ts: Profile page delete account - register.ts: Registration page theme init - toast-error.ts: Error toast with retry button - unlinked_books.ts: Unlinked books management page Each file: - Uses ES imports (showToast, getToken, etc.) - Has proper TypeScript types - Registers with Alpine.js via Alpine.global() - Uses async/await for API calls
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { Alpine } from "./alpine";
|
||||
import { removeToken } from "./storage";
|
||||
import { showToast } from "./toast";
|
||||
|
||||
async function confirmDeleteAccount(): Promise<void> {
|
||||
if (
|
||||
!confirm(
|
||||
"Are you sure? All preferences and devices will be deleted. This action cannot be undone.",
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const token = localStorage.getItem("token");
|
||||
if (!token) {
|
||||
window.location.href = "/login";
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/auth/profile", {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
removeToken();
|
||||
localStorage.removeItem("user");
|
||||
window.location.href = "/login?deleted=true";
|
||||
} else {
|
||||
const error = await response.json();
|
||||
showToast(error.error || "Failed to delete account", "error");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to delete account", error);
|
||||
showToast("Failed to delete account", "error");
|
||||
}
|
||||
}
|
||||
|
||||
export { confirmDeleteAccount };
|
||||
|
||||
Alpine.global("profile", {
|
||||
confirmDeleteAccount,
|
||||
});
|
||||
Reference in New Issue
Block a user