@vtex/faststore-plugin-buyer-portal 1.0.31 → 1.0.32

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vtex/faststore-plugin-buyer-portal",
3
- "version": "1.0.31",
3
+ "version": "1.0.32",
4
4
  "description": "A plugin for faststore with buyer portal",
5
5
  "main": "index.js",
6
6
  "dependencies": {
@@ -1,4 +1,4 @@
1
- import Client from "../../shared/clients/Client";
1
+ import { Client } from "../../shared/clients/Client";
2
2
  import { getApiUrl } from "../../shared/utils";
3
3
  import type { AddressData } from "../types";
4
4
 
@@ -1,4 +1,4 @@
1
- import Client from "../../shared/clients/Client";
1
+ import { Client } from "../../shared/clients/Client";
2
2
  import { getApiUrl } from "../../shared/utils";
3
3
  import type { ContractData } from "../types";
4
4
 
@@ -1,4 +1,4 @@
1
- import Client from "../../shared/clients/Client";
1
+ import { Client } from "../../shared/clients/Client";
2
2
  import { getApiUrl } from "../../shared/utils";
3
3
  import type {
4
4
  OrgUnitBasicData,
@@ -6,7 +6,7 @@ import type {
6
6
  OrgUnitData,
7
7
  } from "../types";
8
8
 
9
- class OrgUnitClient extends Client {
9
+ export class OrgUnitClient extends Client {
10
10
  constructor() {
11
11
  super(getApiUrl());
12
12
  }
@@ -138,5 +138,4 @@ class OrgUnitClient extends Client {
138
138
  }
139
139
 
140
140
  const orgUnitClient = new OrgUnitClient();
141
-
142
141
  export { orgUnitClient };
@@ -108,4 +108,4 @@ class Client {
108
108
  }
109
109
  }
110
110
 
111
- export default Client;
111
+ export { Client };
@@ -1,8 +1,10 @@
1
1
  import { createContext, type ReactNode } from "react";
2
2
  import type { ClientContext } from "../../utils";
3
+ import type { OrgUnitBasicData } from "../../../org-units/types";
3
4
 
4
5
  export type BuyerPortalContextType = {
5
6
  clientContext: ClientContext;
7
+ currentOrgUnit?: OrgUnitBasicData | null;
6
8
  };
7
9
 
8
10
  export const BuyerPortalContext = createContext<
@@ -11,15 +13,17 @@ export const BuyerPortalContext = createContext<
11
13
 
12
14
  export type BuyerPortalProviderProps = {
13
15
  clientContext: ClientContext;
16
+ currentOrgUnit?: OrgUnitBasicData | null;
14
17
  children: ReactNode;
15
18
  };
16
19
 
17
20
  export const BuyerPortalProvider = ({
18
21
  clientContext,
22
+ currentOrgUnit,
19
23
  children,
20
24
  }: BuyerPortalProviderProps) => {
21
25
  return (
22
- <BuyerPortalContext.Provider value={{ clientContext }}>
26
+ <BuyerPortalContext.Provider value={{ clientContext, currentOrgUnit }}>
23
27
  {children}
24
28
  </BuyerPortalContext.Provider>
25
29
  );
@@ -1,10 +1,10 @@
1
1
  import { useContext } from "react";
2
2
  import { type BuyerPortalContextType, BuyerPortalContext } from "../components";
3
3
 
4
- export const useBuyerPortal = (): BuyerPortalContextType["clientContext"] => {
4
+ export const useBuyerPortal = (): BuyerPortalContextType => {
5
5
  const context = useContext(BuyerPortalContext);
6
6
  if (!context) {
7
7
  throw new Error("useBuyerPortal must be used within a BuyerPortalProvider");
8
8
  }
9
- return context?.clientContext;
9
+ return context;
10
10
  };
@@ -23,7 +23,7 @@ export const useMutation = <TData, TVariables>(
23
23
  const [error, setError] = useState<Error | null>(null);
24
24
  const [isLoading, setIsLoading] = useState<boolean>(false);
25
25
 
26
- const clientContext = useBuyerPortal();
26
+ const { clientContext } = useBuyerPortal();
27
27
 
28
28
  const mutate = useCallback(
29
29
  async (variables: TVariables) => {
@@ -22,7 +22,7 @@ export const useQuery = <TData>(
22
22
  const [data, setData] = useState<TData | null>(null);
23
23
  const [error, setError] = useState<Error | null>(null);
24
24
  const [isLoading, setIsLoading] = useState<boolean>(true);
25
- const clientContext = useBuyerPortal();
25
+ const { clientContext } = useBuyerPortal();
26
26
 
27
27
  const fetchData = useCallback(async () => {
28
28
  setIsLoading(true);
@@ -1,8 +1,3 @@
1
- import {
2
- getOrgUnitBasicDataService,
3
- getOrgUnitByUserIdService,
4
- } from "../../org-units/services";
5
- import type { OrgUnitBasicData } from "../../org-units/types";
6
1
  import type { LoaderData } from "../types/LoaderData";
7
2
  import {
8
3
  getAuthCookie,
@@ -16,25 +11,13 @@ export type ClientContext = {
16
11
  cookie: string;
17
12
  customerId: string;
18
13
  userId: string;
19
- currentOrgUnit?: OrgUnitBasicData | null;
20
- userOrgUnit: {
21
- id: string;
22
- name: string;
23
- } | null;
24
14
  };
25
15
 
26
16
  export const getClientContext = async ({
27
17
  req,
28
- query,
29
18
  }: LoaderData): Promise<ClientContext> => {
30
19
  const cookie = getCookieAsString(req.cookies);
31
20
 
32
- const orgUnitId: string = query?.orgUnitId
33
- ? Array.isArray(query?.orgUnitId)
34
- ? query.orgUnitId[0]
35
- : query?.orgUnitId
36
- : null;
37
-
38
21
  const userId = getUserIdFromCookieServerSide({ req });
39
22
 
40
23
  return {
@@ -44,9 +27,5 @@ export const getClientContext = async ({
44
27
  cookie,
45
28
  customerId: getCustomerIdFromCookieServerSide({ req }),
46
29
  userId,
47
- currentOrgUnit: orgUnitId
48
- ? await getOrgUnitBasicDataService({ id: orgUnitId, cookie })
49
- : null,
50
- userOrgUnit: await getOrgUnitByUserIdService({ userId, cookie }),
51
30
  };
52
31
  };
@@ -13,32 +13,48 @@ import type { AddressSummaryData } from "../features/addresses/types";
13
13
  import { AddressLayout } from "../features/addresses/layouts";
14
14
  import { BuyerPortalProvider } from "../features/shared/components";
15
15
  import type { LoaderData } from "../features/shared/types";
16
+ import type { OrgUnitBasicData } from "../features/org-units/types";
17
+ import { getOrgUnitBasicDataService } from "../features/org-units/services";
16
18
 
17
19
  export type AddressesPageData = {
18
20
  data: AddressSummaryData[];
19
21
  clientContext: ClientContext;
22
+ currentOrgUnit: OrgUnitBasicData | null;
20
23
  };
21
24
 
22
25
  export type AddressesPageQuery = GetAddressesServiceProps & {
23
- orgUnitId?: string[];
26
+ orgUnitId: string;
24
27
  };
25
28
 
26
29
  export async function loader(
27
30
  data: LoaderData<AddressesPageQuery>
28
31
  ): Promise<AddressesPageData> {
29
- const { currentOrgUnit, ...clientContext } = await getClientContext(data);
32
+ const { cookie, ...clientContext } = await getClientContext(data);
33
+
34
+ const currentOrgUnit = await getOrgUnitBasicDataService({
35
+ id: data.query.orgUnitId,
36
+ cookie,
37
+ });
30
38
 
31
39
  return {
32
40
  data: await getAddressesService({
33
41
  customerId: getCustomerIdFromCookieServerSide(data),
34
42
  cookie: getCookieAsString(data?.req?.cookies),
35
43
  }),
36
- clientContext: { currentOrgUnit, ...clientContext },
44
+ clientContext: { cookie, ...clientContext },
45
+ currentOrgUnit,
37
46
  };
38
47
  }
39
48
 
40
- const AddressPage = ({ data, clientContext }: AddressesPageData) => (
41
- <BuyerPortalProvider clientContext={clientContext}>
49
+ const AddressPage = ({
50
+ data,
51
+ clientContext,
52
+ currentOrgUnit,
53
+ }: AddressesPageData) => (
54
+ <BuyerPortalProvider
55
+ clientContext={clientContext}
56
+ currentOrgUnit={currentOrgUnit}
57
+ >
42
58
  <AddressLayout data={data} />
43
59
  </BuyerPortalProvider>
44
60
  );
@@ -1,46 +1,55 @@
1
1
  import { type ClientContext, getClientContext } from "../features/shared/utils";
2
2
  import { ContractsLayout } from "../features/contracts/layouts";
3
- import {
4
- getContractsByCustomerIdService,
5
- getContractsByOrgUnitIdService,
6
- } from "../features/contracts/services";
3
+ import { getContractsByOrgUnitIdService } from "../features/contracts/services";
7
4
  import type { ContractData } from "../features/contracts/types";
8
5
  import { BuyerPortalProvider } from "../features/shared/components";
9
6
  import type { LoaderData } from "../features/shared/types";
7
+ import type { OrgUnitBasicData } from "../features/org-units/types";
8
+ import { getOrgUnitBasicDataService } from "../features/org-units/services";
10
9
 
11
10
  export type ContractsPageData = {
12
11
  data: ContractData[];
13
12
  clientContext: ClientContext;
13
+ currentOrgUnit: OrgUnitBasicData | null;
14
14
  };
15
15
 
16
16
  type ContractsPageQuery = {
17
- orgUnitId?: string[];
17
+ orgUnitId: string;
18
18
  };
19
19
 
20
20
  export async function loader(
21
21
  data: LoaderData<ContractsPageQuery>
22
22
  ): Promise<ContractsPageData> {
23
- const { customerId, cookie, currentOrgUnit, ...clientContext } =
24
- await getClientContext(data);
23
+ const { customerId, cookie, ...clientContext } = await getClientContext(data);
24
+
25
+ const currentOrgUnit = await getOrgUnitBasicDataService({
26
+ id: data.query.orgUnitId,
27
+ cookie,
28
+ });
25
29
 
26
30
  return {
27
- data: currentOrgUnit
28
- ? await getContractsByOrgUnitIdService({
29
- orgUnitId: currentOrgUnit.id,
30
- cookie,
31
- })
32
- : await getContractsByCustomerIdService({ customerId, cookie }),
31
+ data: await getContractsByOrgUnitIdService({
32
+ orgUnitId: currentOrgUnit?.id,
33
+ cookie,
34
+ }),
33
35
  clientContext: {
34
36
  customerId,
35
37
  cookie,
36
- currentOrgUnit: currentOrgUnit,
37
38
  ...clientContext,
38
39
  },
40
+ currentOrgUnit,
39
41
  };
40
42
  }
41
43
 
42
- const ContractsPage = ({ data, clientContext }: ContractsPageData) => (
43
- <BuyerPortalProvider clientContext={clientContext}>
44
+ const ContractsPage = ({
45
+ data,
46
+ clientContext,
47
+ currentOrgUnit,
48
+ }: ContractsPageData) => (
49
+ <BuyerPortalProvider
50
+ clientContext={clientContext}
51
+ currentOrgUnit={currentOrgUnit}
52
+ >
44
53
  <ContractsLayout data={data} />
45
54
  </BuyerPortalProvider>
46
55
  );
@@ -14,7 +14,9 @@ type HomePageQuery = {
14
14
  export async function loader(
15
15
  data: LoaderData<HomePageQuery>
16
16
  ): Promise<HomePageData> {
17
- const { userOrgUnit } = await getClientContext(data);
17
+ const { userId, cookie } = await getClientContext(data);
18
+
19
+ const userOrgUnit = await getOrgUnitByUserIdService({ userId, cookie });
18
20
 
19
21
  if (userOrgUnit) {
20
22
  data.res?.writeHead(302, { Location: `/org-unit/${userOrgUnit?.id}` });
@@ -1,7 +1,11 @@
1
1
  import { OrgUnitsLayout } from "../features/org-units/layouts";
2
- import type { OrgUnitData } from "../features/org-units/types";
2
+ import type {
3
+ OrgUnitBasicData,
4
+ OrgUnitData,
5
+ } from "../features/org-units/types";
3
6
  import {
4
7
  getChildrenOrgUnitsService,
8
+ getOrgUnitBasicDataService,
5
9
  getOrgUnitByIdService,
6
10
  searchOrgUnistByNameService,
7
11
  } from "../features/org-units/services";
@@ -13,6 +17,7 @@ export type OrgUnitsPageData = {
13
17
  data: { organizationalUnits: OrgUnitData[]; total: number };
14
18
  clientContext: ClientContext;
15
19
  search: string;
20
+ currentOrgUnit: OrgUnitBasicData | null;
16
21
  };
17
22
 
18
23
  type OrgUnitsPageQuery = {
@@ -26,6 +31,11 @@ export async function loader(
26
31
  const { cookie, customerId, userId, ...restClientContext } =
27
32
  await getClientContext(data);
28
33
 
34
+ const currentOrgUnit = await getOrgUnitBasicDataService({
35
+ id: data.query.orgUnitId,
36
+ cookie,
37
+ });
38
+
29
39
  const clientContext = {
30
40
  cookie,
31
41
  customerId,
@@ -40,6 +50,7 @@ export async function loader(
40
50
  data: await searchOrgUnistByNameService({ name: search }, cookie),
41
51
  clientContext,
42
52
  search,
53
+ currentOrgUnit,
43
54
  };
44
55
  }
45
56
 
@@ -62,6 +73,7 @@ export async function loader(
62
73
  },
63
74
  clientContext,
64
75
  search: "",
76
+ currentOrgUnit,
65
77
  };
66
78
  }
67
79
 
@@ -72,12 +84,21 @@ export async function loader(
72
84
  data: { organizationalUnits: [], total: 0 },
73
85
  clientContext,
74
86
  search: "",
87
+ currentOrgUnit,
75
88
  };
76
89
  }
77
90
 
78
- const OrgUnitsPage = ({ data, clientContext, search }: OrgUnitsPageData) => {
91
+ const OrgUnitsPage = ({
92
+ data,
93
+ clientContext,
94
+ search,
95
+ currentOrgUnit,
96
+ }: OrgUnitsPageData) => {
79
97
  return (
80
- <BuyerPortalProvider clientContext={clientContext}>
98
+ <BuyerPortalProvider
99
+ clientContext={clientContext}
100
+ currentOrgUnit={currentOrgUnit}
101
+ >
81
102
  <OrgUnitsLayout data={data} search={search} />
82
103
  </BuyerPortalProvider>
83
104
  );
@@ -4,6 +4,7 @@ import { type ClientContext, getClientContext } from "../features/shared/utils";
4
4
  import { UserDetailsLayout } from "../features/users/layouts";
5
5
  import {
6
6
  getUserDetailsService,
7
+ getUsersByOrgUnitIdService,
7
8
  getUsersService,
8
9
  } from "../features/users/services";
9
10
  import type { UserData } from "../features/users/types";
@@ -1,27 +1,29 @@
1
1
  import React from "react";
2
2
 
3
3
  import {
4
- getUsersService,
4
+ getUsersByOrgUnitIdService,
5
5
  type GetUsersServiceProps,
6
6
  } from "../features/users/services";
7
7
  import { type ClientContext, getClientContext } from "../features/shared/utils";
8
- import { getRootOrgUnitByCustomerIdService } from "../features/org-units/services";
9
- import type { OrgUnitData } from "../features/org-units/types";
8
+
10
9
  import type { UserData } from "../features/users/types";
11
10
  import { UsersLayout } from "../features/users/layouts";
12
11
  import { BuyerPortalProvider } from "../features/shared/components";
13
12
  import type { LoaderData } from "../features/shared/types";
13
+ import { getOrgUnitBasicDataService } from "../features/org-units/services";
14
+ import type { OrgUnitBasicData } from "../features/org-units/types";
14
15
 
15
16
  export type UsersPageData = {
16
17
  data: {
17
18
  users: UserData[];
18
- orgUnitsHierarchy: OrgUnitData[];
19
+ total: number;
19
20
  };
20
21
  clientContext: ClientContext;
22
+ currentOrgUnit: OrgUnitBasicData | null;
21
23
  };
22
24
 
23
25
  export type UsersPageQuery = GetUsersServiceProps & {
24
- orgUnitId?: string[];
26
+ orgUnitId: string;
25
27
  };
26
28
 
27
29
  export async function loader(
@@ -29,22 +31,28 @@ export async function loader(
29
31
  ): Promise<UsersPageData> {
30
32
  const { customerId, cookie, ...clientContext } = await getClientContext(data);
31
33
 
32
- const initialOrgUnits = await getRootOrgUnitByCustomerIdService(
33
- { customerId },
34
- cookie
35
- );
34
+ const { orgUnitId } = data.query;
35
+
36
+ const currentOrgUnit = await getOrgUnitBasicDataService({
37
+ id: orgUnitId,
38
+ cookie,
39
+ });
36
40
 
37
41
  return {
38
- data: {
39
- users: await getUsersService(data.query),
40
- orgUnitsHierarchy: initialOrgUnits.organizationalUnits,
41
- },
42
+ data: await getUsersByOrgUnitIdService({
43
+ cookie,
44
+ orgUnitId,
45
+ }),
42
46
  clientContext: { customerId, cookie, ...clientContext },
47
+ currentOrgUnit,
43
48
  };
44
49
  }
45
50
 
46
- const UsersPage = ({ data, clientContext }: UsersPageData) => (
47
- <BuyerPortalProvider clientContext={clientContext}>
51
+ const UsersPage = ({ data, clientContext, currentOrgUnit }: UsersPageData) => (
52
+ <BuyerPortalProvider
53
+ clientContext={clientContext}
54
+ currentOrgUnit={currentOrgUnit}
55
+ >
48
56
  <UsersLayout data={data} />
49
57
  </BuyerPortalProvider>
50
58
  );