finished implementing accounts and value. add helper to formt currency

This commit is contained in:
steeven 2024-04-11 19:48:12 +02:00
parent 94f6ad453c
commit 9d7f899386
1 changed files with 56 additions and 15 deletions

69
main.ts
View File

@ -29,13 +29,14 @@ const DEFAULT_SETTINGS: BudgetSettings = {
{ name: "Transport", icon: "car" }, { name: "Transport", icon: "car" },
], ],
expenseAccounts: [ expenseAccounts: [
{ name: "Cash", icon: "Cash" }, { name: "Cash", icon: "coins" },
{ name: "Credit Card", icon: "Credit Card" }, { name: "Credit Card", icon: "credit-card" },
{ name: "Bank", icon: "landmark" },
], ],
expenseValues: [ expenseValues: [
{ name: "Basics", icon: "Basics" }, { name: "Basics", icon: "gem" },
{ name: "Medium", icon: "Medium" }, { name: "Medium", icon: "gem" },
{ name: "Luxury", icon: "Luxury" }, { name: "Luxury", icon: "gem" },
], ],
}; };
@ -90,10 +91,11 @@ export default class budgetPlugin extends Plugin {
} }
export class ExpenseModal extends Modal { export class ExpenseModal extends Modal {
plugin: budgetPlugin; plugin: budgetPlugin = DEFAULT_SETTINGS;
expenseAmount: string = "120"; expenseAmount: string;
expenseAccount: string;
expenseCategory: string; expenseCategory: string;
expenseValue: string = "basics"; expenseValue: string;
onSubmit: ( onSubmit: (
expenseAmount: string, expenseAmount: string,
@ -124,6 +126,12 @@ export class ExpenseModal extends Modal {
// get the data from the plugin settings // get the data from the plugin settings
const pluginData = await this.plugin.loadData(); const pluginData = await this.plugin.loadData();
// create a currency formatter
const formatter = new Intl.NumberFormat("fr-FR", {
style: "currency",
currency: pluginData.currency,
});
// input field for the expense amount // input field for the expense amount
new Setting(contentEl).setName("Amount").addText((text) => new Setting(contentEl).setName("Amount").addText((text) =>
text.onChange((value) => { text.onChange((value) => {
@ -150,11 +158,42 @@ export class ExpenseModal extends Modal {
} }
); );
new Setting(contentEl).setName("Rating").addText((text) => // create account button with icon and tooltip
text.onChange((value) => { const accountSetting = new Setting(contentEl).setName("Account");
this.expenseValue = value;
Object.entries(pluginData.expenseAccounts).forEach(
([key, value]: [string, { icon: string; name: string }]) => {
accountSetting.addButton((btn: ButtonComponent) =>
btn
.setButtonText(key)
.setIcon(value.icon)
.setTooltip(value.name)
.onClick(() => {
this.expenseAccount = value.name;
new Notice(`Selected Account: ${value.name}`);
btn.setCta();
}) })
); );
}
);
const valueSetting = new Setting(contentEl).setName("Value");
Object.entries(pluginData.expenseValues).forEach(
([key, value]: [string, { icon: string; name: string }]) => {
valueSetting.addButton((btn: ButtonComponent) =>
btn
.setButtonText(key)
.setIcon(value.icon)
.setTooltip(value.name)
.onClick(() => {
this.expenseValue = value.name;
new Notice(`Selected Value: ${value.name}`);
btn.setCta();
})
);
}
);
new Setting(contentEl).addButton((btn) => new Setting(contentEl).addButton((btn) =>
btn btn
@ -187,7 +226,11 @@ export class ExpenseModal extends Modal {
editor.editor.setCursor(2, 0); editor.editor.setCursor(2, 0);
// insert the new expense on line 3 // insert the new expense on line 3
editor.editor.replaceSelection( editor.editor.replaceSelection(
`| ${todayFormatted} | ${this.expenseAmount} | ${this.expenseCategory} | ${this.expenseAccount} | ${this.expenseValue} | \n` `| ${todayFormatted} | ${formatter.format(
Number(this.expenseAmount)
)} | ${this.expenseCategory} | ${
this.expenseAccount
} | ${this.expenseValue} | \n`
); );
} }
} else { } else {
@ -229,7 +272,6 @@ class ExpenseSettingTab extends PluginSettingTab {
.setPlaceholder(`Name = ${item.name}`) .setPlaceholder(`Name = ${item.name}`)
.setValue(item.name) .setValue(item.name)
.onChange(async (value) => { .onChange(async (value) => {
console.log("Updated name: " + value);
this.plugin.settings.expenseCategories[ this.plugin.settings.expenseCategories[
index index
].name = value; ].name = value;
@ -241,7 +283,6 @@ class ExpenseSettingTab extends PluginSettingTab {
.setPlaceholder(`Icon = ${item.icon}`) .setPlaceholder(`Icon = ${item.icon}`)
.setValue(item.icon) .setValue(item.icon)
.onChange(async (value) => { .onChange(async (value) => {
console.log("Updated icon: " + value);
this.plugin.settings.expenseCategories[ this.plugin.settings.expenseCategories[
index index
].icon = value; ].icon = value;