Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions frontend/src/pages/CashFlowForecast.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect, useMemo, useCallback } from 'react';
import { useState, useMemo, useCallback } from 'react';
import {
BarChart,
Bar,
Expand Down Expand Up @@ -49,18 +49,22 @@ export default function CashFlowForecast() {
forecastDays: 90,
});

const { distributionAccount, assetIssuer, forecastDays } = params;

const loadForecast = useCallback(async () => {
if (!params.distributionAccount || !params.assetIssuer) {
if (!distributionAccount || !assetIssuer) {
notifyError('Please provide distribution account and asset issuer');
return;
}

const requestParams = { distributionAccount, assetIssuer, forecastDays };

setIsLoading(true);
try {
const [forecastData, historicalData, alertsData] = await Promise.all([
getForecast(params),
getForecast(requestParams),
getHistoricalData(6),
getAlerts(params),
getAlerts(requestParams),
]);

setForecast(forecastData);
Expand All @@ -72,11 +76,7 @@ export default function CashFlowForecast() {
} finally {
setIsLoading(false);
}
}, [params, notifyError, notifySuccess]);

useEffect(() => {
void loadForecast();
}, [loadForecast]);
}, [distributionAccount, assetIssuer, forecastDays, notifyError, notifySuccess]);

const chartData = useMemo(() => {
if (!forecast) return [];
Expand Down
21 changes: 9 additions & 12 deletions frontend/src/services/cashFlowForecastApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import axios, { type AxiosError } from 'axios';
const API_BASE_URL =
(import.meta.env.VITE_API_BASE_URL as string | undefined) || 'http://localhost:3001';

function authHeaders() {
const token = localStorage.getItem('payd_auth_token');
return token ? { Authorization: `Bearer ${token}` } : {};
}

export interface HistoricalPayrollData {
period: string;
totalAmount: number;
Expand Down Expand Up @@ -104,9 +109,7 @@ export const getForecast = async (params: ForecastParams): Promise<CashFlowForec
distributionAccount: params.distributionAccount,
assetIssuer: params.assetIssuer,
},
headers: {
Authorization: `Bearer ${localStorage.getItem('accessToken') || ''}`,
},
headers: authHeaders(),
});

if (!response.data.success) {
Expand Down Expand Up @@ -145,9 +148,7 @@ export const getHistoricalData = async (
params: {
monthsBack: monthsBack || 6,
},
headers: {
Authorization: `Bearer ${localStorage.getItem('accessToken') || ''}`,
},
headers: authHeaders(),
}
);

Expand Down Expand Up @@ -184,9 +185,7 @@ export const getProjections = async (
params: {
forecastDays: forecastDays || 90,
},
headers: {
Authorization: `Bearer ${localStorage.getItem('accessToken') || ''}`,
},
headers: authHeaders(),
}
);

Expand Down Expand Up @@ -226,9 +225,7 @@ export const getAlerts = async (
distributionAccount: params.distributionAccount,
assetIssuer: params.assetIssuer,
},
headers: {
Authorization: `Bearer ${localStorage.getItem('accessToken') || ''}`,
},
headers: authHeaders(),
});

if (!response.data.success) {
Expand Down