amine_dubs commited on
Commit
841e389
·
1 Parent(s): def5634
Files changed (1) hide show
  1. static/script.js +141 -70
static/script.js CHANGED
@@ -1,4 +1,7 @@
1
  document.addEventListener('DOMContentLoaded', () => {
 
 
 
2
  const textForm = document.getElementById('text-translation-form');
3
  const docForm = document.getElementById('doc-translation-form');
4
  const textResultBox = document.getElementById('text-result');
@@ -10,7 +13,25 @@ document.addEventListener('DOMContentLoaded', () => {
10
  const errorMessageDiv = document.getElementById('error-message');
11
  const docLoadingIndicator = document.getElementById('doc-loading');
12
  const debugInfoDiv = document.getElementById('debug-info');
13
- const textLoadingDiv = document.getElementById('text-loading');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  // Helper function to display debug info
16
  function showDebug(message, data = null) {
@@ -21,8 +42,12 @@ document.addEventListener('DOMContentLoaded', () => {
21
  }
22
 
23
  console.log("DEBUG:", message, data || '');
24
- debugInfoDiv.textContent = debugText;
25
- debugInfoDiv.style.display = 'block';
 
 
 
 
26
  }
27
 
28
  // Helper function to display errors
@@ -51,39 +76,74 @@ document.addEventListener('DOMContentLoaded', () => {
51
  }
52
 
53
  console.error("Error details:", message);
54
- errorMessageDiv.textContent = errorText;
55
- errorMessageDiv.style.display = 'block';
56
-
57
- // Hide result boxes on error
58
- textResultBox.style.display = 'none';
59
- docResultBox.style.display = 'none';
 
 
 
 
 
60
  }
61
 
62
  // Helper function to clear errors and results
63
  function clearFeedback() {
64
- errorMessageDiv.style.display = 'none';
65
- errorMessageDiv.textContent = '';
66
- textResultBox.style.display = 'none';
67
- textOutput.textContent = '';
68
- docResultBox.style.display = 'none';
69
- docOutput.textContent = '';
70
- docFilename.textContent = '';
71
- docSourceLang.textContent = '';
72
- docLoadingIndicator.style.display = 'none';
73
- debugInfoDiv.style.display = 'none';
74
- debugInfoDiv.textContent = '';
 
 
 
 
 
 
 
 
75
  }
76
 
77
  // Fix the text form submission handler
78
  if (textForm) {
 
79
  textForm.addEventListener('submit', async (e) => {
 
80
  e.preventDefault();
81
  clearFeedback();
82
 
 
 
 
83
  // Use correct field IDs matching the HTML
84
- const sourceText = document.getElementById('text-input').value.trim();
85
- const sourceLang = document.getElementById('source-lang-text').value;
86
- const targetLang = document.getElementById('target-lang-text').value;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  if (!sourceText) {
89
  displayError('Please enter text to translate');
@@ -92,7 +152,7 @@ document.addEventListener('DOMContentLoaded', () => {
92
 
93
  try {
94
  // Show loading state
95
- textLoadingDiv.style.display = 'block';
96
 
97
  // Log payload for debugging
98
  const payload = {
@@ -102,60 +162,71 @@ document.addEventListener('DOMContentLoaded', () => {
102
  };
103
  showDebug('Sending translation request', payload);
104
 
105
- const response = await fetch('/translate/text', {
106
- method: 'POST',
107
- headers: {
108
- 'Content-Type': 'application/json'
109
- },
110
- body: JSON.stringify(payload)
111
- });
112
-
113
- // Log response status
114
- showDebug(`Response status: ${response.status}`);
115
-
116
- // Get the raw response text first for debugging
117
- const responseText = await response.text();
118
- showDebug('Raw response:', responseText);
119
-
120
- // Then parse it as JSON
121
- let data;
122
  try {
123
- data = JSON.parse(responseText);
124
- } catch (jsonError) {
125
- throw new Error(`Invalid JSON response: ${responseText}`);
126
- }
127
-
128
- // Hide loading state
129
- textLoadingDiv.style.display = 'none';
130
-
131
- if (!response.ok) {
132
- if (data && data.error) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  displayError(data.error);
 
 
 
 
 
 
 
 
 
 
 
134
  } else {
135
- displayError(`Server error: ${response.status}`);
136
  }
137
- return;
 
 
 
138
  }
139
-
140
- if (!data.success && data.error) {
141
- displayError(data.error);
142
- return;
143
- }
144
-
145
- if (!data.translated_text) {
146
- displayError('Translation returned empty text');
147
- return;
148
- }
149
-
150
- textOutput.textContent = data.translated_text;
151
- textResultBox.style.display = 'block';
152
-
153
  } catch (error) {
154
- console.error('Error:', error);
155
- displayError(`Network error: ${error.message}`);
156
 
157
  // Hide loading
158
- textLoadingDiv.style.display = 'none';
159
  }
160
  });
161
  } else {
 
1
  document.addEventListener('DOMContentLoaded', () => {
2
+ // Log when the page loads to confirm JavaScript is working
3
+ console.log("Translation app initialized");
4
+
5
  const textForm = document.getElementById('text-translation-form');
6
  const docForm = document.getElementById('doc-translation-form');
7
  const textResultBox = document.getElementById('text-result');
 
13
  const errorMessageDiv = document.getElementById('error-message');
14
  const docLoadingIndicator = document.getElementById('doc-loading');
15
  const debugInfoDiv = document.getElementById('debug-info');
16
+
17
+ // Check if all elements are found
18
+ if (!textForm) console.error("ERROR: Text translation form not found!");
19
+ if (!textResultBox) console.error("ERROR: Text result box not found!");
20
+ if (!textOutput) console.error("ERROR: Text output element not found!");
21
+ if (!errorMessageDiv) console.error("ERROR: Error message div not found!");
22
+
23
+ // Create text loading indicator if it doesn't exist
24
+ let textLoadingDiv = document.getElementById('text-loading');
25
+ if (!textLoadingDiv) {
26
+ console.log("Creating missing text loading indicator");
27
+ textLoadingDiv = document.createElement('div');
28
+ textLoadingDiv.id = 'text-loading';
29
+ textLoadingDiv.className = 'loading-spinner';
30
+ textLoadingDiv.textContent = 'Translating...';
31
+ if (textForm) {
32
+ textForm.appendChild(textLoadingDiv);
33
+ }
34
+ }
35
 
36
  // Helper function to display debug info
37
  function showDebug(message, data = null) {
 
42
  }
43
 
44
  console.log("DEBUG:", message, data || '');
45
+ if (debugInfoDiv) {
46
+ debugInfoDiv.textContent = debugText;
47
+ debugInfoDiv.style.display = 'block';
48
+ } else {
49
+ console.error("Debug div not found!");
50
+ }
51
  }
52
 
53
  // Helper function to display errors
 
76
  }
77
 
78
  console.error("Error details:", message);
79
+ if (errorMessageDiv) {
80
+ errorMessageDiv.textContent = errorText;
81
+ errorMessageDiv.style.display = 'block';
82
+
83
+ // Hide result boxes on error
84
+ if (textResultBox) textResultBox.style.display = 'none';
85
+ if (docResultBox) docResultBox.style.display = 'none';
86
+ } else {
87
+ // If error div not found, use alert as fallback
88
+ alert("Error: " + errorText);
89
+ }
90
  }
91
 
92
  // Helper function to clear errors and results
93
  function clearFeedback() {
94
+ if (errorMessageDiv) {
95
+ errorMessageDiv.style.display = 'none';
96
+ errorMessageDiv.textContent = '';
97
+ }
98
+ if (textResultBox) {
99
+ textResultBox.style.display = 'none';
100
+ if (textOutput) textOutput.textContent = '';
101
+ }
102
+ if (docResultBox) {
103
+ docResultBox.style.display = 'none';
104
+ if (docOutput) docOutput.textContent = '';
105
+ if (docFilename) docFilename.textContent = '';
106
+ if (docSourceLang) docSourceLang.textContent = '';
107
+ }
108
+ if (docLoadingIndicator) docLoadingIndicator.style.display = 'none';
109
+ if (debugInfoDiv) {
110
+ debugInfoDiv.style.display = 'none';
111
+ debugInfoDiv.textContent = '';
112
+ }
113
  }
114
 
115
  // Fix the text form submission handler
116
  if (textForm) {
117
+ console.log("Adding submit event listener to text form");
118
  textForm.addEventListener('submit', async (e) => {
119
+ console.log("Form submitted!");
120
  e.preventDefault();
121
  clearFeedback();
122
 
123
+ // Show debug immediately to confirm the handler is working
124
+ showDebug('Translation submission triggered');
125
+
126
  // Use correct field IDs matching the HTML
127
+ const textInput = document.getElementById('text-input');
128
+ const sourceLangSelect = document.getElementById('source-lang-text');
129
+ const targetLangSelect = document.getElementById('target-lang-text');
130
+
131
+ if (!textInput) {
132
+ displayError('Text input element not found!');
133
+ return;
134
+ }
135
+ if (!sourceLangSelect) {
136
+ displayError('Source language select not found!');
137
+ return;
138
+ }
139
+ if (!targetLangSelect) {
140
+ displayError('Target language select not found!');
141
+ return;
142
+ }
143
+
144
+ const sourceText = textInput.value.trim();
145
+ const sourceLang = sourceLangSelect.value;
146
+ const targetLang = targetLangSelect.value;
147
 
148
  if (!sourceText) {
149
  displayError('Please enter text to translate');
 
152
 
153
  try {
154
  // Show loading state
155
+ if (textLoadingDiv) textLoadingDiv.style.display = 'block';
156
 
157
  // Log payload for debugging
158
  const payload = {
 
162
  };
163
  showDebug('Sending translation request', payload);
164
 
165
+ // Check if API is available
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  try {
167
+ const response = await fetch('/translate/text', {
168
+ method: 'POST',
169
+ headers: {
170
+ 'Content-Type': 'application/json'
171
+ },
172
+ body: JSON.stringify(payload)
173
+ });
174
+
175
+ // Log response status
176
+ showDebug(`Response status: ${response.status}`);
177
+
178
+ // Get the raw response text first for debugging
179
+ const responseText = await response.text();
180
+ showDebug('Raw response:', responseText);
181
+
182
+ // Then parse it as JSON
183
+ let data;
184
+ try {
185
+ data = JSON.parse(responseText);
186
+ } catch (jsonError) {
187
+ showDebug('JSON parse error:', jsonError);
188
+ throw new Error(`Invalid JSON response: ${responseText}`);
189
+ }
190
+
191
+ // Hide loading state
192
+ if (textLoadingDiv) textLoadingDiv.style.display = 'none';
193
+
194
+ if (!response.ok) {
195
+ if (data && data.error) {
196
+ displayError(data.error);
197
+ } else {
198
+ displayError(`Server error: ${response.status}`);
199
+ }
200
+ return;
201
+ }
202
+
203
+ if (!data.success && data.error) {
204
  displayError(data.error);
205
+ return;
206
+ }
207
+
208
+ if (!data.translated_text) {
209
+ displayError('Translation returned empty text');
210
+ return;
211
+ }
212
+
213
+ if (textOutput) {
214
+ textOutput.textContent = data.translated_text;
215
+ if (textResultBox) textResultBox.style.display = 'block';
216
  } else {
217
+ displayError('Text output element not found!');
218
  }
219
+
220
+ } catch (fetchError) {
221
+ console.error('Fetch error:', fetchError);
222
+ displayError(`Network error: ${fetchError.message}. Make sure the backend server is running.`);
223
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
  } catch (error) {
225
+ console.error('General error:', error);
226
+ displayError(`Error: ${error.message}`);
227
 
228
  // Hide loading
229
+ if (textLoadingDiv) textLoadingDiv.style.display = 'none';
230
  }
231
  });
232
  } else {