Skip to content

Instantly share code, notes, and snippets.

@utkarshbhatt12
Created March 18, 2018 14:00
Show Gist options
  • Save utkarshbhatt12/38d05c3aa6f559bfa185acf42ac1d733 to your computer and use it in GitHub Desktop.
Save utkarshbhatt12/38d05c3aa6f559bfa185acf42ac1d733 to your computer and use it in GitHub Desktop.

Revisions

  1. utkarshbhatt12 created this gist Mar 18, 2018.
    52 changes: 52 additions & 0 deletions drawNote.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@

    /**
    * Takes in a doc ref and displays a note by reading its title,
    * body and timestap in the viewport
    * @param {object} doc firestore document reference
    */
    const drawNote = (doc) => {

    let title, body, timestamp, id, data, hr, h2, h6, p, aDelete, aEdit;

    // these nodes are created for each document
    hr = document.createElement('hr');
    h2 = document.createElement('h2');
    h6 = document.createElement('h6');
    p = document.createElement('p');
    aDelete = document.createElement('a');
    aEdit = document.createElement('a');

    // data() method contains the document data
    data = doc.data();
    // the .(dot) id property gives us the auto-id of the document in context
    id = doc.id;

    // we can access each property with their appropirate name with the
    // . (dot) operator
    title = data.title;
    body = data.body;
    timestamp = data.timestamp;

    // setting the text of the nodes from the Firestore data
    h2.innerText = title;
    h6.innerText = timestamp;
    p.innerText = body;

    // adding a delete method to the notes
    aDelete.innerText = 'delete';
    aDelete.href = '#';
    aDelete.setAttribute('onclick', `deleteDoc("${id}")`);

    aEdit.innerText = ' | edit';
    aEdit.href = '#';
    aEdit.setAttribute('onclick', `editDoc("${id}", "${title}", "${body}")`);

    // finally, we append each element in the div to make them
    // show up in our page
    displayNotesDiv.appendChild(h2)
    displayNotesDiv.appendChild(h6);
    displayNotesDiv.appendChild(p);
    displayNotesDiv.appendChild(aDelete);
    displayNotesDiv.appendChild(aEdit);
    displayNotesDiv.appendChild(hr);
    };