@etsoo/smarterp-core
Version:
TypeScript APIs for SmartERP Core
46 lines (45 loc) • 2.54 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React from "react";
import AccountTreeIcon from "@mui/icons-material/AccountTree";
import { useNavigate } from "react-router-dom";
import { ButtonPopover } from "@etsoo/materialui";
import { useRequiredAppContext } from "../../ICoreServiceApp";
import ButtonGroup from "@mui/material/ButtonGroup";
import Button from "@mui/material/Button";
import Stack from "@mui/material/Stack";
export function OrgSwitchPopover(props) {
// Destruct
const { appId, organizationName } = props;
// Route
const navigate = useNavigate();
// App
const app = useRequiredAppContext();
// Labels
const labels = app.getLabels("currentOrg", "more", "switchOrg");
// Refs
const anchorRef = React.useRef(null);
// Max items to read
const maxItems = 10;
// Current organization
const currentOrg = app.userData?.organization;
// Layout
return (_jsx(ButtonPopover, { button: (callback) => (_jsx(React.Fragment, { children: _jsxs(ButtonGroup, { variant: "text", children: [_jsx(Button, { sx: { display: { xs: "none", md: "block" } }, title: labels.currentOrg, onClick: () => callback(anchorRef.current), children: organizationName ?? labels.switchOrg }), _jsx(Button, { title: labels.switchOrg, onClick: (e) => callback(e.currentTarget), ref: anchorRef, children: _jsx(AccountTreeIcon, {}) })] }) })), loadData: () => app.core.orgApi.getMy({ appId, maxItems }, { showLoading: false }), children: (data) => {
if (data == null)
return _jsx(React.Fragment, {});
// Remove the current organization
if (currentOrg != null) {
const index = data.findIndex((org) => org.id === currentOrg);
if (index >= 0)
data.splice(index, 1);
}
return (_jsxs(Stack, { direction: "column", margin: 2, children: [data.map((org) => (_jsx(Button, { onClick: async () => {
const result = await app.switchOrg(org.id);
if (result == null)
return;
if (!result.ok) {
app.alertResult(result);
return;
}
}, children: org.name }, org.id))), (data.length === 0 || data.length === maxItems) && (_jsxs(Button, { onClick: () => navigate("./org/my"), children: [labels.more, "..."] }))] }));
} }));
}