@hypernetlabs/web-ui
Version:
Web ui react components for hypernet protocol
112 lines • 4.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGateways = void 0;
const contexts_1 = require("../contexts");
const react_1 = require("react");
const react_alert_1 = require("react-alert");
var EActionTypes;
(function (EActionTypes) {
EActionTypes["FETCHING"] = "FETCHING";
EActionTypes["FETCHED"] = "FETCHED";
EActionTypes["ERROR"] = "ERROR";
})(EActionTypes || (EActionTypes = {}));
function useGateways() {
const { coreProxy } = (0, contexts_1.useStoreContext)();
const alert = (0, react_alert_1.useAlert)();
const initialState = {
loading: true,
error: null,
gatewaysMap: new Map(),
openGatewayIFrame,
deauthorizeGateway,
authorizeGateway,
};
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, gatewaysMap: action.payload };
case EActionTypes.ERROR:
return { ...state, loading: false, error: action.payload };
default:
return state;
}
}, initialState);
(0, react_1.useEffect)(() => {
fetchData();
coreProxy.onGatewayAuthorized.subscribe({
next: (gatewayUrl) => {
const gatewaysMap = state.gatewaysMap;
gatewaysMap.set(gatewayUrl, true);
dispatch({
type: EActionTypes.FETCHED,
payload: gatewaysMap,
});
},
});
coreProxy.onAuthorizedGatewayActivationFailed.subscribe({
next: (gatewayUrl) => {
const gatewaysMap = state.gatewaysMap;
gatewaysMap.set(gatewayUrl, false);
dispatch({
type: EActionTypes.FETCHED,
payload: gatewaysMap,
});
},
});
}, []);
async function fetchData() {
dispatch({ type: EActionTypes.FETCHING });
coreProxy.payments
.getAuthorizedGatewaysConnectorsStatus()
.map((gatewaysStatusMap) => {
dispatch({
type: EActionTypes.FETCHED,
payload: gatewaysStatusMap,
});
})
.mapErr((error) => {
alert.error(error.message || "An error had happened while pulling gateway list");
dispatch({ type: EActionTypes.ERROR, payload: error.message });
});
}
function openGatewayIFrame(gatewayUrl) {
coreProxy.payments.displayGatewayIFrame(gatewayUrl).mapErr((error) => {
alert.error(error.message || "An error had happened while pulling gateway list");
dispatch({ type: EActionTypes.ERROR, payload: error.message });
});
}
function deauthorizeGateway(gatewayUrl) {
dispatch({ type: EActionTypes.FETCHING });
coreProxy.payments
.deauthorizeGateway(gatewayUrl)
.map(() => {
alert.success(`Gateway ${gatewayUrl} deauthorized successfully`);
fetchData();
})
.mapErr((error) => {
alert.error(error.message || "An error had happened while deauthorizing gateway");
dispatch({ type: EActionTypes.ERROR, payload: error.message });
});
}
function authorizeGateway(gatewayUrl, successCallback) {
dispatch({ type: EActionTypes.FETCHING });
coreProxy.payments
.authorizeGateway(gatewayUrl)
.map(() => {
alert.success(`Gateway ${gatewayUrl} authorized successfully`);
successCallback && successCallback();
fetchData();
})
.mapErr((error) => {
alert.error(error.message || "An error had happened while authorizing gateway");
dispatch({ type: EActionTypes.ERROR, payload: error.message });
});
}
return {
...state,
};
}
exports.useGateways = useGateways;
//# sourceMappingURL=useGateways.js.map