PWA-Budget-Tracke
models/transaction.js
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const transactionSchema = new Schema({
name: {
type: String,
trim: true,
required: "Enter a name for transaction"
},
value: {
type: Number,
required: "Enter an amount"
},
date: {
type: Date,
default: Date.now
}
});
const Transaction = mongoose.model("Transaction", transactionSchema);
module.exports = Transaction;
PWA-Budget-Tracke
public/db.js
let db;
const request = indexedDB.open("budget", 1);
equest.onupgradeneeded = function (event) {
const db = event.target.result;
db.createObjectStore("pending", { autoIncrement: true });
};
equest.onsuccess = function (event) {
db = event.target.result;
check if app is online before reading from d
if (navigator.onLine) {
checkDatabase();
}
};
equest.one
or = function (event) {
console.log("Woops! " + event.target.e
orCode);
};
function saveRecord(record) {
const transaction = db.transaction(["pending"], "readwrite");
const store = transaction.objectStore("pending");
store.add(record);
}
function checkDatabase() {
const transaction = db.transaction(["pending"], "readwrite");
const store = transaction.objectStore("pending");
const getAll = store.getAll();
getAll.onsuccess = function () {
if (getAll.result.length > 0) {
fetch("/api/transaction
ulk", {
method: "POST",
body: JSON.stringify(getAll.result),
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json"
}
})
.then(response => response.json())
.then(() => {
delete records if successful
const transaction = db.transaction(["pending"], "readwrite");
const store = transaction.objectStore("pending");
store.clear();
});
}
};
}
function deletePending() {
const transaction = db.transaction(["pending"], "readwrite");
const store = transaction.objectStore("pending");
store.clear();
}
listen for app coming back online
window.addEventListener("online", checkDatabase);
PWA-Budget-Tracke
public/index.html
Your total is: $0
Transaction Name:
Transaction Amount:
Add Deposits
Subtract Expense
Transaction Amount
PWA-Budget-Tracke
public/index.js
let transactions = [];
let myChart;
fetch("/api/transaction")
.then(response => response.json())
.then(data => {
save db data on global variable
transactions = data;
populateTotal();
populateTable();
populateChart();
});
function populateTotal() {
reduce transaction amounts to a single total value
const total = transactions.reduce((total, t) => {
return total + parseInt(t.value);
}, 0);
const totalEl = document.querySelector("#total");
totalEl.textContent = total;
}
function populateTable() {
const tbody = document.querySelector("#tbody");
tbody.innerHTML = "";
transactions.forEach(transaction => {
create and populate a table row
const tr = document.createElement("tr");
tr.innerHTML = `
${transaction.name} td | ${transaction.value} td `; tbody.appendChild(tr); }); } function...
SOLUTION.PDF
Answer To This Question Is Available To Download
Submit New Assignment
Please select no of pages for your assignment
Please select references for your assignment
Please select level for your assignment
x
I am Online - Talk to me!
Please fill out the form below to start chatting with the next available agent.
Mehmet Mert
3
|