This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(history): add history fetching and view
- Loading branch information
1 parent
e09473d
commit 1d95c39
Showing
14 changed files
with
182 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 0 additions & 41 deletions
41
front/src/features/app/features/history/helpers/historyToTableData.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
front/src/features/history/components/transaction-list/TransactionList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useMappedState } from 'redux-react-hook' | ||
|
||
import { getHistory } from '@front/domain/money/selectors/getHistory' | ||
import { GroupBy } from '@shared/enum/GroupBy' | ||
import { useMemoState } from '@front/domain/store' | ||
import { fetchHistory } from '@front/domain/money/actions/fetchHistory' | ||
import { getHistoryFetchingStatus } from '@front/domain/money/selectors/getHistoryFetchingStatus' | ||
import { Loader } from '@front/ui/components/layout/loader' | ||
import { Table } from '@front/ui/components/layout/table' | ||
|
||
import { createColumns } from '../../helpers/createColumns' | ||
import { historyToTableData } from '../../helpers/historyToTableData' | ||
|
||
interface ClassNames { | ||
incomes: string | ||
outcomes: string | ||
} | ||
|
||
interface Props { | ||
from: Date | ||
to: Date | ||
classNames: ClassNames | ||
} | ||
|
||
export const TransactionList = ({ from, to, classNames }: Props) => { | ||
const history = useMemoState( | ||
() => getHistory(from, to, GroupBy.Month), | ||
() => fetchHistory(from, to, GroupBy.Month), | ||
[from, to], | ||
) | ||
|
||
const fetching = useMappedState(getHistoryFetchingStatus) | ||
|
||
const incomeColumns = createColumns('comment', 'Source') | ||
const incomes = historyToTableData(history, { | ||
filter: transaction => transaction.amount > 0, | ||
}) | ||
|
||
const outcomesColumns = createColumns('comment', 'Category') | ||
const outcomes = historyToTableData(history, { | ||
filter: transaction => transaction.amount < 0, | ||
}) | ||
|
||
return ( | ||
<Loader status={fetching} dataAvaiable={history.nonEmpty()}> | ||
{outcomes.nonEmpty() && ( | ||
<Table | ||
className={classNames.outcomes} | ||
title="Outcomes" | ||
columns={outcomesColumns} | ||
data={outcomes.get()} | ||
/> | ||
)} | ||
{incomes.nonEmpty() && ( | ||
<Table | ||
className={classNames.incomes} | ||
title="Incomes" | ||
columns={incomeColumns} | ||
data={incomes.get()} | ||
/> | ||
)} | ||
</Loader> | ||
) | ||
} |
1 change: 1 addition & 0 deletions
1
front/src/features/history/components/transaction-list/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { TransactionList } from './TransactionList' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Currency } from '@shared/enum/Currency' | ||
|
||
export interface UnifiedTransaction { | ||
date: Date | undefined | ||
amount: number | ||
currency: Currency | ||
comment: string | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Option } from 'tsoption' | ||
import { take, head, sortBy } from 'lodash' | ||
|
||
import { HistoryGroupModel } from '@shared/models/money/HistoryGroupModel' | ||
import { displayMoney } from '@shared/helpers/displayMoney' | ||
import { UnifiedTransaction } from './UnifiedTransaction' | ||
|
||
type Filter = (transaction: UnifiedTransaction) => boolean | ||
const normalizedFilter = (filter?: Filter) => ( | ||
transaction: UnifiedTransaction, | ||
) => { | ||
if (filter) { | ||
return filter(transaction) | ||
} | ||
|
||
return true | ||
} | ||
|
||
interface Options { | ||
maxLength?: number | ||
filter?: (transaction: UnifiedTransaction) => boolean | ||
} | ||
|
||
export const historyToTableData = ( | ||
history: Option<HistoryGroupModel[]>, | ||
{ maxLength, filter }: Options, | ||
) => | ||
history | ||
.flatMap(items => Option.of(head(items))) | ||
.map(item => | ||
[ | ||
...item.incomes.map(income => ({ | ||
date: income.date, | ||
amount: income.amount, | ||
currency: income.currency, | ||
comment: income.source, | ||
})), | ||
...item.outcomes.map(outcome => ({ | ||
date: outcome.date, | ||
amount: -outcome.amount, | ||
currency: outcome.currency, | ||
comment: outcome.category, | ||
})), | ||
].filter(normalizedFilter(filter)), | ||
) | ||
.map(transactions => | ||
sortBy( | ||
transactions, | ||
transaction => transaction.date && -transaction.date.valueOf(), | ||
), | ||
) | ||
.map(transactions => | ||
maxLength ? take(transactions, maxLength) : transactions, | ||
) | ||
.map(transactions => | ||
transactions.map(transaction => ({ | ||
...transaction, | ||
amount: displayMoney(transaction.currency)(transaction.amount, { | ||
withSign: true, | ||
}), | ||
})), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export { History } from './History' | ||
|
||
export { createColumns } from './helpers/createColumns' | ||
export { historyToTableData } from './helpers/historyToTableData' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters