2

I am using mongoose-paginate-v2 on the backend to paginate the response from my DB. I am trying to setup the front end to move through the data and send a request for the new data as needed. (That part seems to be working). The problem I am having is the DOM seems to freak out after pressing the next / back buttons a few times. VIDEO of problem which is at the end of the 20sec vid: (https://drive.google.com/file/d/1btfeKqXELEMmBnFPpkCbBLY5uhrC1bvE/view?usp=sharing)

What I have tried: I have tried .emtpy, .remove, and now .replace all seem to have the same problem. I have also moved the code around to try and make it unti the prior .then() was complete. Here is the code:

Front End:

<% include ../partials/header%>
<div class="container-fluid pt-3">
    <div class='row'>
        <div class="col-md-1">
            <button class="btn btn-outline-primary sticky-top">Start New Call</button>
        </div>
        <div class="col-md-11">
            <table class="table">
                <thead class="sticky-top">
                    <tr>
                        <th scope="col">Incident #</th>
                        <th scope="col">Type</th>
                        <th scope="col">Location</th>
                        <th scope="col">Units</th>
                        <th scope="col">Dispatch</th>
                        <th scope="col">Clear</th>
                        <th scope="col">Disposition</th>
                        <th scope="col">Recall</th>
                    </tr>
                </thead>
                <tbody id="callTable"> 
                        <tr id="row1"><td></td></tr>
                        <tr id="row2"><td></td></tr>
                        <tr id="row3"><td></td></tr>
                        <tr id="row4"><td></td></tr>
                        <tr id="row5"><td></td></tr>
                        <tr id="row6"><td></td></tr>
                        <tr id="row7"><td></td></tr>
                        <tr id="row8"><td></td></tr>
                        <tr id="row9"><td></td></tr>
                        <tr id="row10"><td></td></tr>
                </tbody>
            </table>
            <div class="row">
                <div class="col-3"></div>

                <div class="text-center col-2">

                    <button id="back" class="btn btn-outline-success mr-auto"><i class="fas fa-step-backward"></i><strong>Back</strong></button>
                </div>
                <div class="justify-content-center d-flex col-2 align-items-center">
                    <strong>Page <span id="pgnum">##</span> of <span id="totalpgs">##</span></strong>
                </div>
                <div class="text-center col-2">
                    <button id="next" class="btn btn-outline-success ml-auto"><strong>Next</strong><i class="fas fa-step-forward"></i></button>
                </div>
                <div class="text-center col-3"></div>
            </div>           
        </div>
    </div>
</div>


<% include ../partials/footer %>

<script>
    //Data Management Script
    let list=[];
    axios.get('/api/cnc/incidents')
    .catch(err=>{
        console.log(err);
    })
    .then(data=>{
        return list=data;
    })

    //Setup Function to get data...

    async function getPage(page){

        axios.get('/api/cnc/incidents/'+page)
    .catch(err=>{
        console.log(err);
    })
    .then(response=>{

        console.log(response);
        calls = response.data.docs;
        callNumber = 'Not Assigned';
            next = response.data.hasNextPage,
            back = response.data.hasPrevPage,
            nextPage = response.data.nextPage,
            prevPage = response.data.prevPage;

        $('#pgnum').text(response.data.page);
        $('#totalpgs').text(response.data.totalPages);

            if(next === true){
                $('#next').removeAttr('disabled');
                $('#next').click(function () { 
                getPage(nextPage);

            });
        }else{
            $('#next').attr('disabled', 'disabled');
        }

        if(back === true){
            $('#back').removeAttr('disabled');
            $('#back').click(function () { 
                getPage(prevPage);

            });
        }else{
            $('#back').attr('disabled', 'disabled');
        }
        // activities.sort((a, b) => b.date - a.date)
        calls.sort((a, b) => (a.times.dispatch > b.times.dispatch) ? -1 : 1)

        return calls;

    }).then(calls=>{
        let i = 1;

            calls.forEach(call => {
            //Set default callNumber.
            callNumber = 'Not Assigned'

            //Update to call number from DB.
            if(call.incidentNumber){
                callNumber = call.incidentNumber;
            }

            //Change _id to Radio ID.
            if(call.units.length > 0){
                let assignedUnits = call.units;
                let idArray = list.data[0];
                const filtered = [];

                function compare(assignedUnits,idArray){
                    assignedUnits.forEach((unit)=>{
                        idArray.forEach((unitId)=>{
                            if(unit === unitId._id){
                                filtered.push(unitId.id);
                            }
                        })
                    })
                    return filtered;
                }                

                compare(assignedUnits, idArray);

               callUnits = filtered.join(', ');

            }else{
                callUnits = ['None']
            }


            if(typeof call.disposition !== 'undefined'){
                callDisposition = call.disposition.selected;
            }else{
                callDisposition = '';
            }

            if(call.times.clear === undefined){

                callClear = '';
            }else{

                callClear = call.times.clear;
            }

            //Insert Data into DOM. and make TR the link to edit

            $('#row'+i).replaceWith('<tr id="row'+i+'" onClick="editRun(\''+call._id+'\')"><th>'+callNumber+'</th><td>'+call.callType+'</td><td>'+call.address.placeName+' '+call.address.streetAddress+'</td><td>'+callUnits+'</td><td>'+call.times.dispatch+'</td><td>'+callClear+'</td><td>'+callDisposition+'</td><td id="recall">'+call.recall.length+' personnel</td></tr>');
            i++;
            });
    })
    }

    //Get the first page of data...
    getPage(1);

    function editRun(runId){
        window.location.href= '/api/cnc/incident/input/'+runId;
    }
   //Update Navbar correctly
   $('document').ready(function(){
        $('.top-nav').removeClass('active').addClass('text-light').removeClass('text-dark');
        $('#incidentTab').addClass('active').addClass('text-dark').removeClass('text-light');    
    });
</script>
  • You are populating `list` asynchronously in the first axios call: the `list` might not be populated when your other responses run. – Terry Mar 15 '20 at 16:24
  • Hmmm... Okay but it works the first time w/o any issues and is not reloaded every button click. The flickering doesn't happen until I click the buttons a few times. – A Wilkinson Mar 15 '20 at 16:42
  • That's because with multiple async requests happening you are running into a race condition: that's why sometimes it works and sometimes it doesn't. – Terry Mar 15 '20 at 16:43
  • Terry the first async is called on load of the page as is the second one. If you press the next or back button it doesn't call the 1st async again (no need), it only calls the 2nd one. So I confused how lag between async calls comes into play on the 3rd or 4th button click. – A Wilkinson Mar 16 '20 at 00:27

0 Answers0