amine_dubs commited on
Commit
f742657
·
1 Parent(s): 7f4d6ac
Files changed (1) hide show
  1. static/script.js +57 -75
static/script.js CHANGED
@@ -56,86 +56,68 @@ document.addEventListener('DOMContentLoaded', () => {
56
  docLoadingIndicator.style.display = 'none';
57
  }
58
 
59
- // Handle Text Translation Form Submission
60
- textForm.addEventListener('submit', async (event) => {
61
- event.preventDefault();
62
- clearFeedback();
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
- console.log('Payload:', payload);
 
 
88
 
89
- const response = await fetch('/translate/text', {
90
- method: 'POST',
91
- headers: {
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
- if (!data) {
113
- throw new Error('Server returned empty response');
114
- }
115
-
116
- // Check if the response indicates an error
117
- if (!response.ok) {
118
- const errorMsg = data.error || data.detail || 'Unknown server error';
119
- throw new Error(errorMsg);
120
- }
121
-
122
- // Check if we have actual translated text
123
- if (data.success === false || !data.translated_text) {
124
- throw new Error(data.error || 'No translation returned from server');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  }
126
-
127
- // Display the translation results
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) => {