function editProduct(obj) { productEditorID.value = obj[0]; productEditorDollars.value = (obj[1] / 100.0).toFixed(2); productEditorDescription.value = obj[2]; next('productEditor'); } function productCreatorFormHandler(event) { event.preventDefault(); const dollars = event.srcElement[1].value; const description = event.srcElement[2].value; const cents = dollars * 100; if (confirm('Do you want to create a product called: "' + description + '" costing $' + dollars + " (ยข" + cents + " cents)?")) { try { fetch(URL + "/api/v1/create/", { method: "POST", body: JSON.stringify({ t: "product", cents: cents, description: description }) }).then(resp => { alert("Succesfully created product"); restart(); }); } catch (error) { alert("Error inserting"); console.error(error.message); } } } function editProductHandler(event) { event.preventDefault(); const p_id = event.target[4].value; const cents = event.target[1].value * 100; const description = event.target[2].value; try { if (confirm(`Are you sure you want to edit product with ID: ${p_id}`)) { const payload = JSON.stringify({ t: "product", p_id: p_id, cents: cents, description: description }) fetch(URL + "/api/v1/update/", { method: "POST", headers: { "Content-Type": "application/json" }, body: payload }).then(resp => { if (resp.status != 201) { const msg = `Error updating product, status: ${resp.status}`; alert(msg); throw msg; } return resp.json(); }).then(json => { alert(`Succesfully updated product with ID: ${json.p_id}`); restart(); }); } } catch (e) { console.error(e.message); } } function deleteProduct(obj) { const p_id = obj[0]; try { const payload = JSON.stringify({ t: "product", p_id: p_id }); if (confirm(`Are you sure you want to delete the product with ID: ${p_id}`)) { fetch(URL + "/api/v1/delete/", { method: "POST", headers: { "Content-Type": "application/json" }, body: payload }).then(resp => { if (resp.status != 204) { const msg = `Error deleting product, status: ${resp.status}`; alert(msg); throw msg; } else { alert(`Succesfully deleted product with ID: ${p_id}`); initTable(allProductsTable, 'product', 'editProduct', 'deleteProduct'); } }); } } catch(e) { console.error(e); } }