@vtex/faststore-plugin-buyer-portal 1.0.23 → 1.0.25
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 +1 -1
- package/src/components/BuyerPortalProvider/BuyerPortalProvider.tsx +26 -0
- package/src/components/CreateOrgUnitDrawer/CreateOrgUnitDrawer.tsx +7 -0
- package/src/hooks/useBuyerPortal.ts +13 -0
- package/src/pages/address-details.tsx +5 -6
- package/src/pages/addresses.tsx +13 -12
- package/src/pages/contracts.tsx +12 -13
- package/src/pages/home.tsx +12 -7
- package/src/pages/org-units.tsx +16 -5
- package/src/pages/profile.tsx +7 -7
- package/src/pages/user-details.tsx +4 -5
- package/src/pages/users.tsx +2 -5
- package/src/types/LoaderData.ts +5 -0
- package/src/utils/api.ts +2 -2
- package/src/utils/cookie.ts +13 -18
- package/src/utils/getClientContext.ts +14 -0
package/package.json
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import React, { createContext, useContext, ReactNode } from "react";
|
|
2
|
+
import { ClientContext } from "../../utils/getClientContext";
|
|
3
|
+
|
|
4
|
+
export type BuyerPortalContextType = {
|
|
5
|
+
clientContext: ClientContext;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const BuyerPortalContext = createContext<
|
|
9
|
+
BuyerPortalContextType | undefined
|
|
10
|
+
>(undefined);
|
|
11
|
+
|
|
12
|
+
export type BuyerPortalProviderProps = {
|
|
13
|
+
clientContext: ClientContext;
|
|
14
|
+
children: ReactNode;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const BuyerPortalProvider = ({
|
|
18
|
+
clientContext,
|
|
19
|
+
children,
|
|
20
|
+
}: BuyerPortalProviderProps) => {
|
|
21
|
+
return (
|
|
22
|
+
<BuyerPortalContext.Provider value={{ clientContext }}>
|
|
23
|
+
{children}
|
|
24
|
+
</BuyerPortalContext.Provider>
|
|
25
|
+
);
|
|
26
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import {
|
|
3
|
+
BuyerPortalContext,
|
|
4
|
+
BuyerPortalContextType,
|
|
5
|
+
} from "../components/BuyerPortalProvider/BuyerPortalProvider";
|
|
6
|
+
|
|
7
|
+
export const useBuyerPortal = (): BuyerPortalContextType["clientContext"] => {
|
|
8
|
+
const context = useContext(BuyerPortalContext);
|
|
9
|
+
if (!context) {
|
|
10
|
+
throw new Error("useBuyerPortal must be used within a BuyerPortalProvider");
|
|
11
|
+
}
|
|
12
|
+
return context?.clientContext;
|
|
13
|
+
};
|
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import { AddressData } from "../types/AddressData";
|
|
2
2
|
import { AddressDetailsLayout } from "../layouts/AddressDetailsLayout/AddressDetailsLayout";
|
|
3
3
|
import { getAddressDetailsService } from "../services/get-address-details.service";
|
|
4
|
+
import { LoaderData } from "../types/LoaderData";
|
|
4
5
|
|
|
5
|
-
type
|
|
6
|
-
|
|
7
|
-
id: string;
|
|
8
|
-
};
|
|
6
|
+
type AddressDetailsPageQuery = {
|
|
7
|
+
id: string;
|
|
9
8
|
};
|
|
10
9
|
|
|
11
10
|
export type AddressDetailsPageData = { data: AddressData };
|
|
12
11
|
|
|
13
12
|
export async function loader(
|
|
14
|
-
data:
|
|
13
|
+
data: LoaderData<AddressDetailsPageQuery>
|
|
15
14
|
): Promise<AddressDetailsPageData> {
|
|
16
|
-
return { data: await getAddressDetailsService(data.query
|
|
15
|
+
return { data: await getAddressDetailsService(data.query?.id) };
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
const AddressDetailsPage = ({ data }: AddressDetailsPageData) => (
|
package/src/pages/addresses.tsx
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
1
1
|
import { AddressLayout } from "../layouts/AddressesLayout/AddressesLayout";
|
|
2
2
|
import {
|
|
3
3
|
getAddressesService,
|
|
4
|
+
GetAddressesServiceProps,
|
|
4
5
|
} from "../services/get-addresses.service";
|
|
5
6
|
import { AddressSummaryData } from "../types/AddressData";
|
|
6
|
-
import {
|
|
7
|
-
|
|
7
|
+
import {
|
|
8
|
+
getCookieAsString,
|
|
9
|
+
getCustomerIdFromCookieServerSide,
|
|
10
|
+
} from "../utils/cookie";
|
|
11
|
+
import { LoaderData } from "../types/LoaderData";
|
|
8
12
|
|
|
9
13
|
export type AddressesPageData = { data: AddressSummaryData[] };
|
|
10
14
|
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export async function loader(data: AddressesPageContext): Promise<AddressesPageData> {
|
|
15
|
+
export async function loader(
|
|
16
|
+
data: LoaderData<GetAddressesServiceProps>
|
|
17
|
+
): Promise<AddressesPageData> {
|
|
18
18
|
return {
|
|
19
19
|
data: await getAddressesService({
|
|
20
|
-
customerId: getCustomerIdFromCookieServerSide(data),
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
customerId: getCustomerIdFromCookieServerSide(data),
|
|
21
|
+
cookie: getCookieAsString(data?.req?.cookies),
|
|
22
|
+
}),
|
|
23
|
+
};
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
const AddressPage = ({ data }: AddressesPageData) => (
|
package/src/pages/contracts.tsx
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
import { ContractsLayout } from "../layouts/ContractsLayout/ContractsLayout";
|
|
2
|
-
import {
|
|
3
|
-
getContractsService,
|
|
4
|
-
GetContractsServiceProps,
|
|
5
|
-
} from "../services/get-contracts.service";
|
|
2
|
+
import { getContractsService } from "../services/get-contracts.service";
|
|
6
3
|
import { ContractData } from "../types/ContractData";
|
|
7
|
-
import {
|
|
8
|
-
|
|
4
|
+
import {
|
|
5
|
+
getCookieAsString,
|
|
6
|
+
getCustomerIdFromCookieServerSide,
|
|
7
|
+
} from "../utils/cookie";
|
|
8
|
+
import { LoaderData } from "../types/LoaderData";
|
|
9
9
|
|
|
10
10
|
export type ContractsPageData = { data: ContractData[] };
|
|
11
11
|
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
export async function loader(data: LoaderData): Promise<ContractsPageData> {
|
|
13
|
+
return {
|
|
14
|
+
data: await getContractsService({
|
|
15
|
+
customerId: getCustomerIdFromCookieServerSide(data),
|
|
16
|
+
cookie: getCookieAsString(data?.req?.cookies),
|
|
17
|
+
}),
|
|
15
18
|
};
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export async function loader(data: ContractsPageContext): Promise<ContractsPageData> {
|
|
19
|
-
return { data: await getContractsService({ customerId: getCustomerIdFromCookieServerSide(data), cookie: getCookieAsString(data?.req?.cookies) }) };
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
const ContractsPage = ({ data }: ContractsPageData) => (
|
package/src/pages/home.tsx
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
import { HomeLayout } from "../layouts/HomeLayout/HomeLayout";
|
|
2
2
|
import { getOrganizationService } from "../services/get-organization.service";
|
|
3
|
+
import { LoaderData } from "../types/LoaderData";
|
|
3
4
|
import { OrganizationData } from "../types/OrganizationData";
|
|
4
5
|
|
|
5
6
|
export type HomePageData = { data: OrganizationData };
|
|
6
7
|
|
|
7
|
-
type
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
}
|
|
8
|
+
type HomePageQuery = {
|
|
9
|
+
contractMode?: string[];
|
|
10
|
+
};
|
|
12
11
|
|
|
13
|
-
export async function loader(
|
|
14
|
-
|
|
12
|
+
export async function loader(
|
|
13
|
+
data: LoaderData<HomePageQuery>
|
|
14
|
+
): Promise<HomePageData> {
|
|
15
|
+
return {
|
|
16
|
+
data: await getOrganizationService(
|
|
17
|
+
data.query.contractMode ? data.query.contractMode[0] : ""
|
|
18
|
+
),
|
|
19
|
+
};
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
const HomePage = ({ data }: HomePageData) => {
|
package/src/pages/org-units.tsx
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
1
|
+
import { BuyerPortalProvider } from "../components/BuyerPortalProvider/BuyerPortalProvider";
|
|
1
2
|
import { OrgUnitsLayout } from "../layouts/OrgUnitsLayout/OrgUnitsLayout";
|
|
2
3
|
import { getOrgUnitsHierarchyService } from "../services/get-org-units-hierarchy.service";
|
|
4
|
+
import { LoaderData } from "../types/LoaderData";
|
|
3
5
|
import { OrgUnitHierarchyData } from "../types/OrgUnitsData";
|
|
6
|
+
import { ClientContext, getClientContext } from "../utils/getClientContext";
|
|
4
7
|
|
|
5
|
-
export type OrgUnitsPageData = {
|
|
8
|
+
export type OrgUnitsPageData = {
|
|
9
|
+
data: OrgUnitHierarchyData;
|
|
10
|
+
clientContext: ClientContext;
|
|
11
|
+
};
|
|
6
12
|
|
|
7
|
-
export async function loader(): Promise<OrgUnitsPageData> {
|
|
8
|
-
return {
|
|
13
|
+
export async function loader(data: LoaderData): Promise<OrgUnitsPageData> {
|
|
14
|
+
return {
|
|
15
|
+
data: await getOrgUnitsHierarchyService(),
|
|
16
|
+
clientContext: getClientContext(data),
|
|
17
|
+
};
|
|
9
18
|
}
|
|
10
19
|
|
|
11
|
-
const OrgUnitsPage = ({ data }: OrgUnitsPageData) => (
|
|
12
|
-
<
|
|
20
|
+
const OrgUnitsPage = ({ data, clientContext }: OrgUnitsPageData) => (
|
|
21
|
+
<BuyerPortalProvider clientContext={clientContext}>
|
|
22
|
+
<OrgUnitsLayout data={data} />
|
|
23
|
+
</BuyerPortalProvider>
|
|
13
24
|
);
|
|
14
25
|
|
|
15
26
|
export default OrgUnitsPage;
|
package/src/pages/profile.tsx
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { ProfileLayout } from "../layouts/ProfileLayout/ProfileLayout";
|
|
2
|
-
import {
|
|
3
|
-
getContractsService,
|
|
4
|
-
GetContractsServiceProps,
|
|
5
|
-
} from "../services/get-contracts.service";
|
|
2
|
+
import { getContractsService } from "../services/get-contracts.service";
|
|
6
3
|
import { ContractData } from "../types/ContractData";
|
|
4
|
+
import { LoaderData } from "../types/LoaderData";
|
|
7
5
|
import { getCustomerIdFromCookieServerSide } from "../utils/cookie";
|
|
8
|
-
import type { ContractsPageContext } from "./contracts";
|
|
9
6
|
|
|
10
7
|
export type ContractsPageData = { data: ContractData | null };
|
|
11
8
|
|
|
12
|
-
export async function loader(data:
|
|
13
|
-
const contracts = await getContractsService({
|
|
9
|
+
export async function loader(data: LoaderData): Promise<ContractsPageData> {
|
|
10
|
+
const contracts = await getContractsService({
|
|
11
|
+
customerId: getCustomerIdFromCookieServerSide(data),
|
|
12
|
+
cookie: JSON.stringify(data?.req?.cookies).replace(/[{}]/g, ""),
|
|
13
|
+
});
|
|
14
14
|
|
|
15
15
|
// We should deal with a better error boundary here later, this is just so an empty array doesn't break the render
|
|
16
16
|
return { data: contracts && contracts.length ? contracts[0] : null };
|
|
@@ -4,11 +4,10 @@ import { UserDetailsLayout } from "../layouts/UserDetailsLayout/UserDetailsLayou
|
|
|
4
4
|
import { UserData } from "../types/UserData";
|
|
5
5
|
import { getUserDetailsService } from "../services/get-user-details.service";
|
|
6
6
|
import { getUsersService } from "../services/get-users.service";
|
|
7
|
+
import { LoaderData } from "../types/LoaderData";
|
|
7
8
|
|
|
8
|
-
type
|
|
9
|
-
|
|
10
|
-
id: string;
|
|
11
|
-
};
|
|
9
|
+
type UserDetailsPageQuery = {
|
|
10
|
+
id: string;
|
|
12
11
|
};
|
|
13
12
|
|
|
14
13
|
export type UserDetailsPageData = {
|
|
@@ -17,7 +16,7 @@ export type UserDetailsPageData = {
|
|
|
17
16
|
|
|
18
17
|
export async function loader({
|
|
19
18
|
query,
|
|
20
|
-
}:
|
|
19
|
+
}: LoaderData<UserDetailsPageQuery>): Promise<UserDetailsPageData> {
|
|
21
20
|
return {
|
|
22
21
|
data: {
|
|
23
22
|
user: await getUserDetailsService(query.id),
|
package/src/pages/users.tsx
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
import { Structure } from "../components/HierarchyTree/HierarchyTree";
|
|
10
10
|
import { OrgUnitData } from "../types/OrgUnitsData";
|
|
11
11
|
import { getOrgUnitsHierarchyService } from "../services/get-org-units-hierarchy.service";
|
|
12
|
+
import { LoaderData } from "../types/LoaderData";
|
|
12
13
|
|
|
13
14
|
export type UsersPageData = {
|
|
14
15
|
data: {
|
|
@@ -17,13 +18,9 @@ export type UsersPageData = {
|
|
|
17
18
|
};
|
|
18
19
|
};
|
|
19
20
|
|
|
20
|
-
export type UsersPageContext = {
|
|
21
|
-
query: GetUsersServiceProps;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
21
|
export async function loader({
|
|
25
22
|
query,
|
|
26
|
-
}:
|
|
23
|
+
}: LoaderData<GetUsersServiceProps>): Promise<UsersPageData> {
|
|
27
24
|
return {
|
|
28
25
|
data: {
|
|
29
26
|
users: await getUsersService(query),
|
package/src/utils/api.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { API_URL } from "./constants"
|
|
2
|
-
|
|
2
|
+
import storeConfig from 'discovery.config'
|
|
3
3
|
|
|
4
4
|
export function getApiUrl(operation: string, customerId: string) {
|
|
5
|
-
return `${API_URL(`
|
|
5
|
+
return `${API_URL(`${storeConfig?.secureSubdomain}` ?? '', operation)}${customerId ? `/${customerId}` : ''}`
|
|
6
6
|
}
|
package/src/utils/cookie.ts
CHANGED
|
@@ -1,35 +1,30 @@
|
|
|
1
|
-
import { AUT_COOKIE_KEY } from "./constants"
|
|
2
|
-
import storeConfig from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
type LoaderData = {
|
|
6
|
-
req: {
|
|
7
|
-
cookies: Record<string, string>
|
|
8
|
-
}
|
|
9
|
-
}
|
|
1
|
+
import { AUT_COOKIE_KEY } from "./constants";
|
|
2
|
+
import storeConfig from "discovery.config";
|
|
3
|
+
import { LoaderData } from "../types/LoaderData";
|
|
10
4
|
|
|
11
5
|
export function getAuthCookie(data: LoaderData) {
|
|
12
|
-
const authCookie =
|
|
6
|
+
const authCookie =
|
|
7
|
+
data?.req?.cookies[`${AUT_COOKIE_KEY}_${storeConfig?.api.storeId}`] ?? "";
|
|
13
8
|
|
|
14
|
-
return authCookie
|
|
9
|
+
return authCookie;
|
|
15
10
|
}
|
|
16
11
|
|
|
17
12
|
export function getCustomerIdFromCookieServerSide(data: LoaderData) {
|
|
18
|
-
const authCookie = getAuthCookie(data)
|
|
13
|
+
const authCookie = getAuthCookie(data);
|
|
19
14
|
|
|
20
|
-
if (!authCookie || authCookie ===
|
|
21
|
-
return
|
|
15
|
+
if (!authCookie || authCookie === "") {
|
|
16
|
+
return "";
|
|
22
17
|
}
|
|
23
18
|
|
|
24
|
-
const parsedCookie = parseJwt(authCookie)
|
|
19
|
+
const parsedCookie = parseJwt(authCookie);
|
|
25
20
|
|
|
26
|
-
return parsedCookie.customerId
|
|
21
|
+
return parsedCookie.customerId;
|
|
27
22
|
}
|
|
28
23
|
|
|
29
24
|
export function getCookieAsString(cookie: Record<string, string>) {
|
|
30
|
-
return JSON.stringify(cookie).replace(/[{}]/g,
|
|
25
|
+
return JSON.stringify(cookie).replace(/[{}]/g, "") ?? "";
|
|
31
26
|
}
|
|
32
27
|
|
|
33
28
|
function parseJwt(token: string) {
|
|
34
|
-
return JSON.parse(Buffer.from(token.split(
|
|
29
|
+
return JSON.parse(Buffer.from(token.split(".")[1], "base64").toString());
|
|
35
30
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { LoaderData } from "../types/LoaderData";
|
|
2
|
+
import { getAuthCookie } from "./cookie";
|
|
3
|
+
|
|
4
|
+
export type ClientContext = {
|
|
5
|
+
vtexIdclientAutCookie: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const getClientContext = ({ req }: LoaderData): ClientContext => {
|
|
9
|
+
return {
|
|
10
|
+
vtexIdclientAutCookie: getAuthCookie({
|
|
11
|
+
req,
|
|
12
|
+
}),
|
|
13
|
+
};
|
|
14
|
+
};
|