How can I save additional data without saving redundant data in a web app using SQL Server?

The problem is when you press the Save/Edit button and add a problem and enter the required information. And the time was lost. When I pressed save, I found that it recorded duplicate data in the section that already had a breakdownId (the data with the breakdownId will be data that was pulled from another database). The newly added data will not have a breakdownId. It will be NULL to use the breakdownId data to pull data from another database here. And there is a function to add data yourself and fix the problem as follows, such as

ID | breakdownId | Date | MachineCode | DocNo | Cause | bdst | bden | UpdatedAt | Downtime | notes | lastupdate | SourceDB

182 | 10458939 | 2024-10-02 | COM007-1 | NULL | Waiting for crane | 2024-10-02 11:19:00.000 | 2024-10-02 12:11:00.000 | NULL | 52 | Waiting for crane from TWO ROLLER | 2024-10-02 12:11:31.153 | mswplus

When I pressed the save button on the web app, even though nothing had been edited. It added this data itself

ID breakdownId Date MachineCode DocNo Cause bdst bden UpdatedAt Downtime notes lastupdate SourceDB

ID | breakdownId | Date | MachineCode | DocNo | Cause | bdst | bden | UpdatedAt | Downtime | notes | lastupdate | SourceDB

189 | NULL | 2024-10-02 | COM007 | NULL | Waiting for crane | NULL | NULL | 2024-10-02 15:24:06.497 | 52 | NULL | NULL |  NULL

Which is like a duplicate record. How can I fix it so that it does not duplicate record data with breakdownId?

And here is the code in this web app.


function editProblem(machineCode, date, docNo) {
    const row = document.querySelector(`#barChartTable tr[data-machine="${machineCode}"][data-date="${date}"]`);
    if (row) {
        const problemDetailsCell = row.querySelector('.problem-details');
        let currentProblems = Array.from(problemDetailsCell.querySelectorAll('div')).map(div => {
            const match = div.textContent.match(/(.*) ((d+) นาที)/);
            return {
                description: match ? match[1].trim() : div.textContent,
                downtime: match ? parseInt(match[2]) : 0,
                breakdownId: div.dataset.breakdownid || null,
                id: div.dataset.id || null,
                originalDowntime: match ? parseInt(match[2]) : 0
            };
        });

        // เพิ่มส่วนนี้เพื่อกำหนด isExisting และ isNew
        currentProblems = currentProblems.map(problem => {
            if (problem.breakdownId) {
                return { ...problem, isExisting: true };
            } else if (problem.id) {
                return { ...problem, isExisting: true };
            } else {
                return { ...problem, isNew: true };
            }
        });

        let formHTML = '<form id="editProblemForm">';
        currentProblems.forEach((problem, index) => {
            formHTML += `
                <div>
                    <input type="text" name="description[]" value="${problem.description}" readonly>
                    <input type="number" name="downtime[]" value="${problem.downtime}" min="0" ${problem.breakdownId ? '' : 'readonly'}> นาที
                    <input type="hidden" name="breakdownId[]" value="${problem.breakdownId || ''}">
                    <input type="hidden" name="id[]" value="${problem.id || ''}">
                    <input type="hidden" name="originalDowntime[]" value="${problem.originalDowntime}">
                    <input type="hidden" name="isExisting[]" value="${problem.isExisting ? 'true' : 'false'}">
                    <input type="hidden" name="isNew[]" value="${problem.isNew ? 'true' : 'false'}">
                </div>
            `;
        });
        formHTML += '<button type="button" onclick="addNewProblemField()">เพิ่มปัญหา</button>';
        formHTML += '<button type="submit">บันทึก</button></form>';

        showEditModal(formHTML);

        document.getElementById('editProblemForm').onsubmit = function(e) {
            e.preventDefault();
            const formData = new FormData(e.target);
            const problems = [];
            const descriptions = formData.getAll('description[]');
            const downtimes = formData.getAll('downtime[]');
            const breakdownIds = formData.getAll('breakdownId[]');
            const ids = formData.getAll('id[]');
            const originalDowntimes = formData.getAll('originalDowntime[]');
            const isExistings = formData.getAll('isExisting[]');
            const isNews = formData.getAll('isNew[]');

            let hasChanges = false;

            for (let i = 0; i < descriptions.length; i++) {
                if (descriptions[i] && downtimes[i]) {
                    const currentDowntime = parseInt(downtimes[i]);
                    const originalDowntime = parseInt(originalDowntimes[i]);
                    
                    if (isExistings[i] === 'true') {
                        // ถ้าเป็นข้อมูลที่มีอยู่แล้วและ downtime เปลี่ยน
                        if (currentDowntime !== originalDowntime) {
                            problems.push({
                                description: descriptions[i],
                                downtime: currentDowntime,
                                breakdownId: breakdownIds[i] || null,
                                id: ids[i] || null,
                                isExisting: true
                            });
                            hasChanges = true;
                        }
                    } else if (isNews[i] === 'true') {
                        // ถ้าเป็นข้อมูลใหม่
                        problems.push({
                            description: descriptions[i],
                            downtime: currentDowntime,
                            isNew: true
                        });
                        hasChanges = true;
                    }
                }
            }

            if (hasChanges) {
                console.log('Problems to be saved:', problems);
                saveProblem(null, machineCode, date, docNo, problems);
            } else {
                alert('ไม่มีการเปลี่ยนแปลงข้อมูล');
                closeEditModal();
            }
        };
    }
}

function addNewProblemField() {
    const form = document.getElementById('editProblemForm');
    const newField = document.createElement('div');
    newField.innerHTML = `
        <input type="text" name="description[]" placeholder="รายละเอียดปัญหา">
        <input type="number" name="downtime[]" placeholder="เวลาที่เสีย" min="0"> นาที
        <input type="hidden" name="breakdownId[]" value="">
        <input type="hidden" name="id[]" value="">
        <input type="hidden" name="originalDowntime[]" value="0">
        <input type="hidden" name="isExisting[]" value="false">
        <input type="hidden" name="isNew[]" value="true">
    `;
    form.insertBefore(newField, form.lastElementChild);
}

function removeProblem(button) {
    button.parentElement.remove();
}

function showEditModal(content) {
    const modal = document.createElement('div');
    modal.id = 'editModal';
    modal.style.position = 'fixed';
    modal.style.left = '50%';
    modal.style.top = '50%';
    modal.style.transform = 'translate(-50%, -50%)';
    modal.style.backgroundColor = 'white';
    modal.style.padding = '20px';
    modal.style.border = '1px solid black';
    modal.style.zIndex = '1000';
    modal.innerHTML = content;
    document.body.appendChild(modal);
}

function closeEditModal() {
    const modal = document.getElementById('editModal');
    if (modal) modal.remove();
}

async function refreshTableData() {
    const rows = document.querySelectorAll('#barChartTable tbody tr');
    const causeData = await fetchCauseData(selectedDate);

    rows.forEach(row => {
        const machineCode = row.getAttribute('data-machine');
        const docNo = row.getAttribute('data-docno');
        const relevantCause = causeData.find(c => c.MachineCode === machineCode && c.DocNo === docNo);

        if (relevantCause) {
            const problemDetailsCell = row.querySelector('.problem-details');
            const downtimeCell = row.querySelector('td:nth-child(7)'); // เปลี่ยนเป็น 7 เนื่องจากลำดับคอลัมน์เปลี่ยนไป

            if (problemDetailsCell && downtimeCell) {
                const causeText = relevantCause.Causes.join(', ');
                problemDetailsCell.textContent = causeText;
                problemDetailsCell.setAttribute('data-total-downtime', relevantCause.TotalDowntime);
                downtimeCell.textContent = relevantCause.TotalDowntime;
            }
        }

        const actionCell = row.querySelector('td:nth-child(8)'); // ปรับตามลำดับคอลัมน์จริง
        if (actionCell) {
            actionCell.innerHTML = `<button onclick="editProblem('${machineCode}', '${docNo}')">แก้ไขปัญหา</button>`;
        }
    });
        // เพิ่มการเรียกใช้ฟังก์ชันปรับความกว้าง header หลังจากอัปเดตข้อมูล
        adjustHeaderWidth();
}

function saveCause() {
    const causeData = [];
    // รวบรวมข้อมูล Cause จากตาราง
    document.querySelectorAll('#barChartTable tbody tr').forEach(row => {
        const docNo = row.getAttribute('data-docno');
        const causeInput = row.querySelector('input[name="cause"]');
        if (causeInput && causeInput.value) {
            causeData.push({
                date: selectedDate,
                machineCode: row.cells[0].textContent,
                docNo: docNo,
                cause: causeInput.value
            });
        }
    })
    
    fetch('/api/saveAllCause', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({ data: causeData}),
    })
    .then(response => response.json())
    .then(data => {
        if (data.message) {
            alert('บันทึก Cause สำเร็จ');
        } else {
            alert('เกิดข้อผิดพลาดในการบันทึก Cause');
        }
    })
    .catch((error) => {
        console.error('Error:', error);
        alert('เกิดข้อผิดพลาดในการบันทึก Cause');
    });
} 

API = 
router.post('/updateCausesMswPlus', async (req, res) => {
    const { date, machineCode, problems } = req.body;
    console.log('ได้รับคำขอสำหรับ updateCausesMswPlus:', { date, machineCode, problems });

    if (!date || !machineCode || !Array.isArray(problems)) {
        return res.status(400).json({ success: false, message: 'ข้อมูลไม่ถูกต้อง' });
    }

    try {
        if (!dbSQL) {
            throw new Error('การเชื่อมต่อฐานข้อมูลไม่ได้ถูกสร้างขึ้น');
        }

        const transaction = new sql.Transaction(dbSQL);
        await transaction.begin();

        try {
            const updatedProblems = [];

            for (const problem of problems) {
                if (problem.isExisting) {
                    // อัพเดท Downtime สำหรับปัญหาที่มีอยู่แล้ว
                    if (problem.breakdownId) {
                        await transaction.request()
                            .input('breakdownId', sql.Int, parseInt(problem.breakdownId))
                            .input('Downtime', sql.Float, parseFloat(problem.downtime))
                            .query`
                                UPDATE [Production_Analytics].[dbo].[DailyProductionCausesMswPlus]
                                SET Downtime = @Downtime, UpdatedAt = GETDATE()
                                WHERE breakdownId = @breakdownId
                            `;
                    } else if (problem.id) {
                        await transaction.request()
                            .input('Id', sql.Int, parseInt(problem.id))
                            .input('Downtime', sql.Float, parseFloat(problem.downtime))
                            .query`
                                UPDATE [Production_Analytics].[dbo].[DailyProductionCausesMswPlus]
                                SET Downtime = @Downtime, UpdatedAt = GETDATE()
                                WHERE ID = @Id
                            `;
                    }
                    updatedProblems.push(problem);
                } else if (problem.isNew) {
                    // เพิ่มข้อมูลใหม่
                    const result = await transaction.request()
                        .input('Date', sql.Date, new Date(date))
                        .input('MachineCode', sql.NVarChar(50), machineCode)
                        .input('Cause', sql.NVarChar(500), problem.description)
                        .input('Downtime', sql.Float, parseFloat(problem.downtime))
                        .query`
                            INSERT INTO [Production_Analytics].[dbo].[DailyProductionCausesMswPlus]
                            (Date, MachineCode, Cause, Downtime, UpdatedAt)
                            OUTPUT INSERTED.ID
                            VALUES (@Date, @MachineCode, @Cause, @Downtime, GETDATE())
                        `;
                    const newId = result.recordset[0].ID;
                    updatedProblems.push({ ...problem, id: newId });
                }
            }

            await transaction.commit();
            console.log('Updated problems:', updatedProblems);
            res.json({ success: true, message: 'อัปเดตสาเหตุสำเร็จ', updatedProblems });
        } catch (error) {
            await transaction.rollback();
            throw error;
        }
    } catch (error) {
        console.error('เกิดข้อผิดพลาดในการอัปเดตสาเหตุ:', error);
        res.status(500).json({ success: false, message: 'ไม่สามารถอัปเดตสาเหตุได้', error: error.message });
    }
});

How to bundle multiple files into one minified in Vite

Trying to migrate existing Webpack mix configuration to Vite:

mix
    // CSS
    .styles([
        'resources/plugins/bootstrap/css/bootstrap.min.css',
        'resources/css/app.css',
        'resources/sass/main.css',
        'resources/sass/dashmix/themes/xsmooth.css',
        'resources/plugins/flatpickr/flatpickr.min.css',
        'resources/plugins/select2/select2.min.css'
    ], 'public/css/app.css')
    // JS
    .js([
        'resources/js/app.js',
        'resources/js/dashmix/app.js',
        'resources/plugins/pusher/pusher.min.js',
        'resources/plugins/flatpickr/flatpickr.min.js',
        'resources/plugins/flatpickr/l10n/ru.js',
        'resources/plugins/select2/select2.min.js',
    ], 'public/js/app.js')

How to do this in Vite?

Found waleedtariq109/vite-multi-bundler repository, but unfortunately it very old and incompatible with up-to-date Vite

Debugging npm packages step by step

How could I debug my local npm packages step by step. Like hit a debugg point and execute line by line.
I tried npm pack, npm link but only thing to do with it is console.log wich is not sufficient.

Would be gratefull if you could give me some tips.

javascript : why can i not get routes from url?

i have 2 pages : index.html ans app.js

  1. index.html looks like

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="app.js" defer></script>
        <title>Bank App</title>
      </head>
      <body>
        <!-- This is where you'll work -->
        <div id="app">Loading...</div>
    
        <template id="login">
            <h1>Bank App</h1>
            <section>
              <a href="/dashboard">Login</a>
            </section>
        </template>
    
        <template id="dashboard">
            <header>
              <h1>Bank App</h1>
              <a href="/login">Logout</a>
            </header>
            <section>
              Balance: 100$
            </section>
            <section>
              <h2>Transactions</h2>
              <table>
                <thead>
                  <tr>
                    <th>Date</th>
                    <th>Object</th>
                    <th>Amount</th>
                  </tr>
                </thead>
                <tbody></tbody>
              </table>
            </section>
          </template>
    
      </body>
    </html>
    
  2. app.js looks like

    const routes = {
        '/login': { templateId: 'login' },
        '/dashboard': { templateId: 'dashboard' },
      };
    
    function updateRoute() {
    
        const path = window.location.pathname;
        const route = routes[path]
        console.log(route.templateId)
    
        const template = document.getElementById(route.templateId);
        const view = template.content.cloneNode(true);
        const app = document.getElementById('app');
        app.innerHTML = '';
        app.appendChild(view);
      }
    
    
    updateRoute()
    

when i run on local (live) server in vs code, and hit localhost:5500/login, i am not getting the login template from index.html

any help please to know why ?

trigger submit on several forms at once in javascript

We have hera at school an older learning platform with low usability. I try to improve it by writing an add-on for me and my collegues. I got som stuff working but i am stuck trying to submit several forms att once. The task is the following: On the webpage is a list of all my students. There is one textarea for each one for assesment and a link “Save” to update and set the current date and time as timestamp for the text.

The link triggers a method which copies some parameters to a form and submits it. it is know a Asp.net Postback Mechanism(doPostBack function).

I would like to trigger that in a loop for all student so every assesment, even empty once, gets a new timestamp just to confirm and older assesment as still valid.

I got a functioning loop but only the first submit works. Thats dues to the fact that a subimt triggers a full request and it reload the page –> The loop stops.

Workaround: the script copies the form to an iframe the prevent reoload. Works, but the problem is now that only the last student in the loop is uppdated.

I guessed it was about timing, the requests are coming to fast for the server. So i slowed it down to 5sec wait between request but only the last student shows an updated timestamp.

I need another idea to fix this. Here is my code so far. count and if(count<3) i only for debugging purposes:

document.getElementById('saveAll').addEventListener('click', () => {
    addText = document.getElementById('add2BoxText').value;

    chrome.tabs.query({
        active: true,
        currentWindow: true
    }, (tabs) => {
        chrome.scripting.executeScript({
            target: {
                tabId: tabs[0].id
            },
            function: saveAllBoxes

        });
    });
});







function saveAllBoxes() {

    var c = 0;
    var theForm = document.forms['aspnetForm'];

    const iframe = document.createElement('iframe');

    iframe.style.display = 'none';
    document.body.appendChild(iframe);
    const formClone = theForm.cloneNode(true);
    iframe.contentDocument.body.appendChild(formClone);
    //document.body.removeChild(theForm); 


    if (!formClone) {
        formClone = document.aspnetForm;
    }

    async function __doPostBack(eventTarget, eventArgument) {
        if (!formClone.onsubmit || (formClone.onsubmit() != false)) {
            formClone.__EVENTTARGET.value = eventTarget;
            formClone.__EVENTARGUMENT.value = eventArgument;

            formClone.submit();
            console.log(formClone.__EVENTTARGET.value);
            c++;
        }
    }

    count = 0;
    var all_td_a = document.querySelectorAll('td a');
    
    all_td_a.forEach((element) => {

        if (element.text == 'Spara') {
            element.text = "Saved";
            fullLink = element.href;
            var fullLink = fullLink.substring(fullLink.indexOf("'") + 1, fullLink.lastIndexOf("'") - 3);
            //console.log(fullLink);
            nu = new Date().valueOf() + 2500;
            console.log('---',nu);
            while (nu > new Date().valueOf()) {
                // my kind of loop. bad! but i did not get SetTimeout to work.
            }
            console.log(new Date().valueOf());
           if(count < 3) __doPostBack(fullLink, '');
            count++;
           
        }


    });
   
}

Redirecting mobile users according to their OS

I need to post a banner ad with hyperlink on my WP site for mobile users so could you please help me how to redirect users to the releveant store according to their OS?
i.e. redirecting android users to play store link, and IOS users to apple store link.

Best regards

How to specify parse mode in Telegram (TMA) native share popup?

Using the telegram bot API, one can specify the parse_mode to HTML or Markdown/MarkdownV2. In a telegram mini-app, the native “Share” popup screen (with Chat select) can be opened as follows:

window.Telegram.WebApp.openTelegramLink(
    `https://t.me/share/msg_url?url=${encodeURIComponent(url)}&text=${encodeURIComponent(text)}`
)

Where url is the URL to be shared and text is the description text accompanying it. When giving text as **Test** or *Test* or <b>Test</b>, it just gets sent/shown with the raw * and <b> tags/chars instead of formatting it as expected (Test).

How can one specify the formatting in this native TMA share popup screen?

Listen on multiple input tags and concatenate inputs to a single output field without need to click a button

With plain JavaScript + HTML, I would like to listen on multiple inputs including a <select>-tag and generate an output text field (which can be copied) without the need to click on a button. If possible, I would like to add the “copy-to-clipboard” sign and functionality to the right of the output text field (e.g. Guthub Pages often provide such a possibility).

Here is the link to fiddle to test and verify what I wrote.

Tried:

<!DOCTYPE html>
<html>
<body>
<script>
  function fun() {
    var fn = document.getElementById("fname");
    var ln = document.getElementById("lname");
    var test = document.getElementById("test");
    out = ln.value + "," + fn.value + "," + test.value;
    document.getElementById("out0").innerHTML = out;
    switch(test.value) {
      default:
       var val = "XXX" 
       break; 
      case "A":
        var val = "YYY"
        break;
      case "B":
        var val = "ZZZ"
        break;
    }
    document.getElementById("out1").innerHTML = val;
    var text = document.getElementById("textField");
    text.style.display = "block";

  }
</script>

<label for="fname">First name:</label><br>
<input type="text" id="fname" value=""><br>

<label for="lname">Last name:</label><br>
<input type="text" id="lname" value=""><br><br>

<label for="test">Test</label>
<select id="test">
  <option value="A">AA</option>
  <option value="B">BB</option>
  <option value="C">CC</option>
</select><br><br>
<button class="button" onclick="fun()">Wihtout button?</button>
<div id="textField" style="display: none;"></div>

<span id="out0"></span><br><br>
<span id="out1"></span>

</body>
</html>

Looking for:

Some eventListening-mechanic which concatenates input fields to an output string which is displayed and ready to be copied.

404 error on Trustpilot Woocommerce plugin Javascript [closed]

Our site is throwing a 404 error related to the some JS from the Trustpilot Woocommerce plugin.

JS error in console

I’ve already tried some thing, like delaying the script, ecluding it from deferr and some other changes related to the site I thought could fix it.

The trustpilot support sadly doesn’t respond and the plugin hasn’t been updated in over a year.

We are on the free plan from trustpilot and therefore I can use the API if I’m not mistaken.

I hope some of you can point me in the right direction to actually solve the issue.

Kind Regards
Chris

  • tried delaying the script
  • tried exluding it from deferr
  • disabled all other plugins to ensure it is not a plugin conflict
  • refreshed the permalink structure

CSS equal height div with max-height equal to shortest div [duplicate]

I am trying to achieve the following through CSS only. When you hover the right div translate in. The objective is to make the right div height equal to the left one and scroll-y axis. For some reason my brain isn’t working on this 🙂

I don’t want to make the right div absolute

let leftH = document.getElementsByClassName('left')[0].clientHeight;
document.getElementsByClassName('right')[0].style.maxHeight = leftH + 'px';
.outer-container {
  max-width: 500px;
  border: 1px solid;
}

.container {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    overflow: hidden;
}

.container:hover .f-item{
  transform: translate(-250px);
}

.f-item {
  flex: none;
  width: 100%
}

.left {
  background-color: #e5e5e5;
}

.right {
  max-height: 0px;
  overflow-y : auto;
}

.item {
  padding: 2px;
}
<div class="outer-container">
  <div class="container group">
    <div class="f-item left">
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Consequuntur cupiditate placeat, culpa provident fugit totam eveniet saepe neque magni, officia sequi quasi hic id, sapiente libero! Odit pariatur ullam at.
    </div>
    <div class="f-item right">
      <ul>
        <li class="item">Item 1</li>
        <li class="item">Item 2</li>
        <li class="item">Item 3</li>
        <li class="item">Item 4</li>
        <li class="item">Item 5</li>
        <li class="item">Item 6</li>
      </ul>
    </div>
  </div>
</div>

Java Script error under footer, caused by Google Site Kit [closed]

I have a problem with google site kit. Site kit adds this kind of Java Script error (JavaScript 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129) under main sites footer. Has anyone had the same issue?

I tried to locate all the abnormal codes without success.

How to Add Headers or Categories When Selecting Specific Ranges in Results

how can I add headers to display categories like DATE, TOTAL, NAME, etc.? Right now, I can only see the results.

function showInputBox(){

 var ui = SpreadsheetApp.getUi();
 var input = ui.prompt("Please enter your rep name.",ui.ButtonSet.OK_CANCEL);

if(input.getSelectedButton() == ui.Button.OK){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ws = ss.getSheetByName("Monitoring (Database)");
  var data = ws.getRange("A2:X" + ws.getLastRow()).getValues();
  var userSelectedRep = input.getResponseText().toLowerCase();
  var newData = data.filter(function(r){ return r[23] .toLowerCase() == userSelectedRep});
  var selectedColumns = newData.map(function (r) {
    return [r[10], r[9], r[1], r[17], r[18], r[19], r[20], r[21], r[22]];
  });

  if (newData.length > 0){
  var newWs = ss.insertSheet(userSelectedRep);
  newWs.getRange(3, 3, selectedColumns.length, selectedColumns[0].length).setValues(selectedColumns);
  } else {
    ui.alert ("No Matching data found for the entered name.");
  } 
  
  } else {
    ui.alert("Operation Canceled.");
  }
}

I tried to change the getrange to A1:X but the result is the same. please see below the screenshot.

How to make my pagination query flexible in firebase?

In firebase document related to query cursors, I have successfully implemented the pagination but it only works when users press arrow next or previous. In my case, when user at page 1 press page 9 and vice versa the query starts to fall off and gives wrong results or even throw out error because data of each page hasn’t recorded yet. Without offset in the instructions it is very hard to know which records are presented at page 1 or page 9 or between them. I also did try to get all data and using array.slice() but it is very slow because data returns from firebase is very big, approximately 9k records. Has anyone ever encountered this problem and has solution for this ?

Open Cart Slider using custom ajax add to cart script Shopify Flow Theme

I have created custom ajax add to cart functionality in my store and I want to open the slider cart after successful add to cart event. I have achieved it using the below code but it only works for the first time. If you remove the products from cart and try to add again then it won’t show the updated cart or if you want to add more to it then also it won’t work. Cart drawer only opens for the first time only. I am using shopify flow theme.

function addProductsToCart(variantIdRegular, variantIdSubscription, selling_plan_id) {
  
  var itemsToAdd = [{ id: variantIdRegular, quantity: 1 }];

  if ($("input[name=payment-option]").is(":checked")) {
      itemsToAdd.push({ id: variantIdSubscription, quantity: 1, selling_plan: selling_plan_id });
  }
  
  $.ajax({
    url: '/cart/add.js',
    type: 'post',
    dataType: 'json',
    contentType: "application/json",
    data: JSON.stringify({ 'items': itemsToAdd }),
    success: function(response) {
      console.log('Both products added', response);
      var cartDrawerButtonSelector = ".js-drawer-open-right-link";
      var cartDrawerButton = document.querySelector(cartDrawerButtonSelector);
      cartDrawerButton.click();
    },
    error: function(xhr, status, error) {
      console.error('Failed to add products', xhr.responseJSON);
    }
  });
}

Please help me out on this.
Thanks to all.