Skip to content

Firefly III: a personal finances manager

5.0 KB
Raw Blame
/*
 * dashboard.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 "../../boot/bootstrap.js";
import sidebar from "../../pages/shared/sidebar.js";
import dates from "../shared/dates.js";
import i18next from "i18next";
import Get from "../../api/model/currency/get.js";
import GetUserGroup from "../../api/user-group/get.js";
import Put from "../../api/user-group/put.js";
import Alpine from "@alpinejs/csp";

let edit = function () {
    return {
        i18next: null,
        pageTitle: "",
        administration: {
            title: "",
            currency_id: 0,
        },
        currencies: [],
        errors: {
            title: [],
            currency_id: [],
        },
        error_message: "",
        success_message: "",

        init() {
            this.i18next = i18next;
            const page = window.location.href.split("/");
            const administrationId = parseInt(page[page.length - 1]);
            this.downloadAdministration(administrationId);
        },
        downloadAdministration: function (id) {
            new GetUserGroup().get(id, {}).then((response) => {
                let current = response.data.data;
                this.administration = {
                    id: current.id,
                    title: current.attributes.title,
                    currency_id: parseInt(current.attributes.primary_currency_id),
                    currency_code: current.attributes.primary_currency_code,
                    currency_name: current.attributes.primary_currency_name,
                };
                this.pageTitle = this.administration.title;
            });
            const params = { page: 1, limit: 1337 };
            new Get().list(params).then((response) => {
                this.currencies = response.data.data.map((currency) => {
                    return {
                        id: currency.id,
                        code: currency.attributes.code,
                        name: currency.attributes.name,
                    };
                });
            });
        },
        submit: function (e) {
            // reset messages
            this.error_message = "";
            this.success_message = "";
            this.errors = {
                title: [],
                currency_id: [],
            };

            document.getElementById("submitButton").disabled = true;

            // collect data
            let data = {
                title: this.administration.title,
                primary_currency_id: parseInt(this.administration.currency_id),
            };

            // post!
            new Put()
                .put(data, { id: this.administration.id })
                .then((response) => {
                    let administrationId = parseInt(response.data.data.id);
                    window.location.href = "./administrations?user_group_id=" + administrationId + "&message=updated";
                })
                .catch((error) => {
                    this.error_message = error.response.data.message;
                    this.errors.title = error.response.data.errors.title;
                    this.errors.primary_currency_id = error.response.data.errors.primary_currency_id;

                    // enable button again
                    document.getElementById("submitButton").disabled = false;
                });
            if (e) {
                e.preventDefault();
            }
        },
        hasError: function (field) {
            return this.errors[field].length > 0;
        },
        clearTitle: function () {
            this.administration.title = "";
        },
        handleInput() {
            // this.$emit('input', this.administration.title);
        },
    };
};

const comps = {
    edit,
    sidebar,
    dates,
};

function loadPage(comps) {
    // console.log("loadPage");
    Object.keys(comps).forEach((comp) => {
        let data = comps[comp]();
        Alpine.data(comp, () => data);
        // console.log(comp);
    });
    Alpine.start();
}

// wait for load until bootstrapped event is received.
document.addEventListener("firefly-iii-bootstrapped", () => {
    console.log("Loaded through event listener.");
    loadPage(comps);
});
// or is bootstrapped before event is triggered.
if (window.bootstrapped) {
    console.log("Loaded through window variable.");
    loadPage(comps);
}