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/contribution", [
    $("#create-savings-contribution")
], {
    value: x => Math.abs(utils.round(parseFloat(x), 2))
}, [
    ["value", "Contribution value must be provided"], // value=0 will be accounted for here
    ["value", "Contribution value must be a number", value => !isNaN(value)],
], async function() {
    await chart.refresh();
    await populate_savings_table();
});

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();
});

populate_savings_table();

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