EHDEM commited on
Commit
d7a5ccb
·
verified ·
1 Parent(s): 88573d4

fix the submit buttons of both attendance forms a and b so i can send data to github

Browse files
Files changed (2) hide show
  1. attendance.html +23 -10
  2. students-ate.html +23 -10
attendance.html CHANGED
@@ -388,24 +388,35 @@
388
  this.innerHTML = '<i class="fas fa-paper-plane mr-2"></i> Submit Attendance';
389
  }
390
  });
391
-
392
  async function sendDataToGitHub(data) {
393
  try {
 
394
  let currentContent = '';
395
  let sha = '';
396
- const getResponse = await fetch(`https://api.github.com/repos/${GITHUB_CONFIG.REPOS.ATTENDANCE}/contents/Data%20Students%20who%20want%20to%20eat`, {
397
- headers: { 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`, 'Accept': 'application/vnd.github.v3+json' }
 
 
 
 
 
 
398
  });
 
399
  if (getResponse.ok) {
400
  const fileData = await getResponse.json();
401
  sha = fileData.sha;
402
  currentContent = atob(fileData.content);
 
 
403
  }
404
 
 
405
  const record = JSON.stringify(data);
406
  const newContent = currentContent + record + '\n';
407
 
408
- const response = await fetch(`https://api.github.com/repos/${GITHUB_CONFIG.REPOS.ATTENDANCE}/contents/Data%20Students%20who%20want%20to%20eat`, {
 
409
  method: 'PUT',
410
  headers: {
411
  'Authorization': `token ${GITHUB_CONFIG.TOKEN}`,
@@ -413,22 +424,24 @@
413
  'Accept': 'application/vnd.github.v3+json'
414
  },
415
  body: JSON.stringify({
416
- message: `Attendance record - ${new Date().toISOString()}`,
417
  content: btoa(newContent),
418
  sha: sha || undefined,
419
  branch: 'main'
420
  })
421
  });
422
 
423
- if (!response.ok) throw new Error('Submission failed');
 
 
 
424
  return true;
425
  } catch (error) {
426
- console.error(error);
427
- throw error;
428
  }
429
  }
430
-
431
- function resetForm() {
432
  document.getElementById('class').value = '';
433
  document.getElementById('form').value = '';
434
  document.getElementById('student').innerHTML = '<option value="">Select Student</option>';
 
388
  this.innerHTML = '<i class="fas fa-paper-plane mr-2"></i> Submit Attendance';
389
  }
390
  });
 
391
  async function sendDataToGitHub(data) {
392
  try {
393
+ // First check if file exists and get current content
394
  let currentContent = '';
395
  let sha = '';
396
+ const filePath = 'Data%20Students%20who%20want%20to%20eat';
397
+ const repoUrl = `https://api.github.com/repos/${GITHUB_CONFIG.REPOS.ATTENDANCE}/contents/${filePath}`;
398
+
399
+ const getResponse = await fetch(repoUrl, {
400
+ headers: {
401
+ 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`,
402
+ 'Accept': 'application/vnd.github.v3+json'
403
+ }
404
  });
405
+
406
  if (getResponse.ok) {
407
  const fileData = await getResponse.json();
408
  sha = fileData.sha;
409
  currentContent = atob(fileData.content);
410
+ } else if (getResponse.status !== 404) {
411
+ throw new Error('Failed to fetch existing data');
412
  }
413
 
414
+ // Prepare new content
415
  const record = JSON.stringify(data);
416
  const newContent = currentContent + record + '\n';
417
 
418
+ // Create or update file
419
+ const response = await fetch(repoUrl, {
420
  method: 'PUT',
421
  headers: {
422
  'Authorization': `token ${GITHUB_CONFIG.TOKEN}`,
 
424
  'Accept': 'application/vnd.github.v3+json'
425
  },
426
  body: JSON.stringify({
427
+ message: `Attendance record - ${new Date().toLocaleString()}`,
428
  content: btoa(newContent),
429
  sha: sha || undefined,
430
  branch: 'main'
431
  })
432
  });
433
 
434
+ if (!response.ok) {
435
+ const errorData = await response.json();
436
+ throw new Error(errorData.message || 'Submission failed');
437
+ }
438
  return true;
439
  } catch (error) {
440
+ console.error('Submission error:', error);
441
+ throw new Error('Failed to submit attendance: ' + error.message);
442
  }
443
  }
444
+ function resetForm() {
 
445
  document.getElementById('class').value = '';
446
  document.getElementById('form').value = '';
447
  document.getElementById('student').innerHTML = '<option value="">Select Student</option>';
students-ate.html CHANGED
@@ -356,24 +356,35 @@
356
  this.innerHTML = '<i class="fas fa-check-circle mr-2"></i> Confirm Meal Taken';
357
  }
358
  });
359
-
360
  async function sendDataToGitHub(data) {
361
  try {
 
362
  let currentContent = '';
363
  let sha = '';
364
- const getResponse = await fetch(`https://api.github.com/repos/${GITHUB_CONFIG.REPOS.CONSUMED}/contents/data%20who%20has%20ate%20at%20dm`, {
365
- headers: { 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`, 'Accept': 'application/vnd.github.v3+json' }
 
 
 
 
 
 
366
  });
 
367
  if (getResponse.ok) {
368
  const fileData = await getResponse.json();
369
  sha = fileData.sha;
370
  currentContent = atob(fileData.content);
 
 
371
  }
372
 
 
373
  const record = JSON.stringify(data);
374
  const newContent = currentContent + record + '\n';
375
 
376
- const response = await fetch(`https://api.github.com/repos/${GITHUB_CONFIG.REPOS.CONSUMED}/contents/data%20who%20has%20ate%20at%20dm`, {
 
377
  method: 'PUT',
378
  headers: {
379
  'Authorization': `token ${GITHUB_CONFIG.TOKEN}`,
@@ -381,22 +392,24 @@
381
  'Accept': 'application/vnd.github.v3+json'
382
  },
383
  body: JSON.stringify({
384
- message: `New attendance record - ${new Date().toISOString()}`,
385
  content: btoa(newContent),
386
  sha: sha || undefined,
387
  branch: 'main'
388
  })
389
  });
390
 
391
- if (!response.ok) throw new Error('Submission failed');
 
 
 
392
  return true;
393
  } catch (error) {
394
- console.error(error);
395
- throw error;
396
  }
397
  }
398
-
399
- function resetForm() {
400
  document.getElementById('class').value = '';
401
  document.getElementById('form').value = '';
402
  document.getElementById('student').value = '';
 
356
  this.innerHTML = '<i class="fas fa-check-circle mr-2"></i> Confirm Meal Taken';
357
  }
358
  });
 
359
  async function sendDataToGitHub(data) {
360
  try {
361
+ // First check if file exists and get current content
362
  let currentContent = '';
363
  let sha = '';
364
+ const filePath = 'data%20who%20has%20ate%20at%20dm';
365
+ const repoUrl = `https://api.github.com/repos/${GITHUB_CONFIG.REPOS.CONSUMED}/contents/${filePath}`;
366
+
367
+ const getResponse = await fetch(repoUrl, {
368
+ headers: {
369
+ 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`,
370
+ 'Accept': 'application/vnd.github.v3+json'
371
+ }
372
  });
373
+
374
  if (getResponse.ok) {
375
  const fileData = await getResponse.json();
376
  sha = fileData.sha;
377
  currentContent = atob(fileData.content);
378
+ } else if (getResponse.status !== 404) {
379
+ throw new Error('Failed to fetch existing data');
380
  }
381
 
382
+ // Prepare new content
383
  const record = JSON.stringify(data);
384
  const newContent = currentContent + record + '\n';
385
 
386
+ // Create or update file
387
+ const response = await fetch(repoUrl, {
388
  method: 'PUT',
389
  headers: {
390
  'Authorization': `token ${GITHUB_CONFIG.TOKEN}`,
 
392
  'Accept': 'application/vnd.github.v3+json'
393
  },
394
  body: JSON.stringify({
395
+ message: `Meal confirmation - ${new Date().toLocaleString()}`,
396
  content: btoa(newContent),
397
  sha: sha || undefined,
398
  branch: 'main'
399
  })
400
  });
401
 
402
+ if (!response.ok) {
403
+ const errorData = await response.json();
404
+ throw new Error(errorData.message || 'Submission failed');
405
+ }
406
  return true;
407
  } catch (error) {
408
+ console.error('Submission error:', error);
409
+ throw new Error('Failed to confirm meal: ' + error.message);
410
  }
411
  }
412
+ function resetForm() {
 
413
  document.getElementById('class').value = '';
414
  document.getElementById('form').value = '';
415
  document.getElementById('student').value = '';