@myparcel-dev/pdk-admin 2.2.7 → 2.4.0
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/CHANGELOG.md +28 -0
- package/dist/actions/composables/mutations/orders/useExportOrdersMutation.d.ts +1 -1
- package/dist/actions/print/resolvePrintParameters.d.ts +1 -0
- package/dist/composables/language/useLanguage.d.ts +6 -0
- package/dist/pdk/PdkAdmin.d.ts +26 -2
- package/dist/pdk/boot.d.ts +5 -0
- package/dist/sdk/index.d.ts +1 -0
- package/dist/sdk/requestHeaders.d.ts +10 -0
- package/dist/services/mutations/getModalMutationOptions.d.ts +7 -1
- package/dist/services/mutations/index.d.ts +1 -0
- package/dist/services/mutations/modalMutationToken.d.ts +16 -0
- package/dist/services/print/createPdfObjectUrl.d.ts +2 -0
- package/dist/services/print/index.d.ts +3 -0
- package/dist/services/print/labelsNotification.d.ts +5 -0
- package/dist/services/print/pollLabelPdf.d.ts +9 -0
- package/dist/stores/useModalStore.d.ts +9 -1
- package/dist/stores/useNotificationStore.d.ts +15 -0
- package/dist/types/actions/endpoints.types.d.ts +1 -1
- package/dist/types/actions/parameters.types.d.ts +3 -0
- package/dist/types/admin.types.d.ts +10 -0
- package/dist/types/common.types.d.ts +7 -0
- package/dist/types/configuration.types.d.ts +12 -0
- package/dist/types/sdk.types.d.ts +14 -1
- package/dist/utils/encodeArrayParameter.d.ts +1 -0
- package/package.json +2 -2
- package/src/actions/composables/mutations/orders/useExportOrdersMutation.spec.ts +75 -0
- package/src/actions/composables/mutations/orders/useExportOrdersMutation.ts +17 -6
- package/src/actions/print/openOrPrintPdf.spec.ts +119 -0
- package/src/actions/print/openOrPrintPdf.ts +48 -3
- package/src/actions/print/resolvePrintParameters.ts +13 -3
- package/src/components/common/TabNavigation.spec.ts +78 -0
- package/src/components/common/TabNavigation.vue +7 -5
- package/src/composables/language/resetLanguageCache.spec.ts +24 -0
- package/src/composables/language/useLanguage.ts +13 -0
- package/src/pdk/PdkAdmin.spec.ts +97 -0
- package/src/pdk/PdkAdmin.ts +63 -7
- package/src/pdk/boot.ts +13 -3
- package/src/pdk/createPdkAdmin.spec.ts +75 -0
- package/src/pdk/createPdkAdmin.ts +2 -0
- package/src/pdk/instance/plugins/createStorePlugin.spec.ts +44 -0
- package/src/pdk/instance/plugins/createStorePlugin.ts +12 -7
- package/src/pdk/instance/plugins/createVueQueryPlugin.spec.ts +29 -0
- package/src/pdk/instance/plugins/createVueQueryPlugin.ts +7 -7
- package/src/pdk/setupAdminApp.ts +3 -0
- package/src/sdk/composables/usePdkAdminApi.ts +3 -0
- package/src/sdk/index.ts +1 -0
- package/src/sdk/requestHeaders.spec.ts +54 -0
- package/src/sdk/requestHeaders.ts +36 -0
- package/src/services/mutations/getModalMutationOptions.spec.ts +59 -0
- package/src/services/mutations/getModalMutationOptions.ts +23 -15
- package/src/services/mutations/index.ts +1 -0
- package/src/services/mutations/modalMutationToken.ts +27 -0
- package/src/services/print/createPdfObjectUrl.ts +6 -0
- package/src/services/print/index.ts +3 -0
- package/src/services/print/labelsNotification.ts +52 -0
- package/src/services/print/pollLabelPdf.spec.ts +58 -0
- package/src/services/print/pollLabelPdf.ts +47 -0
- package/src/stores/useModalStore.spec.ts +33 -0
- package/src/stores/useModalStore.ts +13 -1
- package/src/stores/useNotificationStore.spec.ts +30 -0
- package/src/stores/useNotificationStore.ts +1 -1
- package/src/types/actions/endpoints.types.ts +1 -0
- package/src/types/actions/parameters.types.ts +2 -0
- package/src/types/admin.types.ts +12 -0
- package/src/types/common.types.ts +7 -0
- package/src/types/configuration.types.ts +14 -0
- package/src/types/sdk.types.ts +16 -0
- package/src/utils/encodeArrayParameter.spec.ts +26 -0
- package/src/utils/encodeArrayParameter.ts +2 -1
|
@@ -10,12 +10,12 @@ let queryClient: QueryClient;
|
|
|
10
10
|
/**
|
|
11
11
|
* Instantiate vue query client if it does not exist yet and provide it to the app.
|
|
12
12
|
*/
|
|
13
|
-
export const createVueQueryPlugin: PdkAppPlugin = ({context, logger}) => {
|
|
13
|
+
export const createVueQueryPlugin: PdkAppPlugin = ({context, logger, queryClient: instanceQueryClient}) => {
|
|
14
14
|
return {
|
|
15
15
|
install: (app) => {
|
|
16
16
|
logger.debug('Installing vue-query plugin');
|
|
17
17
|
|
|
18
|
-
queryClient ??= createQueryClient();
|
|
18
|
+
const client = instanceQueryClient ?? (queryClient ??= createQueryClient());
|
|
19
19
|
|
|
20
20
|
// Fill the query client with the context data
|
|
21
21
|
Object.entries(context).forEach(([contextKey, value]) => {
|
|
@@ -23,26 +23,26 @@ export const createVueQueryPlugin: PdkAppPlugin = ({context, logger}) => {
|
|
|
23
23
|
return;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
client.setQueryData([BackendEndpoint.FetchContext, contextKey], value);
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
const instanceContext = createInstanceContext(context);
|
|
30
|
-
|
|
30
|
+
client.setQueryData([BackendEndpoint.FetchContext, AdminContextKey.Instance], instanceContext);
|
|
31
31
|
|
|
32
32
|
// Add each order and its shipments to the query client
|
|
33
33
|
if (context.orderData) {
|
|
34
34
|
context.orderData.forEach((order) => {
|
|
35
|
-
fillShipmentsQueryData(
|
|
35
|
+
fillShipmentsQueryData(client, order.shipments, order);
|
|
36
36
|
});
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
if (context.productData) {
|
|
40
40
|
context.productData.forEach((product) => {
|
|
41
|
-
|
|
41
|
+
client.setQueryData([BackendEndpoint.FetchProducts, {id: product.externalIdentifier}], product);
|
|
42
42
|
});
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
app.use(VueQueryPlugin, {queryClient});
|
|
45
|
+
app.use(VueQueryPlugin, {queryClient: client});
|
|
46
46
|
},
|
|
47
47
|
};
|
|
48
48
|
};
|
package/src/pdk/setupAdminApp.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {type App} from 'vue';
|
|
2
2
|
import {type AdminAppConfig} from '../types';
|
|
3
|
+
import {setRequestHeadersProvider} from '../sdk/requestHeaders';
|
|
3
4
|
import {testIdDirective} from './testIdDirective';
|
|
4
5
|
import {
|
|
5
6
|
createContextPlugin,
|
|
@@ -10,6 +11,8 @@ import {
|
|
|
10
11
|
} from './instance';
|
|
11
12
|
|
|
12
13
|
export const setupAdminApp = (app: App<Element>, appConfig: AdminAppConfig): void => {
|
|
14
|
+
setRequestHeadersProvider(appConfig.config.getRequestHeaders);
|
|
15
|
+
|
|
13
16
|
app.use(createStorePlugin(appConfig));
|
|
14
17
|
app.use(createVueQueryPlugin(appConfig));
|
|
15
18
|
app.use(createContextPlugin(appConfig));
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {createGlobalState} from '@vueuse/core';
|
|
2
2
|
import {createMyParcelSdk, type HttpMethod, type MyParcelSdk} from '@myparcel-dev/sdk';
|
|
3
3
|
import {type BackendEndpoint} from '@myparcel-dev/pdk-common';
|
|
4
|
+
import {addRequestHeaders} from '../requestHeaders';
|
|
4
5
|
import {AbstractPdkEndpoint} from '../endpoints';
|
|
5
6
|
import {PdkFetchClient} from '../PdkFetchClient';
|
|
6
7
|
import {useGlobalContext} from '../../composables';
|
|
@@ -15,6 +16,8 @@ export const usePdkAdminApi = createGlobalState((): MyParcelSdk<AbstractPdkEndpo
|
|
|
15
16
|
baseUrl: globalContext.baseUrl,
|
|
16
17
|
});
|
|
17
18
|
|
|
19
|
+
client.interceptors.request.use(addRequestHeaders);
|
|
20
|
+
|
|
18
21
|
const pdkEndpoints = Object.entries(globalContext.endpoints).map(([endpointName, options]) => {
|
|
19
22
|
// Effectively cast null as 'undefined' to prevent the SDK casting the prefix property to the string 'null'.
|
|
20
23
|
const property = typeof options.property === 'string' ? options.property : undefined;
|
package/src/sdk/index.ts
CHANGED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {afterEach, describe, expect, it} from 'vitest';
|
|
2
|
+
import {addRequestHeaders, setRequestHeadersProvider} from './requestHeaders';
|
|
3
|
+
|
|
4
|
+
describe('addRequestHeaders', () => {
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
setRequestHeadersProvider(undefined);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('returns the options unchanged without a provider', async () => {
|
|
10
|
+
const options = {headers: {Accept: 'application/json'}};
|
|
11
|
+
|
|
12
|
+
expect(await addRequestHeaders(options)).toEqual({headers: {Accept: 'application/json'}});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('merges the headers of a synchronous provider', async () => {
|
|
16
|
+
setRequestHeadersProvider(() => ({Authorization: 'Bearer one'}));
|
|
17
|
+
|
|
18
|
+
expect(await addRequestHeaders({headers: {Accept: 'application/json'}})).toEqual({
|
|
19
|
+
headers: {Accept: 'application/json', Authorization: 'Bearer one'},
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('merges the headers of an asynchronous provider', async () => {
|
|
24
|
+
setRequestHeadersProvider(() => Promise.resolve({Authorization: 'Bearer two'}));
|
|
25
|
+
|
|
26
|
+
expect(await addRequestHeaders({})).toEqual({headers: {Authorization: 'Bearer two'}});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('calls the provider for every request', async () => {
|
|
30
|
+
let token = 0;
|
|
31
|
+
setRequestHeadersProvider(() => ({Authorization: `Bearer ${++token}`}));
|
|
32
|
+
|
|
33
|
+
await addRequestHeaders({});
|
|
34
|
+
const second = await addRequestHeaders({});
|
|
35
|
+
|
|
36
|
+
expect(second.headers).toEqual({Authorization: 'Bearer 2'});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('lets the provider override a static header', async () => {
|
|
40
|
+
setRequestHeadersProvider(() => ({Authorization: 'Bearer new'}));
|
|
41
|
+
|
|
42
|
+
expect(await addRequestHeaders({headers: {Authorization: 'Bearer old'}})).toEqual({
|
|
43
|
+
headers: {Authorization: 'Bearer new'},
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('replaces a header with the same name in another casing', async () => {
|
|
48
|
+
setRequestHeadersProvider(() => ({Authorization: 'Bearer fresh'}));
|
|
49
|
+
|
|
50
|
+
expect(await addRequestHeaders({headers: {Accept: 'application/json', authorization: 'Bearer expired'}})).toEqual({
|
|
51
|
+
headers: {Accept: 'application/json', Authorization: 'Bearer fresh'},
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export type RequestHeaders = Record<string, string>;
|
|
2
|
+
|
|
3
|
+
export type RequestHeadersProvider = () => RequestHeaders | Promise<RequestHeaders>;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Module level, because the PDK API client is a global state that is often
|
|
7
|
+
* created inside a query function, where inject() is not available.
|
|
8
|
+
*/
|
|
9
|
+
let provider: RequestHeadersProvider | undefined;
|
|
10
|
+
|
|
11
|
+
export const setRequestHeadersProvider = (newProvider: RequestHeadersProvider | undefined): void => {
|
|
12
|
+
provider = newProvider;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Header names are case-insensitive: drop an existing header that the provider
|
|
17
|
+
* sets in another casing, so fetch does not send both values.
|
|
18
|
+
*/
|
|
19
|
+
const mergeHeaders = (headers: RequestHeaders, extraHeaders: RequestHeaders): RequestHeaders => {
|
|
20
|
+
const extraNames = Object.keys(extraHeaders).map((name) => name.toLowerCase());
|
|
21
|
+
const kept = Object.entries(headers).filter(([name]) => !extraNames.includes(name.toLowerCase()));
|
|
22
|
+
|
|
23
|
+
return {...Object.fromEntries(kept), ...extraHeaders};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Request interceptor. Calls the provider for every request, so a short-lived
|
|
28
|
+
* token is always current.
|
|
29
|
+
*/
|
|
30
|
+
export const addRequestHeaders = async <T extends {headers?: RequestHeaders}>(options: T): Promise<T> => {
|
|
31
|
+
if (!provider) {
|
|
32
|
+
return options;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {...options, headers: mergeHeaders(options.headers ?? {}, await provider())};
|
|
36
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import {beforeEach, describe, expect, it, vi} from 'vitest';
|
|
4
|
+
import {createPinia, setActivePinia} from 'pinia';
|
|
5
|
+
import {useModalStore, useNotificationStore} from '../../stores';
|
|
6
|
+
import {AdminModalKey} from '../../data';
|
|
7
|
+
import {getModalMutationOptions} from './getModalMutationOptions';
|
|
8
|
+
|
|
9
|
+
describe('getModalMutationOptions', () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
setActivePinia(createPinia());
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('changes the modal that started the mutation', () => {
|
|
15
|
+
const modalStore = useModalStore();
|
|
16
|
+
const options = getModalMutationOptions();
|
|
17
|
+
|
|
18
|
+
modalStore.open(AdminModalKey.ShipmentOptions);
|
|
19
|
+
modalStore.loading = true;
|
|
20
|
+
const context = options.onMutate?.(undefined);
|
|
21
|
+
|
|
22
|
+
options.onError?.(new Error('failed'), undefined, context);
|
|
23
|
+
options.onSettled?.(undefined, null, undefined, context);
|
|
24
|
+
options.onSuccess?.(undefined, undefined, context);
|
|
25
|
+
|
|
26
|
+
expect(useNotificationStore().notifications).toHaveLength(1);
|
|
27
|
+
expect(modalStore.loading).toBe(false);
|
|
28
|
+
expect(modalStore.opened).toBeNull();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('leaves a modal alone that the user opened after the mutation started', () => {
|
|
32
|
+
const modalStore = useModalStore();
|
|
33
|
+
const options = getModalMutationOptions();
|
|
34
|
+
|
|
35
|
+
modalStore.open(AdminModalKey.ShipmentOptions);
|
|
36
|
+
const context = options.onMutate?.(undefined);
|
|
37
|
+
|
|
38
|
+
// The user closes the modal with Escape and opens the modal of another order.
|
|
39
|
+
modalStore.close();
|
|
40
|
+
modalStore.open(AdminModalKey.ShipmentOptions, {orderIds: ['2']});
|
|
41
|
+
modalStore.loading = true;
|
|
42
|
+
|
|
43
|
+
options.onError?.(new Error('failed'), undefined, context);
|
|
44
|
+
options.onSettled?.(undefined, null, undefined, context);
|
|
45
|
+
options.onSuccess?.(undefined, undefined, context);
|
|
46
|
+
|
|
47
|
+
expect(useNotificationStore().notifications).toHaveLength(0);
|
|
48
|
+
expect(modalStore.loading).toBe(true);
|
|
49
|
+
expect(modalStore.opened).toBe(AdminModalKey.ShipmentOptions);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('still runs the onMutate it replaces', () => {
|
|
53
|
+
const onMutate = vi.fn();
|
|
54
|
+
|
|
55
|
+
void getModalMutationOptions({onMutate}).onMutate?.(undefined);
|
|
56
|
+
|
|
57
|
+
expect(onMutate).toHaveBeenCalledOnce();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -3,22 +3,30 @@ import {type MutationOptions} from '@tanstack/vue-query';
|
|
|
3
3
|
import {addErrorToNotifications} from '../addErrorToNotifications';
|
|
4
4
|
import {useModalStore} from '../../stores';
|
|
5
5
|
import {NotificationCategory} from '../../data';
|
|
6
|
+
import {isSameModal, keepModalToken, type ModalMutationContext} from './modalMutationToken';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Options for a mutation that a modal starts. The callbacks only change the
|
|
10
|
+
* modal when it is still the modal that started the mutation. Pass the default
|
|
11
|
+
* mutation options, so their onMutate still runs.
|
|
12
|
+
*/
|
|
13
|
+
export const getModalMutationOptions = <TData = unknown, TError = unknown, TVariables = void>(
|
|
14
|
+
defaultOptions: MutationOptions<TData, TError, TVariables> = {},
|
|
15
|
+
): MutationOptions<TData, TError, TVariables, ModalMutationContext> => ({
|
|
16
|
+
onMutate: keepModalToken(defaultOptions.onMutate),
|
|
17
|
+
onSuccess(data, variables, context) {
|
|
18
|
+
if (isSameModal(context)) {
|
|
15
19
|
useModalStore().close();
|
|
16
|
-
}
|
|
17
|
-
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
onSettled(data, error, variables, context) {
|
|
23
|
+
if (isSameModal(context)) {
|
|
18
24
|
useModalStore().loading = false;
|
|
19
|
-
}
|
|
20
|
-
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
onError(error, variables, context) {
|
|
28
|
+
if (isSameModal(context)) {
|
|
21
29
|
addErrorToNotifications(error, NotificationCategory.Modal);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
};
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {type ModalToken, useModalStore} from '../../stores';
|
|
2
|
+
|
|
3
|
+
export type ModalMutationContext = {modalToken: ModalToken | null};
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates an onMutate that keeps the token of the modal that is open when the
|
|
7
|
+
* mutation starts. Pass the onMutate it replaces, for example the default one
|
|
8
|
+
* of the query client, so that one still runs.
|
|
9
|
+
*/
|
|
10
|
+
export const keepModalToken =
|
|
11
|
+
<TVariables>(onMutate?: (variables: TVariables) => unknown) =>
|
|
12
|
+
(variables: TVariables): ModalMutationContext => {
|
|
13
|
+
void onMutate?.(variables);
|
|
14
|
+
|
|
15
|
+
return {modalToken: useModalStore().token};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Tells if the modal that was open when the mutation started is still open.
|
|
20
|
+
* Use it in late mutation callbacks, so they do not change a modal that the
|
|
21
|
+
* user opened in the meantime.
|
|
22
|
+
*/
|
|
23
|
+
export const isSameModal = (context: ModalMutationContext | undefined): boolean => {
|
|
24
|
+
const modalToken = context?.modalToken;
|
|
25
|
+
|
|
26
|
+
return Boolean(modalToken) && modalToken === useModalStore().token;
|
|
27
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Turns a base64 pdf into an object url that a link can open or download. */
|
|
2
|
+
export const createPdfObjectUrl = (pdf: string): string => {
|
|
3
|
+
const bytes = Uint8Array.from(atob(pdf), (char) => char.charCodeAt(0));
|
|
4
|
+
|
|
5
|
+
return URL.createObjectURL(new Blob([bytes], {type: 'application/pdf'}));
|
|
6
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {Variant} from '@myparcel-dev/pdk-common';
|
|
2
|
+
import {type PdkNotification} from '../../types';
|
|
3
|
+
import {useNotificationStore} from '../../stores';
|
|
4
|
+
import {NotificationCategory} from '../../data';
|
|
5
|
+
import {useLanguage} from '../../composables';
|
|
6
|
+
|
|
7
|
+
export const LABELS_NOTIFICATION_ID = 'print-labels';
|
|
8
|
+
|
|
9
|
+
const replace = (notification: Omit<PdkNotification, 'id' | 'category'>): void => {
|
|
10
|
+
const store = useNotificationStore();
|
|
11
|
+
|
|
12
|
+
store.remove(LABELS_NOTIFICATION_ID);
|
|
13
|
+
store.add({...notification, id: LABELS_NOTIFICATION_ID, category: NotificationCategory.General});
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const showLabelsLoading = (): void => {
|
|
17
|
+
replace({
|
|
18
|
+
variant: Variant.Info,
|
|
19
|
+
content: useLanguage().translate('notification_labels_loading'),
|
|
20
|
+
loading: true,
|
|
21
|
+
timeout: false,
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const showLabelsReady = (open: () => void): void => {
|
|
26
|
+
const {translate} = useLanguage();
|
|
27
|
+
|
|
28
|
+
replace({
|
|
29
|
+
variant: Variant.Success,
|
|
30
|
+
content: translate('notification_labels_ready'),
|
|
31
|
+
timeout: false,
|
|
32
|
+
action: {
|
|
33
|
+
label: translate('action_open_labels'),
|
|
34
|
+
onClick() {
|
|
35
|
+
open();
|
|
36
|
+
hideLabelsNotification();
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const showLabelsFailed = (): void => {
|
|
43
|
+
replace({
|
|
44
|
+
variant: Variant.Error,
|
|
45
|
+
content: useLanguage().translate('notification_labels_failed'),
|
|
46
|
+
timeout: false,
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const hideLabelsNotification = (): void => {
|
|
51
|
+
useNotificationStore().remove(LABELS_NOTIFICATION_ID);
|
|
52
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
|
2
|
+
import {pollLabelPdf} from './pollLabelPdf';
|
|
3
|
+
|
|
4
|
+
const fetchLabelPdf = vi.fn();
|
|
5
|
+
|
|
6
|
+
vi.mock('../../sdk', () => ({usePdkAdminApi: () => ({fetchLabelPdf})}));
|
|
7
|
+
|
|
8
|
+
describe('pollLabelPdf', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
vi.useFakeTimers();
|
|
11
|
+
fetchLabelPdf.mockReset();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
vi.useRealTimers();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('asks the backend again until the pdf is ready', async () => {
|
|
19
|
+
fetchLabelPdf
|
|
20
|
+
.mockResolvedValueOnce({pending: true})
|
|
21
|
+
.mockResolvedValueOnce({pending: true})
|
|
22
|
+
.mockResolvedValueOnce({data: 'cGRm'});
|
|
23
|
+
|
|
24
|
+
const promise = pollLabelPdf('label_hash', {interval: 1000, timeout: 10_000});
|
|
25
|
+
|
|
26
|
+
await vi.advanceTimersByTimeAsync(2000);
|
|
27
|
+
|
|
28
|
+
await expect(promise).resolves.toBe('cGRm');
|
|
29
|
+
expect(fetchLabelPdf).toHaveBeenCalledTimes(3);
|
|
30
|
+
expect(fetchLabelPdf).toHaveBeenCalledWith({parameters: {labelId: 'label_hash'}});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('gives up when the pdf is not ready before the timeout', async () => {
|
|
34
|
+
fetchLabelPdf.mockResolvedValue({pending: true});
|
|
35
|
+
|
|
36
|
+
const promise = pollLabelPdf('label_hash', {interval: 1000, timeout: 3000});
|
|
37
|
+
const assertion = expect(promise).rejects.toThrow('timed out');
|
|
38
|
+
|
|
39
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
40
|
+
|
|
41
|
+
await assertion;
|
|
42
|
+
expect(fetchLabelPdf).toHaveBeenCalledTimes(4);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('stops when the backend reports an error', async () => {
|
|
46
|
+
fetchLabelPdf.mockRejectedValueOnce(new Error('api error'));
|
|
47
|
+
|
|
48
|
+
await expect(pollLabelPdf('label_hash', {interval: 1000, timeout: 3000})).rejects.toThrow('api error');
|
|
49
|
+
expect(fetchLabelPdf).toHaveBeenCalledTimes(1);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('stops when the backend returns an unknown response', async () => {
|
|
53
|
+
fetchLabelPdf.mockResolvedValueOnce('<html>Fatal error</html>');
|
|
54
|
+
|
|
55
|
+
await expect(pollLabelPdf('label_hash', {interval: 1000, timeout: 3000})).rejects.toThrow('Unexpected response');
|
|
56
|
+
expect(fetchLabelPdf).toHaveBeenCalledTimes(1);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {isOfType} from '@myparcel-dev/ts-utils';
|
|
2
|
+
import {type PdfDataResponse, type PdfPendingResponse} from '../../types';
|
|
3
|
+
import {usePdkAdminApi} from '../../sdk';
|
|
4
|
+
|
|
5
|
+
type PollOptions = {
|
|
6
|
+
interval?: number;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const DEFAULT_INTERVAL = 3000;
|
|
11
|
+
|
|
12
|
+
/** Ten minutes. */
|
|
13
|
+
const DEFAULT_TIMEOUT = 600_000;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Ask the backend for a labels pdf until the API has generated it. Resolves with the base64 pdf.
|
|
17
|
+
*/
|
|
18
|
+
export const pollLabelPdf = async (
|
|
19
|
+
labelId: string,
|
|
20
|
+
{interval = DEFAULT_INTERVAL, timeout = DEFAULT_TIMEOUT}: PollOptions = {},
|
|
21
|
+
): Promise<string> => {
|
|
22
|
+
const pdk = usePdkAdminApi();
|
|
23
|
+
const start = Date.now();
|
|
24
|
+
|
|
25
|
+
for (;;) {
|
|
26
|
+
const response = (await pdk.fetchLabelPdf({
|
|
27
|
+
// @ts-expect-error custom endpoints are not typed correctly
|
|
28
|
+
parameters: {labelId},
|
|
29
|
+
})) as PdfDataResponse | PdfPendingResponse;
|
|
30
|
+
|
|
31
|
+
if (isOfType<PdfDataResponse>(response, 'data')) {
|
|
32
|
+
return response.data;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!isOfType<PdfPendingResponse>(response, 'pending')) {
|
|
36
|
+
throw new Error(`Unexpected response while polling for label pdf ${labelId}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (Date.now() - start >= timeout) {
|
|
40
|
+
throw new Error(`Polling for label pdf ${labelId} timed out`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
await new Promise((resolve) => {
|
|
44
|
+
setTimeout(resolve, interval);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import {beforeEach, describe, expect, it} from 'vitest';
|
|
4
|
+
import {createPinia, setActivePinia} from 'pinia';
|
|
5
|
+
import {AdminModalKey} from '../data';
|
|
6
|
+
import {useModalStore} from './useModalStore';
|
|
7
|
+
|
|
8
|
+
describe('useModalStore', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
setActivePinia(createPinia());
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('gives every opened modal a new token', () => {
|
|
14
|
+
const modalStore = useModalStore();
|
|
15
|
+
|
|
16
|
+
modalStore.open(AdminModalKey.ShipmentOptions);
|
|
17
|
+
const first = modalStore.token;
|
|
18
|
+
|
|
19
|
+
modalStore.open(AdminModalKey.ShipmentOptions);
|
|
20
|
+
|
|
21
|
+
expect(first).toEqual({modal: AdminModalKey.ShipmentOptions});
|
|
22
|
+
expect(modalStore.token).not.toBe(first);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('removes the token when the modal closes', () => {
|
|
26
|
+
const modalStore = useModalStore();
|
|
27
|
+
|
|
28
|
+
modalStore.open(AdminModalKey.PrintOptions);
|
|
29
|
+
modalStore.close();
|
|
30
|
+
|
|
31
|
+
expect(modalStore.token).toBeNull();
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {ref, type Ref, type UnwrapNestedRefs} from 'vue';
|
|
1
|
+
import {ref, type Ref, shallowRef, type UnwrapNestedRefs} from 'vue';
|
|
2
2
|
import {defineStore} from 'pinia';
|
|
3
3
|
import {type AdminModalContext} from '../types';
|
|
4
4
|
import {type AdminModalKey, NotificationCategory} from '../data';
|
|
@@ -9,10 +9,17 @@ type ModalOpenFn = <K extends AdminModalKey>(modal: K, context?: AdminModalConte
|
|
|
9
9
|
|
|
10
10
|
type ModalCloseFn = <K extends AdminModalKey>(modal: K) => void;
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Identifies one opened modal. Every open() creates a new object, so a token
|
|
14
|
+
* never matches another modal, also not a modal in another admin instance.
|
|
15
|
+
*/
|
|
16
|
+
export type ModalToken = Readonly<{modal: AdminModalKey}>;
|
|
17
|
+
|
|
12
18
|
type ModalStore = {
|
|
13
19
|
loading: Ref<boolean>;
|
|
14
20
|
opened: Ref<AdminModalKey | null>;
|
|
15
21
|
context: Ref<UnwrapNestedRefs<AdminModalContext>>;
|
|
22
|
+
token: Ref<ModalToken | null>;
|
|
16
23
|
onOpen(callback: ModalOpenFn): void;
|
|
17
24
|
onClose(callback: ModalCloseFn): void;
|
|
18
25
|
open<K extends AdminModalKey>(modal: K, newContext?: AdminModalContext<K>): void;
|
|
@@ -24,6 +31,7 @@ export const useModalStore = defineStore('modal', (): ModalStore => {
|
|
|
24
31
|
|
|
25
32
|
const opened = ref<AdminModalKey | null>(null);
|
|
26
33
|
const context = ref<AdminModalContext>(null);
|
|
34
|
+
const token = shallowRef<ModalToken | null>(null);
|
|
27
35
|
|
|
28
36
|
const openHooks = ref<ModalOpenFn[]>([]);
|
|
29
37
|
const closeHooks = ref<ModalCloseFn[]>([]);
|
|
@@ -35,6 +43,8 @@ export const useModalStore = defineStore('modal', (): ModalStore => {
|
|
|
35
43
|
|
|
36
44
|
context,
|
|
37
45
|
|
|
46
|
+
token,
|
|
47
|
+
|
|
38
48
|
onOpen(callback) {
|
|
39
49
|
openHooks.value.push(callback);
|
|
40
50
|
},
|
|
@@ -49,6 +59,7 @@ export const useModalStore = defineStore('modal', (): ModalStore => {
|
|
|
49
59
|
// @ts-ignore
|
|
50
60
|
context.value = newContext;
|
|
51
61
|
opened.value = modal;
|
|
62
|
+
token.value = {modal};
|
|
52
63
|
|
|
53
64
|
openHooks.value.forEach((hook) => hook(modal, newContext));
|
|
54
65
|
},
|
|
@@ -59,6 +70,7 @@ export const useModalStore = defineStore('modal', (): ModalStore => {
|
|
|
59
70
|
|
|
60
71
|
opened.value = null;
|
|
61
72
|
context.value = null;
|
|
73
|
+
token.value = null;
|
|
62
74
|
closeHooks.value.forEach((hook) => hook(modal));
|
|
63
75
|
},
|
|
64
76
|
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it} from 'vitest';
|
|
2
|
+
import {createPinia, setActivePinia} from 'pinia';
|
|
3
|
+
import {Variant} from '@myparcel-dev/pdk-common';
|
|
4
|
+
import {NotificationCategory, NotificationSource} from '../data';
|
|
5
|
+
import {useNotificationStore} from './useNotificationStore';
|
|
6
|
+
|
|
7
|
+
const ids = () => useNotificationStore().notifications.map((notification) => notification.id);
|
|
8
|
+
|
|
9
|
+
describe('useNotificationStore', () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
setActivePinia(createPinia());
|
|
12
|
+
|
|
13
|
+
const store = useNotificationStore();
|
|
14
|
+
|
|
15
|
+
store.add({id: 'from-api', variant: Variant.Error, timeout: false}, {source: NotificationSource.Api});
|
|
16
|
+
store.add({id: 'labels', variant: Variant.Info, category: NotificationCategory.General, timeout: false});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('removes only the notifications from the given source', () => {
|
|
20
|
+
useNotificationStore().remove(NotificationSource.Api);
|
|
21
|
+
|
|
22
|
+
expect(ids()).toEqual(['labels']);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('removes a notification by id', () => {
|
|
26
|
+
useNotificationStore().remove('labels');
|
|
27
|
+
|
|
28
|
+
expect(ids()).toEqual(['from-api']);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -54,7 +54,7 @@ export const useNotificationStore = defineStore('notifications', () => {
|
|
|
54
54
|
let filter: (notification: ResolvedNotification) => boolean;
|
|
55
55
|
|
|
56
56
|
if (isEnumValue(input, NotificationSource)) {
|
|
57
|
-
filter = (notification) => notification.tags?.source
|
|
57
|
+
filter = (notification) => notification.tags?.source !== input;
|
|
58
58
|
} else {
|
|
59
59
|
filter = (notification) => notification.id !== input;
|
|
60
60
|
}
|
|
@@ -61,6 +61,8 @@ export interface EndpointMutationInputMap extends Record<BackendEndpoint, object
|
|
|
61
61
|
[BackendEndpoint.UpdateShipments]: OrderIdParameters & ShipmentIdParameters;
|
|
62
62
|
[BackendEndpoint.PrintShipments]: OrderIdParameters & ShipmentIdParameters & OptionalFormParameters & LabelParameters;
|
|
63
63
|
|
|
64
|
+
[BackendEndpoint.FetchLabelPdf]: {labelId: string};
|
|
65
|
+
|
|
64
66
|
[BackendEndpoint.FetchProducts]: ProductIdParameters;
|
|
65
67
|
|
|
66
68
|
[BackendEndpoint.UpdatePluginSettings]: FormParameters;
|
package/src/types/admin.types.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import {type Component} from 'vue';
|
|
2
|
+
import {type Pinia} from 'pinia';
|
|
3
|
+
import {type QueryClient} from '@tanstack/vue-query';
|
|
2
4
|
import {type PdkLogger} from '../services';
|
|
3
5
|
import {
|
|
4
6
|
type ADMIN_COMPONENT_PREFIX,
|
|
@@ -40,6 +42,16 @@ export interface AdminAppConfig {
|
|
|
40
42
|
context: AdminContextObject;
|
|
41
43
|
logger: PdkLogger;
|
|
42
44
|
view?: AdminView;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Store of the PdkAdmin instance, shared by its apps. Without it, all apps share one store.
|
|
48
|
+
*/
|
|
49
|
+
store?: Pinia;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Query client of the PdkAdmin instance, shared by its apps. Without it, all apps share one client.
|
|
53
|
+
*/
|
|
54
|
+
queryClient?: QueryClient;
|
|
43
55
|
}
|
|
44
56
|
|
|
45
57
|
export interface AdminInstance extends Omit<AdminAppConfig, 'context'> {
|