Implémentation de category dans le modal en récupérant les setttings du module

This commit is contained in:
steeven 2024-04-10 20:14:08 +02:00
parent f8265ce3a8
commit 94f6ad453c
1 changed files with 30 additions and 62 deletions

92
main.ts
View File

@ -53,6 +53,7 @@ export default class budgetPlugin extends Plugin {
// need to start modal // need to start modal
new ExpenseModal( new ExpenseModal(
this.app, this.app,
this,
( (
expenseAmount, expenseAmount,
expenseCategory, expenseCategory,
@ -73,8 +74,6 @@ export default class budgetPlugin extends Plugin {
this.addSettingTab(new ExpenseSettingTab(this.app, this)); this.addSettingTab(new ExpenseSettingTab(this.app, this));
} }
onunload(): void {}
async loadSettings(): Promise<void> { async loadSettings(): Promise<void> {
this.settings = Object.assign( this.settings = Object.assign(
{}, {},
@ -86,10 +85,12 @@ export default class budgetPlugin extends Plugin {
async saveSettings(): Promise<void> { async saveSettings(): Promise<void> {
await this.saveData(this.settings); await this.saveData(this.settings);
} }
onunload(): void {}
} }
export class ExpenseModal extends Modal { export class ExpenseModal extends Modal {
settings = DEFAULT_SETTINGS; plugin: budgetPlugin;
expenseAmount: string = "120"; expenseAmount: string = "120";
expenseCategory: string; expenseCategory: string;
expenseValue: string = "basics"; expenseValue: string = "basics";
@ -103,6 +104,7 @@ export class ExpenseModal extends Modal {
constructor( constructor(
app: App, app: App,
plugin: budgetPlugin,
onSubmit: ( onSubmit: (
expenseAmount: string, expenseAmount: string,
expenseCategory: string, expenseCategory: string,
@ -111,77 +113,42 @@ export class ExpenseModal extends Modal {
) => void ) => void
) { ) {
super(app); super(app);
this.plugin = plugin;
this.onSubmit = onSubmit; this.onSubmit = onSubmit;
} }
// j'accède aux settings ok, mais pas les bons... ici ce sont les DEFAULT_SETTINGS et je veux les modifés ! async onOpen() {
async loadData(): Promise<void> {
await this.loadSettings();
}
async loadSettings(): Promise<void> {
this.settings = Object.assign(
{},
this.expenseCategory,
await this.loadData()
);
}
onOpen() {
const { contentEl } = this; const { contentEl } = this;
console.log(this.settings.expenseCategories);
contentEl.createEl("h1", { text: "Enter new Expense" }); contentEl.createEl("h1", { text: "Enter new Expense" });
// get the data from the plugin settings
const pluginData = await this.plugin.loadData();
// 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) => {
this.expenseAmount = value; this.expenseAmount = value;
}) })
); );
/* move this to the setting // create category button with icon and tooltip
const category = [
{ categoryId: 1, categoryName: "Eat", categoryIcon: "carrot" },
{
categoryId: 2,
categoryName: "Home",
categoryIcon: "home",
},
{
categoryId: 3,
categoryName: "Leisure",
categoryIcon: "armchair",
},
{
categoryId: 4,
categoryName: "Beauty",
categoryIcon: "glasses",
},
{
categoryId: 5,
categoryName: "Holidays",
categoryIcon: "caravan",
},
{
categoryId: 6,
categoryName: "Transport",
categoryIcon: "car",
},
];
const categorySetting = new Setting(contentEl).setName("Category"); const categorySetting = new Setting(contentEl).setName("Category");
category.forEach((c) => {
const setting = categorySetting.addButton((btn: ButtonComponent) => Object.entries(pluginData.expenseCategories).forEach(
btn ([key, value]: [string, { icon: string; name: string }]) => {
.setButtonText(c.categoryName) categorySetting.addButton((btn: ButtonComponent) =>
.setIcon(c.categoryIcon) btn
.setTooltip(c.categoryName) .setButtonText(key)
.onClick(() => { .setIcon(value.icon)
this.expenseCategory = c.categoryName; .setTooltip(value.name)
new Notice(`Selected Category: ${c.categoryName}`); .onClick(() => {
btn.setCta(); this.expenseCategory = value.name;
}) new Notice(`Selected Category: ${value.name}`);
}); btn.setCta();
}); */ })
);
}
);
new Setting(contentEl).setName("Rating").addText((text) => new Setting(contentEl).setName("Rating").addText((text) =>
text.onChange((value) => { text.onChange((value) => {
@ -230,7 +197,7 @@ export class ExpenseModal extends Modal {
); );
} }
onClose() { onClose() {
let { contentEl } = this; const { contentEl } = this;
contentEl.empty(); contentEl.empty();
} }
} }
@ -248,6 +215,7 @@ class ExpenseSettingTab extends PluginSettingTab {
containerEl.empty(); containerEl.empty();
const expenseCategories = this.plugin.settings.expenseCategories; const expenseCategories = this.plugin.settings.expenseCategories;
console.log(expenseCategories);
const expenseAccounts = this.plugin.settings.expenseAccounts; const expenseAccounts = this.plugin.settings.expenseAccounts;
const expenseValues = this.plugin.settings.expenseValues; const expenseValues = this.plugin.settings.expenseValues;