Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Convert account forms to AJAX submission for seamless user experience

Murtaza Nasir20c1a5b75a49
diff --git a/app.py b/app.py
index 48fa0564..de195301 100644
--- a/app.py
+++ b/app.py
@@ -4059,6 +4059,12 @@ def account():
current_user.diarize = 'diarize' in request.form
db.session.commit()
+
+ # Return JSON response for AJAX requests
+ if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.accept_mimetypes.best == 'application/json':
+ return jsonify({'success': True, 'message': 'Account details updated successfully!'})
+
+ # Regular form submission with redirect
flash('Account details updated successfully!', 'success')
# Preserve the active tab when redirecting
diff --git a/templates/account.html b/templates/account.html
index e00d7365..bb35e4eb 100644
--- a/templates/account.html
+++ b/templates/account.html
@@ -117,7 +117,7 @@
<div class="grid grid-cols-1 md:grid-cols-2 gap-8">
<!-- Left Column: Form -->
<div>
- <form method="POST" action="{{ url_for('account') }}" class="space-y-6">
+ <form id="accountInfoForm" method="POST" action="{{ url_for('account') }}" class="space-y-6">
<div class="mb-6">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<h3 class="text-lg font-medium text-[var(--text-secondary)] mb-2">Personal Information</h3>
@@ -232,7 +232,7 @@
<div id="content-prompts" class="hidden">
<h2 class="text-2xl font-semibold text-[var(--text-primary)] mb-6">Custom Prompts</h2>
- <form method="POST" action="{{ url_for('account') }}" class="space-y-6">
+ <form id="customPromptsForm" method="POST" action="{{ url_for('account') }}" class="space-y-6">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<div class="bg-[var(--bg-tertiary)] p-6 rounded-lg border border-[var(--border-primary)]">
@@ -1647,6 +1647,58 @@
}
});
+ // AJAX form submission for Account Information
+ const accountInfoForm = document.getElementById('accountInfoForm');
+ if (accountInfoForm) {
+ accountInfoForm.addEventListener('submit', async (e) => {
+ e.preventDefault();
+
+ const formData = new FormData(accountInfoForm);
+
+ try {
+ const response = await fetch('/account', {
+ method: 'POST',
+ body: formData
+ });
+
+ if (response.ok) {
+ showToast('Account information updated successfully', 'success');
+ } else {
+ const text = await response.text();
+ showToast('Failed to update account information', 'error');
+ }
+ } catch (error) {
+ showToast('Network error: ' + error.message, 'error');
+ }
+ });
+ }
+
+ // AJAX form submission for Custom Prompts
+ const customPromptsForm = document.getElementById('customPromptsForm');
+ if (customPromptsForm) {
+ customPromptsForm.addEventListener('submit', async (e) => {
+ e.preventDefault();
+
+ const formData = new FormData(customPromptsForm);
+
+ try {
+ const response = await fetch('/account', {
+ method: 'POST',
+ body: formData
+ });
+
+ if (response.ok) {
+ showToast('Custom prompt saved successfully', 'success');
+ } else {
+ const text = await response.text();
+ showToast('Failed to save custom prompt', 'error');
+ }
+ } catch (error) {
+ showToast('Network error: ' + error.message, 'error');
+ }
+ });
+ }
+
// Check for URL hash and show appropriate tab
const urlHash = window.location.hash.substring(1); // Remove the '#' symbol
if (urlHash === 'prompts') {