@vtex/faststore-plugin-buyer-portal 1.0.18 → 1.0.19
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/AddressLine/AddressLine.tsx +8 -7
- package/src/layouts/AddressesLayout/AddressesLayout.tsx +1 -1
- package/src/pages/addresses.tsx +11 -6
- package/src/pages/contracts.tsx +2 -2
- package/src/services/get-address-details.service.ts +1 -0
- package/src/services/get-addresses.service.ts +37 -7
- package/src/services/get-contracts.service.ts +3 -2
- package/src/types/AddressData.ts +2 -2
- package/src/utils/api.ts +7 -0
- package/src/utils/constants.ts +5 -1
- package/src/utils/cookie.ts +4 -0
package/package.json
CHANGED
|
@@ -6,17 +6,17 @@ import { Icon } from "../Icon";
|
|
|
6
6
|
export type AddressLineProps = {
|
|
7
7
|
id: string;
|
|
8
8
|
name: string;
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
types: string[];
|
|
10
|
+
isActive: boolean;
|
|
11
11
|
href: string;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
export const AddressLine = ({
|
|
15
15
|
name,
|
|
16
|
-
|
|
16
|
+
types,
|
|
17
17
|
id,
|
|
18
18
|
href,
|
|
19
|
-
|
|
19
|
+
isActive,
|
|
20
20
|
}: AddressLineProps) => (
|
|
21
21
|
<li data-fs-addresses-line>
|
|
22
22
|
<Link href={href} data-fs-addresses-line-link>
|
|
@@ -30,11 +30,12 @@ export const AddressLine = ({
|
|
|
30
30
|
{name}
|
|
31
31
|
</span>
|
|
32
32
|
|
|
33
|
-
{
|
|
34
|
-
<Tag key={index}>{
|
|
33
|
+
{types.map((type, index) => (
|
|
34
|
+
<Tag key={index}>{type}</Tag>
|
|
35
35
|
))}
|
|
36
36
|
</Link>
|
|
37
|
-
|
|
37
|
+
|
|
38
|
+
<Toggle id={id} defaultChecked={isActive} />
|
|
38
39
|
<button data-fs-addresses-dropdown-trigger>
|
|
39
40
|
<Icon
|
|
40
41
|
name="MoreVert"
|
|
@@ -20,7 +20,7 @@ export const AddressLayout = ({ data }: AddressLayoutProps) => {
|
|
|
20
20
|
const { setQueryString, removeQueryString } = useQueryParams();
|
|
21
21
|
|
|
22
22
|
const types = data
|
|
23
|
-
.flatMap((address) => address.
|
|
23
|
+
.flatMap((address) => address.types)
|
|
24
24
|
.filter((tag, index, self) => self.indexOf(tag) === index);
|
|
25
25
|
|
|
26
26
|
return (
|
package/src/pages/addresses.tsx
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
import { AddressLayout } from "../layouts/AddressesLayout/AddressesLayout";
|
|
2
2
|
import {
|
|
3
3
|
getAddressesService,
|
|
4
|
-
GetAddressesServiceProps,
|
|
5
4
|
} from "../services/get-addresses.service";
|
|
6
5
|
import { AddressSummaryData } from "../types/AddressData";
|
|
6
|
+
import { getCookieAsString, getCustomerIdFromCookieServerSide } from "../utils/cookie";
|
|
7
|
+
|
|
7
8
|
|
|
8
9
|
export type AddressesPageData = { data: AddressSummaryData[] };
|
|
9
10
|
|
|
10
11
|
export type AddressesPageContext = {
|
|
11
|
-
|
|
12
|
+
req: {
|
|
13
|
+
cookies: Record<string, string>
|
|
14
|
+
};
|
|
12
15
|
};
|
|
13
16
|
|
|
14
|
-
export async function loader({
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
export async function loader(data: AddressesPageContext): Promise<AddressesPageData> {
|
|
18
|
+
return {
|
|
19
|
+
data: await getAddressesService({
|
|
20
|
+
customerId: getCustomerIdFromCookieServerSide(data), cookie: getCookieAsString(data?.req?.cookies)
|
|
21
|
+
})
|
|
22
|
+
}
|
|
18
23
|
}
|
|
19
24
|
|
|
20
25
|
const AddressPage = ({ data }: AddressesPageData) => (
|
package/src/pages/contracts.tsx
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
GetContractsServiceProps,
|
|
5
5
|
} from "../services/get-contracts.service";
|
|
6
6
|
import { ContractData } from "../types/ContractData";
|
|
7
|
-
import { getCustomerIdFromCookieServerSide } from "../utils/cookie";
|
|
7
|
+
import { getCookieAsString, getCustomerIdFromCookieServerSide } from "../utils/cookie";
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
export type ContractsPageData = { data: ContractData[] };
|
|
@@ -16,7 +16,7 @@ export type ContractsPageContext = {
|
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
export async function loader(data: ContractsPageContext): Promise<ContractsPageData> {
|
|
19
|
-
return { data: await getContractsService({ customerId: getCustomerIdFromCookieServerSide(data), cookie:
|
|
19
|
+
return { data: await getContractsService({ customerId: getCustomerIdFromCookieServerSide(data), cookie: getCookieAsString(data?.req?.cookies) }) };
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
const ContractsPage = ({ data }: ContractsPageData) => (
|
|
@@ -4,6 +4,7 @@ import { AddressData } from "../types/AddressData";
|
|
|
4
4
|
export const getAddressDetailsService = async (
|
|
5
5
|
id: string
|
|
6
6
|
): Promise<AddressData> => {
|
|
7
|
+
// TODO: Need to create get of single address on api
|
|
7
8
|
const addressData = addressesData.find(
|
|
8
9
|
(address) => String(address.id) === id
|
|
9
10
|
)!;
|
|
@@ -1,27 +1,57 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AddressSummaryData } from "../types/AddressData";
|
|
1
|
+
import { AddressData, AddressSummaryData } from "../types/AddressData";
|
|
3
2
|
import { statusFilters } from "../utils/search";
|
|
3
|
+
import { API_URL } from '../utils/constants';
|
|
4
|
+
import storeConfig from '../../../../discovery.config'
|
|
5
|
+
import { getApiUrl } from "../utils/api";
|
|
4
6
|
|
|
5
7
|
export type GetAddressesServiceProps = {
|
|
6
8
|
search: string;
|
|
9
|
+
customerId: string,
|
|
7
10
|
status: string;
|
|
8
11
|
type: string;
|
|
9
12
|
sort: string;
|
|
13
|
+
cookie: string;
|
|
10
14
|
};
|
|
11
15
|
|
|
12
16
|
export const getAddressesService = async ({
|
|
13
17
|
search = "",
|
|
18
|
+
customerId,
|
|
14
19
|
status,
|
|
15
20
|
type,
|
|
16
21
|
sort,
|
|
22
|
+
cookie,
|
|
17
23
|
}: Partial<GetAddressesServiceProps> = {}): Promise<AddressSummaryData[]> => {
|
|
18
|
-
|
|
24
|
+
|
|
25
|
+
const getAddresses = async () => {
|
|
26
|
+
try {
|
|
27
|
+
if (!customerId) {
|
|
28
|
+
return []
|
|
29
|
+
}
|
|
30
|
+
const headers = {
|
|
31
|
+
"Content-type": "application/json",
|
|
32
|
+
'Cookie': cookie ?? '',
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const rawResposne = await fetch(getApiUrl('addresses', customerId), { method: 'GET', credentials: 'include', headers })
|
|
36
|
+
|
|
37
|
+
const response = await rawResposne.json()
|
|
38
|
+
|
|
39
|
+
return response?.addresses ?? []
|
|
40
|
+
} catch (err) {
|
|
41
|
+
console.log('>> err', err)
|
|
42
|
+
return []
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const addressesData = await getAddresses() as AddressData[]
|
|
47
|
+
|
|
48
|
+
return addressesData && addressesData
|
|
19
49
|
.filter((address) => {
|
|
20
50
|
const matchesName = address.name
|
|
21
51
|
.toLowerCase()
|
|
22
52
|
.includes(search.toLowerCase());
|
|
23
|
-
const matchesStatus = !status || address.
|
|
24
|
-
const matchesType = !type || address.
|
|
53
|
+
const matchesStatus = !status || address.isActive === statusFilters[status];
|
|
54
|
+
const matchesType = !type || address.types.includes(type);
|
|
25
55
|
|
|
26
56
|
return matchesName && matchesStatus && matchesType;
|
|
27
57
|
})
|
|
@@ -32,9 +62,9 @@ export const getAddressesService = async ({
|
|
|
32
62
|
case "Alphabetical Order Desc":
|
|
33
63
|
return b.name.localeCompare(a.name);
|
|
34
64
|
case "Active First":
|
|
35
|
-
return (b.
|
|
65
|
+
return (b.isActive ? 1 : 0) - (a.isActive ? 1 : 0);
|
|
36
66
|
case "Inactive First":
|
|
37
|
-
return (a.
|
|
67
|
+
return (a.isActive ? 1 : 0) - (b.isActive ? 1 : 0);
|
|
38
68
|
default:
|
|
39
69
|
return 0;
|
|
40
70
|
}
|
|
@@ -2,6 +2,7 @@ import { ContractData } from "../types/ContractData";
|
|
|
2
2
|
import { statusFilters } from "../utils/search";
|
|
3
3
|
import { API_URL } from '../utils/constants';
|
|
4
4
|
import storeConfig from '../../../../discovery.config'
|
|
5
|
+
import { getApiUrl } from "../utils/api";
|
|
5
6
|
|
|
6
7
|
|
|
7
8
|
export type GetContractsServiceProps = {
|
|
@@ -29,9 +30,9 @@ export const getContractsService = async ({
|
|
|
29
30
|
'Cookie': cookie ?? '',
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
const
|
|
33
|
+
const rawResposne = await fetch(getApiUrl('contracts', customerId), { method: 'GET', credentials: 'include', headers })
|
|
33
34
|
|
|
34
|
-
const response = await
|
|
35
|
+
const response = await rawResposne.json()
|
|
35
36
|
|
|
36
37
|
return response?.data?.contracts ?? []
|
|
37
38
|
} catch (err) {
|
package/src/types/AddressData.ts
CHANGED
package/src/utils/api.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { API_URL } from "./constants"
|
|
2
|
+
import storeConfig from '../../../../discovery.config'
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export function getApiUrl(operation: string, customerId: string) {
|
|
6
|
+
return `${API_URL(storeConfig?.secureSubdomain ?? '', operation)}${customerId ? `/${customerId}` : ''}`
|
|
7
|
+
}
|
package/src/utils/constants.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
export const AUT_COOKIE_KEY = 'VtexIdclientAutCookie'
|
|
2
|
-
|
|
2
|
+
// PROD URL
|
|
3
|
+
// export const API_URL = (checkoutUrl: string, operation: string) => `${checkoutUrl}/_v/buyer-portal/${operation}`
|
|
4
|
+
// DEV URL - CHANGE BEFORE MERGE
|
|
5
|
+
export const API_URL = (checkoutUrl?: string, operation?: string) => `https://everton--b2bfaststoredev.myvtex.com/_v/buyer-portal/${operation}`
|
|
6
|
+
|
package/src/utils/cookie.ts
CHANGED
|
@@ -22,6 +22,10 @@ export function getCustomerIdFromCookieServerSide(data: LoaderData) {
|
|
|
22
22
|
return parsedCookie.customerId
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
export function getCookieAsString(cookie: Record<string, string>) {
|
|
26
|
+
return JSON.stringify(cookie).replace(/[{}]/g, '')
|
|
27
|
+
}
|
|
28
|
+
|
|
25
29
|
function parseJwt(token: string) {
|
|
26
30
|
return JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString());
|
|
27
31
|
}
|