UNPKG

omnipay-savings-sdk

Version:

Omnipay Savings SDK

130 lines (129 loc) 6.34 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const react_native_1 = require("react-native"); const react_query_1 = require("@tanstack/react-query"); const utils_1 = require("../utils"); const savings_1 = __importDefault(require("../app/savings")); const suggestedPlans_1 = __importDefault(require("../app/suggestedPlans")); const savingsForm_1 = __importDefault(require("../app/savingsForm")); const existingSavings_1 = __importDefault(require("../app/existingSavings")); const history_1 = __importDefault(require("../app/history")); const allPlan_1 = __importDefault(require("../app/allPlan")); const plan_1 = __importDefault(require("../app/plan")); const SDKConfigContext_1 = require("../contexts/SDKConfigContext"); const actions_1 = require("../services/actions"); const Header_1 = __importDefault(require("../components/Header")); const AppStackNavigator = () => { const { apiKey, onClose, primaryColor } = (0, SDKConfigContext_1.useSDKConfig)(); const [screenStack, setScreenStack] = (0, react_1.useState)([]); // Use React Query to check for existing accounts const { data: accountSummary, isLoading, isError, } = (0, react_query_1.useQuery)({ queryKey: ['customerAccountSummary', apiKey], queryFn: () => (0, actions_1.getTotalAccountTransactionsByCustomerId)(apiKey), staleTime: 5 * 60 * 1000, retry: 2, }); // Set initial screen based on query results (0, react_1.useEffect)(() => { if (!isLoading) { // If user has existing accounts (success response), navigate to existingSavings // If no accounts (error response), navigate to savings if (accountSummary && !accountSummary.error && accountSummary.customerId) { setScreenStack([{ name: 'existingSavings' }]); } else { setScreenStack([{ name: 'savings', params: {} }]); } } }, [isLoading, accountSummary]); (0, react_1.useEffect)(() => { const onBackPress = () => { if (screenStack.length > 1) { goBack(); return true; } else if (onClose) { // If it's the initial screen and onClose is provided, call it onClose(); return true; } return false; }; const subscription = react_native_1.BackHandler.addEventListener('hardwareBackPress', onBackPress); return () => subscription.remove(); }, [screenStack, onClose]); const navigate = (name, params) => { setScreenStack([...screenStack, { name, params }]); }; const replace = (name, params) => { if (screenStack.length > 0) { // Replace the current screen with the new one const newStack = [...screenStack]; newStack[newStack.length - 1] = { name, params }; setScreenStack(newStack); } else { // If no screens in stack, just navigate setScreenStack([{ name, params }]); } }; const goBack = () => { if (screenStack.length > 1) { setScreenStack(screenStack.slice(0, -1)); } else if (onClose) { // If trying to go back from initial screen, call onClose onClose(); } }; // Show loading state during initialization if (isLoading || screenStack.length === 0) { return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(react_native_1.StatusBar, { backgroundColor: utils_1.Colors.white, barStyle: 'dark-content', animated: false }), (0, jsx_runtime_1.jsxs)(react_native_1.SafeAreaView, Object.assign({ style: [utils_1.globalStyles.appContainer] }, { children: [(0, jsx_runtime_1.jsx)(Header_1.default, { headerText: 'Savings', containerStyle: { paddingBottom: (0, utils_1.ms)(10), paddingTop: (0, utils_1.ms)(4), borderBottomWidth: (0, utils_1.ms)(1), borderBottomColor: utils_1.Colors.headerBottomColor, }, onPressBack: () => { goBack(); }, headerChild: (0, jsx_runtime_1.jsx)(react_native_1.View, { style: { flex: 0.125 } }) }), (0, jsx_runtime_1.jsx)(react_native_1.View, Object.assign({ style: { flex: 1, justifyContent: 'center', alignItems: 'center', } }, { children: (0, jsx_runtime_1.jsx)(react_native_1.ActivityIndicator, { size: "small", color: primaryColor }) }))] }))] })); } const { name: currentScreen, params } = screenStack[screenStack.length - 1]; const isInitialScreen = screenStack.length === 1; const screenProps = { navigate, replace, goBack, params, isInitialScreen, }; switch (currentScreen) { case 'savings': return (0, jsx_runtime_1.jsx)(savings_1.default, Object.assign({}, screenProps)); case 'suggestedPlans': return (0, jsx_runtime_1.jsx)(suggestedPlans_1.default, Object.assign({}, screenProps)); case 'savingsForm': return (0, jsx_runtime_1.jsx)(savingsForm_1.default, Object.assign({}, screenProps)); case 'existingSavings': return (0, jsx_runtime_1.jsx)(existingSavings_1.default, Object.assign({}, screenProps)); case 'history': return (0, jsx_runtime_1.jsx)(history_1.default, Object.assign({}, screenProps)); case 'allPlan': return (0, jsx_runtime_1.jsx)(allPlan_1.default, Object.assign({}, screenProps)); case 'plan': return (0, jsx_runtime_1.jsx)(plan_1.default, Object.assign({}, screenProps)); default: return (0, jsx_runtime_1.jsx)(savings_1.default, Object.assign({}, screenProps)); } }; exports.default = AppStackNavigator;