Tristan Yu commited on
Commit
eec598e
·
1 Parent(s): eaf86e0

Fix API calls to use api service for TutorialTasks - restore Week 1 submissions

Browse files
Files changed (1) hide show
  1. src/pages/TutorialTasks.tsx +70 -134
src/pages/TutorialTasks.tsx CHANGED
@@ -87,14 +87,16 @@ const TutorialTasks: React.FC = () => {
87
  const fetchUserSubmissions = useCallback(async (tasks: TutorialTask[]) => {
88
  try {
89
  const token = localStorage.getItem('token');
90
- const response = await fetch('/api/submissions/my-submissions', {
91
- headers: {
92
- 'Authorization': `Bearer ${token}`
93
- }
94
- });
 
 
95
 
96
- if (response.ok) {
97
- const data = await response.json();
98
 
99
  const groupedSubmissions: {[key: string]: UserSubmission[]} = {};
100
 
@@ -104,14 +106,16 @@ const TutorialTasks: React.FC = () => {
104
  });
105
 
106
  // Then populate with actual submissions
107
- tasks.forEach(task => {
108
- const taskSubmissions = data.submissions.filter((sub: any) =>
109
- sub.sourceTextId && sub.sourceTextId._id === task._id
110
- );
111
- if (taskSubmissions.length > 0) {
112
- groupedSubmissions[task._id] = taskSubmissions;
113
- }
114
- });
 
 
115
 
116
  setUserSubmissions(groupedSubmissions);
117
  }
@@ -124,14 +128,17 @@ const TutorialTasks: React.FC = () => {
124
  try {
125
  setLoading(true);
126
  const token = localStorage.getItem('token');
127
- const response = await fetch(`/api/search/tutorial-tasks/${selectedWeek}`, {
128
- headers: {
129
- 'Authorization': `Bearer ${token}`
130
- }
131
- });
 
 
 
132
 
133
- if (response.ok) {
134
- const tasks = await response.json();
135
  setTutorialTasks(tasks);
136
 
137
  // Organize tasks into week structure
@@ -156,7 +163,7 @@ const TutorialTasks: React.FC = () => {
156
  } finally {
157
  setLoading(false);
158
  }
159
- }, [selectedWeek, fetchUserSubmissions]);
160
 
161
  useEffect(() => {
162
  const user = localStorage.getItem('user');
@@ -188,34 +195,24 @@ const TutorialTasks: React.FC = () => {
188
 
189
  try {
190
  setSubmitting({ ...submitting, [taskId]: true });
191
- const token = localStorage.getItem('token');
192
  const user = JSON.parse(localStorage.getItem('user') || '{}');
193
- const response = await fetch('/api/submissions', {
194
- method: 'POST',
195
- headers: {
196
- 'Authorization': `Bearer ${token}`,
197
- 'Content-Type': 'application/json'
198
- },
199
- body: JSON.stringify({
200
- sourceTextId: taskId,
201
- transcreation: translationText[taskId],
202
- groupNumber: selectedGroups[taskId],
203
- culturalAdaptations: [],
204
- username: user.name || 'Unknown'
205
- })
206
  });
207
 
208
- if (response.ok) {
209
- const result = await response.json();
210
  console.log('Submission created successfully:', result);
211
 
212
  setTranslationText({ ...translationText, [taskId]: '' });
213
  setSelectedGroups({ ...selectedGroups, [taskId]: 0 });
214
  await fetchUserSubmissions(tutorialTasks);
215
  } else {
216
- const error = await response.json();
217
- console.error('Failed to submit translation:', error);
218
-
219
  }
220
  } catch (error) {
221
  console.error('Error submitting translation:', error);
@@ -237,26 +234,17 @@ const TutorialTasks: React.FC = () => {
237
  if (!editingSubmission || !editSubmissionText.trim()) return;
238
 
239
  try {
240
- const token = localStorage.getItem('token');
241
- const response = await fetch(`/api/submissions/${editingSubmission.id}`, {
242
- method: 'PUT',
243
- headers: {
244
- 'Authorization': `Bearer ${token}`,
245
- 'Content-Type': 'application/json'
246
- },
247
- body: JSON.stringify({
248
- transcreation: editSubmissionText
249
- })
250
  });
251
 
252
- if (response.ok) {
253
 
254
  setEditingSubmission(null);
255
  setEditSubmissionText('');
256
  await fetchUserSubmissions(tutorialTasks);
257
  } else {
258
- const error = await response.json();
259
-
260
  }
261
  } catch (error) {
262
  console.error('Error updating translation:', error);
@@ -342,24 +330,16 @@ const TutorialTasks: React.FC = () => {
342
  return;
343
  }
344
 
345
- const response = await fetch(`/api/auth/admin/tutorial-brief/${selectedWeek}`, {
346
- method: 'PUT',
347
- headers: {
348
- 'Authorization': `Bearer ${token}`,
349
- 'Content-Type': 'application/json',
350
- 'user-role': user.role
351
- },
352
- body: JSON.stringify({
353
- translationBrief: '',
354
- weekNumber: selectedWeek
355
- })
356
  });
357
 
358
- if (response.ok) {
359
  await fetchTutorialTasks();
360
 
361
  } else {
362
- const error = await response.json();
363
 
364
  }
365
  } catch (error) {
@@ -395,25 +375,17 @@ const TutorialTasks: React.FC = () => {
395
  return;
396
  }
397
 
398
- const response = await fetch(`/api/auth/admin/tutorial-tasks/${editingTask}`, {
399
- method: 'PUT',
400
- headers: {
401
- 'Authorization': `Bearer ${token}`,
402
- 'Content-Type': 'application/json',
403
- 'user-role': user.role
404
- },
405
- body: JSON.stringify({
406
- ...editForm,
407
- weekNumber: selectedWeek
408
- })
409
  });
410
 
411
- if (response.ok) {
412
  await fetchTutorialTasks();
413
  setEditingTask(null);
414
 
415
  } else {
416
- const error = await response.json();
417
 
418
  }
419
  } catch (error) {
@@ -436,25 +408,17 @@ const TutorialTasks: React.FC = () => {
436
  return;
437
  }
438
 
439
- const response = await fetch(`/api/auth/admin/tutorial-brief/${selectedWeek}`, {
440
- method: 'PUT',
441
- headers: {
442
- 'Authorization': `Bearer ${token}`,
443
- 'Content-Type': 'application/json',
444
- 'user-role': user.role
445
- },
446
- body: JSON.stringify({
447
- translationBrief: editForm.translationBrief,
448
- weekNumber: selectedWeek
449
- })
450
  });
451
 
452
- if (response.ok) {
453
  await fetchTutorialTasks();
454
  setEditingBrief(prev => ({ ...prev, [selectedWeek]: false }));
455
 
456
  } else {
457
- const error = await response.json();
458
 
459
  }
460
  } catch (error) {
@@ -502,28 +466,20 @@ const TutorialTasks: React.FC = () => {
502
  return;
503
  }
504
 
505
- const response = await fetch('/api/auth/admin/tutorial-tasks', {
506
- method: 'POST',
507
- headers: {
508
- 'Authorization': `Bearer ${token}`,
509
- 'Content-Type': 'application/json',
510
- 'user-role': user.role
511
- },
512
- body: JSON.stringify({
513
- title: `Week ${selectedWeek} Tutorial Task`,
514
- content: editForm.content,
515
- sourceLanguage: 'English',
516
- weekNumber: selectedWeek,
517
- category: 'tutorial'
518
- })
519
  });
520
 
521
- if (response.ok) {
522
  await fetchTutorialTasks();
523
  setAddingTask(false);
524
 
525
  } else {
526
- const error = await response.json();
527
 
528
  }
529
  } catch (error) {
@@ -547,19 +503,13 @@ const TutorialTasks: React.FC = () => {
547
  return;
548
  }
549
 
550
- const response = await fetch(`/api/auth/admin/tutorial-tasks/${taskId}`, {
551
- method: 'DELETE',
552
- headers: {
553
- 'Authorization': `Bearer ${token}`,
554
- 'user-role': user.role
555
- }
556
- });
557
 
558
- if (response.ok) {
559
  await fetchTutorialTasks();
560
 
561
  } else {
562
- const error = await response.json();
563
 
564
  }
565
  } catch (error) {
@@ -891,21 +841,7 @@ const TutorialTasks: React.FC = () => {
891
  placeholder="Enter source text..."
892
  />
893
  ) : (
894
- <>
895
- <p className="text-indigo-900 leading-relaxed text-lg font-source-text mb-4">{task.content}</p>
896
- {task.imageUrl && (
897
- <div className="mt-4">
898
- <img
899
- src={task.imageUrl}
900
- alt={task.imageAlt || 'Task image'}
901
- className="w-full max-w-md mx-auto rounded-lg shadow-md"
902
- onError={(e) => {
903
- e.currentTarget.style.display = 'none';
904
- }}
905
- />
906
- </div>
907
- )}
908
- </>
909
  )}
910
  </div>
911
 
 
87
  const fetchUserSubmissions = useCallback(async (tasks: TutorialTask[]) => {
88
  try {
89
  const token = localStorage.getItem('token');
90
+ const user = localStorage.getItem('user');
91
+
92
+ if (!token || !user) {
93
+ return;
94
+ }
95
+
96
+ const response = await api.get('/submissions/my-submissions');
97
 
98
+ if (response.data && response.data.submissions) {
99
+ const data = response.data;
100
 
101
  const groupedSubmissions: {[key: string]: UserSubmission[]} = {};
102
 
 
106
  });
107
 
108
  // Then populate with actual submissions
109
+ if (data.submissions && Array.isArray(data.submissions)) {
110
+ data.submissions.forEach((submission: any) => {
111
+ // Extract the actual ID from sourceTextId (could be string or object)
112
+ const sourceTextId = submission.sourceTextId?._id || submission.sourceTextId;
113
+
114
+ if (sourceTextId && groupedSubmissions[sourceTextId]) {
115
+ groupedSubmissions[sourceTextId].push(submission);
116
+ }
117
+ });
118
+ }
119
 
120
  setUserSubmissions(groupedSubmissions);
121
  }
 
128
  try {
129
  setLoading(true);
130
  const token = localStorage.getItem('token');
131
+ const user = localStorage.getItem('user');
132
+
133
+ if (!token || !user) {
134
+ navigate('/login');
135
+ return;
136
+ }
137
+
138
+ const response = await api.get(`/search/tutorial-tasks/${selectedWeek}`);
139
 
140
+ if (response.data) {
141
+ const tasks = response.data;
142
  setTutorialTasks(tasks);
143
 
144
  // Organize tasks into week structure
 
163
  } finally {
164
  setLoading(false);
165
  }
166
+ }, [selectedWeek, fetchUserSubmissions, navigate]);
167
 
168
  useEffect(() => {
169
  const user = localStorage.getItem('user');
 
195
 
196
  try {
197
  setSubmitting({ ...submitting, [taskId]: true });
 
198
  const user = JSON.parse(localStorage.getItem('user') || '{}');
199
+ const response = await api.post('/submissions', {
200
+ sourceTextId: taskId,
201
+ transcreation: translationText[taskId],
202
+ groupNumber: selectedGroups[taskId],
203
+ culturalAdaptations: [],
204
+ username: user.name || 'Unknown'
 
 
 
 
 
 
 
205
  });
206
 
207
+ if (response.status >= 200 && response.status < 300) {
208
+ const result = response.data;
209
  console.log('Submission created successfully:', result);
210
 
211
  setTranslationText({ ...translationText, [taskId]: '' });
212
  setSelectedGroups({ ...selectedGroups, [taskId]: 0 });
213
  await fetchUserSubmissions(tutorialTasks);
214
  } else {
215
+ console.error('Failed to submit translation:', response.data);
 
 
216
  }
217
  } catch (error) {
218
  console.error('Error submitting translation:', error);
 
234
  if (!editingSubmission || !editSubmissionText.trim()) return;
235
 
236
  try {
237
+ const response = await api.put(`/submissions/${editingSubmission.id}`, {
238
+ transcreation: editSubmissionText
 
 
 
 
 
 
 
 
239
  });
240
 
241
+ if (response.status >= 200 && response.status < 300) {
242
 
243
  setEditingSubmission(null);
244
  setEditSubmissionText('');
245
  await fetchUserSubmissions(tutorialTasks);
246
  } else {
247
+ console.error('Failed to update translation:', response.data);
 
248
  }
249
  } catch (error) {
250
  console.error('Error updating translation:', error);
 
330
  return;
331
  }
332
 
333
+ const response = await api.put(`/auth/admin/tutorial-brief/${selectedWeek}`, {
334
+ translationBrief: '',
335
+ weekNumber: selectedWeek
 
 
 
 
 
 
 
 
336
  });
337
 
338
+ if (response.status >= 200 && response.status < 300) {
339
  await fetchTutorialTasks();
340
 
341
  } else {
342
+ console.error('Failed to remove translation brief:', response.data);
343
 
344
  }
345
  } catch (error) {
 
375
  return;
376
  }
377
 
378
+ const response = await api.put(`/auth/admin/tutorial-tasks/${editingTask}`, {
379
+ ...editForm,
380
+ weekNumber: selectedWeek
 
 
 
 
 
 
 
 
381
  });
382
 
383
+ if (response.status >= 200 && response.status < 300) {
384
  await fetchTutorialTasks();
385
  setEditingTask(null);
386
 
387
  } else {
388
+ console.error('Failed to update tutorial task:', response.data);
389
 
390
  }
391
  } catch (error) {
 
408
  return;
409
  }
410
 
411
+ const response = await api.put(`/auth/admin/tutorial-brief/${selectedWeek}`, {
412
+ translationBrief: editForm.translationBrief,
413
+ weekNumber: selectedWeek
 
 
 
 
 
 
 
 
414
  });
415
 
416
+ if (response.status >= 200 && response.status < 300) {
417
  await fetchTutorialTasks();
418
  setEditingBrief(prev => ({ ...prev, [selectedWeek]: false }));
419
 
420
  } else {
421
+ console.error('Failed to update translation brief:', response.data);
422
 
423
  }
424
  } catch (error) {
 
466
  return;
467
  }
468
 
469
+ const response = await api.post('/auth/admin/tutorial-tasks', {
470
+ title: `Week ${selectedWeek} Tutorial Task`,
471
+ content: editForm.content,
472
+ sourceLanguage: 'English',
473
+ weekNumber: selectedWeek,
474
+ category: 'tutorial'
 
 
 
 
 
 
 
 
475
  });
476
 
477
+ if (response.status >= 200 && response.status < 300) {
478
  await fetchTutorialTasks();
479
  setAddingTask(false);
480
 
481
  } else {
482
+ console.error('Failed to add tutorial task:', response.data);
483
 
484
  }
485
  } catch (error) {
 
503
  return;
504
  }
505
 
506
+ const response = await api.delete(`/auth/admin/tutorial-tasks/${taskId}`);
 
 
 
 
 
 
507
 
508
+ if (response.status >= 200 && response.status < 300) {
509
  await fetchTutorialTasks();
510
 
511
  } else {
512
+ console.error('Failed to delete tutorial task:', response.data);
513
 
514
  }
515
  } catch (error) {
 
841
  placeholder="Enter source text..."
842
  />
843
  ) : (
844
+ <p className="text-indigo-900 leading-relaxed text-lg font-source-text">{task.content}</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
845
  )}
846
  </div>
847