Skip to content

Commit 48f6c28

Browse files
Corey Robertsonelasticmachine
andauthored
[Canvas] Fixes the Copy Post Url link (#54831) (#55114)
* Fixes the Copy Post Url link * Adds tests Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
1 parent 7f04faf commit 48f6c28

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

x-pack/legacy/plugins/canvas/public/components/workpad_header/workpad_export/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@ export const WorkpadExport = compose<ComponentProps, {}>(
6262
enabled,
6363
getExportUrl: type => {
6464
if (type === 'pdf') {
65-
const { createPdfUri } = getPdfUrl(
66-
workpad,
67-
{ pageCount },
68-
kibana.services.http.basePath.prepend
69-
);
70-
return getAbsoluteUrl(createPdfUri);
65+
const pdfUrl = getPdfUrl(workpad, { pageCount }, kibana.services.http.basePath.prepend);
66+
return getAbsoluteUrl(pdfUrl);
7167
}
7268

7369
throw new Error(strings.getUnknownExportErrorMessage(type));
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
jest.mock('../../../../common/lib/fetch');
8+
9+
import { getPdfUrl, createPdf } from './utils';
10+
import { workpads } from '../../../../__tests__/fixtures/workpads';
11+
import { fetch } from '../../../../common/lib/fetch';
12+
13+
const addBasePath = jest.fn().mockImplementation(s => `basepath/${s}`);
14+
const workpad = workpads[0];
15+
16+
test('getPdfUrl returns the correct url', () => {
17+
const url = getPdfUrl(workpad, { pageCount: 2 }, addBasePath);
18+
19+
expect(url).toMatchInlineSnapshot(
20+
`"basepath//api/reporting/generate/printablePdf?jobParams=(browserTimezone:America%2FPhoenix,layout:(dimensions:(height:0,width:0),id:preserve_layout),objectType:'canvas%20workpad',relativeUrls:!(%2Fapp%2Fcanvas%23%2Fexport%2Fworkpad%2Fpdf%2Fbase-workpad%2Fpage%2F1,%2Fapp%2Fcanvas%23%2Fexport%2Fworkpad%2Fpdf%2Fbase-workpad%2Fpage%2F2),title:'base%20workpad')"`
21+
);
22+
});
23+
24+
test('createPdf posts to create the pdf', () => {
25+
createPdf(workpad, { pageCount: 2 }, addBasePath);
26+
27+
expect(fetch.post).toBeCalled();
28+
29+
const args = (fetch.post as jest.MockedFunction<typeof fetch.post>).mock.calls[0];
30+
31+
expect(args[0]).toMatchInlineSnapshot(`"basepath//api/reporting/generate/printablePdf"`);
32+
expect(args[1]).toMatchInlineSnapshot(`
33+
Object {
34+
"jobParams": "(browserTimezone:America/Phoenix,layout:(dimensions:(height:0,width:0),id:preserve_layout),objectType:'canvas workpad',relativeUrls:!(/app/canvas#/export/workpad/pdf/base-workpad/page/1,/app/canvas#/export/workpad/pdf/base-workpad/page/2),title:'base workpad')",
35+
}
36+
`);
37+
});

x-pack/legacy/plugins/canvas/public/components/workpad_header/workpad_export/utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import rison from 'rison-node';
88
// @ts-ignore Untyped local.
99
import { fetch } from '../../../../common/lib/fetch';
10+
import { getStartPlugins } from '../../../legacy';
1011
import { CanvasWorkpad } from '../../../../types';
1112

1213
// type of the desired pdf output (print or preserve_layout)
@@ -25,7 +26,7 @@ interface PdfUrlData {
2526
createPdfPayload: { jobParams: string };
2627
}
2728

28-
export function getPdfUrl(
29+
function getPdfUrlParts(
2930
{ id, name: title, width, height }: CanvasWorkpad,
3031
{ pageCount }: PageCount,
3132
addBasePath: (path: string) => string
@@ -68,7 +69,16 @@ export function getPdfUrl(
6869
};
6970
}
7071

72+
export function getPdfUrl(...args: Arguments): string {
73+
const urlParts = getPdfUrlParts(...args);
74+
75+
return `${urlParts.createPdfUri}?${getStartPlugins().__LEGACY.QueryString.param(
76+
'jobParams',
77+
urlParts.createPdfPayload.jobParams
78+
)}`;
79+
}
80+
7181
export function createPdf(...args: Arguments) {
72-
const { createPdfUri, createPdfPayload } = getPdfUrl(...args);
82+
const { createPdfUri, createPdfPayload } = getPdfUrlParts(...args);
7383
return fetch.post(createPdfUri, createPdfPayload);
7484
}

x-pack/legacy/plugins/canvas/public/legacy.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { absoluteToParsedUrl } from 'ui/url/absolute_to_parsed_url'; // eslint-d
1515
import { Storage } from '../../../../../src/plugins/kibana_utils/public'; // eslint-disable-line import/order
1616
// @ts-ignore Untyped Kibana Lib
1717
import { formatMsg } from 'ui/notify/lib/format_msg'; // eslint-disable-line import/order
18+
// @ts-ignore Untyped Kibana Lib
19+
import { QueryString } from 'ui/utils/query_string'; // eslint-disable-line import/order
1820

1921
const shimCoreSetup = {
2022
...npSetup.core,
@@ -30,6 +32,7 @@ const shimStartPlugins: CanvasStartDeps = {
3032
absoluteToParsedUrl,
3133
// ToDo: Copy directly into canvas
3234
formatMsg,
35+
QueryString,
3336
// ToDo: Remove in favor of core.application.register
3437
setRootController: chrome.setRootController,
3538
storage: Storage,

x-pack/legacy/plugins/canvas/public/plugin.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface CanvasStartDeps {
3939
__LEGACY: {
4040
absoluteToParsedUrl: (url: string, basePath: string) => any;
4141
formatMsg: any;
42+
QueryString: any;
4243
setRootController: Chrome['setRootController'];
4344
storage: typeof Storage;
4445
trackSubUrlForApp: Chrome['trackSubUrlForApp'];

0 commit comments

Comments
 (0)