127.0.0.1:8000 budget / master src / js / pages / savings.js
master

Tree @master (Download .tar.gz)

savings.js @masterraw · history · blame

const $ = require('jquery');
const api = require('../utils/api');
const utils = require('../utils');
const chart = require('./chart_and_messages');
const ui = require('../ui/common');
const list_row = require('../ui/list_row');
const notifications = require('../utils/notifications');
const modal = require('../utils/modal');

let timeout_id;

async function queue_next_notification() {
    const enabled = await notifications.enabled();
    if (!enabled) {
        return;
    }

    const {
        next_savings_contribution
    } = await api.get('/budget/savings/next');

    if (next_savings_contribution) {
        timeout_id = notifications.queue(
            'Savings Contribution',
            `Contribute $${next_savings_contribution.value} to savings today`,
            `${next_savings_contribution.date}T10:00:00`,
            queue_next_notification
        );
    }
}

queue_next_notification();

chart.on_refresh(function () {
    clearTimeout(timeout_id);
    queue_next_notification();
});

api.on_submit("/budget/savings", [
    $("#create-savings-goal"), $("#edit-savings-goal")
], {
    value: x => Math.abs(utils.round(parseFloat(x), 2)),
    accumulated: x => Math.abs(utils.round(parseFloat(x || "0"), 2))
}, [
    ["name", "Name of goal must be provided"],
    ["value", "Goal value must be provided"], // value=0 will be accounted for here
    ["value", "Goal value must be a number", value => !isNaN(value)],
    ["accumulated", "Accumulated value must be a number", accumulated => !isNaN(accumulated)],
    ["target_date", "Target Date must be provided"],
    ["target_date", "Target Date must be in YYYY-MM-DD format", utils.validate.iso_date],
    ["target_date", "Target Date must be in the future", date => new Date(date) > new Date()]
], async function () {
    modal.close();
    await chart.refresh();
    await populate_savings_table();
});

$("#delete-savings-goal").on("click", async function () {
    const id = $("#edit-savings-goal").find('[name="id"]').val();
    await api.delete('/budget/savings/' + id);
    ui.show_banner({
        message: `Savings Goal ${id} deleted`
    });
    modal.close();
    await chart.refresh();
    await populate_savings_table();
});

api.get("/budget/accounts").then(function ({
    accounts
}) {
    const payment_account_fields = ui.get_inputs(["#create-savings-goal", "#edit-savings-goal"], "savings_account");
    for (const $payment_account_field of payment_account_fields) {
        ui.generate_account_options($payment_account_field, accounts);
    }
});

api.get("/budget/savings/statistics").then(function ({
    savings_statistics
}) {
    console.log('savings_statistics', savings_statistics);
});

populate_savings_table();

async function populate_savings_table() {
    $("#savings-list").empty();
    const {
        savings
    } = await api.get("/budget/savings");
    for (const savings_goal of savings) {
        console.log(savings_goal);
        list_row.create("savings", {
            title: `${savings_goal.name}:`,
            text: `$${savings_goal.accumulated.toFixed(2)} out of $${savings_goal.value} by ${savings_goal.target_date}`
        }).on('click', function () {
            ui.prefill_inputs("#edit-savings-goal", savings_goal);
            modal.create('edit-savings-modal');
        });
    }
}