UNPKG

@etsoo/smarterp-core

Version:

TypeScript APIs for SmartERP Core

81 lines (79 loc) 4.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AppSwitchPopover = AppSwitchPopover; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const react_router_dom_1 = require("react-router-dom"); const materialui_1 = require("@etsoo/materialui"); const ICoreServiceApp_1 = require("../../ICoreServiceApp"); const appscript_1 = require("@etsoo/appscript"); const Typography_1 = __importDefault(require("@mui/material/Typography")); const Stack_1 = __importDefault(require("@mui/material/Stack")); const Button_1 = __importDefault(require("@mui/material/Button")); function AppSwitchPopover(props) { // Destruct const { appName } = props; // Route const navigate = (0, react_router_dom_1.useNavigate)(); // App const app = (0, ICoreServiceApp_1.useRequiredAppContext)(); // Labels const labels = app.getLabels("more", "switchApp"); // Max items to read const maxItems = 10; // Current app const currentApp = app.settings.appId; // Layout return ((0, jsx_runtime_1.jsx)(materialui_1.ButtonPopover, { button: (callback) => ((0, jsx_runtime_1.jsx)(Typography_1.default, { variant: "h6", sx: { color: (theme) => theme.palette.primary.main, fontWeight: "700", ml: 0.5, whiteSpace: "nowrap", cursor: "pointer" }, title: labels.switchApp, onClick: (e) => callback(e.currentTarget), children: appName })), loadData: () => app.core.appApi.getMy({ maxItems, identityType: appscript_1.IdentityType.User }, { showLoading: false }), position: "left", children: (data) => { if (data == null) return (0, jsx_runtime_1.jsx)(react_1.default.Fragment, {}); // Remove the current app const index = data.findIndex((a) => a.id === currentApp); if (index >= 0) data.splice(index, 1); return ((0, jsx_runtime_1.jsxs)(Stack_1.default, { direction: "column", margin: 2, children: [data.map((appData) => ((0, jsx_runtime_1.jsx)(Button_1.default, { onClick: async () => { /* // Method 1, get the login URL and redirect const tasks = appData.urls.map((u) => app.core.authApi.getLogInUrl( "APP", { showLoading: false, onError: () => false }, u.api ) ); const result = await Promise.allSettled(tasks); const success = result.find( (r) => r.status === "fulfilled" && r.value != null ) as PromiseFulfilledResult<string> | undefined; if (success) { app.clearSession(); app.loadUrlEx(success.value); } else { app.notifier.alert(app.get("networkFailure")); } */ // Method 2, get the RequestAuth, sign in and redirect const tasks = appData.urls.map((u) => app.core.authApi.getAuthRequest("APP", { showLoading: false, onError: () => false }, u.api)); const result = await Promise.allSettled(tasks); const success = result.find((r) => r.status === "fulfilled" && r.value != null); if (success) { const url = await app.core.authApi.authRequest(success.value); if (url) { app.clearSession(); app.loadUrlEx(url); return; } } app.notifier.alert(app.get("networkFailure")); }, children: app.core.getAppName(appData) }, appData.id))), (data.length === 0 || data.length === maxItems) && ((0, jsx_runtime_1.jsxs)(Button_1.default, { onClick: () => navigate("./app/my"), children: [labels.more, "..."] }))] })); } })); }