11import { useEffect } from "react" ;
22import { getAppStore } from "../../external/bcanSatchel/store.ts" ;
3- import { fetchCashflowCosts , fetchCashflowRevenues } from "../../external/bcanSatchel/actions.ts" ;
3+ import { fetchCashflowCosts , fetchCashflowRevenues , setCashflowSettings } from "../../external/bcanSatchel/actions.ts" ;
44import { CashflowRevenue } from "../../../../middle-layer/types/CashflowRevenue.ts" ;
55import { CashflowCost } from "../../../../middle-layer/types/CashflowCost.ts" ;
6+ import { CashflowSettings } from "../../../../middle-layer/types/CashflowSettings.ts" ;
67import { api } from "../../api.ts" ;
78
89// This has not been tested yet but the basic structure when implemented should be the same
@@ -37,13 +38,27 @@ export const fetchRevenues = async () => {
3738 }
3839} ;
3940
41+ export const fetchCashflowSettings = async ( ) => {
42+ try {
43+ const response = await api ( "/default-values" ) ;
44+ if ( ! response . ok ) {
45+ throw new Error ( `HTTP Error, Status: ${ response . status } ` ) ;
46+ }
47+ const settings : CashflowSettings = await response . json ( ) ;
48+ setCashflowSettings ( settings ) ;
49+ } catch ( error ) {
50+ console . error ( "Error fetching cashflow settings:" , error ) ;
51+ }
52+ } ;
53+
4054
4155// could contain callbacks for sorting and filtering line items
4256// stores state for list of costs/revenues
4357export const ProcessCashflowData = ( ) => {
4458 const {
4559 costSources,
46- revenueSources
60+ revenueSources,
61+ cashflowSettings
4762 } = getAppStore ( ) ;
4863
4964 // fetch costs on mount if empty
@@ -56,5 +71,10 @@ export const ProcessCashflowData = () => {
5671 if ( revenueSources . length === 0 ) fetchRevenues ( ) ;
5772 } , [ revenueSources . length ] ) ;
5873
59- return { costs : costSources , revenues : revenueSources } ;
74+ // fetch settings on mount if null
75+ useEffect ( ( ) => {
76+ if ( ! cashflowSettings ) fetchCashflowSettings ( ) ;
77+ } , [ cashflowSettings ] ) ;
78+
79+ return { costs : costSources , revenues : revenueSources , cashflowSettings } ;
6080} ;
0 commit comments