Carlo Gorter
Carlo Gorter

Reputation: 33

How do I count up in Firebase Firestore in front-end

I am creating an app that allows users to track new habits, using Firestore to save the users' data. The data stored in the database are the following:

My goal is to essentially click on an object (in this case the flame in the #container), and for the count to add up in the database.

This is what I have got right now:

// Real-time listener
db.collection('habits').onSnapshot((snapshot) => {
    snapshot.docChanges().forEach(change => {
        if(change.type === 'added'){
            renderHabit(change.doc.data(), change.doc.id);
        }
    });
})

// Add new habit
const form = document.querySelector('form');
form.addEventListener('submit', evt => {
    evt.preventDefault();
    const habit = {
        title: form.habitTitle.value,
        count: 0,
        goal: form.habitGoal.value,
        streak: 0
    };

    db.collection('habits').add(habit)
        .catch(err => console.log(err));
    form.habitTitle.value = '';
    form.habitGoal.value = '';
})

// Update counter
const habitContainer = document.querySelector('.newHabit');
habitContainer.addEventListener('click', evt => {
    //console.log(evt);
    if(evt.target.tagName === 'I'){
        const id = evt.target.getAttribute('data-id');
        db.collection('habits').doc(id).update({
            count: 1
        });
    }
});

const habits = document.querySelector('.newHabit');

// Render habit data
const renderHabit = (data, id) => {
    const html = `
        <div id="wrapper" data-id="${id}">
            <div id="container">
                <span id="title">${data.title}</span>
                <div class="streakContainer">
                    <span id="streak">${data.streak}</span>
                    <i data-id="${id}">🔥</i>
                </div>

                <div class="countContainer">
                    <span id="count">${data.count}</span>
                    <span>/</span>
                    <span id="goal">${data.goal}</span>
                </div>

                <div id="progressbar">
                    <div id="timer"></div>
                    <div id="progressbaroverlay"></div> 
                </div>
            </div>
        </div>
    `;

    habits.innerHTML += html;
};

I have tried to declare var countNumber = 0; to eventually use countNumber = countNumber++ on the clickevent inside // Update counter, this doesn't work because countNumber gets set to 0 again on refresh (I believe).

Hopefully I'm clear enough :)

Thanks!

Upvotes: 0

Views: 407

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600141

The easiest way to increment a number in the database is with the increment operator:

habitContainer.addEventListener('click', evt => {
    if(evt.target.tagName === 'I'){
        const id = evt.target.getAttribute('data-id');
        db.collection('habits').doc(id).update({
            count: firebase.firestore.FieldValue.increment(1)
        });
    }
});

Every time you call this, your onSnapshot listener will be called again with the updated document. You'll want to handle if(change.type === 'modified'){ in there to show the updated value/document in the UI.

Upvotes: 1

Related Questions