scottohara/loot

View on GitHub
src/transactions/controllers/flag.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import type {
    Transaction,
    TransactionFlag,
    TransactionFlagType,
} from "~/transactions/types";
import type TransactionModel from "~/transactions/models/transaction";

export default class TransactionFlagController {
    public errorMessage: string | null = null;

    public flagType: TransactionFlagType;

    public flag: TransactionFlag;

    public readonly flagged: boolean;

    public constructor(
        private readonly $uibModalInstance: angular.ui.bootstrap.IModalInstanceService,
        private readonly transactionModel: TransactionModel,
        private readonly transaction: Transaction,
    ) {
        this.flagType = transaction.flag_type ?? "followup";
        this.flag =
            "(no memo)" === String(transaction.flag) ? "" : transaction.flag ?? "";
        this.flagged = Boolean(transaction.flag);
    }

    // Save and close the modal
    public save(): void {
        this.errorMessage = null;
        this.transaction.flag_type = this.flagType;
        this.transaction.flag = "" === this.flag ? "(no memo)" : this.flag;
        this.transactionModel.flag(this.transaction).then(
            (): void => this.$uibModalInstance.close(this.transaction),
            (error: angular.IHttpResponse<string>): string =>
                (this.errorMessage = error.data),
        );
    }

    // Delete and close the modal
    public deleteFlag(): void {
        this.errorMessage = null;
        this.transactionModel.unflag(Number(this.transaction.id)).then(
            (): void => {
                this.transaction.flag_type = null;
                this.transaction.flag = null;
                this.$uibModalInstance.close(this.transaction);
            },
            (error: angular.IHttpResponse<string>): string =>
                (this.errorMessage = error.data),
        );
    }

    // Dismiss the modal without deleting
    public cancel(): void {
        this.$uibModalInstance.dismiss();
    }
}

TransactionFlagController.$inject = [
    "$uibModalInstance",
    "transactionModel",
    "transaction",
];