Skip to content

Commit

Permalink
feat(simple buy): set up fetch orders
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip London committed Mar 21, 2020
1 parent ea201a6 commit d0df5a7
Show file tree
Hide file tree
Showing 20 changed files with 223 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export const FETCH_SB_FIAT_ELIGIBLE_LOADING =
export const FETCH_SB_FIAT_ELIGIBLE_SUCCESS =
'@EVENT.FETCH_SB_FIAT_ELIGIBLE_SUCCESS'

export const FETCH_SB_ORDERS = '@EVENT.FETCH_SB_ORDERS'
export const FETCH_SB_ORDERS_FAILURE = '@EVENT.FETCH_SB_ORDERS_FAILURE'
export const FETCH_SB_ORDERS_LOADING = '@EVENT.FETCH_SB_ORDERS_LOADING'
export const FETCH_SB_ORDERS_SUCCESS = '@EVENT.FETCH_SB_ORDERS_SUCCESS'

export const FETCH_SB_PAIRS = '@EVENT.FETCH_SB_PAIRS'
export const FETCH_SB_PAIRS_FAILURE = '@EVENT.FETCH_SB_PAIRS_FAILURE'
export const FETCH_SB_PAIRS_LOADING = '@EVENT.FETCH_SB_PAIRS_LOADING'
Expand Down Expand Up @@ -37,3 +42,5 @@ export const HANDLE_SB_SUGGESTED_AMOUNT_CLICK =
export const INITIALIZE_CHECKOUT = '@EVENT.INITIALIZE_SB_CHECKOUT'

export const SET_STEP = '@EVENT.SET_SB_STEP'

export const SHOW_MODAL = '@EVENT.SHOW_SB_MODAL'
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as AT from './actionTypes'
import {
CurrenciesType,
FiatCurrenciesType,
FiatEligibleType,
SBAccountType,
SBOrderType,
Expand All @@ -17,7 +18,7 @@ export const destroyCheckout = () => ({
type: AT.DESTROY_CHECKOUT
})

export const fetchSBFiatEligible = (currency: keyof CurrenciesType) => ({
export const fetchSBFiatEligible = (currency: keyof FiatCurrenciesType) => ({
type: AT.FETCH_SB_FIAT_ELIGIBLE,
currency
})
Expand All @@ -44,7 +45,32 @@ export const fetchSBFiatEligibleSuccess = (
}
})

export const fetchSBPairs = (currency: keyof CurrenciesType) => ({
export const fetchSBOrders = (currency: keyof FiatCurrenciesType) => ({
type: AT.FETCH_SB_ORDERS,
currency
})

export const fetchSBOrdersFailure = (error: string): SimpleBuyActionTypes => ({
type: AT.FETCH_SB_ORDERS_FAILURE,
payload: {
error
}
})

export const fetchSBOrdersLoading = (): SimpleBuyActionTypes => ({
type: AT.FETCH_SB_ORDERS_LOADING
})

export const fetchSBOrdersSuccess = (
orders: Array<SBOrderType>
): SimpleBuyActionTypes => ({
type: AT.FETCH_SB_ORDERS_SUCCESS,
payload: {
orders
}
})

export const fetchSBPairs = (currency: keyof FiatCurrenciesType) => ({
type: AT.FETCH_SB_PAIRS,
currency
})
Expand Down Expand Up @@ -138,7 +164,7 @@ export const setStep = (
payload:
| { step: 'CURRENCY_SELECTION' }
| { order: SBOrderType; step: 'ORDER_DETAILS' }
| { fiatCurrency: keyof CurrenciesType; step: 'ENTER_AMOUNT' }
| { fiatCurrency: keyof FiatCurrenciesType; step: 'ENTER_AMOUNT' }
): SimpleBuyActionTypes => ({
type: AT.SET_STEP,
payload:
Expand All @@ -153,3 +179,7 @@ export const setStep = (
step: payload.step
}
})

export const showModal = (): SimpleBuyActionTypes => ({
type: AT.SHOW_MODAL
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Remote from 'blockchain-wallet-v4/src/remote/remote'
const INITIAL_STATE: SimpleBuyState = {
account: Remote.NotAsked,
order: undefined,
orders: Remote.NotAsked,
fiatCurrency: undefined,
fiatEligible: Remote.NotAsked,
pairs: Remote.NotAsked,
Expand Down Expand Up @@ -41,6 +42,22 @@ export function simpleBuyReducer (
...state,
fiatEligible: Remote.Success(action.payload.fiatEligible)
}
case AT.FETCH_SB_ORDERS_FAILURE: {
return {
...state,
orders: Remote.Failure(action.payload.error)
}
}
case AT.FETCH_SB_ORDERS_LOADING:
return {
...state,
orders: Remote.Loading
}
case AT.FETCH_SB_ORDERS_SUCCESS:
return {
...state,
orders: Remote.Success(action.payload.orders)
}
case AT.FETCH_SB_PAIRS_FAILURE: {
return {
...state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ export default ({ api, coreSagas, networks }) => {
AT.FETCH_SB_FIAT_ELIGIBLE,
simpleBuySagas.fetchSBFiatEligible
)
yield takeLatest(AT.FETCH_SB_ORDERS, simpleBuySagas.fetchSBOrders)
yield takeLatest(AT.FETCH_SB_PAIRS, simpleBuySagas.fetchSBPairs)
yield takeLatest(
AT.FETCH_SB_PAYMENT_ACCOUNT,
simpleBuySagas.fetchSBPaymentAccount
)
yield takeLatest(AT.FETCH_SB_PAIRS, simpleBuySagas.fetchSBPairs)
yield takeLatest(
AT.HANDLE_SB_SUGGESTED_AMOUNT_CLICK,
simpleBuySagas.handleSBSuggestedAmountClick
)
yield takeLatest(AT.INITIALIZE_CHECKOUT, simpleBuySagas.initializeCheckout)
yield takeLatest(AT.SHOW_MODAL, simpleBuySagas.showModal)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ export default ({
}
}

const fetchSBOrders = function * () {
try {
yield put(A.fetchSBOrdersLoading())
const { pairs } = yield call(api.getSBOrders, {})
yield put(A.fetchSBOrdersSuccess(pairs))
} catch (e) {
const error = errorHandler(e)
yield put(A.fetchSBOrdersFailure(error))
}
}

const fetchSBPairs = function * ({
currency
}: ReturnType<typeof A.fetchSBPairs>) {
Expand Down Expand Up @@ -114,8 +125,11 @@ export default ({
try {
yield call(createUser)
yield call(waitForUserData)

const fiatCurrency = S.getFiatCurrency(yield select())
if (!fiatCurrency) throw new Error('NO_FIAT_CURRENCY')
yield put(actions.preferences.setSBFiatCurrency(fiatCurrency))

yield put(A.fetchSBSuggestedAmountsLoading())
const amounts = yield call(api.getSBSuggestedAmounts, fiatCurrency)
yield put(A.fetchSBSuggestedAmountsSuccess(amounts))
Expand All @@ -128,12 +142,25 @@ export default ({
}
}

const showModal = function * () {
yield put(actions.modals.showModal('SIMPLE_BUY_MODAL'))
const fiatCurrency = selectors.preferences.getSBFiatCurrency(yield select())

if (!fiatCurrency) {
yield put(A.setStep({ step: 'CURRENCY_SELECTION' }))
} else {
yield put(A.setStep({ step: 'ENTER_AMOUNT', fiatCurrency }))
}
}

return {
createSBOrder,
fetchSBOrders,
fetchSBPairs,
fetchSBPaymentAccount,
fetchSBFiatEligible,
handleSBSuggestedAmountClick,
initializeCheckout
initializeCheckout,
showModal
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as AT from './actionTypes'
import {
CurrenciesType,
FiatCurrenciesType,
FiatEligibleType,
RemoteDataType,
SBAccountType,
Expand All @@ -26,9 +27,10 @@ export enum SimpleBuyStepType {
// State
export type SimpleBuyState = {
account: RemoteDataType<string, SBAccountType>
fiatCurrency: undefined | keyof CurrenciesType
fiatCurrency: undefined | keyof FiatCurrenciesType
fiatEligible: RemoteDataType<string, FiatEligibleType>
order: undefined | SBOrderType
orders: RemoteDataType<string, Array<SBOrderType>>
pairs: RemoteDataType<string, Array<SBPairType>>
step: keyof typeof SimpleBuyStepType
suggestedAmounts: RemoteDataType<Error | string, SBSuggestedAmountType>
Expand Down Expand Up @@ -62,6 +64,26 @@ interface FetchSBFiatEligibleSuccess {
}
type: typeof AT.FETCH_SB_FIAT_ELIGIBLE_SUCCESS
}
interface FetchSBOrders {
type: typeof AT.FETCH_SB_ORDERS
}
interface FetchSBOrdersFailure {
payload: {
error: string
}
type: typeof AT.FETCH_SB_ORDERS_FAILURE
}

interface FetchSBOrdersLoading {
type: typeof AT.FETCH_SB_ORDERS_LOADING
}

interface FetchSBOrdersSuccess {
payload: {
orders: Array<SBOrderType>
}
type: typeof AT.FETCH_SB_ORDERS_SUCCESS
}
interface FetchSBPairs {
payload: {
currency: keyof CurrenciesType
Expand Down Expand Up @@ -137,7 +159,7 @@ interface FetchSBSuggestedAmountsSuccess {
interface SetStepAction {
payload:
| {
fiatCurrency: keyof CurrenciesType
fiatCurrency: keyof FiatCurrenciesType
step: 'ENTER_AMOUNT'
}
| {
Expand All @@ -149,13 +171,21 @@ interface SetStepAction {
}
type: typeof AT.SET_STEP
}
interface ShowModalAction {
type: typeof AT.SHOW_MODAL
}

export type SimpleBuyActionTypes =
| DestroyCheckout
| FetchSBFiatEligible
| FetchSBFiatEligibleFailure
| FetchSBFiatEligibleLoading
| FetchSBFiatEligibleSuccess
| FetchSBOrders
| FetchSBOrdersFailure
| FetchSBOrdersLoading
| FetchSBOrdersSuccess
| FetchSBPaymentAccount
| FetchSBPairs
| FetchSBPairsFailure
| FetchSBPairsLoading
Expand All @@ -169,3 +199,4 @@ export type SimpleBuyActionTypes =
| FetchSBSuggestedAmountsLoading
| FetchSBSuggestedAmountsSuccess
| SetStepAction
| ShowModalAction
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const SET_CULTURE = '@DATA.PREFERENCES.SET_CULTURE'
export const SET_LANGUAGE = '@DATA.PREFERENCES.SET_LANGUAGE'
export const SET_THEME = '@DATA.PREFERENCES.SET_THEME'
export const SET_SB_FIAT_CURRENCY = '@DATA.PREFERENCES.SET_SB_FIAT_CURRENCY'
export const TOGGLE_COIN_DISPLAY = '@DATA.PREFERENCES.TOGGLE_COIN_DISPLAY'
export const HIDE_KYC_COMPLETED = '@DATA.PREFERENCES.HIDE_KYC_COMPLETED'
export const SET_TOTAL_BALANCES_DROPDOWN =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as AT from './actionTypes'
import { FiatCurrenciesType } from 'core/types'

export const setCulture = culture => ({
type: AT.SET_CULTURE,
Expand All @@ -8,6 +9,10 @@ export const setLanguage = (language, showAlert) => ({
type: AT.SET_LANGUAGE,
payload: { language, showAlert }
})
export const setSBFiatCurrency = (currency: keyof FiatCurrenciesType) => ({
type: AT.SET_SB_FIAT_CURRENCY,
payload: { currency }
})
export const setTheme = theme => ({ type: AT.SET_THEME, payload: { theme } })
export const toggleCoinDisplayed = () => ({ type: AT.TOGGLE_COIN_DISPLAY })
export const hideKycCompleted = () => ({ type: AT.HIDE_KYC_COMPLETED })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as AT from './actionTypes'
import * as priceChartActionTypes from '../components/priceChart/actionTypes'
import { assoc, assocPath } from 'ramda'
import { PreferencesState } from './types'

const INITIAL_STATE = {
const INITIAL_STATE: PreferencesState = {
language: 'en',
culture: 'en-GB',
theme: 'default',
sbFiatCurrency: undefined,
coinDisplayed: true,
showKycCompleted: true,
showBackupReminder: true,
Expand All @@ -23,19 +25,21 @@ const INITIAL_STATE = {
}
}

const preferences = (state = INITIAL_STATE, action) => {
const { type, payload } = action
switch (type) {
export function preferencesReducer (
state = INITIAL_STATE,
action
): PreferencesState {
switch (action.type) {
case AT.SET_LANGUAGE: {
const { language } = payload
const { language } = action.payload
return assoc('language', language, state)
}
case AT.SET_CULTURE: {
const { culture } = payload
const { culture } = action.payload
return assoc('culture', culture, state)
}
case AT.SET_THEME: {
const { theme } = payload
const { theme } = action.payload
return assoc('theme', theme, state)
}
case AT.TOGGLE_COIN_DISPLAY: {
Expand All @@ -48,15 +52,28 @@ const preferences = (state = INITIAL_STATE, action) => {
return assoc('showLockboxSoftwareDownload', false, state)
}
case AT.SET_TOTAL_BALANCES_DROPDOWN: {
const { key, val } = payload
return assocPath(['totalBalancesDropdown', key], val, state)
const { key, val } = action.payload
return {
...state,
totalBalancesDropdown: {
...state.totalBalancesDropdown,
[key]: val
}
}
}
case AT.SET_SB_FIAT_CURRENCY: {
const { currency } = action.payload
return {
...state,
sbFiatCurrency: currency
}
}
case priceChartActionTypes.PRICE_CHART_COIN_CLICKED: {
const { coin } = payload
const { coin } = action.payload
return assocPath(['priceChart', 'coin'], coin, state)
}
case priceChartActionTypes.PRICE_CHART_TIME_CLICKED: {
const { time } = payload
const { time } = action.payload
return assocPath(['priceChart', 'time'], time, state)
}
case AT.HIDE_AIRDROP_CLAIM_MODAL: {
Expand All @@ -78,5 +95,3 @@ const preferences = (state = INITIAL_STATE, action) => {
return state
}
}

export default preferences
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { path } from 'ramda'
import { RootState } from 'data/rootReducer'

export const getCulture = path(['preferences', 'culture'])
export const getLanguage = path(['preferences', 'language'])
Expand All @@ -10,6 +11,8 @@ export const getTotalBalancesDropdown = path([
'preferences',
'totalBalancesDropdown'
])
export const getSBFiatCurrency = (state: RootState) =>
state.preferences.sbFiatCurrency
export const getShowKycGetStarted = path(['preferences', 'showKycGetStarted'])
export const getShowSwapBanner = path(['preferences', 'showSwapBanner'])
export const getShowSwapUpgrade = path(['preferences', 'showSwapUpgradeModal'])
Expand Down

0 comments on commit d0df5a7

Please sign in to comment.