@yoroi/exchange
Version:
The Exchange package of Yoroi SDK
84 lines (81 loc) • 2.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.exchangeReducer = exports.exchangeInitialExchangeContext = exports.exchangeDefaultState = exports.ExchangeActionType = void 0;
var _immer = require("immer");
var _manager = require("../../../manager.mocks");
const exchangeReducer = (state, action) => {
return (0, _immer.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`);
}
});
};
exports.exchangeReducer = exchangeReducer;
const exchangeDefaultState = (0, _immer.freeze)({
orderType: 'buy',
providerSuggestedByOrderType: {
buy: '',
sell: ''
},
amount: {
disabled: false,
error: undefined,
displayValue: '',
value: 0
},
canExchange: false,
providerId: ''
}, true);
exports.exchangeDefaultState = exchangeDefaultState;
let ExchangeActionType = /*#__PURE__*/function (ExchangeActionType) {
ExchangeActionType["OrderTypeChanged"] = "orderTypeChanged";
ExchangeActionType["AmountInputChanged"] = "amountInputChanged";
ExchangeActionType["ProviderIdChanged"] = "providerIdChanged";
return ExchangeActionType;
}({});
exports.ExchangeActionType = ExchangeActionType;
const exchangeInitialExchangeContext = (0, _immer.freeze)({
...exchangeDefaultState,
..._manager.errorManagerMock,
orderTypeChanged: missingInit,
amountInputChanged: missingInit,
providerIdChanged: missingInit
}, true);
/* istanbul ignore next */
exports.exchangeInitialExchangeContext = exchangeInitialExchangeContext;
function missingInit() {
console.error('[ExchangeContext] missing initialization');
}
//# sourceMappingURL=state.js.map