AECC database project.
at master 96 lines 2.4 kB view raw
1function activityCreatorFormHandler(event) { 2 event.preventDefault(); 3 try { 4 const payload = JSON.stringify({ 5 t: "activity", 6 title: event.target[1].value, 7 description: event.target[2].value, 8 date: event.target[3].value 9 }); 10 if (confirm("Are you sure you want to create a new activity?")) { 11 fetch(URL + "/api/v1/create/", { 12 method: "POST", 13 headers: { 14 "Content-Type": "application/json" 15 }, 16 body: payload 17 }).then(resp => { 18 if (resp.status != 201) { 19 const msg = `Error creating activity, status: ${resp.status}`; 20 alert(msg); 21 throw msg; 22 } 23 return resp.json(); 24 }).then(json => { 25 alert(`Succesfully created activity with ID: ${json.a_id}`); 26 restart(); 27 }); 28 } 29 } catch (error) { 30 console.error(error.message); 31 } 32} 33 34function editActivity(obj) { 35 activityEditorID.value = obj[0]; 36 activityEditorTitle.value = obj[1]; 37 activityEditorDescription.value = obj[2]; 38 activityEditorDate.value = obj[3]; 39 next('activityEditor'); 40} 41 42function activityEditorFormHandler(event) { 43 event.preventDefault(); 44 try { 45 if (confirm("Do you want to edit this product?")) { 46 payload = JSON.stringify({ 47 t: "activity", 48 a_id: event.target[5].value, 49 title: event.target[1].value, 50 description: event.target[2].value, 51 date: event.target[3].value 52 }) 53 fetch(URL + "/api/v1/update/", { 54 method: "POST", 55 headers: { 56 "Content-Type": "application/json", 57 }, 58 body: payload 59 }).then(resp => { 60 if (resp.status != 201) { 61 const msg = `Error updating activity, status: ${resp.status}`; 62 alert(msg); 63 throw msg; 64 } 65 return resp.json(); 66 }).then(json => { 67 alert(`Succesfully updated activity with ID: ${json.a_id}`); 68 restart(); 69 }); 70 } 71 } catch (e) { 72 console.error(e.message); 73 } 74} 75 76function deleteActivity(obj) { 77 const a_id = obj[0]; 78 if (confirm("Are you sure?")) { 79 fetch(URL + "/api/v1/delete/", { 80 method: "POST", 81 headers: { 82 "Content-Type": "application/json" 83 }, 84 body: JSON.stringify({t: "activity", a_id: a_id}) 85 }).then(resp => { 86 if (resp.status != 204) { 87 const msg = `Error deleting activity, status: ${resp.status}`; 88 alert(msg); 89 throw msg; 90 } else { 91 alert(`Succesfully deleted activity with ID: ${a_id}`); 92 initTable(allActivitiesTable, 'activity', 'editActivity', 'deleteActivity'); 93 } 94 }); 95 } 96}