import Page from "./Page.js";
import Elem from "../Elem.js";
import Format from "../Format.js";
import Notifier from "../Notifier.js";
import Transaction from "../data/Transaction.js";
export default class Home extends Page{
constructor(){
super("Home");
const date = new Date();
const month = date.toLocaleString("en-US", {month: "long"});
let logoutSvg = '';
let viewSvg = '';
if(!user.account.isPopulated){
let from = new Date();
from.setDate(1);
from = Format.transactionDate(from);
let to = new Date();
to = Format.transactionDate(to);
Transaction.fetch(user.account.id, from, to)
.then((transactions)=>{
user.account.addManyTransactions(transactions);
user.account.isPopulated = true;
})
.catch((err)=>{
new Notifier("error", "Unable to retrieve transactions");
});
}
this.render(month, logoutSvg, viewSvg);
}
async logout(){
let response;
try{
response = await fetch("/api/user/logout", {
method: "GET",
headers: {"Content-Type": "application/json"}
});
}catch(e){
new Notifier("error", "Something went wrong, try refreshing the page");
}
if(!response.ok) return new Notifier("error", response.error.message);
changePage("login");
}
render(month, logoutSvg, viewSvg){
new Elem("div")
.addClass("buttonBox")
.append(new Elem("button")
.text("+")
.addClass("circleButton")
.onclick(()=>{changePage("addMenu")})
)
.append(new Elem("button")
.innerHtml(viewSvg)
.addClass("circleButton")
.onclick(()=>{changePage("viewMenu")})
)
.append(new Elem("button")
.innerHtml(logoutSvg)
.addClass("circleButton")
.onclick(this.logout)
)
.appendTo(this.container);
new Elem("h1")
.text(month)
.appendTo(this.container);
new Elem("dl")
.append(new Elem("dt")
.text("Balance: ")
)
.append(new Elem("dd")
.text(Format.currency(user.account.balance))
)
.appendTo(this.container);
new Elem("dl")
.append(new Elem("dt")
.text("Income: ")
)
.append(new Elem("dd")
.text(Format.currency(user.account.incomeTotal()))
)
.appendTo(this.container);
new Elem("dl")
.append(new Elem("dt")
.text("Bills: ")
)
.append(new Elem("dd")
.text(Format.currency(user.account.billsTotal()))
)
.appendTo(this.container);
}
}