function sendTransaction(isAdding) {
  let nameEl = document.querySelector("#t-name");
  let amountEl = document.querySelector("#t-amount");
  let errorEl = document.querySelector(".form .error");

  // validate form
  if (nameEl.value === "" || amountEl.value === "") {
    errorEl.textContent = "Missing Information";
    return;
  }
  else {
    errorEl.textContent = "";
  }

  // create record
  let transaction = {
    name: nameEl.value,
    value: amountEl.value,
    date: new Date().toISOString()
  };

  // if subtracting funds, convert amount to negative number
  if (!isAdding) {
    transaction.value *= -1;
  }

  // add to beginning of current array of data
  transactions.unshift(transaction);

  // re-run logic to populate ui with new record
  populateChart();
  populateTable();
  populateTotal();
  
  // also send to server
  fetch("/api/transaction", {
    method: "POST",
    body: JSON.stringify(transaction),
    headers: {
      Accept: "application/json, text/plain, */*",
      "Content-Type": "application/json"
    }
  })
  .then(response => {    
    return response.json();
  })
  .then(data => {
    if (data.errors) {
      errorEl.textContent = "Missing Information";
    }
    else {
      // clear form
      nameEl.value = "";
      amountEl.value = "";
    }
  })
  .catch(err => {
    // fetch failed, so save in indexed db
    saveRecord(transaction);

    // clear form
    nameEl.value = "";
    amountEl.value = "";
  });
}
// The transaction record is sent to the server
The transaction record is sent to the server
The transaction record is sent to the server
function sendTransaction(isAdding) {
  let nameEl = document.querySelector("#t-name");
  let amountEl = document.querySelector("#t-amount");
  let errorEl = document.querySelector(".form .error");
  if (nameEl.value === "" || amountEl.value === "") {
    errorEl.textContent = "Missing Information";
    return;
  }
  else {
    errorEl.textContent = "";
  }
  let transaction = {
    name: nameEl.value,
    value: amountEl.value,
    date: new Date().toISOString()
  };
  if (!isAdding) {
    transaction.value *= -1;
  }
  transactions.unshift(transaction);
  populateChart();
  populateTable();
  populateTotal();
  fetch("/api/transaction", {
    method: "POST",
    body: JSON.stringify(transaction),
    headers: {
      Accept: "application/json, text/plain, */*",
      "Content-Type": "application/json"
    }
  })
  .then(response => {    
    return response.json();
  })
  .then(data => {
    if (data.errors) {
      errorEl.textContent = "Missing Information";
    }
    else {
      nameEl.value = "";
      amountEl.value = "";
    }
  })
  .catch(err => {
    saveRecord(transaction);
    nameEl.value = "";
    amountEl.value = "";
  });
}
function sendTransaction ( isAdding ) { let nameEl = document . querySelector ( "#t-name" ) ; let amountEl = document . querySelector ( "#t-amount" ) ; let errorEl = document . querySelector ( ".form .error" ) ; if ( nameEl . value === "" || amountEl . value === "" ) { errorEl . textContent = "Missing Information" ; return ; } else { errorEl . textContent = "" ; } let transaction = { name : nameEl . value , value : amountEl . value , date : new Date ( ) . toISOString ( ) } ; if ( ! isAdding ) { transaction . value *= - 1 ; } transactions . unshift ( transaction ) ; populateChart ( ) ; populateTable ( ) ; populateTotal ( ) ; fetch ( "/api/transaction" , { method : "POST" , body : JSON . stringify ( transaction ) , headers : { Accept : "application/json, text/plain, */*" , "Content-Type" : "application/json" } } ) . then ( response => { return response . json ( ) ; } ) . then ( data => { if ( data . errors ) { errorEl . textContent = "Missing Information" ; } else { nameEl . value = "" ; amountEl . value = "" ; } } ) . catch ( err => { saveRecord ( transaction ) ; nameEl . value = "" ; amountEl . value = "" ; } ) ; }
The transaction record is sent to the server
The transaction record is sent to the server
// validate form // create record // if subtracting funds, convert amount to negative number // add to beginning of current array of data // re-run logic to populate ui with new record // also send to server // clear form // fetch failed, so save in indexed db // clear form