rolling and calender toggle
This commit is contained in:
@@ -80,7 +80,8 @@ export interface AggregatedDashboardData {
|
||||
// ---------------- AGGREGATION ----------------
|
||||
// ---------------- AGGREGATION ----------------
|
||||
export async function fetchAggregatedData(
|
||||
type: "expense" | "income"
|
||||
type: "expense" | "income",
|
||||
mode: "rolling" | "calendar" = "rolling"
|
||||
): Promise<AggregatedDashboardData> {
|
||||
const res = await api.get("/expenses", { params: { limit: 0 } });
|
||||
const all: any[] = res.data?.items || res.data || [];
|
||||
@@ -105,56 +106,76 @@ export async function fetchAggregatedData(
|
||||
const weekEnd = endOfDay(new Date(weekStart.getTime() + 6 * 86400000));
|
||||
|
||||
// ---------------- MONTH (rolling 5 weeks, Mon–Sun aligned) ----------------
|
||||
const weeklyBuckets: {
|
||||
label: string;
|
||||
start: Date;
|
||||
end: Date;
|
||||
amount: number;
|
||||
}[] = [];
|
||||
const weeklyBuckets = [];
|
||||
|
||||
const currentWeekStart = getStartOfWeek(now);
|
||||
if (mode === "rolling") {
|
||||
const currentWeekStart = getStartOfWeek(now);
|
||||
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const start = new Date(currentWeekStart.getTime() - i * 7 * 86400000);
|
||||
const end = endOfDay(new Date(start.getTime() + 6 * 86400000));
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const start = new Date(currentWeekStart.getTime() - i * 7 * 86400000);
|
||||
const end = endOfDay(new Date(start.getTime() + 6 * 86400000));
|
||||
|
||||
weeklyBuckets.push({
|
||||
label: `${format(start)} - ${format(end)}`,
|
||||
start,
|
||||
end,
|
||||
amount: 0
|
||||
});
|
||||
weeklyBuckets.push({
|
||||
label: `${format(start)} - ${format(end)}`,
|
||||
start,
|
||||
end,
|
||||
amount: 0
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// calendar weeks within current month
|
||||
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
|
||||
let cursor = getStartOfWeek(startOfMonth);
|
||||
|
||||
while (cursor <= now) {
|
||||
const start = new Date(cursor);
|
||||
const end = endOfDay(new Date(start.getTime() + 6 * 86400000));
|
||||
|
||||
weeklyBuckets.push({
|
||||
label: `${format(start)} - ${format(end)}`,
|
||||
start,
|
||||
end,
|
||||
amount: 0
|
||||
});
|
||||
|
||||
cursor = new Date(cursor.getTime() + 7 * 86400000);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------- YEAR (rolling 12 months) ----------------
|
||||
const monthlyBuckets: {
|
||||
label: string;
|
||||
start: Date;
|
||||
end: Date;
|
||||
amount: number;
|
||||
}[] = [];
|
||||
const monthlyBuckets = [];
|
||||
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const d = new Date(now);
|
||||
d.setMonth(d.getMonth() - i);
|
||||
if (mode === "rolling") {
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const d = new Date(now);
|
||||
d.setMonth(d.getMonth() - i);
|
||||
|
||||
const start = new Date(d.getFullYear(), d.getMonth(), 1);
|
||||
const start = new Date(d.getFullYear(), d.getMonth(), 1);
|
||||
|
||||
const end =
|
||||
i === 0
|
||||
? endOfDay(now) // current month → till now
|
||||
: endOfDay(new Date(d.getFullYear(), d.getMonth() + 1, 0));
|
||||
const end =
|
||||
i === 0
|
||||
? endOfDay(now) // current month → till now
|
||||
: endOfDay(new Date(d.getFullYear(), d.getMonth() + 1, 0));
|
||||
monthlyBuckets.push({
|
||||
label: `${d.toLocaleString("default", { month: "short" })}-${String(d.getFullYear()).slice(2)}`,
|
||||
start,
|
||||
end,
|
||||
amount: 0
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// calendar year (Jan → current month)
|
||||
for (let i = 0; i <= now.getMonth(); i++) {
|
||||
const start = new Date(now.getFullYear(), i, 1);
|
||||
const end = endOfDay(new Date(now.getFullYear(), i + 1, 0));
|
||||
|
||||
const label = `${d.toLocaleString("default", {
|
||||
month: "short"
|
||||
})}-${String(d.getFullYear()).slice(2)}`;
|
||||
|
||||
monthlyBuckets.push({
|
||||
label,
|
||||
start,
|
||||
end,
|
||||
amount: 0
|
||||
});
|
||||
monthlyBuckets.push({
|
||||
label: `${start.toLocaleString("default", { month: "short" })}-${String(start.getFullYear()).slice(2)}`,
|
||||
start,
|
||||
end,
|
||||
amount: 0
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------- LOOP ----------------
|
||||
@@ -231,8 +252,12 @@ export async function fetchAggregatedData(
|
||||
}
|
||||
|
||||
// ---------------- EXPORTS ----------------
|
||||
export const fetchAggregatedExpenses = () =>
|
||||
fetchAggregatedData("expense");
|
||||
export const fetchAggregatedExpenses = (
|
||||
mode: "rolling" | "calendar"
|
||||
) =>
|
||||
fetchAggregatedData("expense", mode);
|
||||
|
||||
export const fetchAggregatedIncome = () =>
|
||||
fetchAggregatedData("income");
|
||||
export const fetchAggregatedIncome = (
|
||||
mode: "rolling" | "calendar"
|
||||
) =>
|
||||
fetchAggregatedData("income", mode);
|
||||
Reference in New Issue
Block a user