Skip to content

Instantly share code, notes, and snippets.

View utkarshbhatt12's full-sized avatar
🎯
Focusing

Utkarsh Bhatt utkarshbhatt12

🎯
Focusing
View GitHub Profile
/**
* 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;
noteViewButton.addEventListener('click', () => {
statusSpan.innerText = 'getting docs from Firestore...';
// hide the fields
inputDiv.style.display = 'none';
let p;
notesRef.get()
/**
* Writes the note to the firestore database creating a document
* inside the 'notes' collection
* @param {object} note object containing the note title, body and the event timestamp
*/
const saveNote = (note) => {
notesRef.doc().set(note)
.then(()=> {
// updates the status after the note is created in the firestore
statusSpan.innerText = 'note created';
// execution starts here when user clicks the 'save' button
noteSaveButton.addEventListener('click', (evt) => {
statusSpan.innerText='working...';
// get values from text fields
consttitle = noteTitleInput.value.trim();
constbody = noteBodyTexArea.value.trim();
// won't store notes without a title
// reference to an instance of Firebase Firestore
const db = firebase.firestore();
// a reference to the notes collection inside the default firestore database
// this is the collection where each note will be stored as a unique document
const notesRef = db.collection('notes');
const config = {
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: "",
  };
firebase.initializeApp(config);
<!-- firebase imports -->
<script src="https://www.gstatic.com/firebasejs/4.10.0/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.10.0/firebase-firestore.js"></script>
<!-- Firebase init -->
<script src="./js/firebase/__init.js"></script>
<script src="./js/firebase/crud.js"></script>
<!-- custom js -->
<script src="./js/main.js"></script>
<div id="view-notes-div">
<button id="note-view-button" class="button button-outline" type="submit">VIEW NOTES</button>
<div id="display-notes-div">
<hr>
</div>
</div>
<div id="inputs-div">
<form>
<fieldset>
<input id="note-title-input" type="text" placeholder="note title" maxlength="100" required autofocus>
<textarea rows="8" id="note-body-textarea" type="text" placeholder="note body" required></textarea>
</fieldset>
<button id="note-save-button" class="button button-outline" type="submit">SAVE</button>
</form>
</div>
<div id="header-div">
<h1>
<a href="/">FIRESTORE NOTES</a>
</h1>
<div id="add-notes-div">
<h4>Status:
<span id="status-span">idle...</span>
</h4>
</div>
</div>