UNPKG

@yoroi/exchange

Version:

The Exchange package of Yoroi SDK

74 lines (72 loc) 2.32 kB
import { freeze, produce } from 'immer'; import { errorManagerMock } from '../../../manager.mocks'; export const exchangeReducer = (state, action) => { return produce(state, draft => { switch (action.type) { case ExchangeActionType.OrderTypeChanged: draft.orderType = action.orderType; // resets draft.amount = { ...exchangeDefaultState.amount }; draft.canExchange = exchangeDefaultState.canExchange; // when switching orderType it will replace the provider with the suggested one if (action.orderType === 'buy') { draft.providerId = state.providerSuggestedByOrderType.buy; break; } draft.providerId = state.providerSuggestedByOrderType.sell; break; case ExchangeActionType.AmountInputChanged: draft.amount = { ...action.amount }; draft.canExchange = action.canExchange; break; // not in use, but for now changing provider resets amount case ExchangeActionType.ProviderIdChanged: draft.providerId = action.providerId; //resets draft.canExchange = exchangeDefaultState.canExchange; draft.amount = { ...exchangeDefaultState.amount }; break; default: throw new Error(`ExchangeFormReducer invalid action`); } }); }; export const exchangeDefaultState = freeze({ orderType: 'buy', providerSuggestedByOrderType: { buy: '', sell: '' }, amount: { disabled: false, error: undefined, displayValue: '', value: 0 }, canExchange: false, providerId: '' }, true); export let ExchangeActionType = /*#__PURE__*/function (ExchangeActionType) { ExchangeActionType["OrderTypeChanged"] = "orderTypeChanged"; ExchangeActionType["AmountInputChanged"] = "amountInputChanged"; ExchangeActionType["ProviderIdChanged"] = "providerIdChanged"; return ExchangeActionType; }({}); export const exchangeInitialExchangeContext = freeze({ ...exchangeDefaultState, ...errorManagerMock, orderTypeChanged: missingInit, amountInputChanged: missingInit, providerIdChanged: missingInit }, true); /* istanbul ignore next */ function missingInit() { console.error('[ExchangeContext] missing initialization'); } //# sourceMappingURL=state.js.map