@oondemand/oon-core-front 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1095,7 +1095,7 @@ interface AuthContextValue {
1095
1095
  status: AuthStatus;
1096
1096
  isAuthenticated: boolean;
1097
1097
  hasPermission: (permission?: string | string[]) => boolean;
1098
- login: (email: string, password: string) => Promise<void>;
1098
+ login: (email: string, password: string, tenantId?: string) => Promise<void>;
1099
1099
  completeFirstAccess: (password: string) => Promise<void>;
1100
1100
  retry: () => Promise<void>;
1101
1101
  logout: () => void;
package/dist/index.js CHANGED
@@ -693,14 +693,20 @@ function AuthProvider({ http, storage, auth, loginUrl, children }) {
693
693
  }
694
694
  }, [validateSession, applyValidationError]);
695
695
  const login = useCallback2(
696
- async (email, password) => {
696
+ async (email, password, tenantId) => {
697
697
  setStatus("loading");
698
698
  setUser(null);
699
699
  try {
700
+ const normalizedTenantId = String(tenantId || "").trim();
700
701
  const response = await http.post(
701
702
  "/auth/autenticar",
702
703
  {},
703
- { headers: { Authorization: `Basic ${encodeBasicCredentials(email, password)}` } }
704
+ {
705
+ headers: {
706
+ Authorization: `Basic ${encodeBasicCredentials(email, password)}`,
707
+ ...normalizedTenantId ? { "x-oon-tenant-id": normalizedTenantId } : {}
708
+ }
709
+ }
704
710
  );
705
711
  if (!response.data.token) {
706
712
  const error = { code: "AUTH_TOKEN_MISSING", message: "Token n\xE3o retornado pelo backend.", status: 502 };
@@ -709,7 +715,11 @@ function AuthProvider({ http, storage, auth, loginUrl, children }) {
709
715
  storage.set(response.data.token);
710
716
  await validateSession();
711
717
  } catch (error) {
712
- applyValidationError(error);
718
+ if (error?.code === "TENANT_SELECTION_REQUIRED") {
719
+ setStatus("unauthenticated");
720
+ } else {
721
+ applyValidationError(error);
722
+ }
713
723
  throw error;
714
724
  }
715
725
  },
@@ -2799,6 +2809,7 @@ function CoreRelatedGrid({
2799
2809
  onRefresh
2800
2810
  }) {
2801
2811
  const tab = rawTab;
2812
+ const { http } = useOonApi();
2802
2813
  const relation = relationFrom(tab.relation, relations);
2803
2814
  const schemaQuery = useModelSchema(relation?.model ?? "");
2804
2815
  const schema = schemaQuery.data;
@@ -2852,11 +2863,21 @@ function CoreRelatedGrid({
2852
2863
  onError: setEditError
2853
2864
  });
2854
2865
  const createMutation = useMutation({
2855
- mutationFn: (values) => resource.create({
2856
- ...createInitialValues,
2857
- ...values,
2858
- ...relation && parentValue != null ? { [relation.foreignKey]: parentValue } : {}
2859
- }),
2866
+ mutationFn: (values) => {
2867
+ const payload = {
2868
+ ...createInitialValues,
2869
+ ...values,
2870
+ ...relation && parentValue != null ? { [relation.foreignKey]: parentValue } : {}
2871
+ };
2872
+ if (tab.create?.endpoint) {
2873
+ return http.request({
2874
+ url: interpolateEndpoint(tab.create.endpoint, payload, parent),
2875
+ method: tab.create.method ?? "POST",
2876
+ data: payload
2877
+ }).then((response) => response.data);
2878
+ }
2879
+ return resource.create(payload);
2880
+ },
2860
2881
  onSuccess: async () => {
2861
2882
  await invalidate();
2862
2883
  setCreating(false);
@@ -4918,7 +4939,7 @@ function Sidebar({ app, menu, registry }) {
4918
4939
  // package.json
4919
4940
  var package_default = {
4920
4941
  name: "@oondemand/oon-core-front",
4921
- version: "0.4.2",
4942
+ version: "0.4.3",
4922
4943
  repository: {
4923
4944
  type: "git",
4924
4945
  url: "git+https://github.com/oondemand/oon-platform.git",
@@ -5379,6 +5400,17 @@ import {
5379
5400
  } from "@chakra-ui/react";
5380
5401
  import { Navigate as Navigate2, useLocation as useLocation3 } from "react-router-dom";
5381
5402
  import { jsx as jsx35, jsxs as jsxs24 } from "react/jsx-runtime";
5403
+ function tenantOptions(error) {
5404
+ const details = error.details;
5405
+ if (!details || typeof details !== "object" || Array.isArray(details)) return [];
5406
+ const tenants = details.tenants;
5407
+ if (!Array.isArray(tenants)) return [];
5408
+ return tenants.filter((tenant) => Boolean(tenant) && typeof tenant === "object").map((tenant) => ({
5409
+ id: String(tenant.id || ""),
5410
+ nome: String(tenant.nome || tenant.slug || tenant.id || ""),
5411
+ slug: tenant.slug ? String(tenant.slug) : void 0
5412
+ })).filter((tenant) => tenant.id && tenant.nome);
5413
+ }
5382
5414
  function safeRedirect(search) {
5383
5415
  const value2 = new URLSearchParams(search).get("redirect") || "/";
5384
5416
  if (!value2.startsWith("/") || value2.startsWith("//")) return "/";
@@ -5394,6 +5426,8 @@ function LoginPage({ app }) {
5394
5426
  const [password, setPassword] = useState16("");
5395
5427
  const [submitting, setSubmitting] = useState16(false);
5396
5428
  const [message, setMessage] = useState16(null);
5429
+ const [tenants, setTenants] = useState16([]);
5430
+ const [tenantId, setTenantId] = useState16("");
5397
5431
  const redirect = safeRedirect(location.search);
5398
5432
  if (status === "authenticated") {
5399
5433
  return /* @__PURE__ */ jsx35(Navigate2, { to: redirect, replace: true });
@@ -5406,9 +5440,22 @@ function LoginPage({ app }) {
5406
5440
  setSubmitting(true);
5407
5441
  setMessage(null);
5408
5442
  try {
5409
- await login(email.trim(), password);
5443
+ if (tenants.length > 1 && !tenantId) {
5444
+ setMessage("Selecione a organiza\xE7\xE3o para continuar.");
5445
+ return;
5446
+ }
5447
+ await login(email.trim(), password, tenantId || void 0);
5410
5448
  } catch (error) {
5411
5449
  const authError = error;
5450
+ if (authError.code === "TENANT_SELECTION_REQUIRED") {
5451
+ const options = tenantOptions(authError);
5452
+ if (options.length > 1) {
5453
+ setTenants(options);
5454
+ setTenantId("");
5455
+ setMessage("Seu usu\xE1rio possui acesso a mais de uma organiza\xE7\xE3o. Selecione uma para continuar.");
5456
+ return;
5457
+ }
5458
+ }
5412
5459
  if (authError.status === 403) return;
5413
5460
  setMessage(
5414
5461
  authError.status === 401 ? "E-mail ou senha inv\xE1lidos." : authError.message || "N\xE3o foi poss\xEDvel entrar. Tente novamente."
@@ -5452,7 +5499,11 @@ function LoginPage({ app }) {
5452
5499
  {
5453
5500
  type: "email",
5454
5501
  value: email,
5455
- onChange: (event) => setEmail(event.target.value),
5502
+ onChange: (event) => {
5503
+ setEmail(event.target.value);
5504
+ setTenants([]);
5505
+ setTenantId("");
5506
+ },
5456
5507
  autoComplete: "email",
5457
5508
  placeholder: "voce@empresa.com.br",
5458
5509
  required: true
@@ -5472,7 +5523,38 @@ function LoginPage({ app }) {
5472
5523
  required: true
5473
5524
  }
5474
5525
  )
5475
- ] })
5526
+ ] }),
5527
+ tenants.length > 1 ? /* @__PURE__ */ jsxs24(Box23, { children: [
5528
+ /* @__PURE__ */ jsx35(Text24, { mb: 2, fontSize: "sm", fontWeight: "600", color: "#24323A", children: "Organiza\xE7\xE3o" }),
5529
+ /* @__PURE__ */ jsxs24(
5530
+ "select",
5531
+ {
5532
+ "aria-label": "Organiza\xE7\xE3o",
5533
+ value: tenantId,
5534
+ onChange: (event) => {
5535
+ setTenantId(event.target.value);
5536
+ setMessage(null);
5537
+ },
5538
+ required: true,
5539
+ style: {
5540
+ width: "100%",
5541
+ minHeight: "40px",
5542
+ padding: "0 12px",
5543
+ border: "1px solid #CDD5DF",
5544
+ borderRadius: "6px",
5545
+ background: "white",
5546
+ color: "#24323A"
5547
+ },
5548
+ children: [
5549
+ /* @__PURE__ */ jsx35("option", { value: "", children: "Selecione uma organiza\xE7\xE3o" }),
5550
+ tenants.map((tenant) => /* @__PURE__ */ jsxs24("option", { value: tenant.id, children: [
5551
+ tenant.nome,
5552
+ tenant.slug ? ` \u2014 ${tenant.slug}` : ""
5553
+ ] }, tenant.id))
5554
+ ]
5555
+ }
5556
+ )
5557
+ ] }) : null
5476
5558
  ] }),
5477
5559
  message ? /* @__PURE__ */ jsx35(
5478
5560
  Box23,
@@ -5494,7 +5576,7 @@ function LoginPage({ app }) {
5494
5576
  bg: "#07262E",
5495
5577
  color: "white",
5496
5578
  _hover: { bg: "#0B3843" },
5497
- disabled: submitting || !email.trim() || !password,
5579
+ disabled: submitting || !email.trim() || !password || tenants.length > 1 && !tenantId,
5498
5580
  children: submitting ? "Validando acesso..." : "Entrar"
5499
5581
  }
5500
5582
  )