generated from vuestorefront/ecommerce-integration-boilerplate
/
index.ts
169 lines (133 loc) · 7.35 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {
Context,
useUserShippingFactory,
UseUserShippingFactoryParams
} from '@vue-storefront/core';
import type {
UserShippingAddress as Address,
UserShippingAddressItem as AddressItem
} from '@vue-storefront/prestashop-api';
const params: UseUserShippingFactoryParams<Address, AddressItem> = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
addAddress: async (context: Context, params) => {
const { address } = params;
const vsfCookieKey = context.$prestashop.config.app.$config.psCustomerCookieKey;
const vsfCookieValue = context.$prestashop.config.app.$config.psCustomerCookieValue;
const psCookieKey = context.$prestashop.config.app.$cookies.get(vsfCookieKey);
const psCookieValue = context.$prestashop.config.app.$cookies.get(vsfCookieValue);
const lang = context.$prestashop.config.app.i18n.locales &&
context.$prestashop.config.app.i18n.locales.length > 1 &&
context.$prestashop.config.app.$cookies.get('vsf-locale')
? '/' + context.$prestashop.config.app.$cookies.get('vsf-locale') : '';
const currency = context.$prestashop.config.app.$cookies.get('vsf-currency');
await context.$prestashop.api.addNewAddress({address, psCookieKey, psCookieValue, lang: lang, currency: currency });
const { data, cookieObject } = await context.$prestashop.api.loadAddresses({ psCookieKey, psCookieValue, lang: lang, currency: currency });
if (data.code === 200) {
if (cookieObject) {
context.$prestashop.config.app.$cookies.set(vsfCookieKey, cookieObject.vsfPsKeyCookie);
context.$prestashop.config.app.$cookies.set(vsfCookieValue, cookieObject.vsfPsValCookie);
}
return data.psdata;
} else {
return {};
}
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
deleteAddress: async (context: Context, params) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const { id } = params.address;
const vsfCookieKey = context.$prestashop.config.app.$config.psCustomerCookieKey;
const vsfCookieValue = context.$prestashop.config.app.$config.psCustomerCookieValue;
const psCookieKey = context.$prestashop.config.app.$cookies.get(vsfCookieKey);
const psCookieValue = context.$prestashop.config.app.$cookies.get(vsfCookieValue);
const lang = context.$prestashop.config.app.i18n.locales &&
context.$prestashop.config.app.i18n.locales.length > 1 &&
context.$prestashop.config.app.$cookies.get('vsf-locale')
? '/' + context.$prestashop.config.app.$cookies.get('vsf-locale') : '';
const currency = context.$prestashop.config.app.$cookies.get('vsf-currency');
await context.$prestashop.api.removeAddress({id, psCookieKey, psCookieValue, lang: lang, currency: currency });
const { data, cookieObject } = await context.$prestashop.api.loadAddresses({ psCookieKey, psCookieValue, lang: lang, currency: currency });
if (data.code === 200) {
if (cookieObject) {
context.$prestashop.config.app.$cookies.set(vsfCookieKey, cookieObject.vsfPsKeyCookie);
context.$prestashop.config.app.$cookies.set(vsfCookieValue, cookieObject.vsfPsValCookie);
}
return data.psdata;
} else {
return {};
}
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
updateAddress: async (context: Context, params) => {
const { address } = params;
const vsfCookieKey = context.$prestashop.config.app.$config.psCustomerCookieKey;
const vsfCookieValue = context.$prestashop.config.app.$config.psCustomerCookieValue;
const psCookieKey = context.$prestashop.config.app.$cookies.get(vsfCookieKey);
const psCookieValue = context.$prestashop.config.app.$cookies.get(vsfCookieValue);
const lang = context.$prestashop.config.app.i18n.locales &&
context.$prestashop.config.app.i18n.locales.length > 1 &&
context.$prestashop.config.app.$cookies.get('vsf-locale')
? '/' + context.$prestashop.config.app.$cookies.get('vsf-locale') : '';
const currency = context.$prestashop.config.app.$cookies.get('vsf-currency');
await context.$prestashop.api.updateOneAddress({address, psCookieKey, psCookieValue, lang: lang, currency: currency });
const { data, cookieObject } = await context.$prestashop.api.loadAddresses({ psCookieKey, psCookieValue, lang: lang, currency: currency });
if (data.code === 200) {
if (cookieObject) {
context.$prestashop.config.app.$cookies.set(vsfCookieKey, cookieObject.vsfPsKeyCookie);
context.$prestashop.config.app.$cookies.set(vsfCookieValue, cookieObject.vsfPsValCookie);
}
return data.psdata;
} else {
return {};
}
},
load: async (context: Context) => {
const vsfCookieKey = context.$prestashop.config.app.$config.psCustomerCookieKey;
const vsfCookieValue = context.$prestashop.config.app.$config.psCustomerCookieValue;
const psCookieKey = context.$prestashop.config.app.$cookies.get(vsfCookieKey);
const psCookieValue = context.$prestashop.config.app.$cookies.get(vsfCookieValue);
const lang = context.$prestashop.config.app.i18n.locales &&
context.$prestashop.config.app.i18n.locales.length > 1 &&
context.$prestashop.config.app.$cookies.get('vsf-locale')
? '/' + context.$prestashop.config.app.$cookies.get('vsf-locale') : '';
const currency = context.$prestashop.config.app.$cookies.get('vsf-currency');
const { data, cookieObject } = await context.$prestashop.api.loadAddresses({ psCookieKey, psCookieValue, lang: lang, currency: currency });
if (data.code === 200) {
if (cookieObject) {
context.$prestashop.config.app.$cookies.set(vsfCookieKey, cookieObject.vsfPsKeyCookie);
context.$prestashop.config.app.$cookies.set(vsfCookieValue, cookieObject.vsfPsValCookie);
}
return data.psdata;
} else {
return {};
}
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
setDefaultAddress: async (context: Context, params) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const { id } = params.address;
const vsfCookieKey = context.$prestashop.config.app.$config.psCustomerCookieKey;
const vsfCookieValue = context.$prestashop.config.app.$config.psCustomerCookieValue;
const psCookieKey = context.$prestashop.config.app.$cookies.get(vsfCookieKey);
const psCookieValue = context.$prestashop.config.app.$cookies.get(vsfCookieValue);
const lang = context.$prestashop.config.app.i18n.locales &&
context.$prestashop.config.app.i18n.locales.length > 1 &&
context.$prestashop.config.app.$cookies.get('vsf-locale')
? '/' + context.$prestashop.config.app.$cookies.get('vsf-locale') : '';
const currency = context.$prestashop.config.app.$cookies.get('vsf-currency');
await context.$prestashop.api.setAddress({ id, psCookieKey, psCookieValue, lang: lang, currency: currency });
const { data, cookieObject } = await context.$prestashop.api.loadAddresses({ psCookieKey, psCookieValue, lang: lang, currency: currency });
if (data.code === 200) {
if (cookieObject) {
context.$prestashop.config.app.$cookies.set(vsfCookieKey, cookieObject.vsfPsKeyCookie);
context.$prestashop.config.app.$cookies.set(vsfCookieValue, cookieObject.vsfPsValCookie);
}
return data.psdata;
} else {
return {};
}
}
};
export const useUserShipping = useUserShippingFactory<Address, AddressItem>(params);