@vtex/faststore-plugin-buyer-portal 1.1.106 → 1.1.108

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.
Files changed (37) hide show
  1. package/package.json +1 -1
  2. package/src/features/custom-fields/layouts/CustomFieldsLayout/CustomFieldsLayout.tsx +1 -0
  3. package/src/features/shared/clients/Auth.ts +25 -0
  4. package/src/features/shared/clients/Client.ts +11 -1
  5. package/src/features/shared/components/withErrorBoundary/withErrorBoundary.tsx +7 -6
  6. package/src/features/shared/services/index.ts +4 -0
  7. package/src/features/shared/services/validate-access.service.ts +11 -0
  8. package/src/features/shared/types/AuthRouteProps.ts +5 -0
  9. package/src/features/shared/types/index.ts +1 -0
  10. package/src/features/shared/utils/constants.ts +4 -4
  11. package/src/features/shared/utils/index.ts +5 -0
  12. package/src/features/shared/utils/withAuth.tsx +31 -0
  13. package/src/features/shared/utils/withAuthLoader.ts +47 -0
  14. package/src/features/shared/utils/withAuthProvider.tsx +38 -0
  15. package/src/features/shared/utils/withBuyerPortal.tsx +24 -0
  16. package/src/features/shared/utils/withLoaderErrorBoundary.ts +79 -71
  17. package/src/features/shared/utils/withProviders.tsx +35 -0
  18. package/src/pages/address-details.tsx +95 -91
  19. package/src/pages/addresses.tsx +63 -63
  20. package/src/pages/budgets-details.tsx +44 -43
  21. package/src/pages/budgets.tsx +58 -56
  22. package/src/pages/buying-policies.tsx +84 -78
  23. package/src/pages/buying-policy-details.tsx +43 -44
  24. package/src/pages/collections.tsx +59 -60
  25. package/src/pages/cost-centers.tsx +48 -51
  26. package/src/pages/credit-cards.tsx +44 -49
  27. package/src/pages/home.tsx +28 -23
  28. package/src/pages/org-unit-details.tsx +39 -36
  29. package/src/pages/org-units.tsx +86 -90
  30. package/src/pages/payment-methods.tsx +41 -42
  31. package/src/pages/po-numbers.tsx +43 -46
  32. package/src/pages/profile.tsx +54 -52
  33. package/src/pages/releases.tsx +43 -41
  34. package/src/pages/role-details.tsx +57 -60
  35. package/src/pages/roles.tsx +42 -39
  36. package/src/pages/user-details.tsx +58 -58
  37. package/src/pages/users.tsx +83 -80
@@ -1,22 +1,20 @@
1
1
  import { getContractDetailsService } from "../features/contracts/services";
2
2
  import { CustomFieldsLayout } from "../features/custom-fields/layouts";
3
3
  import { getOrgUnitBasicDataService } from "../features/org-units/services";
4
- import {
5
- BuyerPortalProvider,
6
- withErrorBoundary,
7
- } from "../features/shared/components";
4
+ import { withErrorBoundary } from "../features/shared/components";
8
5
  import { ErrorBoundaryProps } from "../features/shared/components/ErrorBoundary/types";
9
6
  import { ErrorTabsLayout } from "../features/shared/layouts/ErrorTabsLayout/ErrorTabsLayout";
10
7
  import {
11
8
  ClientContext,
12
- getClientContext,
13
9
  withLoaderErrorBoundary,
10
+ withAuthLoader,
11
+ withProviders,
14
12
  } from "../features/shared/utils";
15
13
  import { getUserByIdService } from "../features/users/services";
16
14
 
17
15
  import type { ContractData } from "../features/contracts/types";
18
16
  import type { OrgUnitBasicData } from "../features/org-units/types";
19
- import type { LoaderData } from "../features/shared/types";
17
+ import type { AuthRouteProps, LoaderData } from "../features/shared/types";
20
18
  import type { UserData } from "../features/users/types";
21
19
 
22
20
  export type CostCentersPageData = {
@@ -40,41 +38,43 @@ type OrgUnitsPageQuery = {
40
38
 
41
39
  const loaderFunction = async (
42
40
  data: LoaderData<OrgUnitsPageQuery>
43
- ): Promise<CostCentersPageData> => {
44
- const { customerId, cookie, userId, ...clientContext } =
45
- await getClientContext(data);
46
-
41
+ ): Promise<AuthRouteProps<CostCentersPageData>> => {
47
42
  const { contractId, orgUnitId, search } = data.query;
48
43
 
49
- if (!contractId || !orgUnitId) {
50
- return {
51
- data: null,
52
- context: {
53
- clientContext: { cookie, userId, customerId, ...clientContext },
54
- currentContract: null,
55
- currentUser: null,
56
- currentOrgUnit: null,
57
- search: "",
58
- },
59
- };
60
- }
44
+ return withAuthLoader(
45
+ data,
46
+ async ({ customerId, cookie, userId, ...clientContext }) => {
47
+ if (!contractId || !orgUnitId) {
48
+ return {
49
+ data: null,
50
+ context: {
51
+ clientContext: { customerId, cookie, userId, ...clientContext },
52
+ currentContract: null,
53
+ currentUser: null,
54
+ currentOrgUnit: null,
55
+ search: "",
56
+ },
57
+ };
58
+ }
61
59
 
62
- const [orgUnit, contract, user] = await Promise.all([
63
- getOrgUnitBasicDataService({ id: orgUnitId, cookie }),
64
- getContractDetailsService({ contractId, unitId: orgUnitId, cookie }),
65
- getUserByIdService({ orgUnitId, userId, cookie }),
66
- ]);
60
+ const [orgUnit, contract, user] = await Promise.all([
61
+ getOrgUnitBasicDataService({ id: orgUnitId, cookie }),
62
+ getContractDetailsService({ contractId, unitId: orgUnitId, cookie }),
63
+ getUserByIdService({ orgUnitId, userId, cookie }),
64
+ ]);
67
65
 
68
- return {
69
- data: contract,
70
- context: {
71
- search: search ?? "",
72
- clientContext: { customerId, cookie, userId, ...clientContext },
73
- currentOrgUnit: orgUnit,
74
- currentContract: contract,
75
- currentUser: user,
76
- },
77
- };
66
+ return {
67
+ data: contract,
68
+ context: {
69
+ search: search ?? "",
70
+ clientContext: { customerId, cookie, userId, ...clientContext },
71
+ currentOrgUnit: orgUnit,
72
+ currentContract: contract,
73
+ currentUser: user,
74
+ },
75
+ };
76
+ }
77
+ );
78
78
  };
79
79
 
80
80
  export const loader = withLoaderErrorBoundary(loaderFunction, {
@@ -82,26 +82,23 @@ export const loader = withLoaderErrorBoundary(loaderFunction, {
82
82
  redirectToError: true,
83
83
  });
84
84
 
85
- const CostCentersPage = ({
86
- data,
87
- context,
88
- hasError,
89
- error,
90
- }: CostCentersPageData) => {
85
+ const CostCentersPage = ({ data, hasError, error }: CostCentersPageData) => {
91
86
  return (
92
- <BuyerPortalProvider {...context}>
87
+ <>
93
88
  {hasError ? (
94
89
  <ErrorTabsLayout error={error} />
95
90
  ) : (
96
91
  <CustomFieldsLayout data={data} customFieldsLabel="Cost Center" />
97
92
  )}
98
- </BuyerPortalProvider>
93
+ </>
99
94
  );
100
95
  };
101
96
 
102
- export default withErrorBoundary(CostCentersPage, {
103
- tags: {
104
- component: "CostCentersPage",
105
- errorType: "cost_centers_error",
106
- },
107
- });
97
+ export default withProviders(
98
+ withErrorBoundary(CostCentersPage, {
99
+ tags: {
100
+ component: "CostCentersPage",
101
+ errorType: "cost_centers_error",
102
+ },
103
+ })
104
+ );
@@ -3,22 +3,20 @@ import { CreditCardLayout } from "../features/credit-cards/layouts";
3
3
  import { getCreditCardsListService } from "../features/credit-cards/services";
4
4
  import { CreditCardData } from "../features/credit-cards/types";
5
5
  import { getOrgUnitBasicDataService } from "../features/org-units/services";
6
- import {
7
- BuyerPortalProvider,
8
- withErrorBoundary,
9
- } from "../features/shared/components";
6
+ import { withErrorBoundary } from "../features/shared/components";
10
7
  import { ErrorBoundaryProps } from "../features/shared/components/ErrorBoundary/types";
11
8
  import { ErrorTabsLayout } from "../features/shared/layouts/ErrorTabsLayout/ErrorTabsLayout";
12
9
  import {
13
10
  type ClientContext,
14
- getClientContext,
15
11
  withLoaderErrorBoundary,
12
+ withAuthLoader,
13
+ withProviders,
16
14
  } from "../features/shared/utils";
17
15
  import { getUserByIdService } from "../features/users/services";
18
16
 
19
17
  import type { ContractData } from "../features/contracts/types";
20
18
  import type { OrgUnitBasicData } from "../features/org-units/types";
21
- import type { LoaderData } from "../features/shared/types";
19
+ import type { AuthRouteProps, LoaderData } from "../features/shared/types";
22
20
  import type { UserData } from "../features/users/types";
23
21
 
24
22
  export type CreditCardPageData = {
@@ -40,38 +38,38 @@ export type CreditCardPageQuery = {
40
38
 
41
39
  const loaderFunction = async (
42
40
  data: LoaderData<CreditCardPageQuery>
43
- ): Promise<CreditCardPageData> => {
41
+ ): Promise<AuthRouteProps<CreditCardPageData>> => {
44
42
  const { contractId, orgUnitId } = data.query;
45
43
 
46
- const { cookie, userId, ...clientContext } = await getClientContext(data);
47
-
48
- const [currentOrgUnit, user, contract, creditCards] = await Promise.all([
49
- getOrgUnitBasicDataService({
50
- id: orgUnitId,
51
- cookie,
52
- }),
53
- getUserByIdService({ orgUnitId, userId, cookie }),
54
- getContractDetailsService({
55
- contractId,
56
- cookie,
57
- unitId: orgUnitId,
58
- }),
59
- getCreditCardsListService({
60
- cookie,
61
- unitId: orgUnitId,
62
- customerId: contractId,
63
- }),
64
- ]);
44
+ return withAuthLoader(data, async ({ cookie, userId, ...clientContext }) => {
45
+ const [currentOrgUnit, user, contract, creditCards] = await Promise.all([
46
+ getOrgUnitBasicDataService({
47
+ id: orgUnitId,
48
+ cookie,
49
+ }),
50
+ getUserByIdService({ orgUnitId, userId, cookie }),
51
+ getContractDetailsService({
52
+ contractId,
53
+ cookie,
54
+ unitId: orgUnitId,
55
+ }),
56
+ getCreditCardsListService({
57
+ cookie,
58
+ unitId: orgUnitId,
59
+ customerId: contractId,
60
+ }),
61
+ ]);
65
62
 
66
- return {
67
- data: creditCards,
68
- context: {
69
- clientContext: { cookie, userId, ...clientContext },
70
- currentOrgUnit,
71
- currentUser: user,
72
- currentContract: contract,
73
- },
74
- };
63
+ return {
64
+ data: creditCards,
65
+ context: {
66
+ clientContext: { cookie, userId, ...clientContext },
67
+ currentOrgUnit,
68
+ currentUser: user,
69
+ currentContract: contract,
70
+ },
71
+ };
72
+ });
75
73
  };
76
74
 
77
75
  export const loader = withLoaderErrorBoundary(loaderFunction, {
@@ -79,24 +77,21 @@ export const loader = withLoaderErrorBoundary(loaderFunction, {
79
77
  redirectToError: true,
80
78
  });
81
79
 
82
- const CreditCardsPage = ({
83
- data,
84
- context,
85
- hasError,
86
- error,
87
- }: CreditCardPageData) => (
88
- <BuyerPortalProvider {...context}>
80
+ const CreditCardsPage = ({ data, hasError, error }: CreditCardPageData) => (
81
+ <>
89
82
  {hasError ? (
90
83
  <ErrorTabsLayout error={error} />
91
84
  ) : (
92
85
  <CreditCardLayout data={data} />
93
86
  )}
94
- </BuyerPortalProvider>
87
+ </>
95
88
  );
96
89
 
97
- export default withErrorBoundary(CreditCardsPage, {
98
- tags: {
99
- component: "CreditCardsPage",
100
- errorType: "credit_cards_error",
101
- },
102
- });
90
+ export default withProviders(
91
+ withErrorBoundary(CreditCardsPage, {
92
+ tags: {
93
+ component: "CreditCardsPage",
94
+ errorType: "credit_cards_error",
95
+ },
96
+ })
97
+ );
@@ -4,12 +4,13 @@ import { getOrgUnitByUserIdService } from "../features/org-units/services";
4
4
  import { withErrorBoundary } from "../features/shared/components";
5
5
  import { ErrorBoundaryProps } from "../features/shared/components/ErrorBoundary/types";
6
6
  import {
7
- getClientContext,
8
7
  withLoaderErrorBoundary,
8
+ withAuthLoader,
9
+ withProviders,
9
10
  } from "../features/shared/utils";
10
11
  import { buyerPortalRoutes } from "../features/shared/utils/buyerPortalRoutes";
11
12
 
12
- import type { LoaderData } from "../features/shared/types";
13
+ import type { AuthRouteProps, LoaderData } from "../features/shared/types";
13
14
 
14
15
  export type HomePageData = {
15
16
  redirected: boolean;
@@ -23,28 +24,30 @@ type HomePageQuery = {
23
24
 
24
25
  const loaderFunction = async (
25
26
  data: LoaderData<HomePageQuery>
26
- ): Promise<HomePageData> => {
27
- const { userId, cookie } = await getClientContext(data);
27
+ ): Promise<AuthRouteProps<HomePageData>> => {
28
+ return withAuthLoader(data, async ({ userId, cookie }) => {
29
+ const userOrgUnit = await getOrgUnitByUserIdService({ userId, cookie });
28
30
 
29
- const userOrgUnit = await getOrgUnitByUserIdService({ userId, cookie });
31
+ if (userOrgUnit) {
32
+ data.res?.writeHead(302, {
33
+ Location: buyerPortalRoutes.orgUnitDetails({
34
+ orgUnitId: userOrgUnit.id,
35
+ }),
36
+ });
37
+ data.res?.end();
30
38
 
31
- if (userOrgUnit) {
32
- data.res?.writeHead(302, {
33
- Location: buyerPortalRoutes.orgUnitDetails({ orgUnitId: userOrgUnit.id }),
34
- });
39
+ return {
40
+ redirected: true,
41
+ };
42
+ }
43
+
44
+ data.res?.writeHead(302, { Location: "/" });
35
45
  data.res?.end();
36
46
 
37
47
  return {
38
48
  redirected: true,
39
49
  };
40
- }
41
-
42
- data.res?.writeHead(302, { Location: "/" });
43
- data.res?.end();
44
-
45
- return {
46
- redirected: true,
47
- };
50
+ });
48
51
  };
49
52
 
50
53
  export const loader = withLoaderErrorBoundary(loaderFunction, {
@@ -57,9 +60,11 @@ const HomePage = () => {
57
60
  return null;
58
61
  };
59
62
 
60
- export default withErrorBoundary(HomePage, {
61
- tags: {
62
- component: "HomePage",
63
- errorType: "home_error",
64
- },
65
- });
63
+ export default withProviders(
64
+ withErrorBoundary(HomePage, {
65
+ tags: {
66
+ component: "HomePage",
67
+ errorType: "home_error",
68
+ },
69
+ })
70
+ );
@@ -1,21 +1,19 @@
1
1
  import { getContractsByOrgUnitIdService } from "../features/contracts/services";
2
2
  import { OrgUnitsDetailsLayout } from "../features/org-units/layouts";
3
3
  import { getOrgUnitBasicDataService } from "../features/org-units/services";
4
- import {
5
- BuyerPortalProvider,
6
- withErrorBoundary,
7
- } from "../features/shared/components";
4
+ import { withErrorBoundary } from "../features/shared/components";
8
5
  import { ErrorTabsLayout } from "../features/shared/layouts/ErrorTabsLayout/ErrorTabsLayout";
9
6
  import {
10
7
  type ClientContext,
11
- getClientContext,
12
8
  withLoaderErrorBoundary,
9
+ withAuthLoader,
10
+ withProviders,
13
11
  } from "../features/shared/utils";
14
12
  import { getUserByIdService } from "../features/users/services";
15
13
 
16
14
  import type { ContractData } from "../features/contracts/types";
17
15
  import type { OrgUnitBasicData } from "../features/org-units/types";
18
- import type { LoaderData } from "../features/shared/types";
16
+ import type { AuthRouteProps, LoaderData } from "../features/shared/types";
19
17
  import type { UserData } from "../features/users/types";
20
18
 
21
19
  export type OrgUnitDetailsPageData = {
@@ -24,7 +22,9 @@ export type OrgUnitDetailsPageData = {
24
22
  contracts: ContractData[];
25
23
  user: UserData | null;
26
24
  };
27
- clientContext: ClientContext;
25
+ context: {
26
+ clientContext: ClientContext;
27
+ };
28
28
  hasError?: boolean;
29
29
  error?: {
30
30
  error: {
@@ -45,35 +45,37 @@ type OrgUnitDetailsPageQuery = {
45
45
 
46
46
  const loaderFunction = async (
47
47
  data: LoaderData<OrgUnitDetailsPageQuery>
48
- ): Promise<OrgUnitDetailsPageData> => {
49
- const { orgUnitId } = data.query ?? {};
48
+ ): Promise<AuthRouteProps<OrgUnitDetailsPageData>> => {
49
+ const { orgUnitId } = data.query;
50
50
 
51
51
  if (!orgUnitId) {
52
52
  throw new Error(`Missing required query param: orgUnitId=${orgUnitId}`);
53
53
  }
54
54
 
55
- const { cookie, userId, ...clientContext } = await getClientContext(data);
55
+ return withAuthLoader(data, async ({ cookie, userId, ...clientContext }) => {
56
+ const orgUnit = await getOrgUnitBasicDataService({
57
+ id: orgUnitId,
58
+ cookie,
59
+ });
56
60
 
57
- const orgUnit = await getOrgUnitBasicDataService({
58
- id: orgUnitId,
59
- cookie,
60
- });
61
+ const user = await getUserByIdService({ orgUnitId, userId, cookie });
61
62
 
62
- const user = await getUserByIdService({ orgUnitId, userId, cookie });
63
+ const contracts = await getContractsByOrgUnitIdService({
64
+ orgUnitId,
65
+ cookie,
66
+ });
63
67
 
64
- const contracts = await getContractsByOrgUnitIdService({
65
- orgUnitId,
66
- cookie,
68
+ return {
69
+ data: {
70
+ contracts,
71
+ orgUnit,
72
+ user,
73
+ },
74
+ context: {
75
+ clientContext: { cookie, userId, ...clientContext },
76
+ },
77
+ };
67
78
  });
68
-
69
- return {
70
- data: {
71
- contracts,
72
- orgUnit,
73
- user,
74
- },
75
- clientContext: { cookie, userId, ...clientContext },
76
- };
77
79
  };
78
80
 
79
81
  export const loader = withLoaderErrorBoundary(loaderFunction, {
@@ -83,24 +85,25 @@ export const loader = withLoaderErrorBoundary(loaderFunction, {
83
85
 
84
86
  const OrgUnitDetailsPage = ({
85
87
  data,
86
- clientContext,
87
88
  hasError,
88
89
  error,
89
90
  }: OrgUnitDetailsPageData) => {
90
91
  return (
91
- <BuyerPortalProvider clientContext={clientContext}>
92
+ <>
92
93
  {hasError ? (
93
94
  <ErrorTabsLayout error={error} />
94
95
  ) : (
95
96
  <OrgUnitsDetailsLayout data={data} />
96
97
  )}
97
- </BuyerPortalProvider>
98
+ </>
98
99
  );
99
100
  };
100
101
 
101
- export default withErrorBoundary(OrgUnitDetailsPage, {
102
- tags: {
103
- component: "OrgUnitDetailsPage",
104
- errorType: "org_unit_details_error",
105
- },
106
- });
102
+ export default withProviders(
103
+ withErrorBoundary(OrgUnitDetailsPage, {
104
+ tags: {
105
+ component: "OrgUnitDetailsPage",
106
+ errorType: "org_unit_details_error",
107
+ },
108
+ })
109
+ );