This repository has been archived by the owner on May 10, 2020. 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(money): add tables to history and simplify controls
- Loading branch information
1 parent
902396b
commit a6c5fe2
Showing
10 changed files
with
169 additions
and
61 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
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
42 changes: 27 additions & 15 deletions
42
front/src/features/app/features/history/organisms/Incomes.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 |
---|---|---|
@@ -1,23 +1,35 @@ | ||
import { Option } from 'tsoption' | ||
|
||
import { displayMoney } from '@front/helpers/displayMoney' | ||
import { displayNullableDate } from '@front/helpers/displayNullableDtae' | ||
import { Table } from '@front/ui/molecules/table' | ||
import { IncomeModel } from '@shared/models/money/IncomeModel' | ||
|
||
interface Props { | ||
incomes: IncomeModel[] | ||
className?: string | ||
periodName: string | ||
} | ||
|
||
const columns = { | ||
date: { | ||
title: 'Date', | ||
transform: displayNullableDate, | ||
}, | ||
amount: { | ||
title: 'Amount', | ||
}, | ||
source: { | ||
title: 'Source', | ||
}, | ||
} | ||
|
||
export const Incomes = ({ incomes, className }: Props) => ( | ||
<section className={className}> | ||
<h4>Incomes</h4> | ||
{incomes.map(({ amount, currency, source, date }) => ( | ||
<p key={`${amount}-${date}`}> | ||
{Option.of(date) | ||
.map(_ => _.toDateString()) | ||
.getOrElse('')} | ||
{' — '} | ||
{amount / 100} {currency} ({source}) | ||
</p> | ||
))} | ||
</section> | ||
export const Incomes = ({ incomes, periodName, className }: Props) => ( | ||
<Table | ||
title={`Incomes: ${periodName}`} | ||
className={className} | ||
data={incomes.map(income => ({ | ||
...income, | ||
amount: displayMoney(income.amount, income.currency), | ||
}))} | ||
columns={columns} | ||
/> | ||
) |
42 changes: 27 additions & 15 deletions
42
front/src/features/app/features/history/organisms/Outcomes.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 |
---|---|---|
@@ -1,23 +1,35 @@ | ||
import { Option } from 'tsoption' | ||
|
||
import { displayMoney } from '@front/helpers/displayMoney' | ||
import { displayNullableDate } from '@front/helpers/displayNullableDtae' | ||
import { Table } from '@front/ui/molecules/table' | ||
import { OutcomeModel } from '@shared/models/money/OutcomeModel' | ||
|
||
interface Props { | ||
outcomes: OutcomeModel[] | ||
className?: string | ||
periodName: string | ||
} | ||
|
||
const columns = { | ||
date: { | ||
title: 'Date', | ||
transform: displayNullableDate, | ||
}, | ||
amount: { | ||
title: 'Amount', | ||
}, | ||
category: { | ||
title: 'Category', | ||
}, | ||
} | ||
|
||
export const Outcomes = ({ outcomes, className }: Props) => ( | ||
<section className={className}> | ||
<h4>Outcomes</h4> | ||
{outcomes.map(({ amount, currency, category, date }) => ( | ||
<p key={`${amount}-${date}`}> | ||
{Option.of(date) | ||
.map(_ => _.toDateString()) | ||
.getOrElse('')} | ||
{' — '} | ||
{amount / 100} {currency} ({category}) | ||
</p> | ||
))} | ||
</section> | ||
export const Outcomes = ({ outcomes, periodName, className }: Props) => ( | ||
<Table | ||
title={`Outcomes: ${periodName}`} | ||
className={className} | ||
data={outcomes.map(outcome => ({ | ||
...outcome, | ||
amount: displayMoney(outcome.amount, outcome.currency), | ||
}))} | ||
columns={columns} | ||
/> | ||
) |
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 const NON_BREAKING_SPACE = String.fromCharCode(160) |
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,13 @@ | ||
import { Currency } from '@shared/enum/Currency' | ||
|
||
import { getCurrencySign } from './getCurrencySign' | ||
import { NON_BREAKING_SPACE } from './NON_BREAKING_SPACE' | ||
|
||
export const displayMoney = (amount: number, currency: Currency) => { | ||
const formattedAmount = (amount / 100) | ||
.toFixed(2) | ||
.toString() | ||
.replace(/\B(?=(\d{3})+(?!\d))/g, NON_BREAKING_SPACE) | ||
|
||
return `${getCurrencySign(currency)}${NON_BREAKING_SPACE}${formattedAmount}` | ||
} |
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,7 @@ | ||
import { format } from 'date-fns' | ||
import { Option } from 'tsoption' | ||
|
||
export const displayNullableDate = (date?: Date) => | ||
Option.of(date) | ||
.map(d => format(d, 'D.MM.YYYY')) | ||
.getOrElse('') |
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,56 @@ | ||
import { Table as AntTable } from 'antd' | ||
import { ReactNode, useMemo } from 'react' | ||
|
||
interface Column { | ||
title: string | ||
transform?: (v: any) => ReactNode | ||
} | ||
|
||
interface Columns { | ||
[key: string]: Column | ||
} | ||
|
||
interface Props<Data extends Array<{}>> { | ||
data: Data | ||
columns: Columns | ||
className?: string | ||
title: string | ||
} | ||
|
||
export const Table = <Data extends Array<{}>>({ | ||
className, | ||
data, | ||
columns, | ||
title, | ||
}: Props<Data>) => { | ||
const adoptedData = useMemo( | ||
() => | ||
data.map((dataItem, key) => ({ | ||
key, | ||
...dataItem, | ||
})), | ||
[data], | ||
) | ||
|
||
const adoptedColumns = useMemo( | ||
() => | ||
Object.entries(columns).map(([key, value]) => ({ | ||
...value, | ||
dataIndex: key, | ||
render: value.transform, | ||
})), | ||
[columns], | ||
) | ||
|
||
return ( | ||
<AntTable | ||
dataSource={adoptedData} | ||
columns={adoptedColumns} | ||
className={className} | ||
bordered | ||
size="middle" | ||
pagination={false} | ||
title={() => title} | ||
/> | ||
) | ||
} |
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 { Table } from './Table' |