Browse Source

Merge pull request #610 from gre/selectedDayRange-settings

selectedDayRange is now a settings
master
Gaëtan Renaudeau 7 years ago
committed by GitHub
parent
commit
2e12438379
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 43
      src/components/AccountPage/index.js
  2. 10
      src/components/BalanceSummary/index.js
  3. 44
      src/components/DashboardPage/index.js
  4. 25
      src/components/PillsDaysCount.js
  5. 4
      src/components/base/Chart/refreshDraw.js
  6. 11
      src/reducers/settings.js

43
src/components/AccountPage/index.js

@ -16,8 +16,15 @@ import type { T } from 'types/common'
import { rgba } from 'styles/helpers' import { rgba } from 'styles/helpers'
import { saveSettings } from 'actions/settings'
import { accountSelector } from 'reducers/accounts' import { accountSelector } from 'reducers/accounts'
import { counterValueCurrencySelector, localeSelector } from 'reducers/settings' import {
counterValueCurrencySelector,
localeSelector,
selectedTimeRangeSelector,
timeRangeDaysByKey,
} from 'reducers/settings'
import type { TimeRange } from 'reducers/settings'
import { openModal } from 'reducers/modals' import { openModal } from 'reducers/modals'
import IconAccountSettings from 'icons/AccountSettings' import IconAccountSettings from 'icons/AccountSettings'
@ -63,10 +70,12 @@ const mapStateToProps = (state, props) => ({
account: accountSelector(state, { accountId: props.match.params.id }), account: accountSelector(state, { accountId: props.match.params.id }),
counterValue: counterValueCurrencySelector(state), counterValue: counterValueCurrencySelector(state),
settings: localeSelector(state), settings: localeSelector(state),
selectedTimeRange: selectedTimeRangeSelector(state),
}) })
const mapDispatchToProps = { const mapDispatchToProps = {
openModal, openModal,
saveSettings,
} }
type Props = { type Props = {
@ -74,30 +83,20 @@ type Props = {
t: T, t: T,
account?: Account, account?: Account,
openModal: Function, openModal: Function,
saveSettings: ({ selectedTimeRange: TimeRange }) => *,
selectedTimeRange: TimeRange,
} }
type State = { class AccountPage extends PureComponent<Props> {
selectedTime: string, handleChangeSelectedTime = item => {
daysCount: number, this.props.saveSettings({ selectedTimeRange: item.key })
}
class AccountPage extends PureComponent<Props, State> {
state = {
selectedTime: 'month',
daysCount: 30,
} }
handleChangeSelectedTime = item =>
this.setState({
selectedTime: item.key,
daysCount: item.value,
})
_cacheBalance = null _cacheBalance = null
render() { render() {
const { account, openModal, t, counterValue } = this.props const { account, openModal, t, counterValue, selectedTimeRange } = this.props
const { selectedTime, daysCount } = this.state const daysCount = timeRangeDaysByKey[selectedTimeRange]
// Don't even throw if we jumped in wrong account route // Don't even throw if we jumped in wrong account route
if (!account) { if (!account) {
@ -148,7 +147,7 @@ class AccountPage extends PureComponent<Props, State> {
chartId={`account-chart-${account.id}`} chartId={`account-chart-${account.id}`}
counterValue={counterValue} counterValue={counterValue}
daysCount={daysCount} daysCount={daysCount}
selectedTime={selectedTime} selectedTimeRange={selectedTimeRange}
renderHeader={({ totalBalance, sinceBalance, refBalance }) => ( renderHeader={({ totalBalance, sinceBalance, refBalance }) => (
<Box flow={4} mb={2}> <Box flow={4} mb={2}>
<Box horizontal> <Box horizontal>
@ -165,7 +164,7 @@ class AccountPage extends PureComponent<Props, State> {
</BalanceTotal> </BalanceTotal>
<Box> <Box>
<PillsDaysCount <PillsDaysCount
selectedTime={selectedTime} selected={selectedTimeRange}
onChange={this.handleChangeSelectedTime} onChange={this.handleChangeSelectedTime}
/> />
</Box> </Box>
@ -177,7 +176,7 @@ class AccountPage extends PureComponent<Props, State> {
totalBalance={totalBalance} totalBalance={totalBalance}
sinceBalance={sinceBalance} sinceBalance={sinceBalance}
refBalance={refBalance} refBalance={refBalance}
since={selectedTime} since={selectedTimeRange}
/> />
<BalanceSinceDiff <BalanceSinceDiff
t={t} t={t}
@ -186,7 +185,7 @@ class AccountPage extends PureComponent<Props, State> {
totalBalance={totalBalance} totalBalance={totalBalance}
sinceBalance={sinceBalance} sinceBalance={sinceBalance}
refBalance={refBalance} refBalance={refBalance}
since={selectedTime} since={selectedTimeRange}
/> />
</Box> </Box>
</Box> </Box>

10
src/components/BalanceSummary/index.js

@ -14,10 +14,10 @@ type Props = {
chartColor: string, chartColor: string,
chartId: string, chartId: string,
accounts: Account[], accounts: Account[],
selectedTime: string, selectedTimeRange: string,
daysCount: number, daysCount: number,
renderHeader?: ({ renderHeader?: ({
selectedTime: *, selectedTimeRange: *,
totalBalance: number, totalBalance: number,
sinceBalance: number, sinceBalance: number,
refBalance: number, refBalance: number,
@ -31,7 +31,7 @@ const BalanceSummary = ({
counterValue, counterValue,
daysCount, daysCount,
renderHeader, renderHeader,
selectedTime, selectedTimeRange,
}: Props) => { }: Props) => {
const account = accounts.length === 1 ? accounts[0] : undefined const account = accounts.length === 1 ? accounts[0] : undefined
return ( return (
@ -43,7 +43,7 @@ const BalanceSummary = ({
{renderHeader ? ( {renderHeader ? (
<Box px={6}> <Box px={6}>
{renderHeader({ {renderHeader({
selectedTime, selectedTimeRange,
// FIXME refactor these // FIXME refactor these
totalBalance: balanceEnd, totalBalance: balanceEnd,
sinceBalance: balanceStart, sinceBalance: balanceStart,
@ -59,7 +59,7 @@ const BalanceSummary = ({
data={balanceHistory} data={balanceHistory}
height={200} height={200}
currency={counterValue} currency={counterValue}
tickXScale={selectedTime} tickXScale={selectedTimeRange}
renderTickY={val => formatShort(counterValue.units[0], val)} renderTickY={val => formatShort(counterValue.units[0], val)}
renderTooltip={ renderTooltip={
isAvailable && !account isAvailable && !account

44
src/components/DashboardPage/index.js

@ -13,7 +13,13 @@ import type { T } from 'types/common'
import { colors } from 'styles/theme' import { colors } from 'styles/theme'
import { accountsSelector } from 'reducers/accounts' import { accountsSelector } from 'reducers/accounts'
import { counterValueCurrencySelector, localeSelector } from 'reducers/settings' import {
counterValueCurrencySelector,
localeSelector,
selectedTimeRangeSelector,
timeRangeDaysByKey,
} from 'reducers/settings'
import type { TimeRange } from 'reducers/settings'
import { reorderAccounts } from 'actions/accounts' import { reorderAccounts } from 'actions/accounts'
import { saveSettings } from 'actions/settings' import { saveSettings } from 'actions/settings'
@ -35,6 +41,7 @@ const mapStateToProps = createStructuredSelector({
accounts: accountsSelector, accounts: accountsSelector,
counterValue: counterValueCurrencySelector, counterValue: counterValueCurrencySelector,
locale: localeSelector, locale: localeSelector,
selectedTimeRange: selectedTimeRangeSelector,
}) })
const mapDispatchToProps = { const mapDispatchToProps = {
@ -48,20 +55,11 @@ type Props = {
accounts: Account[], accounts: Account[],
push: Function, push: Function,
counterValue: Currency, counterValue: Currency,
selectedTimeRange: TimeRange,
saveSettings: ({ selectedTimeRange: TimeRange }) => *,
} }
type State = { class DashboardPage extends PureComponent<Props> {
selectedTime: string,
daysCount: number,
}
class DashboardPage extends PureComponent<Props, State> {
state = {
// save to user preference?
selectedTime: 'month',
daysCount: 30,
}
onAccountClick = account => this.props.push(`/account/${account.id}`) onAccountClick = account => this.props.push(`/account/${account.id}`)
handleGreeting = () => { handleGreeting = () => {
@ -77,17 +75,15 @@ class DashboardPage extends PureComponent<Props, State> {
return 'app:dashboard.greeting.morning' return 'app:dashboard.greeting.morning'
} }
handleChangeSelectedTime = item => handleChangeSelectedTime = item => {
this.setState({ this.props.saveSettings({ selectedTimeRange: item.key })
selectedTime: item.key, }
daysCount: item.value,
})
_cacheBalance = null _cacheBalance = null
render() { render() {
const { accounts, t, counterValue } = this.props const { accounts, t, counterValue, selectedTimeRange } = this.props
const { selectedTime, daysCount } = this.state const daysCount = timeRangeDaysByKey[selectedTimeRange]
const timeFrame = this.handleGreeting() const timeFrame = this.handleGreeting()
const totalAccounts = accounts.length const totalAccounts = accounts.length
@ -111,7 +107,7 @@ class DashboardPage extends PureComponent<Props, State> {
</Box> </Box>
<Box> <Box>
<PillsDaysCount <PillsDaysCount
selectedTime={selectedTime} selected={selectedTimeRange}
onChange={this.handleChangeSelectedTime} onChange={this.handleChangeSelectedTime}
/> />
</Box> </Box>
@ -122,14 +118,14 @@ class DashboardPage extends PureComponent<Props, State> {
chartId="dashboard-chart" chartId="dashboard-chart"
chartColor={colors.wallet} chartColor={colors.wallet}
accounts={accounts} accounts={accounts}
selectedTime={selectedTime} selectedTimeRange={selectedTimeRange}
daysCount={daysCount} daysCount={daysCount}
renderHeader={({ totalBalance, selectedTime, sinceBalance, refBalance }) => ( renderHeader={({ totalBalance, selectedTimeRange, sinceBalance, refBalance }) => (
<BalanceInfos <BalanceInfos
t={t} t={t}
counterValue={counterValue} counterValue={counterValue}
totalBalance={totalBalance} totalBalance={totalBalance}
since={selectedTime} since={selectedTimeRange}
sinceBalance={sinceBalance} sinceBalance={sinceBalance}
refBalance={refBalance} refBalance={refBalance}
/> />

25
src/components/PillsDaysCount.js

@ -2,33 +2,28 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import { translate } from 'react-i18next' import { translate } from 'react-i18next'
import type { T } from 'types/common' import type { T } from 'types/common'
import Pills from 'components/base/Pills' import Pills from 'components/base/Pills'
import { timeRangeDaysByKey } from 'reducers/settings'
import type { TimeRange } from 'reducers/settings'
type Props = { type Props = {
selectedTime: string, selected: string,
onChange: ({ key: string, value: *, label: string }) => void, onChange: ({ key: string, value: *, label: string }) => *,
t: T, t: T,
} }
const itemsTimes = [
{ key: 'week', value: 7 },
{ key: 'month', value: 30 },
{ key: 'year', value: 365 },
]
class PillsDaysCount extends PureComponent<Props> { class PillsDaysCount extends PureComponent<Props> {
render() { render() {
const { selectedTime, onChange, t } = this.props const { selected, onChange, t } = this.props
return ( return (
<Pills <Pills
items={itemsTimes.map(item => ({ items={Object.keys(timeRangeDaysByKey).map((key: TimeRange) => ({
...item, key,
label: t(`app:time.${item.key}`), value: timeRangeDaysByKey[key],
label: t(`app:time.${key}`),
}))} }))}
activeKey={selectedTime} activeKey={selected}
onChange={onChange} onChange={onChange}
/> />
) )

4
src/components/base/Chart/refreshDraw.js

@ -24,8 +24,8 @@ const RENDER_TICK_X = {
default: 'MMM D', default: 'MMM D',
} }
function getRenderTickX(selectedTime) { function getRenderTickX(selectedTimeRange) {
return t => moment(t).format(RENDER_TICK_X[selectedTime] || RENDER_TICK_X.default) return t => moment(t).format(RENDER_TICK_X[selectedTimeRange] || RENDER_TICK_X.default)
} }
export default function refreshDraw({ ctx, props }: { ctx: CTX, props: Props }) { export default function refreshDraw({ ctx, props }: { ctx: CTX, props: Props }) {

11
src/reducers/settings.js

@ -18,6 +18,14 @@ import type { State } from 'reducers'
export const intermediaryCurrency = getCryptoCurrencyById('bitcoin') export const intermediaryCurrency = getCryptoCurrencyById('bitcoin')
export const timeRangeDaysByKey = {
week: 7,
month: 30,
year: 365,
}
export type TimeRange = $Keys<typeof timeRangeDaysByKey>
export type SettingsState = { export type SettingsState = {
loaded: boolean, // is the settings loaded from db (it not we don't save them) loaded: boolean, // is the settings loaded from db (it not we don't save them)
hasCompletedOnboarding: boolean, hasCompletedOnboarding: boolean,
@ -29,6 +37,7 @@ export type SettingsState = {
isEnabled: boolean, isEnabled: boolean,
value: string, value: string,
}, },
selectedTimeRange: TimeRange,
marketIndicator: 'eastern' | 'western', marketIndicator: 'eastern' | 'western',
currenciesSettings: { currenciesSettings: {
[currencyId: string]: CurrencySettings, [currencyId: string]: CurrencySettings,
@ -67,6 +76,7 @@ const INITIAL_STATE: SettingsState = {
isEnabled: false, isEnabled: false,
value: '', value: '',
}, },
selectedTimeRange: 'month',
marketIndicator: 'western', marketIndicator: 'western',
currenciesSettings: {}, currenciesSettings: {},
region, region,
@ -207,5 +217,6 @@ export const exchangeSettingsForAccountSelector: ESFAS = createSelector(
export const marketIndicatorSelector = (state: State) => state.settings.marketIndicator export const marketIndicatorSelector = (state: State) => state.settings.marketIndicator
export const sentryLogsBooleanSelector = (state: State) => state.settings.sentryLogs export const sentryLogsBooleanSelector = (state: State) => state.settings.sentryLogs
export const selectedTimeRangeSelector = (state: State) => state.settings.selectedTimeRange
export default handleActions(handlers, INITIAL_STATE) export default handleActions(handlers, INITIAL_STATE)

Loading…
Cancel
Save