mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 11:01:45 +00:00
refactor(dashboard): reorganiza widgets e remove magnet-lines
This commit is contained in:
56
lib/dashboard/use-goals-progress-widget-controller.ts
Normal file
56
lib/dashboard/use-goals-progress-widget-controller.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo, useState } from "react";
|
||||
import type { Budget, BudgetCategory } from "@/components/orcamentos/types";
|
||||
import type {
|
||||
GoalProgressItem,
|
||||
GoalsProgressData,
|
||||
} from "@/lib/dashboard/goals-progress";
|
||||
import {
|
||||
mapGoalProgressCategoriesToBudgetCategories,
|
||||
mapGoalProgressItemToBudget,
|
||||
} from "@/lib/dashboard/goals-progress-helpers";
|
||||
|
||||
export type GoalsProgressWidgetController = {
|
||||
selectedBudget: Budget | null;
|
||||
editOpen: boolean;
|
||||
categories: BudgetCategory[];
|
||||
defaultPeriod: string;
|
||||
handleEdit: (item: GoalProgressItem) => void;
|
||||
handleEditOpenChange: (open: boolean) => void;
|
||||
};
|
||||
|
||||
export function useGoalsProgressWidgetController(
|
||||
data: GoalsProgressData,
|
||||
): GoalsProgressWidgetController {
|
||||
const [editOpen, setEditOpen] = useState(false);
|
||||
const [selectedBudget, setSelectedBudget] = useState<Budget | null>(null);
|
||||
|
||||
const categories = useMemo<BudgetCategory[]>(
|
||||
() => mapGoalProgressCategoriesToBudgetCategories(data.categories),
|
||||
[data.categories],
|
||||
);
|
||||
|
||||
const defaultPeriod = data.items[0]?.period ?? "";
|
||||
|
||||
const handleEdit = (item: GoalProgressItem) => {
|
||||
setSelectedBudget(mapGoalProgressItemToBudget(item));
|
||||
setEditOpen(true);
|
||||
};
|
||||
|
||||
const handleEditOpenChange = (open: boolean) => {
|
||||
setEditOpen(open);
|
||||
if (!open) {
|
||||
setSelectedBudget(null);
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
selectedBudget,
|
||||
editOpen,
|
||||
categories,
|
||||
defaultPeriod,
|
||||
handleEdit,
|
||||
handleEditOpenChange,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user