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(money): add history contolling on front
- Loading branch information
1 parent
2bb14c5
commit 29ea988
Showing
11 changed files
with
168 additions
and
23 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
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,14 @@ | ||
import { fetchOrFail } from '@front/domain/store/fetchingRedux/fetchOrFail' | ||
import { GroupBy } from '@shared/enum/GroupBy' | ||
|
||
import { fetchHistoryRequest } from '../api/fetchHistoryRequest' | ||
import { actions as dataActions } from '../reducer/history' | ||
|
||
const { addHistory } = dataActions | ||
|
||
export const forceFetchHistory = (from: Date, to: Date, groupBy: GroupBy) => | ||
fetchOrFail(undefined, async (dispatch, getApi) => { | ||
const history = await fetchHistoryRequest(getApi())(from, to, groupBy) | ||
|
||
dispatch(addHistory({ from, to, groupBy }, history)) | ||
}) |
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,33 +1,64 @@ | ||
import { drop } from 'lodash' | ||
import { ClearAction, createClearRedux } from 'redux-clear' | ||
|
||
import { omitFirst } from '@front/helpers/omitFirst' | ||
import { GroupBy } from '@shared/enum/GroupBy' | ||
import { HistoryGroupModel } from '@shared/models/money/HistoryGroupModel' | ||
|
||
interface State { | ||
import { createHistoryKey } from '../helpers/createHistoryKey' | ||
|
||
interface CachedPeriod { | ||
groupBy: GroupBy | ||
from: Date | ||
to: Date | ||
} | ||
|
||
interface Data { | ||
[key: string]: HistoryGroupModel[] | ||
} | ||
|
||
interface State { | ||
data: Data | ||
cachedPeriods: CachedPeriod[] | ||
} | ||
|
||
interface Actions { | ||
addHistory: ClearAction<[string, HistoryGroupModel[]]> | ||
addHistory: ClearAction<[CachedPeriod, HistoryGroupModel[]]> | ||
} | ||
|
||
const MAX_HISTORY_LENGTH = 10 | ||
|
||
const { actions, reducer } = createClearRedux<State, Actions>( | ||
{ | ||
addHistory: state => (key, newHistory) => { | ||
const itemsCount = Object.keys(state).length | ||
addHistory: ({ data, cachedPeriods, ...state }) => (period, newHistory) => { | ||
const { from, to, groupBy } = period | ||
const key = createHistoryKey(from, to, groupBy) | ||
|
||
const dataCount = Object.keys(data).length | ||
const cachedPeriodsCount = cachedPeriods.length | ||
|
||
const oldData = dataCount >= MAX_HISTORY_LENGTH ? omitFirst(data) : data | ||
|
||
const oldState = | ||
itemsCount > MAX_HISTORY_LENGTH ? omitFirst(state) : state | ||
const oldPeriods = | ||
cachedPeriodsCount >= MAX_HISTORY_LENGTH | ||
? drop(cachedPeriods) | ||
: cachedPeriods | ||
|
||
// TODO: filter non-uniq cachedPeriods | ||
return { | ||
...oldState, | ||
[key]: newHistory, | ||
...state, | ||
data: { | ||
...oldData, | ||
[key]: newHistory, | ||
}, | ||
cachedPeriods: [...oldPeriods, period], | ||
} | ||
}, | ||
}, | ||
{}, | ||
{ | ||
data: {}, | ||
cachedPeriods: [], | ||
}, | ||
) | ||
|
||
export { State, actions, reducer } |
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,4 @@ | ||
import { State } from '@front/domain/store/State' | ||
|
||
export const getHistoryCachedPeriods = (state: State) => | ||
state.money.history.cachedPeriods |
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,18 @@ | ||
import { GroupBy } from '@shared/enum/GroupBy' | ||
|
||
interface Props { | ||
groupBy: GroupBy | ||
updateGroupBy: (newGroupBy: GroupBy) => void | ||
} | ||
|
||
export const Groupment = ({ groupBy, updateGroupBy }: Props) => ( | ||
<> | ||
{Object.values(GroupBy).map(value => | ||
value === groupBy ? ( | ||
<span>{value}</span> | ||
) : ( | ||
<button onClick={() => updateGroupBy(value)}>{value}</button> | ||
), | ||
)} | ||
</> | ||
) |
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,49 @@ | ||
import { format } from 'date-fns' | ||
import { ChangeEvent, useCallback } from 'react' | ||
|
||
interface Props { | ||
start: Date | ||
updateStart: (newStart: Date) => void | ||
end: Date | ||
updateEnd: (newEnd: Date) => void | ||
} | ||
|
||
export const Period = ({ start, end, updateStart, updateEnd }: Props) => { | ||
const createHandle = useCallback( | ||
(update: (d: Date) => void) => ({ | ||
target, | ||
}: ChangeEvent<HTMLInputElement>) => { | ||
if (target.value) { | ||
return update(new Date(target.value)) | ||
} | ||
}, | ||
[], | ||
) | ||
|
||
const formatForInput = useCallback( | ||
(date: Date) => format(date, 'YYYY-MM-DD'), | ||
[], | ||
) | ||
|
||
return ( | ||
<> | ||
<label> | ||
<input | ||
type="date" | ||
value={formatForInput(start)} | ||
onChange={createHandle(updateStart)} | ||
/> | ||
Start | ||
</label> | ||
|
||
<label> | ||
<input | ||
type="date" | ||
value={formatForInput(end)} | ||
onChange={createHandle(updateEnd)} | ||
/> | ||
End | ||
</label> | ||
</> | ||
) | ||
} |
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,7 +1,11 @@ | ||
import { head, omitBy } from 'lodash' | ||
import { head, omit } from 'lodash' | ||
|
||
export const omitFirst = (obj: object) => { | ||
const firstKey = head(Object.keys(obj)) | ||
|
||
return omitBy(obj, key => key === firstKey) | ||
if (!firstKey) { | ||
return obj | ||
} | ||
|
||
return omit(obj, [firstKey]) | ||
} |