amine_dubs
commited on
Commit
·
f742657
1
Parent(s):
7f4d6ac
no
Browse files- static/script.js +57 -75
static/script.js
CHANGED
@@ -56,86 +56,68 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
56 |
docLoadingIndicator.style.display = 'none';
|
57 |
}
|
58 |
|
59 |
-
//
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
const formData = new FormData(textForm);
|
65 |
-
const button = textForm.querySelector('button');
|
66 |
-
const textInput = document.getElementById('text-input');
|
67 |
-
|
68 |
-
// Validation check
|
69 |
-
if (!textInput.value.trim()) {
|
70 |
-
displayError('Please enter text to translate');
|
71 |
-
return;
|
72 |
-
}
|
73 |
-
|
74 |
-
button.disabled = true;
|
75 |
-
button.textContent = 'Translating...';
|
76 |
-
|
77 |
-
try {
|
78 |
-
console.log('Sending translation request...');
|
79 |
-
|
80 |
-
// Create JSON payload from FormData
|
81 |
-
const payload = {
|
82 |
-
text: formData.get('text'),
|
83 |
-
source_lang: formData.get('source_lang'),
|
84 |
-
target_lang: formData.get('target_lang')
|
85 |
-
};
|
86 |
|
87 |
-
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
'Content-Type': 'application/json',
|
93 |
-
},
|
94 |
-
body: JSON.stringify(payload)
|
95 |
-
});
|
96 |
-
|
97 |
-
console.log('Response status:', response.status, response.statusText);
|
98 |
-
|
99 |
-
// Get response data as text first for debugging
|
100 |
-
const responseText = await response.text();
|
101 |
-
console.log('Raw response:', responseText);
|
102 |
-
|
103 |
-
// Try to parse as JSON
|
104 |
-
let data;
|
105 |
-
try {
|
106 |
-
data = responseText ? JSON.parse(responseText) : null;
|
107 |
-
} catch (parseError) {
|
108 |
-
console.error('Error parsing JSON response:', parseError);
|
109 |
-
throw new Error(`Failed to parse server response: ${responseText}`);
|
110 |
}
|
111 |
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
}
|
126 |
-
|
127 |
-
|
128 |
-
textOutput.textContent = data.translated_text;
|
129 |
-
textResultBox.style.display = 'block';
|
130 |
-
|
131 |
-
} catch (error) {
|
132 |
-
console.error('Translation error:', error);
|
133 |
-
displayError(error);
|
134 |
-
} finally {
|
135 |
-
button.disabled = false;
|
136 |
-
button.textContent = 'Translate';
|
137 |
-
}
|
138 |
-
});
|
139 |
|
140 |
// Handle Document Translation Form Submission
|
141 |
docForm.addEventListener('submit', async (event) => {
|
|
|
56 |
docLoadingIndicator.style.display = 'none';
|
57 |
}
|
58 |
|
59 |
+
// Improve the text form submission handler
|
60 |
+
if (textForm) {
|
61 |
+
textForm.addEventListener('submit', async (e) => {
|
62 |
+
e.preventDefault();
|
63 |
+
clearFeedback();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
const sourceText = document.getElementById('source-text').value.trim();
|
66 |
+
const sourceLang = document.getElementById('text-source-lang').value;
|
67 |
+
const targetLang = document.getElementById('text-target-lang').value;
|
68 |
|
69 |
+
if (!sourceText) {
|
70 |
+
displayError('Please enter text to translate');
|
71 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
}
|
73 |
|
74 |
+
try {
|
75 |
+
// Show loading state
|
76 |
+
document.getElementById('text-loading').style.display = 'block';
|
77 |
+
|
78 |
+
const response = await fetch('/translate/text', {
|
79 |
+
method: 'POST',
|
80 |
+
headers: {
|
81 |
+
'Content-Type': 'application/json'
|
82 |
+
},
|
83 |
+
body: JSON.stringify({
|
84 |
+
text: sourceText,
|
85 |
+
source_lang: sourceLang,
|
86 |
+
target_lang: targetLang
|
87 |
+
})
|
88 |
+
});
|
89 |
+
|
90 |
+
// Hide loading state
|
91 |
+
document.getElementById('text-loading').style.display = 'none';
|
92 |
+
|
93 |
+
const data = await response.json();
|
94 |
+
|
95 |
+
if (!response.ok) {
|
96 |
+
// Properly extract error message from the response
|
97 |
+
if (data && data.error) {
|
98 |
+
displayError(data.error);
|
99 |
+
} else {
|
100 |
+
displayError(`Server error: ${response.status}`);
|
101 |
+
}
|
102 |
+
return;
|
103 |
+
}
|
104 |
+
|
105 |
+
if (!data.success && data.error) {
|
106 |
+
displayError(data.error);
|
107 |
+
return;
|
108 |
+
}
|
109 |
+
|
110 |
+
// Display the successful translation
|
111 |
+
textOutput.textContent = data.translated_text;
|
112 |
+
textResultBox.style.display = 'block';
|
113 |
+
|
114 |
+
} catch (error) {
|
115 |
+
console.error('Error:', error);
|
116 |
+
displayError('Network error or invalid response format');
|
117 |
+
document.getElementById('text-loading').style.display = 'none';
|
118 |
}
|
119 |
+
});
|
120 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
// Handle Document Translation Form Submission
|
123 |
docForm.addEventListener('submit', async (event) => {
|