Skip to content

Commit

Permalink
fix: move utils to domain
Browse files Browse the repository at this point in the history
Signed-off-by: rare-magma <[email protected]>
  • Loading branch information
rare-magma committed Aug 31, 2024
1 parent 0ffa619 commit d3ce51d
Show file tree
Hide file tree
Showing 19 changed files with 396 additions and 429 deletions.
4 changes: 2 additions & 2 deletions src/components/CalculateButton/CalculateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import { CgMathDivide, CgMathPlus } from "react-icons/cg";
import { useBudget } from "../../context/BudgetContext";
import { useConfig } from "../../context/ConfigContext";
import { useDB } from "../../hooks/useDB";
import type { ItemForm } from "../ItemForm/ItemForm";
import "./CalculateButton.css";
import type {
CalculationHistoryItem,
ItemOperation,
} from "../../guitos/domain/calculationHistoryItem";
import type { BudgetItem } from "../../guitos/domain/budgetItem";

interface CalculateButtonProps {
itemForm: ItemForm;
itemForm: BudgetItem;
label: string;
onCalculate: (changeValue: number, operation: ItemOperation) => void;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`CalculateButton > matches snapshot 1`] = `
<BrowserRouter>
<CalculateButton
itemForm={
ItemForm {
BudgetItem {
"id": 1,
"name": "name1",
"value": 10,
Expand Down
13 changes: 0 additions & 13 deletions src/components/ItemForm/ItemForm.test.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/components/ItemForm/ItemForm.ts

This file was deleted.

40 changes: 19 additions & 21 deletions src/components/ItemForm/ItemFormGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,15 @@ import { useConfig } from "../../context/ConfigContext";
import type { Expenses } from "../../guitos/domain/expenses";
import type { Incomes } from "../../guitos/domain/incomes";
import { useDB } from "../../hooks/useDB";
import {
calc,
calcAvailable,
calcSaved,
calcTotal,
calcWithGoal,
parseLocaleNumber,
roundBig,
} from "../../utils";
import { calc, parseLocaleNumber, roundBig } from "../../utils";
import { CalculateButton } from "../CalculateButton/CalculateButton";
import type { ItemForm } from "./ItemForm";
import "./ItemFormGroup.css";
import type { BudgetItem } from "../../guitos/domain/budgetItem";
import type { ItemOperation } from "../../guitos/domain/calculationHistoryItem";
import { Budget } from "../../guitos/domain/budget";

interface ItemFormProps {
itemForm: ItemForm;
itemForm: BudgetItem;
costPercentage: number;
label: string;
inputRef: RefObject<HTMLInputElement>;
Expand Down Expand Up @@ -116,19 +108,25 @@ export function ItemFormGroup({
}

if (isExpense) {
draft.expenses.total = roundBig(calcTotal(draft.expenses.items), 2);
draft.expenses.total = roundBig(
Budget.itemsTotal(draft.expenses.items),
2,
);
} else {
draft.incomes.total = roundBig(calcTotal(draft.incomes.items), 2);
draft.incomes.total = roundBig(
Budget.itemsTotal(draft.incomes.items),
2,
);
}
draft.stats.available = roundBig(calcAvailable(draft), 2);
draft.stats.withGoal = calcWithGoal(draft);
draft.stats.saved = calcSaved(draft);
draft.stats.available = roundBig(Budget.available(draft as Budget), 2);
draft.stats.withGoal = Budget.availableWithGoal(draft as Budget);
draft.stats.saved = Budget.saved(draft as Budget);
}, budget);

setBudget(newState(), saveInHistory);
}

function handleRemove(toBeDeleted: ItemForm) {
function handleRemove(toBeDeleted: BudgetItem) {
if (!table?.items) return;
if (!budget) return;

Expand All @@ -142,10 +140,10 @@ export function ItemFormGroup({
newTable.items = table.items.filter(
(item: BudgetItem) => item.id !== toBeDeleted.id,
);
newTable.total = roundBig(calcTotal(newTable.items), 2);
draft.stats.available = roundBig(calcAvailable(draft), 2);
draft.stats.withGoal = calcWithGoal(draft);
draft.stats.saved = calcSaved(draft);
newTable.total = roundBig(Budget.itemsTotal(newTable.items), 2);
draft.stats.available = roundBig(Budget.available(draft as Budget), 2);
draft.stats.withGoal = Budget.availableWithGoal(draft as Budget);
draft.stats.saved = Budget.saved(draft as Budget);
}, budget);
setBudget(newState(), true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports[`ItemFormGroup > matches snapshot 1`] = `
}
}
itemForm={
ItemForm {
BudgetItem {
"id": 1,
"name": "name1",
"value": 10,
Expand Down
4 changes: 2 additions & 2 deletions src/components/NavBar/NavBarImpExp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useHotkeys } from "react-hotkeys-hook";
import { BsArrowDownUp, BsUpload } from "react-icons/bs";
import { useBudget } from "../../context/BudgetContext";
import { useDB } from "../../hooks/useDB";
import { budgetToCsv } from "../../utils";
import { Budget } from "../../guitos/domain/budget";

interface NavBarImpExpProps {
expanded: boolean;
Expand Down Expand Up @@ -61,7 +61,7 @@ export function NavBarImpExp({ expanded, setExpanded }: NavBarImpExpProps) {
function handleExportCSV() {
if (budget) {
const filename = `${budget.name}.csv`;
const url = window.URL.createObjectURL(new Blob([budgetToCsv(budget)]));
const url = window.URL.createObjectURL(new Blob([Budget.toCsv(budget)]));
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", filename);
Expand Down
25 changes: 9 additions & 16 deletions src/components/StatCard/StatCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,9 @@ import {
} from "react-icons/bs";
import { useBudget } from "../../context/BudgetContext";
import { useConfig } from "../../context/ConfigContext";
import {
calcAutoGoal,
calcAvailable,
calcSaved,
calcWithGoal,
focusRef,
parseLocaleNumber,
roundBig,
} from "../../utils";
import { focusRef, parseLocaleNumber, roundBig } from "../../utils";
import "./StatCard.css";
import { Budget } from "../../guitos/domain/budget";

interface StatCardProps {
onShowGraphs: () => void;
Expand Down Expand Up @@ -64,9 +57,9 @@ export function StatCard({ onShowGraphs }: StatCardProps) {
setAutoGoal(false);
const newState = produce((draft) => {
draft.stats.goal = item.target.valueAsNumber;
draft.stats.available = roundBig(calcAvailable(draft), 2);
draft.stats.withGoal = calcWithGoal(draft);
draft.stats.saved = calcSaved(draft);
draft.stats.available = roundBig(Budget.available(draft as Budget), 2);
draft.stats.withGoal = Budget.availableWithGoal(draft as Budget);
draft.stats.saved = Budget.saved(draft as Budget);
}, budget);
setBudget(newState(), false);
}
Expand All @@ -84,10 +77,10 @@ export function StatCard({ onShowGraphs }: StatCardProps) {
function handleAutoGoal() {
if (budget && stat) {
const newState = produce((draft) => {
draft.stats.goal = calcAutoGoal(draft);
draft.stats.available = roundBig(calcAvailable(draft), 2);
draft.stats.withGoal = calcWithGoal(draft);
draft.stats.saved = calcSaved(draft);
draft.stats.goal = Budget.automaticGoal(draft as Budget);
draft.stats.available = roundBig(Budget.available(draft as Budget), 2);
draft.stats.withGoal = Budget.availableWithGoal(draft as Budget);
draft.stats.saved = Budget.saved(draft as Budget);
}, budget);
setBudget(newState(), true);
}
Expand Down
30 changes: 11 additions & 19 deletions src/components/TableCard/TableCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,13 @@ import {
import { BsArrowsVertical, BsPlusLg } from "react-icons/bs";
import { useBudget } from "../../context/BudgetContext";
import { useConfig } from "../../context/ConfigContext";
import {
calcAvailable,
calcPercentage,
calcSaved,
calcTotal,
calcWithGoal,
intlFormat,
roundBig,
} from "../../utils";
import type { ItemForm } from "../ItemForm/ItemForm";
import { intlFormat, roundBig } from "../../utils";
import { ItemFormGroup } from "../ItemForm/ItemFormGroup";
import "./TableCard.css";
import type { BudgetItem } from "../../guitos/domain/budgetItem";
import { BudgetItem } from "../../guitos/domain/budgetItem";
import type { Expenses } from "../../guitos/domain/expenses";
import type { Incomes } from "../../guitos/domain/incomes";
import { Budget } from "../../guitos/domain/budget";

interface TableCardProps {
header: "Revenue" | "Expenses";
Expand All @@ -46,7 +38,7 @@ export default function TableCard({ header: label }: TableCardProps) {
const table = isExpense ? budget?.expenses : budget?.incomes;
const total = table?.total ?? 0;

function reorderTable(newOrder: ItemForm[]) {
function reorderTable(newOrder: BudgetItem[]) {
if (!budget) return;
const newState = produce((draft) => {
if (isExpense) {
Expand All @@ -66,17 +58,17 @@ export default function TableCard({ header: label }: TableCardProps) {
} else {
draft.incomes = item;
}
draft.stats.available = roundBig(calcAvailable(draft), 2);
draft.stats.withGoal = calcWithGoal(draft);
draft.stats.saved = calcSaved(draft);
draft.stats.available = roundBig(Budget.available(draft as Budget), 2);
draft.stats.withGoal = Budget.availableWithGoal(draft as Budget);
draft.stats.saved = Budget.saved(draft as Budget);
}, budget);
setBudget(newState(), true);
}

function addItemToTable(tableBeingEdited: Incomes | Expenses | undefined) {
if (!tableBeingEdited) return;
const tableHasItems = table && table.items.length !== 0;
const newItemForm = {} as ItemForm;
const newItemForm = BudgetItem.create();
const newTable = isRevenue ? ({} as Incomes) : ({} as Expenses);
let maxId: number;

Expand All @@ -95,7 +87,7 @@ export default function TableCard({ header: label }: TableCardProps) {
newItemForm.value = 0;

newTable.items = tableBeingEdited.items.concat(newItemForm);
newTable.total = roundBig(calcTotal(newTable.items), 2);
newTable.total = roundBig(Budget.itemsTotal(newTable.items), 2);

handleTableChange(newTable);
}
Expand Down Expand Up @@ -140,7 +132,7 @@ export default function TableCard({ header: label }: TableCardProps) {
onReorder={reorderTable}
as="div"
>
{table.items?.map((item: ItemForm) => (
{table.items?.map((item: BudgetItem) => (
<Reorder.Item
key={item.id}
value={item}
Expand All @@ -153,7 +145,7 @@ export default function TableCard({ header: label }: TableCardProps) {
label={label}
costPercentage={
budget
? calcPercentage(item.value, budget.incomes.total)
? BudgetItem.percentage(item.value, budget.incomes.total)
: 0
}
inputRef={inputRef}
Expand Down
8 changes: 2 additions & 6 deletions src/context/BudgetContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
} from "react";
import useUndo from "use-undo";
import type { SearchOption } from "../components/NavBar/NavBar";
import type { Budget } from "../guitos/domain/budget";
import { calcPercentage } from "../utils";
import { Budget } from "../guitos/domain/budget";
import { useGeneralContext } from "./GeneralContext";

interface BudgetContextInterface {
Expand Down Expand Up @@ -105,10 +104,7 @@ function BudgetProvider({ children }: PropsWithChildren) {
const past = pastState.filter((b) => b?.id === budget?.id);
const future = futureState.filter((b) => b?.id === budget?.id);

const revenuePercentage = calcPercentage(
budget?.expenses.total ?? 0,
budget?.incomes.total ?? 0,
);
const revenuePercentage = Budget.revenuePercentage(budget);

const canReallyUndo = undoPossible && past[past.length - 1] !== undefined;
const canReallyRedo = redoPossible && future[0] !== undefined;
Expand Down
Loading

0 comments on commit d3ce51d

Please sign in to comment.