Browse Source

Bug fix: saving an account was double stringifying.

Lee Morgan 11 months ago
parent
commit
af17683d58

+ 36 - 3
src/views/js/data/Account.js

@@ -13,6 +13,33 @@ export default class Account{
         this._transactions = [];
     }
 
+    static async create(name, balance){
+        const data = {
+            name: name,
+            balance: Account.toCents(balance),
+            income: [],
+            bills: [],
+            allowances: []
+        };
+        const iv = encryptionHandler.generateIv();
+        const encryptedData = await encryptionHandler.encrypt(data, iv);
+
+        let response;
+        try{
+            response = await fetch("/api/account", {
+                method: "POST",
+                headers: {"Content-Type": "application/json"},
+                body: JSON.stringify({data: encryptedData, iv})
+            });
+        }catch(e){
+            new Notifier("error", "Something went wrong, try refreshing the page");
+        }
+
+        if(response.error) new Notifier("error", response.error.message);
+
+        return new Account(response.id, iv, data);
+    }
+
     async addIncome(name, amount){
         this._income.push({
             id: crypto.randomUUID(),
@@ -29,17 +56,23 @@ export default class Account{
         return Math.round(num * 100);
     }
 
+    static toCents(num){
+        if(typeof num === "string") num = Number(num);
+
+        return Math.round(num * 100);
+    }
+
     async save(){
-        const data = JSON.stringify({
+        const data = {
             name: this._name,
             balance: this._balance,
             income: this._income,
             bills: this._bills,
             allowances: this._allowances
-        });
+        };
         const encryptedData = await encryptionHandler.encrypt(data, this._iv);
 
-        let response
+        let response;
         try{
             response = await fetch("/api/account", {
                 method: "PUT",

+ 2 - 1
src/views/js/data/User.js

@@ -42,7 +42,7 @@ export default class User{
             }
         }else{
             for(let i = 0; i < this._accounts.length; i++){
-                if(this.__accounts[i] === account){
+                if(this._accounts[i] === account){
                     this._currentAccount = i;
                     break;
                 }
@@ -52,6 +52,7 @@ export default class User{
 
     async decryptAndAddAccount(account){
         const data = await encryptionHandler.decrypt(account.data, account.iv);
+        console.log(data);
 
         const newAccount = new Account(account.id, account.iv, data);
         this._accounts.push(newAccount);

File diff suppressed because it is too large
+ 0 - 0
src/views/js/index.js


+ 7 - 34
src/views/js/pages/CreateAccount.js

@@ -13,41 +13,14 @@ export default class CreateAccount extends Page{
     async submit(){
         event.preventDefault();
 
-        const data = {
-            name: this.container.querySelector(".name").value,
-            balance: this.container.querySelector(".balance").value,
-            income: [],
-            bills: [],
-            allowances: []
-        };
-        const iv = encryptionHandler.generateIv();
-        const encryptedData = await encryptionHandler.encrypt(data, iv);
+        let account = await Account.create(
+            this.container.querySelector(".name").value,
+            this.container.querySelector(".balance").value
+        );
 
-        fetch("/api/account", {
-            method: "POST",
-            headers: {"Content-Type": "application/json"},
-            body: JSON.stringify({
-                data: encryptedData,
-                iv: iv
-            })
-        })
-            .then(r=>r.json())
-            .then((response)=>{
-                if(response.error) throw response;
-
-                const account = new Account(response.id, iv, data);
-                user.addAccount(account);
-                user.changeAccount(account);
-                changePage("home");
-            })
-            .catch((err)=>{
-                console.log(err);
-                if(err.error){
-                    new Notifier("error", err.error.message);
-                }else{
-                    new Notifier("error", "Something went wrong, try refreshing the page");
-                }
-            });
+        user.addAccount(account);
+        user.changeAccount(account);
+        changePage("home");
     }
 
     render(){

Some files were not shown because too many files changed in this diff