Fixed persistent localstorage for transactions, incl adding and removing

This commit is contained in:
Josh Creek
2019-04-13 23:42:17 +01:00
parent 310adecdc0
commit 8c50b68b97
2 changed files with 11 additions and 10 deletions
+6 -10
View File
@@ -48,31 +48,27 @@ export default {
} }
}, },
mounted() { mounted() {
if (localStorage.getItem('transactions')) { if (localStorage.getItem('transactionStorage')) {
try { try {
this.transactions = JSON.parse(localStorage.getItem('transactions')); this.transactions = JSON.parse(localStorage.getItem('transactionStorage'));
} catch(e) { } catch(e) {
alert(e); alert(e);
localStorage.removeItem('transactions'); localStorage.removeItem('transactionStorage');
} }
} }
}, },
watch: {
transactions(newTransaction) {
localStorage.transactions = [...localStorage.transactions, newTransaction];
}
},
methods: { methods: {
deleteTransaction(id) { deleteTransaction(id) {
this.transactions = this.transactions.filter(transaction => transaction.id !== id); this.transactions = this.transactions.filter(transaction => transaction.id !== id);
localStorage.setItem("transactionStorage", JSON.stringify(this.transactions));
}, },
addTransaction(newTransaction) { addTransaction(newTransaction) {
this.transactions.push(newTransaction); this.transactions.push(newTransaction);
this.saveTransactions(); this.saveTransactions();
}, },
saveTransactions() { saveTransactions() {
alert(JSON.stringify(this.transactions)); localStorage.setItem("lastname", "Smith");
localStorage.setItem('transactions', JSON.stringify(this.transactions)); localStorage.setItem("transactionStorage", JSON.stringify(this.transactions));
} }
} }
} }
+5
View File
@@ -0,0 +1,5 @@
module.exports = {
configureWebpack: {
devtool: 'source-map'
}
}