Pages

Javascript Call API can Fetch Data - Get Method

  // disable QR search button on empty and page load
    const btnSerialS = document.getElementById('btnSerialS');

    btnSerialS.disabled = true;

    function validateEmpty(){
        const serialNumberD = document.getElementById("serialNum").value;

        if (serialNumberD.length > 5) {
            btnSerialS.disabled = false;
        }
        else {
            btnSerialS.disabled = true;
        }
    }

  ///

function getWdata() {
    const hostName = window.location.host;
    const serialNumber = document.getElementById("serialNum").value;
    const url = `https://${hostName}/Api/get-check-warranty-activetion-by-serial-number?serialNumber=${serialNumber}`;

    fetch(url)
        .then(response => {
            if (!response.ok) {
                throw new Error(`No Information available. Please re-check the Serial Number again.`);
            }
            return response.json();
        })
        .then(data => {
            if (!data.data || data.data.length === 0) {
                alert("No data available, Please check the serial number");
                // Reset the displayed values
                resetDisplayedValues(); 
            } else {
                const firstData = data.data[0];
                document.getElementById("serialNumber").textContent = firstData.serialNumber;
                document.getElementById("productName").textContent = firstData.productName;
                document.getElementById("productCode").textContent = firstData.productCode;
                document.getElementById("consumerName").textContent = firstData.consumerName;
                document.getElementById("consumerAddress").textContent = firstData.consumerAddress;
                document.getElementById("consumerContactNo").textContent = firstData.consumerNumber;
                document.getElementById("warrantyActivatedDate").textContent = firstData.warrantyActivatedDate;
                document.getElementById("warrantyExpiryDate").textContent = firstData.warrantyExpiryDate;
                document.getElementById("status").textContent = firstData.warrantyStatus;
                
                var myModal = new bootstrap.Modal(document.getElementById('warrantyModal'), {
                    keyboard: false
                });
                myModal.show();
            }
        })
        .catch(error => {
            alert(error.message);
            console.error(error);
            // Reset the displayed values
            resetDisplayedValues();
        });
}

function resetDisplayedValues() {
    document.getElementById("serialNumber").textContent = "";
    document.getElementById("productName").textContent = "";
    document.getElementById("productCode").textContent = "";
    document.getElementById("consumerName").textContent = "";
    document.getElementById("consumerAddress").textContent = "";
    document.getElementById("consumerContactNo").textContent = "";
    document.getElementById("warrantyActivatedDate").textContent = "";
    document.getElementById("warrantyExpiryDate").textContent = "";
    document.getElementById("status").textContent = "";
}


    function getNumdata() {
        //const hostName = window.location.hostname;
        const hostName = window.location.host;
        const phoneNum = document.getElementById("phoneNum").value;
        const url = `https://${hostName}/Api/get-consumer-active-warranty-serial-number-by-phone-number?phoneNumber=${phoneNum}`;

        //alert(phoneNum);
        //alert(url);

        fetch(url)
            .then(response => {
                if (!response.ok) {
                    //throw new Error(`HTTP error! status: ${response.status}`);
                    throw new Error(`No Information available. Please re-check the Serial Number again.`);
                }
                return response.json();
            })
            .then(data => {
                if (!data) {
                    document.getElementById("errorText").innerHTML = "<p>Please recheck the serial number.</p>"
                    alert("No data available");
                }
                else {

                   //alert(JSON.stringify(data.data));

                    const deviceList = document.getElementById("deviceList");
                    deviceList.innerHTML = "";
                    
                    const headertxtbym = document.getElementById("headertxtbym");
                    headertxtbym.innerHTML = ' <li class="list-group-item active" aria-current="true">Registered Devices</li>';


                    data.data.forEach(serialNumber => {
                        
                        const listitem = document.createElement("li");
                        listitem.classList.add("list-group-item");
                        listitem.setAttribute("data-seno", serialNumber);
                        

    const link = document.createElement("a");
    link.href = "#site";
    link.classList.add("snoClick");
    link.textContent = serialNumber; 
    link.setAttribute("data-seno", serialNumber);

    listitem.appendChild(link);
    deviceList.appendChild(listitem);
                    });

                    ////////////////////////////////////////////////////////////////////////
                        //// pass SN from link auto to serial number form
                        var listItems = document.querySelectorAll('.snoClick');

    // Add a click event listener to each list item
    listItems.forEach(function(item) {
      item.addEventListener('click', function(e) {
        e.preventDefault();
        // Get the value of the "data-seno" attribute
        var dataSeno = item.getAttribute('data-seno');

        const dataSenoValue = this.getAttribute("data-seno");

        const serialNumTextbox = document.getElementById("serialNum");
    serialNumTextbox.value = dataSenoValue;

    btnSerialS.disabled = false;

        if (btnSerialS) {
      btnSerialS.click(); // Trigger click event on btnSerialS if it exists
    }



      });
    });


                }

            })
            .catch(error => {
                alert(error.message);
                console.error(error);
            });
    }



  // JavaScript code

No comments:

Post a Comment

Search This Blog