• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to prevent validation based on latest action


New Coder
Hi everyone,
I need some help. I am by no means a programmer
, but following tutorials I am trying to put together a form with WebApp for check out/in and reserving tools.

The trouble I have is that I would like to prevent tools that have been already checked out from being check out again. Similarly, I want to avoid having tools reserved when they have been already checked out.

Now, I’ve managed to block double check-outs, but I can’t prevent the reservations and I need help with that.

<!DOCTYPE html>
<html lang="en">
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Geräte Management System</title>
    <base target="_top">   
    <!-- Metro UI -->
    <link rel="stylesheet" href="https://cdn.korzh.com/metroui/v4/css/metro-all.min.css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.css" rel="stylesheet">



 <!-- HEAD HTML -->

<!-- BODY HTML -->
  <div class="container">
    <h1 class="brand">Geräte & Autos Management System</h1>           
    <div class="wrapper animated bounceInLeft">
      <div class="company-info">
        <h3>AAB Archäologie</h3>       
          <li><span class='mif-home fg-darkTeal'></span> company address</li>
          <li> <span class='mif-phone fg-darkTeal'></span>company email address</li>
          <li><span class='mif-envelop fg-darkTeal'></span>company phone</li>
      <div class="main">
        <h3>Geräte aus- oder einchecken</h3>
        <form id="main-form">       
          <div class="form-group">
            <input type="text"  placeholder="Nachname" id="lname" data-prepend="<span class='mif-user fg-darkTeal'></span>"><br>
          <div class="form-group">
            <label>Persönlicher Code</label>
             <input type="password"  data-role="big-input" placeholder="Code für den eingefügten Nachnamen" id="persid" data-prepend="<span class='mif-key fg-darkTeal'></span>"><br>

          <div class="form-group">
            <input type="text" data-role="big-input" placeholder="Gerätename (wie auf dem Etikett)" id="gerid" data-prepend="<span class='mif-wrench fg-darkTeal'></span>"><br>

          <div class="form-group">         
            <textarea data-role="big-textarea" placeholder="relevante Kommentare über die Geräte" id="kommentar"data-prepend="<span class='mif-paragraph-left fg-darkTeal'></span>" rows="7"></textarea><br>

          <div class="form-group">
            <button id="checkOut" type="submit" data-action="Check OUT">Check OUT</button>
            <button id="checkIn" type="submit" data-action="Check IN">Check IN</button>
            <button id="reserviert" type="submit" data-action="Reserviert">Reserve</button>               
          <div id="message" class="d-none">


<!-- SCRIPT .JS -->
    <!-- Metro UI -->
    <script src="https://cdn.korzh.com/metroui/v4/js/metro.min.js"></script>

      const CheckInOutApp = {}
       CheckInOutApp.onLoad = function () {
        CheckInOutApp.form = document.getElementById("main-form")
        CheckInOutApp.lnameInput = document.getElementById("lname")
        CheckInOutApp.persidInput = document.getElementById("persid")
        CheckInOutApp.geridInput = document.getElementById("gerid")
        CheckInOutApp.kommentarInput = document.getElementById("kommentar")
        CheckInOutApp.checkOutButton = document.getElementById("checkOut")
        CheckInOutApp.checkInButton = document.getElementById("checkIn")
        CheckInOutApp.reserviertButton = document.getElementById("reserviert")
        CheckInOutApp.message = document.getElementById("message")

        CheckInOutApp.form.addEventListener ("submit",CheckInOutApp.onSubmit)

      } // CheckInOutApp.onLoad function

      CheckInOutApp.onSubmit = function(e){
        console.log("Form Submitted")
      } //CheckInOutApp.onSubmit function

      CheckInOutApp.checkInOut = function(e){
        const management = {
                           lname: CheckInOutApp.lnameInput.value,
                           persid: CheckInOutApp.persidInput.value,
                           gerid: CheckInOutApp.geridInput.value,
                           kommentar: CheckInOutApp.kommentarInput.value,
                           action: e.target.dataset.action

        google.script.run.withSuccessHandler(() => {
          lname: CheckInOutApp.lnameInput.value = ""
          persid: CheckInOutApp.persidInput.value = ""
          gerid: CheckInOutApp.geridInput.value = ""
          kommentar: CheckInOutApp.kommentarInput.value = ""
        }).withFailureHandler(() => {
          setTimeout(() => {CheckInOutApp.message.classList.add("d-none") },3000)

      } // CheckInOutApp.checkInOut function



function doGet() {
return HtmlService.createHtmlOutputFromFile ("Form")

//function acceptData(formData){

function checkInOut(management){
  // const management = {
   //                  lname: "Savu",
   //                  persid: "5584",
   //                  gerid: "Ray",
   //                  action: "Check OUT"
  //                   }

  if(!["Check OUT","Check IN"].includes(management.action)){
    throw new Error ("Check IN or Check OUT Failed")

  console.log("Initial check passed")

  const ss = SpreadsheetApp.getActiveSpreadsheet ()
  const wsData = ss.getSheetByName("Data")
  const wsPersonal = ss.getSheetByName("Personal")
  const personalData = wsPersonal.getRange(2,1,wsPersonal.getLastRow() -1,2).getValues()

  const matchingPersonal = personalData.filter(r=> r[0].toString() === management.persid && r[1] === management.lname)

  const wsInventar = ss.getSheetByName("Inventar")
  const inventarName = wsInventar.getRange(2,1,wsInventar.getLastRow() -1,1).getValues()

  const matchingInventar = inventarName.filter (r=> r[0] === management.gerid)

  if (matchingPersonal.length !== 1){
    throw new Error ("Check IN or Check OUT Failed")
  if (matchingInventar.length !== 1){
    throw new Error ("Check IN or Check OUT Failed")

  const idsData = wsData.getRange(2,4,wsData.getLastRow() -1,2).getValues()
  const matchingIdsData = idsData.filter(r => r[0].toString() === management.gerid)


  const latestAction = matchingIdsData.length === 0 ? "Check IN" : matchingIdsData[matchingIdsData.length -1][1]


   if(latestAction === management.action){
    throw new Error ("Check IN or Check OUT Failed")

   wsData.appendRow([new Date(),management.lname,management.persid,management.gerid,management.action])

Extra: I only have a general error message for when something is not correctly inserted, but I would like them to be more concise depending on the action which is wrong (last name not valid, personal code not valid etc.). This is however not so urgent. I appreciate any help.
Is this is a long-term solution? Because what I mean is that I have items checked-out and appear so in the inventory list and they can be out for weeks. In the meantime I don't want anyone to be able to reserve them until they are checked back in.
So the form is connected to a google sheet in AppScript where the responses are collected like in the image bellow. Actually in a different sheet I have all the info like time something was checked out or reserved etc and by whom like in the second image. What I can't prevent is for an item that is marked as out to be reserved. I am not sure if I make sense.


Also the bits of code here


  if(!["Check OUT","Check IN"].includes(management.action)){
    throw new Error ("Check IN or Check OUT Failed")

 const latestAction = matchingIdsData.length === 0 ? "Check IN" : matchingIdsData[matchingIdsData.length -1][1]


   if(latestAction === management.action){
    throw new Error ("Check IN or Check OUT Failed")

is what I used to prevent items which are out from being checked-out again until they are checked back in and this works perfectly. What I can't manage is to do the same for reserved items.
Top Bottom