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

Commit

Permalink
feat(user reducer): add getProfile action
Browse files Browse the repository at this point in the history
  • Loading branch information
dmorenogogoleva committed Apr 10, 2019
1 parent 25f6a39 commit fda0b1a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 9 deletions.
6 changes: 6 additions & 0 deletions front/src/domain/user/actions/UserProfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Currency } from '@shared/enum/Currency'

export interface UserProfile {
name: string
defaultCurrency: Currency
}
13 changes: 13 additions & 0 deletions front/src/domain/user/actions/getUserProfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { fetchOrFail } from '@front/domain/store'

import { getUserProfileRequest } from '../api/getUserProfileRequest'
import { actions as userActions } from '../reducer/user'

const { getProfile } = userActions.data

export const getUserProfile = () => {
return fetchOrFail(userActions.fetching, async (dispatch, getApi) => {
const profile = await getUserProfileRequest(getApi())()
dispatch(getProfile(profile))
})
}
4 changes: 2 additions & 2 deletions front/src/domain/user/actions/setDefaultCurrency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { setCurrency } = userActions.data

export const setDefaultCurrency = (currency: Currency) => {
return fetchOrFail(userActions.fetching, async (dispatch, getApi) => {
const defaultCurrency = await setCurrencyRequest(getApi())(currency)
dispatch(setCurrency(defaultCurrency!))
await setCurrencyRequest(getApi())(currency)
dispatch(setCurrency(currency))
})
}
5 changes: 5 additions & 0 deletions front/src/domain/user/api/getUserProfileRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Api } from '@front/domain/api'
import { UserProfile } from '../actions/UserProfile'

export const getUserProfileRequest = (api: Api) => (): Promise<UserProfile> =>
api.client.get(`user/profile/`).then(response => response.data)
22 changes: 16 additions & 6 deletions front/src/domain/user/reducer/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@ import {
FetchingState,
} from 'redux-clear'
import { Currency } from '@shared/enum/Currency'
import { UserProfile } from '../actions/UserProfile'

interface State extends FetchingState {
defaultCurrency: Currency
profile: { defaultCurrency: Currency }
}

interface Actions {
setCurrency: ClearAction<[Currency]>
getProfile: ClearAction<[UserProfile]>
}

const { reducer, actions } = createClearReduxWithFetching<State, Actions>(
{
setCurrency: state => currency => ({
setCurrency: state => (defaultCurrency: Currency) => ({
...state,
profile: {
defaultCurrency,
},
}),
getProfile: state => (profile: UserProfile) => ({
...state,
currency,
profile,
}),
},
{
defaultCurrency: Currency.USD,
profile: {
defaultCurrency: Currency.USD,
},
error: Error as any,
loading: false,
},
'user-data',
)

export { reducer, actions, State }
export { State, reducer, actions }
3 changes: 2 additions & 1 deletion front/src/domain/user/selectors/getCurrency.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { State } from '@front/domain/store'

export const getCurrency = (state: State) => state.user.user.defaultCurrency
export const getCurrency = (state: State) =>
state.user.user.profile.defaultCurrency
6 changes: 6 additions & 0 deletions front/src/features/hello/Hello.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useCallback } from 'react'

import * as styles from './Hello.css'
import { setDefaultCurrency } from '@front/domain/user/actions/setDefaultCurrency'
import { getUserProfile } from '@front/domain/user/actions/getUserProfile'
import { Currency } from '@shared/enum/Currency'

export const Hello = () => {
Expand All @@ -15,11 +16,16 @@ export const Hello = () => {
await dispatch(setDefaultCurrency(currency))
}, [])

const getProfile = useCallback(async () => {
await dispatch(getUserProfile())
}, [])

return (
<section className={styles.container}>
<h1>
Checkmoney Space
<br />
<button onClick={() => getProfile()}>get your profile</button>
<button onClick={() => setCurrency(Currency.RUB)}>
set your currency
</button>
Expand Down

0 comments on commit fda0b1a

Please sign in to comment.