UNPKG

@hypernetlabs/web-ui

Version:
219 lines 8.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.usePayment = void 0; const objects_1 = require("@hypernetlabs/objects"); const contexts_1 = require("../contexts"); const objects_2 = require("../interfaces/objects"); const react_1 = require("react"); class PaymentTypeOption { constructor(typeName, type) { this.typeName = typeName; this.type = type; } } var EActionTypes; (function (EActionTypes) { EActionTypes["FETCHING"] = "FETCHING"; EActionTypes["FETCHED"] = "FETCHED"; EActionTypes["ERROR"] = "ERROR"; EActionTypes["TOKEN_SELECTED"] = "TOKEN_SELECTED"; EActionTypes["COUNTER_PARTY_CHANGED"] = "COUNTER_PARTY_CHANGED"; EActionTypes["AMOUNT_CHANGED"] = "AMOUNT_CHANGED"; EActionTypes["EXPIRATION_DATE_CHANGED"] = "EXPIRATION_DATE_CHANGED"; EActionTypes["REQUIRED_STACK_CHANGED"] = "REQUIRED_STACK_CHANGED"; EActionTypes["PAYMENT_TYPE_SELECTED"] = "PAYMENT_TYPE_SELECTED"; EActionTypes["SUCCESS"] = "SUCCESS"; })(EActionTypes || (EActionTypes = {})); // export function usePayment(): IReducerStateReducer { function usePayment(initialParams) { var _a, _b; const { coreProxy } = (0, contexts_1.useStoreContext)(); const initialState = { loading: true, error: false, resultMessage: new objects_2.ResultMessage(objects_2.EResultStatus.IDLE, ""), tokenSelectorOptions: (initialParams === null || initialParams === void 0 ? void 0 : initialParams.tokenSelectorOptions) || [], selectedPaymentToken: ((_b = (_a = initialParams === null || initialParams === void 0 ? void 0 : initialParams.tokenSelectorOptions) === null || _a === void 0 ? void 0 : _a.find((option) => (option === null || option === void 0 ? void 0 : option.address) === (initialParams === null || initialParams === void 0 ? void 0 : initialParams.paymentTokenAddress))) === null || _b === void 0 ? void 0 : _b.address) || undefined, counterPartyAccount: (initialParams === null || initialParams === void 0 ? void 0 : initialParams.counterPartyAccount) || "", amount: (initialParams === null || initialParams === void 0 ? void 0 : initialParams.amount) || "0", expirationDate: (initialParams === null || initialParams === void 0 ? void 0 : initialParams.expirationDate) || "", requiredStake: (initialParams === null || initialParams === void 0 ? void 0 : initialParams.requiredStake) || "0", gatewayUrl: (initialParams === null || initialParams === void 0 ? void 0 : initialParams.gatewayUrl) || "http://localhost:5010/", paymentType: (initialParams === null || initialParams === void 0 ? void 0 : initialParams.paymentType) || objects_1.EPaymentType.Push, paymentTypeOptions: [ new PaymentTypeOption("Push", objects_1.EPaymentType.Push), new PaymentTypeOption("Pull", objects_1.EPaymentType.Pull), ], }; const [state, dispatch] = (0, react_1.useReducer)((state, action) => { switch (action.type) { case EActionTypes.FETCHING: return { ...state, loading: true }; case EActionTypes.FETCHED: return { ...state, loading: false, tokenSelectorOptions: action.payload, }; case EActionTypes.ERROR: return { ...state, loading: false, error: true, resultMessage: new objects_2.ResultMessage(objects_2.EResultStatus.FAILURE, action.payload), }; case EActionTypes.SUCCESS: return { ...state, loading: false, error: false, resultMessage: new objects_2.ResultMessage(objects_2.EResultStatus.SUCCESS, action.payload), }; case EActionTypes.TOKEN_SELECTED: return { ...state, loading: false, selectedPaymentToken: action.payload, }; case EActionTypes.COUNTER_PARTY_CHANGED: return { ...state, loading: false, counterPartyAccount: action.payload, }; case EActionTypes.AMOUNT_CHANGED: return { ...state, loading: false, amount: action.payload }; case EActionTypes.EXPIRATION_DATE_CHANGED: return { ...state, loading: false, expirationDate: action.payload }; case EActionTypes.REQUIRED_STACK_CHANGED: return { ...state, loading: false, requiredStake: action.payload }; case EActionTypes.PAYMENT_TYPE_SELECTED: return { ...state, loading: false, paymentType: action.payload }; default: return { ...state }; } }, initialState); (0, react_1.useEffect)(() => { let cancelRequest = false; const fetchData = async () => { dispatch({ type: EActionTypes.FETCHING }); try { if (cancelRequest) return; // get data from coreProxy coreProxy === null || coreProxy === void 0 ? void 0 : coreProxy.payments.getBalances().map((balance) => { // prepare balances dispatch({ type: EActionTypes.FETCHED, payload: prepareTokenSelector(balance), }); }); } catch (error) { if (cancelRequest) return; dispatch({ type: EActionTypes.ERROR, payload: error.message, }); } }; fetchData(); coreProxy === null || coreProxy === void 0 ? void 0 : coreProxy.onBalancesChanged.subscribe({ next: (balance) => { if (cancelRequest) return; dispatch({ type: EActionTypes.FETCHED, payload: prepareTokenSelector(balance), }); }, }); return function cleanup() { cancelRequest = true; }; }, []); const prepareTokenSelector = (balance) => { return balance.assets.reduce((acc, assetBalance) => { acc.push(new objects_2.PaymentTokenOptionViewModel(assetBalance.assetAddress, assetBalance.assetAddress)); return acc; }, new Array()); }; const setSelectedPaymentToken = (param) => { dispatch({ type: EActionTypes.TOKEN_SELECTED, payload: param, }); }; const setCounterPartyAccount = (param) => { dispatch({ type: EActionTypes.COUNTER_PARTY_CHANGED, payload: param, }); }; const setAmount = (param) => { dispatch({ type: EActionTypes.AMOUNT_CHANGED, payload: param, }); }; const setExpirationDate = (param) => { dispatch({ type: EActionTypes.EXPIRATION_DATE_CHANGED, payload: param, }); }; const setRequiredStake = (param) => { dispatch({ type: EActionTypes.REQUIRED_STACK_CHANGED, payload: param, }); }; const setPaymentType = (param) => { dispatch({ type: EActionTypes.PAYMENT_TYPE_SELECTED, payload: param, }); }; // TODO: Determine if we can make this functionality work via the gateway // const sendFunds = () => { // coreProxy // .sendFunds( // state.counterPartyAccount, // BigNumberString(utils.parseUnits(state.amount, "wei").toString()), // state.expirationDate, // BigNumberString( // utils.parseUnits(state.requiredStake, "wei").toString(), // ), // state.selectedPaymentToken?.address, // state.gatewayUrl, // null, // ) // .match( // (balances) => { // dispatch({ // type: EActionTypes.SUCCESS, // payload: "your fund has succeeded", // }); // }, // (err) => { // console.log("err: ", err); // dispatch({ // type: EActionTypes.ERROR, // payload: err.message || "your fund has failed", // }); // }, // ); // }; return { ...state, setSelectedPaymentToken, setCounterPartyAccount, setAmount, setExpirationDate, setRequiredStake, setPaymentType, }; } exports.usePayment = usePayment; //# sourceMappingURL=usePayment.js.map