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: replace profile by default currency
- Loading branch information
1 parent
ba2a868
commit 943205e
Showing
33 changed files
with
396 additions
and
129 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,39 @@ | ||
import getConfig from 'next/config'; | ||
import { select } from 'redux-saga/effects'; | ||
import { select, put } from 'redux-saga/effects'; | ||
|
||
import { StatisticsApi } from '&front/app/api/parts/StatisticsApi'; | ||
import { getTokenValue } from '&front/domain/user/selectors/getToken'; | ||
import { actions as authAction } from '&front/app/auth/auth.actions'; | ||
|
||
import { ProfileApi } from './parts/ProfileApi'; | ||
|
||
const { publicRuntimeConfig } = getConfig(); | ||
const { statsUrl } = publicRuntimeConfig; | ||
const { statsUrl, backUrl } = publicRuntimeConfig; | ||
|
||
export function* createStatisticsApi() { | ||
const token: ReturnType<typeof getTokenValue> = yield select(getTokenValue); | ||
|
||
if (!token) { | ||
// TODO: specicif | ||
yield put(authAction.unauthorized()); | ||
throw new Error('Try to use private API for anon user'); | ||
} | ||
|
||
const apiClient = new StatisticsApi(statsUrl, token); | ||
|
||
return apiClient; | ||
} | ||
|
||
export function* createProfileApi() { | ||
const token: ReturnType<typeof getTokenValue> = yield select(getTokenValue); | ||
|
||
if (!token) { | ||
yield put(authAction.unauthorized()); | ||
throw new Error('Try to use private API for anon user'); | ||
} | ||
|
||
const profileUrl = `${backUrl}/det-bell`; | ||
|
||
const apiClient = new ProfileApi(profileUrl, token); | ||
|
||
return apiClient; | ||
} |
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,26 @@ | ||
import Axios, { AxiosInstance } from 'axios'; | ||
|
||
import { Currency } from '&shared/enum/Currency'; | ||
|
||
import { addTokenToHttpConfig } from '../api.utils'; | ||
|
||
export class ProfileApi { | ||
private readonly http: AxiosInstance; | ||
|
||
constructor(url: string, private readonly token: string) { | ||
const serviceUrl = url; | ||
|
||
this.http = Axios.create({ | ||
baseURL: serviceUrl, | ||
}); | ||
} | ||
|
||
getCurrency = async (): Promise<Currency> => { | ||
const { data } = await this.http.get( | ||
`v1/default-currency`, | ||
addTokenToHttpConfig(this.token, {}), | ||
); | ||
|
||
return data; | ||
}; | ||
} |
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,7 @@ | ||
import actionCreatorFactory from 'typescript-fsa'; | ||
|
||
const actionCreator = actionCreatorFactory('AUTH'); | ||
|
||
export const actions = { | ||
unauthorized: actionCreator('UNAUTHORIZED'), | ||
}; |
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 { takeLatest, put } from 'redux-saga/effects'; | ||
|
||
import { actions } from './auth.actions'; | ||
import { actions as routerActions } from '../router/router.actions'; | ||
|
||
export function* handleLogoutSaga() { | ||
yield takeLatest( | ||
actions.unauthorized.type, | ||
function* handleUnauthorizedAction() { | ||
yield put(routerActions.push('/')); | ||
}, | ||
); | ||
} |
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,13 @@ | ||
import actionCreatorFactory from 'typescript-fsa'; | ||
|
||
import { Currency } from '&shared/enum/Currency'; | ||
|
||
const actionCreator = actionCreatorFactory('DEFAULT_CURRENCY'); | ||
|
||
export const actions = actionCreator.async< | ||
{ | ||
attempt?: number; | ||
}, | ||
Currency, | ||
string | ||
>('FETCHING'); |
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,26 @@ | ||
import { reducerWithInitialState } from 'typescript-fsa-reducers'; | ||
|
||
import { Currency } from '&shared/enum/Currency'; | ||
|
||
import { actions } from './default_currency.actions'; | ||
|
||
type StateData = { value?: Currency }; | ||
|
||
export interface DefaultCurrencyState extends StateData { | ||
error: string | null; | ||
} | ||
|
||
const initialState: DefaultCurrencyState = { | ||
error: null, | ||
}; | ||
|
||
export const defaultCurrencyReducer = reducerWithInitialState(initialState) | ||
.case(actions.done, (state, { result }) => ({ | ||
...state, | ||
value: result, | ||
error: null, | ||
})) | ||
.case(actions.failed, (state, { error }) => ({ | ||
...state, | ||
error, | ||
})); |
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,38 @@ | ||
import { takeLatest, put, call, delay } from 'redux-saga/effects'; | ||
|
||
import { createProfileApi } from '&front/app/api/api.sagas'; | ||
import { logError } from '&front/app/utility/errors.sagas'; | ||
import { Currency } from '&shared/enum/Currency'; | ||
|
||
import { actions } from './default_currency.actions'; | ||
import { ProfileApi } from '../api/parts/ProfileApi'; | ||
|
||
const ATTEMPT_THRESHOLD = 3; | ||
const RETRY_DELAY = 100; | ||
|
||
export function* handleDefaultCurrencyFetchingSaga() { | ||
yield takeLatest(actions.started.type, function* ( | ||
action: ReturnType<typeof actions.started>, | ||
) { | ||
const { attempt = 0 } = action.payload; | ||
|
||
try { | ||
const apiClient: ProfileApi = yield createProfileApi(); | ||
const data: Currency = yield call(apiClient.getCurrency); | ||
yield put(actions.done({ result: data, params: action.payload })); | ||
} catch (error) { | ||
yield logError(error); | ||
|
||
if (attempt >= ATTEMPT_THRESHOLD) { | ||
yield put( | ||
actions.failed({ params: action.payload, error: 'Unknown error' }), | ||
); | ||
return; | ||
} | ||
|
||
// Ok, lets retry | ||
yield delay(RETRY_DELAY * attempt); | ||
yield put(actions.started({ attempt: attempt + 1 })); | ||
} | ||
}); | ||
} |
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 { State } from '&front/domain/store/State'; | ||
|
||
export const selectDefaultCurrency = (state: State) => | ||
state.application.profile.defaultCurrency.value; | ||
|
||
export const selectDefaultCurrencyIsAvailable = (state: State) => | ||
Boolean(selectDefaultCurrency(state)); |
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 actionCreatorFactory from 'typescript-fsa'; | ||
|
||
const actionCreator = actionCreatorFactory('ROUTER'); | ||
|
||
export const actions = { | ||
push: actionCreator<string>('PUSH'), | ||
}; |
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 { takeLatest } from 'redux-saga/effects'; | ||
|
||
import { pushRoute } from '&front/features/routing'; | ||
|
||
import { actions } from './router.actions'; | ||
|
||
export function* handleRouterPushSaga() { | ||
// eslint-disable-next-line require-yield | ||
yield takeLatest(actions.push.type, function* ( | ||
action: ReturnType<typeof actions.push>, | ||
) { | ||
pushRoute(action.payload); | ||
}); | ||
} |
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,9 @@ | ||
import actionCreatorFactory from 'typescript-fsa'; | ||
|
||
import { Currency } from '&shared/enum/Currency'; | ||
|
||
const actionCreator = actionCreatorFactory('STATISTICS_META'); | ||
|
||
export const actions = { | ||
setCurrency: actionCreator<Currency>('SET_CURRENCY'), | ||
}; |
Oops, something went wrong.