@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
@@ -4,10 +4,7 @@ import { getAddressLocationService } from "../features/addresses/services/locati
4
4
  import { getAddressRecipientsService } from "../features/addresses/services/recipients/get-address-recipients.service";
5
5
  import { getContractDetailsService } from "../features/contracts/services";
6
6
  import { getOrgUnitBasicDataService } from "../features/org-units/services";
7
- import {
8
- BuyerPortalProvider,
9
- withErrorBoundary,
10
- } from "../features/shared/components";
7
+ import { withErrorBoundary } from "../features/shared/components";
11
8
  import { ErrorBoundaryProps } from "../features/shared/components/ErrorBoundary/types";
12
9
  import {
13
10
  PAGE_PARAMS,
@@ -16,9 +13,10 @@ import {
16
13
  import { ErrorTabsLayout } from "../features/shared/layouts/ErrorTabsLayout/ErrorTabsLayout";
17
14
  import {
18
15
  type ClientContext,
19
- getClientContext,
20
16
  getValidPage,
21
17
  withLoaderErrorBoundary,
18
+ withAuthLoader,
19
+ withProviders,
22
20
  } from "../features/shared/utils";
23
21
  import { getUserByIdService } from "../features/users/services";
24
22
 
@@ -29,7 +27,7 @@ import type {
29
27
  } from "../features/addresses/types/AddressData";
30
28
  import type { ContractData } from "../features/contracts/types";
31
29
  import type { OrgUnitBasicData } from "../features/org-units/types";
32
- import type { LoaderData } from "../features/shared/types";
30
+ import type { AuthRouteProps, LoaderData } from "../features/shared/types";
33
31
  import type { UserData } from "../features/users/types";
34
32
 
35
33
  const DEFAULT_ADDRESS_PAGINATED_DATA = {
@@ -69,7 +67,7 @@ export type AddressDetailsPageData = {
69
67
 
70
68
  const loaderFunction = async (
71
69
  data: LoaderData<AddressDetailsPageQuery>
72
- ): Promise<AddressDetailsPageData> => {
70
+ ): Promise<AuthRouteProps<AddressDetailsPageData>> => {
73
71
  const {
74
72
  contractId,
75
73
  orgUnitId,
@@ -82,83 +80,88 @@ const loaderFunction = async (
82
80
  const pageRecipients = getValidPage(pageRecipientsString);
83
81
  const pageLocation = getValidPage(pageLocationString);
84
82
 
85
- const { customerId, cookie, userId, ...clientContext } =
86
- await getClientContext(data);
87
-
88
- if (!contractId || !orgUnitId) {
89
- return {
90
- data: {
91
- addressDetails: null,
92
- recipientsData: null,
93
- locationsData: null,
94
- },
95
- context: {
96
- clientContext: { customerId, cookie, userId, ...clientContext },
97
- currentOrgUnit: null,
98
- currentContract: null,
99
- currentUser: null,
100
- },
101
- };
102
- }
103
-
104
- const orgUnit = await getOrgUnitBasicDataService({ cookie, id: orgUnitId });
105
-
106
- const contract = await getContractDetailsService({
107
- contractId,
108
- cookie,
109
- unitId: orgUnitId,
110
- });
111
-
112
- const user = await getUserByIdService({ orgUnitId, userId, cookie });
113
-
114
- const { data: recipients = [], total = 0 } =
115
- await getAddressRecipientsService({
116
- unitId: orgUnitId,
117
- customerId: contractId,
118
- addressId,
119
- cookie,
120
- search: searchRecipients,
121
- page: pageRecipients,
122
- });
123
-
124
- const { data: locations, total: totalLocations } =
125
- await getAddressLocationService({
126
- contractId,
127
- addressId,
128
- cookie,
129
- unitId: orgUnitId,
130
- search: searchLocations,
131
- page: pageLocation,
132
- });
133
-
134
- return {
135
- data: {
136
- addressDetails: await getAddressDetailsService(
137
- addressId,
83
+ return withAuthLoader(
84
+ data,
85
+ async ({ customerId, cookie, userId, ...clientContext }) => {
86
+ if (!contractId || !orgUnitId) {
87
+ return {
88
+ data: {
89
+ addressDetails: null,
90
+ recipientsData: null,
91
+ locationsData: null,
92
+ },
93
+ context: {
94
+ clientContext: { customerId, cookie, userId, ...clientContext },
95
+ currentOrgUnit: null,
96
+ currentContract: null,
97
+ currentUser: null,
98
+ },
99
+ };
100
+ }
101
+
102
+ const orgUnit = await getOrgUnitBasicDataService({
138
103
  cookie,
104
+ id: orgUnitId,
105
+ });
106
+
107
+ const contract = await getContractDetailsService({
139
108
  contractId,
140
- orgUnitId
141
- ),
142
- recipientsData: {
143
- data: recipients,
144
- total,
145
- search: searchRecipients ?? "",
146
- page: pageRecipients ?? 1,
147
- },
148
- locationsData: {
149
- data: locations,
150
- total: totalLocations,
151
- search: searchLocations ?? "",
152
- page: pageLocation ?? 1,
153
- },
154
- },
155
- context: {
156
- clientContext: { customerId, cookie, userId, ...clientContext },
157
- currentOrgUnit: orgUnit,
158
- currentContract: contract,
159
- currentUser: user,
160
- },
161
- };
109
+ cookie,
110
+ unitId: orgUnitId,
111
+ });
112
+
113
+ const user = await getUserByIdService({ orgUnitId, userId, cookie });
114
+
115
+ const { data: recipients = [], total = 0 } =
116
+ await getAddressRecipientsService({
117
+ unitId: orgUnitId,
118
+ customerId: contractId,
119
+ addressId,
120
+ cookie,
121
+ search: searchRecipients,
122
+ page: pageRecipients,
123
+ });
124
+
125
+ const { data: locations, total: totalLocations } =
126
+ await getAddressLocationService({
127
+ contractId,
128
+ addressId,
129
+ cookie,
130
+ unitId: orgUnitId,
131
+ search: searchLocations,
132
+ page: pageLocation,
133
+ });
134
+
135
+ return {
136
+ data: {
137
+ addressDetails: await getAddressDetailsService(
138
+ addressId,
139
+ cookie,
140
+ contractId,
141
+ orgUnitId
142
+ ),
143
+ recipientsData: {
144
+ data: recipients,
145
+ total,
146
+ search: searchRecipients ?? "",
147
+ page: pageRecipients ?? 1,
148
+ },
149
+ locationsData: {
150
+ data: locations,
151
+ total: totalLocations,
152
+ search: searchLocations ?? "",
153
+ page: pageLocation ?? 1,
154
+ },
155
+ },
156
+ context: {
157
+ clientContext: { customerId, cookie, userId, ...clientContext },
158
+ currentOrgUnit: orgUnit,
159
+ currentContract: contract,
160
+ currentUser: user,
161
+ },
162
+ };
163
+ }
164
+ );
162
165
  };
163
166
 
164
167
  export const loader = withLoaderErrorBoundary(loaderFunction, {
@@ -168,11 +171,10 @@ export const loader = withLoaderErrorBoundary(loaderFunction, {
168
171
 
169
172
  const AddressDetailsPage = ({
170
173
  data,
171
- context,
172
174
  hasError,
173
175
  error,
174
176
  }: AddressDetailsPageData) => (
175
- <BuyerPortalProvider {...context}>
177
+ <>
176
178
  {hasError ? (
177
179
  <ErrorTabsLayout error={error} />
178
180
  ) : (
@@ -182,12 +184,14 @@ const AddressDetailsPage = ({
182
184
  locationsData={data.locationsData ?? DEFAULT_ADDRESS_PAGINATED_DATA}
183
185
  />
184
186
  )}
185
- </BuyerPortalProvider>
187
+ </>
186
188
  );
187
189
 
188
- export default withErrorBoundary(AddressDetailsPage, {
189
- tags: {
190
- component: "AddressDetailsPage",
191
- errorType: "address_details_error",
192
- },
193
- });
190
+ export default withProviders(
191
+ withErrorBoundary(AddressDetailsPage, {
192
+ tags: {
193
+ component: "AddressDetailsPage",
194
+ errorType: "address_details_error",
195
+ },
196
+ })
197
+ );
@@ -5,24 +5,22 @@ import {
5
5
  } from "../features/addresses/services";
6
6
  import { getContractDetailsService } from "../features/contracts/services";
7
7
  import { getOrgUnitBasicDataService } from "../features/org-units/services";
8
- import {
9
- BuyerPortalProvider,
10
- withErrorBoundary,
11
- } from "../features/shared/components";
8
+ import { withErrorBoundary } from "../features/shared/components";
12
9
  import { ErrorBoundaryProps } from "../features/shared/components/ErrorBoundary/types";
13
10
  import { ErrorTabsLayout } from "../features/shared/layouts/ErrorTabsLayout/ErrorTabsLayout";
14
11
  import {
15
12
  type ClientContext,
16
- getClientContext,
17
13
  getValidPage,
18
14
  withLoaderErrorBoundary,
15
+ withAuthLoader,
16
+ withProviders,
19
17
  } from "../features/shared/utils";
20
18
  import { getUserByIdService } from "../features/users/services";
21
19
 
22
20
  import type { AddressData } from "../features/addresses/types";
23
21
  import type { ContractData } from "../features/contracts/types";
24
22
  import type { OrgUnitBasicData } from "../features/org-units/types";
25
- import type { LoaderData } from "../features/shared/types";
23
+ import type { AuthRouteProps, LoaderData } from "../features/shared/types";
26
24
  import type { UserData } from "../features/users/types";
27
25
 
28
26
  export type AddressesPageData = {
@@ -48,62 +46,63 @@ export type AddressesPageQuery = GetAddressesServiceProps & {
48
46
 
49
47
  const loaderFunction = async (
50
48
  data: LoaderData<AddressesPageQuery>
51
- ): Promise<AddressesPageData> => {
49
+ ): Promise<AuthRouteProps<AddressesPageData>> => {
52
50
  const { contractId, orgUnitId, search, page: pageString } = data.query;
53
51
 
54
52
  const page = getValidPage(pageString);
55
53
 
56
- const { cookie, userId, customerId, ...clientContext } =
57
- await getClientContext(data);
54
+ return withAuthLoader(
55
+ data,
56
+ async ({ cookie, userId, customerId, ...clientContext }) => {
57
+ if (!contractId || !orgUnitId) {
58
+ return {
59
+ data: [],
60
+ search: search ?? "",
61
+ total: 0,
62
+ page: page ?? 1,
63
+ context: {
64
+ clientContext: { customerId, cookie, userId, ...clientContext },
65
+ currentOrgUnit: null,
66
+ currentContract: null,
67
+ currentUser: null,
68
+ },
69
+ };
70
+ }
58
71
 
59
- if (!contractId || !orgUnitId) {
60
- return {
61
- data: [],
62
- search: search ?? "",
63
- total: 0,
64
- page: page ?? 1,
65
- context: {
66
- clientContext: { customerId, cookie, userId, ...clientContext },
67
- currentOrgUnit: null,
68
- currentContract: null,
69
- currentUser: null,
70
- },
71
- };
72
- }
72
+ const [currentOrgUnit, user, contract, addressesResponse] =
73
+ await Promise.all([
74
+ getOrgUnitBasicDataService({
75
+ id: orgUnitId,
76
+ cookie,
77
+ }),
78
+ getUserByIdService({ orgUnitId, userId, cookie }),
79
+ getContractDetailsService({
80
+ contractId,
81
+ cookie,
82
+ unitId: orgUnitId,
83
+ }),
84
+ getAddressesByUnitIdService({
85
+ orgUnitId,
86
+ search,
87
+ page,
88
+ cookie,
89
+ }),
90
+ ]);
73
91
 
74
- const [currentOrgUnit, user, contract, addressesResponse] = await Promise.all(
75
- [
76
- getOrgUnitBasicDataService({
77
- id: orgUnitId,
78
- cookie,
79
- }),
80
- getUserByIdService({ orgUnitId, userId, cookie }),
81
- getContractDetailsService({
82
- contractId,
83
- cookie,
84
- unitId: orgUnitId,
85
- }),
86
- getAddressesByUnitIdService({
87
- orgUnitId,
88
- search,
92
+ return {
93
+ data: addressesResponse.data,
94
+ total: addressesResponse.total,
89
95
  page,
90
- cookie,
91
- }),
92
- ]
96
+ search: search ?? "",
97
+ context: {
98
+ clientContext: { cookie, userId, customerId, ...clientContext },
99
+ currentOrgUnit,
100
+ currentUser: user,
101
+ currentContract: contract,
102
+ },
103
+ };
104
+ }
93
105
  );
94
-
95
- return {
96
- data: addressesResponse.data,
97
- total: addressesResponse.total,
98
- page,
99
- search: search ?? "",
100
- context: {
101
- clientContext: { cookie, userId, customerId, ...clientContext },
102
- currentOrgUnit,
103
- currentUser: user,
104
- currentContract: contract,
105
- },
106
- };
107
106
  };
108
107
 
109
108
  export const loader = withLoaderErrorBoundary(loaderFunction, {
@@ -116,11 +115,10 @@ const AddressPage = ({
116
115
  search,
117
116
  page,
118
117
  total,
119
- context,
120
118
  hasError,
121
119
  error,
122
120
  }: AddressesPageData) => (
123
- <BuyerPortalProvider {...context}>
121
+ <>
124
122
  {hasError ? (
125
123
  <ErrorTabsLayout error={error} />
126
124
  ) : (
@@ -131,12 +129,14 @@ const AddressPage = ({
131
129
  total={total}
132
130
  />
133
131
  )}
134
- </BuyerPortalProvider>
132
+ </>
135
133
  );
136
134
 
137
- export default withErrorBoundary(AddressPage, {
138
- tags: {
139
- component: "AddressesPage",
140
- errorType: "addresses_error",
141
- },
142
- });
135
+ export default withProviders(
136
+ withErrorBoundary(AddressPage, {
137
+ tags: {
138
+ component: "AddressesPage",
139
+ errorType: "addresses_error",
140
+ },
141
+ })
142
+ );
@@ -3,22 +3,20 @@ import { getBudgetByIdService } from "../features/budgets/services";
3
3
  import { Budget } from "../features/budgets/types";
4
4
  import { getContractDetailsService } from "../features/contracts/services";
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 BudgetsDetailsData = {
@@ -41,37 +39,39 @@ export type BudgetsDetailsQuery = {
41
39
 
42
40
  const loaderFunction = async (
43
41
  data: LoaderData<BudgetsDetailsQuery>
44
- ): Promise<BudgetsDetailsData> => {
45
- const { customerId, cookie, userId, ...clientContext } =
46
- await getClientContext(data);
47
-
42
+ ): Promise<AuthRouteProps<BudgetsDetailsData>> => {
48
43
  const { contractId, orgUnitId, budgetId } = data.query;
49
44
 
50
- const [orgUnit, contract, user, budgetsData] = await Promise.all([
51
- getOrgUnitBasicDataService({ cookie, id: orgUnitId }),
52
- getContractDetailsService({
53
- contractId,
54
- cookie,
55
- unitId: orgUnitId,
56
- }),
57
- getUserByIdService({ orgUnitId, userId, cookie }),
58
- getBudgetByIdService({
59
- budgetId,
60
- customerId,
61
- unitId: orgUnitId,
62
- cookie,
63
- }),
64
- ]);
45
+ return withAuthLoader(
46
+ data,
47
+ async ({ customerId, cookie, userId, ...clientContext }) => {
48
+ const [orgUnit, contract, user, budgetsData] = await Promise.all([
49
+ getOrgUnitBasicDataService({ cookie, id: orgUnitId }),
50
+ getContractDetailsService({
51
+ contractId,
52
+ cookie,
53
+ unitId: orgUnitId,
54
+ }),
55
+ getUserByIdService({ orgUnitId, userId, cookie }),
56
+ getBudgetByIdService({
57
+ budgetId,
58
+ customerId,
59
+ unitId: orgUnitId,
60
+ cookie,
61
+ }),
62
+ ]);
65
63
 
66
- return {
67
- budget: budgetsData,
68
- context: {
69
- clientContext: { customerId, cookie, userId, ...clientContext },
70
- currentOrgUnit: orgUnit,
71
- currentContract: contract,
72
- currentUser: user,
73
- },
74
- };
64
+ return {
65
+ budget: budgetsData,
66
+ context: {
67
+ clientContext: { customerId, cookie, userId, ...clientContext },
68
+ currentOrgUnit: orgUnit,
69
+ currentContract: contract,
70
+ currentUser: user,
71
+ },
72
+ };
73
+ }
74
+ );
75
75
  };
76
76
 
77
77
  export const loader = withLoaderErrorBoundary(loaderFunction, {
@@ -81,22 +81,23 @@ export const loader = withLoaderErrorBoundary(loaderFunction, {
81
81
 
82
82
  const BudgetsDetailsPage = ({
83
83
  budget,
84
- context,
85
84
  hasError,
86
85
  error,
87
86
  }: BudgetsDetailsData) => (
88
- <BuyerPortalProvider {...context}>
87
+ <>
89
88
  {hasError ? (
90
89
  <ErrorTabsLayout error={error} />
91
90
  ) : (
92
91
  <BudgetsDetailsLayout budget={budget} />
93
92
  )}
94
- </BuyerPortalProvider>
93
+ </>
95
94
  );
96
95
 
97
- export default withErrorBoundary(BudgetsDetailsPage, {
98
- tags: {
99
- component: "BudgetsDetailsPage",
100
- errorType: "budgets_details_error",
101
- },
102
- });
96
+ export default withProviders(
97
+ withErrorBoundary(BudgetsDetailsPage, {
98
+ tags: {
99
+ component: "BudgetsDetailsPage",
100
+ errorType: "budgets_details_error",
101
+ },
102
+ })
103
+ );