Преглед на файлове

Transaction can now be successfully updated.

Lee Morgan преди 11 месеца
родител
ревизия
5416da4af3
променени са 2 файла, в които са добавени 64 реда и са изтрити 3 реда
  1. 47 0
      src/views/js/data/Transaction.js
  2. 17 3
      src/views/js/pages/EditTransaction.js

+ 47 - 0
src/views/js/data/Transaction.js

@@ -19,10 +19,29 @@ export default class Transaction{
         return this._date;
     }
 
+    set date(v){
+        if(v instanceof Date){
+            this._date = Format.transactionDate(v);
+        }else{
+            this._date = v;
+        }
+    }
+
     get category(){
         return this._parent.getCategory(this._category, this._categoryId);
     }
 
+    set category(v){
+        if(v === "discretionary"){
+            this._category = v;
+            this._categoryId = null;
+        }else{
+            v = v.split(":");
+            this._category = v[0];
+            this._categoryId = v[1];
+        }
+    }
+
     get rawCategory(){
         return this._category;
     }
@@ -31,18 +50,39 @@ export default class Transaction{
         return this._location;
     }
 
+    set location(v){
+        this._location = v;
+    }
+
     get amount(){
         return Format.centsToDollars(this._amount);
     }
 
+    set amount(v){
+        if(typeof v !== "number") v = Number(v);
+        this._amount = Format.dollarsToCents(v);
+    }
+
     get tags(){
         return this._tags;
     }
 
+    set tags(v){
+        this._tags = [];
+        let tags = v.split(",");
+        for(let i = 0; i < tags.length; i++){
+            this._tags.push(tags[i].trim());
+        }
+    }
+
     get note(){
         return this._note;
     }
 
+    set note(v){
+        this._note = v;
+    }
+
     selectValue(){
         if(this._category === "discretionary") return this._category;
         return `${this._category}:${this._categoryId}`;
@@ -145,6 +185,13 @@ export default class Transaction{
                 data: data,
                 iv: this._iv
             };
+        }else{
+            method = "PUT";
+            body = {
+                id: this._id,
+                date: this._date,
+                data: data
+            }
         }
 
         fetch("/api/transaction", {

+ 17 - 3
src/views/js/pages/EditTransaction.js

@@ -1,5 +1,6 @@
 import Page from "./Page.js";
 import Elem from "../Elem.js";
+import Notifier from "../Notifier.js";
 
 export default class EditTransaction extends Page{
     constructor(transaction){
@@ -13,17 +14,30 @@ export default class EditTransaction extends Page{
         );
     }
 
-    submit(event){
+    async submit(transaction){
         event.preventDefault();
 
-        console.log("submit");
+        try{
+            const select = this.container.querySelector.bind(this.container);
+            transaction.amount = select(".amount").value;
+            transaction.category = select(".category").value;
+            transaction.tags = select(".tags").value;
+            transaction.location = select(".location").value;
+            transaction.date = select(".date").value;
+            transaction.note = select(".note").value;
+        }catch(e){
+            return new Notifier("error", e);
+        }
+        
+        await transaction.save();
+        changePage("transactionDetails", transaction);
     }
 
     render(transaction, income, bills, allowances){
         let select, incomeOpt, billsOpt, allowancesOpt;
         new Elem("form")
             .addClass("standardForm")
-            .onsubmit(this.submit.bind(this))
+            .onsubmit(()=>{this.submit(transaction)})
             .append(new Elem("h1")
                 .text("Edit Transaction")
             )