Skip to content

Firefly III: a personal finances manager

2.5 KB
Raw Blame
/*
 * respond-to-first-date-change.js
 * Copyright (c) 2026 [email protected]
 *
 * This file is part of Firefly III (https://github.com/firefly-iii).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import { api } from "../../../boot/axios";

function parseRepetitionSuggestions(response) {
    let data = response.data;
    let select = document.getElementById("ffInput_repetition_type");
    // currently selected value:
    let currentValue = select.value.split(",")[0];

    select.innerHTML = "";
    let opt;
    for (var k in data) {
        if (Object.hasOwn(data, k)) {
            // console.log(
            //     "label: " + data[k].label + ", selected: " + data[k].selected,
            // );
            opt = document.createElement("option");
            opt.value = k;
            if ("" !== currentValue && k.startsWith(currentValue)) {
                opt.selected = true;
            }
            opt.label = data[k].label;
            opt.text = data[k].label;
            if (data[k].selected) {
                opt.selected = true;
            }
            select.appendChild(opt);
        }
    }
    select.disabled = false;
}

function respondToFirstDateChange() {
    let suggestUrl = "./recurring/suggest";
    let obj = document.getElementById("ffInput_first_date");
    let select = document.getElementById("ffInput_repetition_type");
    let date = obj.value;
    select.disabled = true;

    // preselected value:
    var preSelected = this.type;
    if (preSelected === "") {
        preSelected = select.value;
    }
    api.get(suggestUrl, {
        params: { date: date, pre_select: preSelected, past: "true" },
    })
        .then(parseRepetitionSuggestions)
        .catch(function () {
            console.error("Could not load repetition suggestions");
            alert("Could not load repetition suggestions. Please enter a valid date.");
        });
}

export { respondToFirstDateChange };