Skip to content

Commit

Permalink
feat(BudgetInput): support value accessors in BudgetInputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiphe committed Aug 23, 2020
1 parent 7d1d26f commit 007eadf
Show file tree
Hide file tree
Showing 21 changed files with 1,275 additions and 158 deletions.
21 changes: 20 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@ name: build & release
on: push

jobs:
jest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12.x
- uses: actions/cache@v1
id: dependencies-v1
with:
path: node_modules
key: ${{ runner.os }}-dependencies-v1-${{ hashFiles('package-lock.json') }}
restore-keys: |
${{ runner.os }}-dependencies-v1-
- run: npm install
if: steps.dependencies-v1.outputs.cache-hit != 'true'
env:
ADBLOCK: true
- run: npm test
test:
runs-on: ubuntu-latest
strategy:
Expand Down Expand Up @@ -39,7 +58,7 @@ jobs:
CYPRESS_RECORD_KEY: ${{ secrets.cypress_record_key }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
release:
needs: [test]
needs: [jest, test]
runs-on: macos-latest
steps:
- uses: actions/checkout@v1
Expand Down
2 changes: 2 additions & 0 deletions src/budget/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export type BudgetListEntry = {
export type BudgetCategoryGroup = BudgetRow & {
uuid: string;
group: true;
name: string;
indentation: number;
};
export type BudgetCategoryRow = Omit<BudgetCategoryGroup, 'group'> & {
Expand All @@ -117,6 +118,7 @@ export type InterMonthData = {
categories: (BudgetCategoryRow | BudgetCategoryGroup)[];
toBudget: number;
total: BudgetRow;
income: AmountWithPartialTransactions;
overspendPrevMonth: number;
overspendRolloverState: OverspendRollover;
available: AmountWithPartialTransactions[];
Expand Down
16 changes: 10 additions & 6 deletions src/budget/getMonthData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ function assignAvailable(
}
function addBudgeted(
toBudget: number,
available: AmountWithPartialTransactions = {
amount: 0,
transactions: [],
},
available: AmountWithPartialTransactions,
): AmountWithPartialTransactions {
if (toBudget === 0) {
return available;
Expand Down Expand Up @@ -97,14 +94,15 @@ function getCategoryRows({
const newOverspendRolloverState: OverspendRollover = {};
const rollover: Rollover = { total: 0 };

const cats = categories.map(({ group, uuid, indentation }):
const cats = categories.map(({ group, uuid, indentation, name }):
| BudgetCategoryGroup
| BudgetCategoryRow => {
parentRows.splice(indentation + 1);
if (group) {
const row = {
...emptyBudgetRow(),
uuid,
name,
group: true as const,
indentation,
};
Expand Down Expand Up @@ -143,6 +141,7 @@ function getCategoryRows({
}

return {
name,
indentation,
overspendRollover,
group: false as const,
Expand Down Expand Up @@ -178,7 +177,11 @@ const calcMonth: MonthDataGetter<InterMonthData> = function calcMonth(
rollover: { total: overspendPrevMonth, ...rolloverCategories },
} = getPrev();
const available = assignAvailable(incomeCategories, prevAvailable, balance);
const availableThisMonth = addBudgeted(prevToBudget, available.shift());
const income = available.shift() || {
amount: 0,
transactions: [],
};
const availableThisMonth = addBudgeted(prevToBudget, income);
const {
rollover,
categories: budgetCategories,
Expand Down Expand Up @@ -208,6 +211,7 @@ const calcMonth: MonthDataGetter<InterMonthData> = function calcMonth(
toBudget,
total,
available,
income,
availableThisMonth,
rollover,
overspendPrevMonth,
Expand Down
1 change: 1 addition & 0 deletions src/budget/useBudgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default function useBudgets(
categories: [],
overspendPrevMonth: 0,
toBudget: 0,
income: { amount: 0, transactions: [] },
overspendRolloverState: {},
rollover: { total: 0 },
availableThisMonth: {
Expand Down
19 changes: 19 additions & 0 deletions src/lib/MonthProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createContext, useContext } from 'react';
import { MonthData } from '../budget';

const MonthsContext = createContext<MonthData[]>([]);
const MonthContext = createContext<MonthData | null>(null);

export const MonthsContextProvider = MonthsContext.Provider;
export const MonthContextProvider = MonthContext.Provider;

export function useMonths() {
return useContext(MonthsContext);
}
export function useMonth() {
const month = useContext(MonthContext);
if (month === null) {
throw new Error('Can not useMonth outside of MonthContextProvider');
}
return month;
}
28 changes: 28 additions & 0 deletions src/lib/__test__/factories/categoryRow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { BudgetCategoryRow } from '../../../budget';
import faker from 'faker';

let i = 0;

export function createCategoryRow({
budgeted = 0,
spend = 0,
balance = 0,
name = `${faker.commerce.department()}${i++}`,
uuid = `${faker.random.uuid()}${i}`,
indentation = 0,
overspendRollover = false,
group = false,
transactions = [],
}: Partial<BudgetCategoryRow>): BudgetCategoryRow {
return {
budgeted,
spend,
balance,
name,
uuid,
indentation,
overspendRollover,
group,
transactions,
};
}
3 changes: 3 additions & 0 deletions src/lib/__test__/factories/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './categoryRow';
export * from './monthData';
export * from './numberFormatter';
37 changes: 37 additions & 0 deletions src/lib/__test__/factories/monthData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import format from 'date-fns/format';
import { MonthData, DetailedMonthData } from '../../../budget';
import formatDateKey from '../../formatDateKey';

export function createMonthData(
date: Date,
{
uncategorized = { amount: 0, transactions: [] },
categories = [],
toBudget = 0,
total = { spend: 0, balance: 0, budgeted: 0 },
overspendPrevMonth = 0,
overspendRolloverState = {},
available = [],
income = { amount: 0, transactions: [] },
availableThisMonth = { amount: 0, transactions: [] },
rollover = { total: 0 },
}: Partial<DetailedMonthData> = {},
): MonthData {
return {
key: formatDateKey(date),
date,
name: format(date, 'MMMM'),
get: () => ({
uncategorized,
categories,
toBudget,
total,
income,
overspendPrevMonth,
overspendRolloverState,
available,
availableThisMonth,
rollover,
}),
};
}
20 changes: 20 additions & 0 deletions src/lib/__test__/factories/numberFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NumberFormatter } from '../../createNumberFormatter';

const ni = () => {
throw new Error('Not Implemented');
};
export function createNumberFormatter({
fractionStep = 0.1,
fractionDelimiter = '.',
delimiters = [',', '.'],
format = ni,
parse = ni,
}: Partial<NumberFormatter> = {}): NumberFormatter {
return {
fractionDelimiter,
fractionStep,
delimiters,
format,
parse,
};
}
Loading

0 comments on commit 007eadf

Please sign in to comment.