Skip to content

Commit

Permalink
refactor MonthsContext to tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
Jod929 committed Sep 18, 2023
1 parent ddb78af commit 4c3d73e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 30 deletions.
30 changes: 0 additions & 30 deletions packages/desktop-client/src/components/budget/MonthsContext.js

This file was deleted.

49 changes: 49 additions & 0 deletions packages/desktop-client/src/components/budget/MonthsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { createContext, type ReactNode } from 'react';

import * as monthUtils from 'loot-core/src/shared/months';

type BoundsParams = {
start: string | Date;
end: string | Date;
};

type GetValidMonthBoundsParams = {
bounds: BoundsParams;
startMonth: string | Date;
endMonth: string | Date;
};

export function getValidMonthBounds({ bounds, startMonth, endMonth }: GetValidMonthBoundsParams) {
return {
start: startMonth < bounds.start ? bounds.start : startMonth,
end: endMonth > bounds.end ? bounds.end : endMonth,
};
}

export let MonthsContext = createContext(null);

type MonthsProviderProps = {
startMonth: string | Date;
numMonths: number;
monthBounds: BoundsParams;
type: string;
children?: ReactNode;
};

export function MonthsProvider({
startMonth,
numMonths,
monthBounds,
type,
children,
}: MonthsProviderProps) {
let endMonth = monthUtils.addMonths(startMonth, numMonths - 1);
let bounds = getValidMonthBounds({ bounds: monthBounds, startMonth, endMonth });
let months = monthUtils.rangeInclusive(bounds.start, bounds.end);

return (
<MonthsContext.Provider value={{ months, type }}>
{children}
</MonthsContext.Provider>
);
}

0 comments on commit 4c3d73e

Please sign in to comment.