diff --git a/src/services/dataService.ts b/src/services/dataService.ts index 4650420..5435157 100644 --- a/src/services/dataService.ts +++ b/src/services/dataService.ts @@ -118,14 +118,14 @@ export class DataService { [field]: [...currentIds, createdTransaction.id] }); - // Update local state without re-fetching + // Fetch fresh job data from the server + const updatedJob = await jobService.getJob(jobId); + if (!updatedJob) throw new Error(`Job with id ${jobId} not found after update`); + + // Update local state with fresh data const jobIndex = this.jobs.findIndex(j => j.id === jobId); if (jobIndex !== -1) { - if (type === 'expenditure') { - this.jobs[jobIndex].expenditures.push(createdTransaction); - } else { - this.jobs[jobIndex].income.push(createdTransaction); - } + this.jobs[jobIndex] = updatedJob; this.notifyListeners(); return this.jobs[jobIndex]; } @@ -156,14 +156,14 @@ export class DataService { [field]: [...currentIds, ...newIds] }); - // Update local state + // Fetch fresh job data from the server + const updatedJob = await jobService.getJob(jobId); + if (!updatedJob) throw new Error(`Job with id ${jobId} not found after update`); + + // Update local state with fresh data const jobIndex = this.jobs.findIndex(j => j.id === jobId); if (jobIndex !== -1) { - if (type === 'expenditure') { - this.jobs[jobIndex].expenditures.push(...createdTransactions); - } else { - this.jobs[jobIndex].income.push(...createdTransactions); - } + this.jobs[jobIndex] = updatedJob; this.notifyListeners(); return this.jobs[jobIndex]; } @@ -179,15 +179,14 @@ export class DataService { const updatedTransaction = await transactionService.updateTransaction(job, transactionId, updates); - // Update local state + // Fetch fresh job data from the server + const updatedJob = await jobService.getJob(jobId); + if (!updatedJob) throw new Error(`Job with id ${jobId} not found after update`); + + // Update local state with fresh data const jobIndex = this.jobs.findIndex(j => j.id === jobId); if (jobIndex !== -1) { - this.jobs[jobIndex].expenditures = this.jobs[jobIndex].expenditures.map(tx => - tx.id === transactionId ? updatedTransaction : tx - ); - this.jobs[jobIndex].income = this.jobs[jobIndex].income.map(tx => - tx.id === transactionId ? updatedTransaction : tx - ); + this.jobs[jobIndex] = updatedJob; this.notifyListeners(); return this.jobs[jobIndex]; } @@ -203,11 +202,14 @@ export class DataService { await transactionService.deleteTransaction(job, transactionId); - // Update local state + // Fetch fresh job data from the server + const updatedJob = await jobService.getJob(jobId); + if (!updatedJob) throw new Error(`Job with id ${jobId} not found after update`); + + // Update local state with fresh data const jobIndex = this.jobs.findIndex(j => j.id === jobId); if (jobIndex !== -1) { - this.jobs[jobIndex].expenditures = this.jobs[jobIndex].expenditures.filter(tx => tx.id !== transactionId); - this.jobs[jobIndex].income = this.jobs[jobIndex].income.filter(tx => tx.id !== transactionId); + this.jobs[jobIndex] = updatedJob; this.notifyListeners(); return this.jobs[jobIndex]; }