Skip to content
This repository has been archived by the owner on May 10, 2020. It is now read-only.

Commit

Permalink
Merge pull request #50 from igorkamyshev/prefetch
Browse files Browse the repository at this point in the history
Prefetch
  • Loading branch information
igorkamyshev authored Feb 20, 2019
2 parents f469731 + 60d896c commit 194b98d
Show file tree
Hide file tree
Showing 117 changed files with 200 additions and 188 deletions.
5 changes: 2 additions & 3 deletions front/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import { StoreContext } from 'redux-react-hook'
import { Option } from 'tsoption'

import { AppContext } from '@front/domain/AppContext'
import { WithReduxProps } from '@front/domain/store/WithReduxProps'
import { withReduxStore } from '@front/domain/store/withReduxStore'
import { WithReduxProps, withReduxStore } from '@front/domain/store'
import { actions } from '@front/domain/user/reducer/data'
import { getToken } from '@front/domain/user/selectors/getToken'
import { pushRoute } from '@front/pushRoute'
import { pushRoute } from '@front/features/routing'

class CheckmoneyWeb extends App<WithReduxProps> {
public static async getInitialProps(appContext: NextAppContext) {
Expand Down
2 changes: 1 addition & 1 deletion front/src/domain/AppContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IncomingMessage, OutgoingMessage } from 'http'

import { Store } from './store/Store'
import { Store } from './store'

interface Params {
[key: string]: string
Expand Down
2 changes: 2 additions & 0 deletions front/src/domain/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Api } from './Api'
export { createApi } from './createApi'
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { AnyAction, Dispatch } from 'redux'
import { AnyAction } from 'redux'
import { ThunkDispatch } from 'redux-thunk'
import { Option } from 'tsoption'

import { Api } from '@front/domain/api/Api'
import { Api } from '@front/domain/api'
import { ExtraArg, State } from '@front/domain/store'
import { getToken } from '@front/domain/user/selectors/getToken'

import { State } from '../State'

interface FetchActions {
request: () => AnyAction
success: () => AnyAction
failure: (error: string) => AnyAction
}

type Execute = (
dispatch: Dispatch,
dispatch: ThunkDispatch<State, ExtraArg, AnyAction>,
getApi: () => Api,
getState: () => State,
) => Promise<void | any>
Expand All @@ -28,7 +28,7 @@ export const fetchOrFail = (
fetchActions: FetchActions = defaultActions,
execute: Execute,
) => async (
dispatch: Dispatch<AnyAction>,
dispatch: ThunkDispatch<State, ExtraArg, AnyAction>,
getState: () => State,
createApi: (token: Option<string>) => Api,
) => {
Expand Down
3 changes: 3 additions & 0 deletions front/src/domain/fetching-redux/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { createFetchingStore } from './createFetchingRedux'
export { FetchingState } from './FetchingState'
export { fetchOrFail } from './fetchOrFail'
5 changes: 2 additions & 3 deletions front/src/domain/money/actions/createIncome.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { fetchOrFail } from '@front/domain/fetching-redux'
import { IncomeModel } from '@shared/models/money/IncomeModel'

import { fetchOrFail } from '@front/domain/store/fetchingRedux/fetchOrFail'

import { createIncomeRequest } from '../api/createIncomeRequest'
import { actions as incomeFetchingActions } from '../reducer/createIncomeFetching'
import { refetchData } from './refetchData'
Expand All @@ -10,5 +9,5 @@ export const createIncome = (incomeFields: IncomeModel) =>
fetchOrFail(incomeFetchingActions, async (dispatch, getApi) => {
await createIncomeRequest(getApi())(incomeFields)

await dispatch(refetchData() as any)
await dispatch(refetchData())
})
5 changes: 2 additions & 3 deletions front/src/domain/money/actions/createOutcome.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { fetchOrFail } from '@front/domain/fetching-redux'
import { OutcomeModel } from '@shared/models/money/OutcomeModel'

import { fetchOrFail } from '@front/domain/store/fetchingRedux/fetchOrFail'

import { createOutcomeRequest } from '../api/createOutcomeRequest'
import { actions as outcomeFetchingActions } from '../reducer/createOutcomeFetching'
import { refetchData } from './refetchData'
Expand All @@ -10,5 +9,5 @@ export const createOutcome = (outcomeFields: OutcomeModel) =>
fetchOrFail(outcomeFetchingActions, async (dispatch, getApi) => {
await createOutcomeRequest(getApi())(outcomeFields)

await dispatch(refetchData() as any)
await dispatch(refetchData())
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchOrFail } from '@front/domain/store/fetchingRedux/fetchOrFail'
import { fetchOrFail } from '@front/domain/fetching-redux'

import { fetchFirstTransactionDateRequest } from '../api/fetchFirstTransactionDateRequest'
import { actions as dataActions } from '../reducer/firstTransactionDate'
Expand Down
2 changes: 1 addition & 1 deletion front/src/domain/money/actions/fetchHistory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchOrFail } from '@front/domain/store/fetchingRedux/fetchOrFail'
import { fetchOrFail } from '@front/domain/fetching-redux'
import { GroupBy } from '@shared/enum/GroupBy'

import { fetchHistoryRequest } from '../api/fetchHistoryRequest'
Expand Down
2 changes: 1 addition & 1 deletion front/src/domain/money/actions/fetchStats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchOrFail } from '@front/domain/store/fetchingRedux/fetchOrFail'
import { fetchOrFail } from '@front/domain/fetching-redux'
import { Currency } from '@shared/enum/Currency'
import { GroupBy } from '@shared/enum/GroupBy'

Expand Down
2 changes: 1 addition & 1 deletion front/src/domain/money/actions/forceFetchHistory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchOrFail } from '@front/domain/store/fetchingRedux/fetchOrFail'
import { fetchOrFail } from '@front/domain/fetching-redux'
import { GroupBy } from '@shared/enum/GroupBy'

import { fetchHistoryRequest } from '../api/fetchHistoryRequest'
Expand Down
2 changes: 1 addition & 1 deletion front/src/domain/money/actions/forceFetchStats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchOrFail } from '@front/domain/store/fetchingRedux/fetchOrFail'
import { fetchOrFail } from '@front/domain/fetching-redux'
import { Currency } from '@shared/enum/Currency'
import { GroupBy } from '@shared/enum/GroupBy'

Expand Down
8 changes: 4 additions & 4 deletions front/src/domain/money/actions/refetchData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchOrFail } from '@front/domain/store/fetchingRedux/fetchOrFail'
import { fetchOrFail } from '@front/domain/fetching-redux'

import { getHistoryCachedPeriods } from '../selectors/getHistoryCachedPeriods'
import { getStatsCachedPeriods } from '../selectors/getStatsCachedPeriods'
Expand All @@ -12,12 +12,12 @@ export const refetchData = () =>
const statsCachedPeriods = getStatsCachedPeriods(getState())

await Promise.all([
dispatch(fetchFirstTransactionDate() as any),
dispatch(fetchFirstTransactionDate()),
...historyCachedPeriods.map(({ from, to, groupBy }) =>
dispatch(forceFetchHistory(from, to, groupBy) as any),
dispatch(forceFetchHistory(from, to, groupBy)),
),
...statsCachedPeriods.map(({ from, to, groupBy, currency }) =>
dispatch(forceFetchStats(from, to, groupBy, currency) as any),
dispatch(forceFetchStats(from, to, groupBy, currency)),
),
])
})
2 changes: 1 addition & 1 deletion front/src/domain/money/api/createIncomeRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IncomeModel } from '@shared/models/money/IncomeModel'

import { Api } from '@front/domain/api/Api'
import { Api } from '@front/domain/api'

export const createIncomeRequest = (api: Api) => (
income: IncomeModel,
Expand Down
2 changes: 1 addition & 1 deletion front/src/domain/money/api/createOutcomeRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OutcomeModel } from '@shared/models/money/OutcomeModel'

import { Api } from '@front/domain/api/Api'
import { Api } from '@front/domain/api'

export const createOutcomeRequest = (api: Api) => (
income: OutcomeModel,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Api } from '@front/domain/api/Api'
import { Api } from '@front/domain/api'
import { actualizeStore } from '@front/domain/store/utils/actualizeStore'

export const fetchFirstTransactionDateRequest = (api: Api) => (): Promise<
Expand Down
2 changes: 1 addition & 1 deletion front/src/domain/money/api/fetchHistoryRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Api } from '@front/domain/api/Api'
import { Api } from '@front/domain/api'
import { actualizeStore } from '@front/domain/store/utils/actualizeStore'
import { GroupBy } from '@shared/enum/GroupBy'
import { HistoryGroupModel } from '@shared/models/money/HistoryGroupModel'
Expand Down
2 changes: 1 addition & 1 deletion front/src/domain/money/api/fetchStatsRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Api } from '@front/domain/api/Api'
import { Api } from '@front/domain/api'
import { actualizeStore } from '@front/domain/store/utils/actualizeStore'
import { Currency } from '@shared/enum/Currency'
import { GroupBy } from '@shared/enum/GroupBy'
Expand Down
16 changes: 0 additions & 16 deletions front/src/domain/money/hooks/useCreateIncome.ts

This file was deleted.

16 changes: 0 additions & 16 deletions front/src/domain/money/hooks/useCreateOutcome.ts

This file was deleted.

6 changes: 4 additions & 2 deletions front/src/domain/money/reducer/createIncomeFetching.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createFetchingStore } from '@front/domain/store/fetchingRedux/createFetchingRedux'
import { FetchingState } from '@front/domain/store/fetchingRedux/FetchingState'
import {
createFetchingStore,
FetchingState,
} from '@front/domain/fetching-redux'

type State = FetchingState

Expand Down
6 changes: 4 additions & 2 deletions front/src/domain/money/reducer/createOutcomeFetching.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createFetchingStore } from '@front/domain/store/fetchingRedux/createFetchingRedux'
import { FetchingState } from '@front/domain/store/fetchingRedux/FetchingState'
import {
createFetchingStore,
FetchingState,
} from '@front/domain/fetching-redux'

type State = FetchingState

Expand Down
6 changes: 4 additions & 2 deletions front/src/domain/money/reducer/historyFetching.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createFetchingStore } from '@front/domain/store/fetchingRedux/createFetchingRedux'
import { FetchingState } from '@front/domain/store/fetchingRedux/FetchingState'
import {
createFetchingStore,
FetchingState,
} from '@front/domain/fetching-redux'

type State = FetchingState

Expand Down
6 changes: 4 additions & 2 deletions front/src/domain/money/reducer/statsFetching.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createFetchingStore } from '@front/domain/store/fetchingRedux/createFetchingRedux'
import { FetchingState } from '@front/domain/store/fetchingRedux/FetchingState'
import {
createFetchingStore,
FetchingState,
} from '@front/domain/fetching-redux'

type State = FetchingState

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { State } from '@front/domain/store/State'
import { State } from '@front/domain/store'

export const getCreateIncomeFetching = (state: State) =>
state.money.createIncomeFetching
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { State } from '@front/domain/store/State'
import { State } from '@front/domain/store'

export const getCreateOutcomeFetching = (state: State) =>
state.money.createOutcomeFetching
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { State } from '@front/domain/store/State'
import { State } from '@front/domain/store'

export const getFirstTransactionDate = (state: State) =>
state.money.firstTransactionDate
2 changes: 1 addition & 1 deletion front/src/domain/money/selectors/getHistory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Option } from 'tsoption'

import { State } from '@front/domain/store/State'
import { State } from '@front/domain/store'
import { GroupBy } from '@shared/enum/GroupBy'

import { createHistoryKey } from '../helpers/createHistoryKey'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { State } from '@front/domain/store/State'
import { State } from '@front/domain/store'

export const getHistoryCachedPeriods = (state: State) =>
state.money.history.cachedPeriods
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { State } from '@front/domain/store/State'
import { State } from '@front/domain/store'

export const getHistoryFetchingStatus = (state: State) =>
state.money.historyFetching
2 changes: 1 addition & 1 deletion front/src/domain/money/selectors/getStats.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Option } from 'tsoption'

import { State } from '@front/domain/store/State'
import { State } from '@front/domain/store'
import { Currency } from '@shared/enum/Currency'
import { GroupBy } from '@shared/enum/GroupBy'
import { createStatsKey } from '../helpers/createStatsKey'
Expand Down
2 changes: 1 addition & 1 deletion front/src/domain/money/selectors/getStatsCachedPeriods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { State } from '@front/domain/store/State'
import { State } from '@front/domain/store'

export const getStatsCachedPeriods = (state: State) =>
state.money.stats.cachedPeriods
2 changes: 1 addition & 1 deletion front/src/domain/money/selectors/getStatsFetchingStatus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { State } from '@front/domain/store/State'
import { State } from '@front/domain/store'

export const getStatsFetchingStatus = (state: State) =>
state.money.statsFetching
5 changes: 5 additions & 0 deletions front/src/domain/store/ExtraArg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Option } from 'tsoption'

import { Api } from '../api'

export type ExtraArg = (token: Option<string>) => Api
6 changes: 6 additions & 0 deletions front/src/domain/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { State } from './State'
export { Store } from './Store'
export { WithReduxProps } from './WithReduxProps'
export { withReduxStore } from './withReduxStore'
export { useThunk } from './useThunk'
export { ExtraArg } from './ExtraArg'
2 changes: 1 addition & 1 deletion front/src/domain/store/initializeStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { applyMiddleware, createStore } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunk from 'redux-thunk'

import { createApi } from '@front/domain/api/createApi'
import { createApi } from '@front/domain/api'

import { reducer } from './reducer'
import { State } from './State'
Expand Down
14 changes: 14 additions & 0 deletions front/src/domain/store/useThunk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AnyAction } from 'redux'
import { useDispatch } from 'redux-react-hook'
import { ThunkAction } from 'redux-thunk'

import { ExtraArg } from './ExtraArg'
import { State } from './State'

export const useThunk = () => {
const dispatch = useDispatch()

return <Result = Promise<void>>(
action: ThunkAction<Result, State, ExtraArg, AnyAction>,
) => dispatch(action as any)
}
4 changes: 2 additions & 2 deletions front/src/domain/user/actions/signIn.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { fetchOrFail } from '@front/domain/store/fetchingRedux/fetchOrFail'
import { fetchOrFail } from '@front/domain/fetching-redux'

import { signIn as signInRequest } from '../api/signIn'
import { setCookie } from '../helpers/setCookie'
import { actions as dataActions } from '../reducer/data'
import { actions as signInActions } from '../reducer/signIn'
import { setCookie } from '../setCookie'

const { setToken } = dataActions

Expand Down
4 changes: 2 additions & 2 deletions front/src/domain/user/actions/signUp.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { fetchOrFail } from '@front/domain/store/fetchingRedux/fetchOrFail'
import { fetchOrFail } from '@front/domain/fetching-redux'

import { signUp as signUpRequest } from '../api/signUp'
import { setCookie } from '../helpers/setCookie'
import { actions as dataActions } from '../reducer/data'
import { actions as signUpActions } from '../reducer/signUp'
import { setCookie } from '../setCookie'

const { setToken } = dataActions

Expand Down
2 changes: 1 addition & 1 deletion front/src/domain/user/api/signIn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TokenModel } from '@shared/models/user/TokenModel'

import { Api } from '@front/domain/api/Api'
import { Api } from '@front/domain/api'

export const signIn = (api: Api) => (
email: string,
Expand Down
2 changes: 1 addition & 1 deletion front/src/domain/user/api/signUp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TokenModel } from '@shared/models/user/TokenModel'

import { Api } from '@front/domain/api/Api'
import { Api } from '@front/domain/api'

export const signUp = (api: Api) => (
email: string,
Expand Down
Loading

0 comments on commit 194b98d

Please sign in to comment.