Skip to content

Commit c6c300e

Browse files
[Canvas][tech-debt] Add Typescript to apps directory (#73766)
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 7dc33f9 commit c6c300e

File tree

24 files changed

+274
-263
lines changed

24 files changed

+274
-263
lines changed

x-pack/plugins/canvas/public/apps/export/export/__tests__/export_app.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
import React from 'react';
88
import { mount } from 'enzyme';
9-
// @ts-expect-error untyped local
10-
import { ExportApp } from '../export_app';
9+
import { ExportApp } from '../export_app.component';
10+
import { CanvasWorkpad } from '../../../../../types';
1111

1212
jest.mock('style-it', () => ({
1313
it: (css: string, Component: any) => Component,
@@ -23,7 +23,7 @@ jest.mock('../../../../components/link', () => ({
2323

2424
describe('<ExportApp />', () => {
2525
test('renders as expected', () => {
26-
const sampleWorkpad = {
26+
const sampleWorkpad = ({
2727
id: 'my-workpad-abcd',
2828
css: '',
2929
pages: [
@@ -34,7 +34,7 @@ describe('<ExportApp />', () => {
3434
elements: [3, 4, 5, 6],
3535
},
3636
],
37-
};
37+
} as any) as CanvasWorkpad;
3838

3939
const page1 = mount(
4040
<ExportApp workpad={sampleWorkpad} selectedPageIndex={0} initializeWorkpad={() => {}} />
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
import React, { FC, useEffect } from 'react';
8+
import PropTypes from 'prop-types';
9+
// @ts-expect-error untyped library
10+
import Style from 'style-it';
11+
// @ts-expect-error untyped local
12+
import { WorkpadPage } from '../../../components/workpad_page';
13+
import { Link } from '../../../components/link';
14+
import { CanvasWorkpad } from '../../../../types';
15+
16+
interface Props {
17+
workpad: CanvasWorkpad;
18+
selectedPageIndex: number;
19+
initializeWorkpad: () => void;
20+
}
21+
22+
export const ExportApp: FC<Props> = ({ workpad, selectedPageIndex, initializeWorkpad }) => {
23+
const { id, pages, height, width } = workpad;
24+
const activePage = pages[selectedPageIndex];
25+
const pageElementCount = activePage.elements.length;
26+
27+
useEffect(() => initializeWorkpad());
28+
29+
return (
30+
<div className="canvasExport" data-shared-page={selectedPageIndex + 1}>
31+
<div className="canvasExport__stage">
32+
<div className="canvasLayout__stageHeader">
33+
<Link name="loadWorkpad" params={{ id }}>
34+
Edit Workpad
35+
</Link>
36+
</div>
37+
{Style.it(
38+
workpad.css,
39+
<div className="canvasExport__stageContent" data-shared-items-count={pageElementCount}>
40+
<WorkpadPage
41+
isSelected
42+
key={activePage.id}
43+
pageId={activePage.id}
44+
height={height}
45+
width={width}
46+
registerLayout={() => {}}
47+
unregisterLayout={() => {}}
48+
/>
49+
</div>
50+
)}
51+
</div>
52+
</div>
53+
);
54+
};
55+
56+
ExportApp.propTypes = {
57+
workpad: PropTypes.shape({
58+
id: PropTypes.string.isRequired,
59+
pages: PropTypes.array.isRequired,
60+
}).isRequired,
61+
selectedPageIndex: PropTypes.number.isRequired,
62+
initializeWorkpad: PropTypes.func.isRequired,
63+
};

x-pack/plugins/canvas/public/apps/export/export/export_app.js

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
import { connect } from 'react-redux';
8+
import { initializeWorkpad } from '../../../state/actions/workpad';
9+
import { getWorkpad, getSelectedPageIndex } from '../../../state/selectors/workpad';
10+
import { ExportApp as Component } from './export_app.component';
11+
import { State } from '../../../../types';
12+
13+
export const ExportApp = connect(
14+
(state: State) => ({
15+
workpad: getWorkpad(state),
16+
selectedPageIndex: getSelectedPageIndex(state),
17+
}),
18+
(dispatch) => ({
19+
initializeWorkpad: () => dispatch(initializeWorkpad()),
20+
})
21+
)(Component);

x-pack/plugins/canvas/public/apps/export/export/index.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

x-pack/plugins/canvas/public/apps/workpad/workpad_app/load_workpad.js renamed to x-pack/plugins/canvas/public/apps/export/export/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import React from 'react';
8-
9-
export const LoadWorkpad = () => <div>Load a workpad...</div>;
7+
export { ExportApp } from './export_app';
8+
export { ExportApp as ExportAppComponent } from './export_app.component';

x-pack/plugins/canvas/public/apps/export/routes.js renamed to x-pack/plugins/canvas/public/apps/export/routes.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { Dispatch } from 'redux';
8+
// @ts-expect-error Untyped local
79
import * as workpadService from '../../lib/workpad_service';
810
import { setWorkpad } from '../../state/actions/workpad';
11+
// @ts-expect-error Untyped local
912
import { fetchAllRenderables } from '../../state/actions/elements';
13+
// @ts-expect-error Untyped local
1014
import { setPage } from '../../state/actions/pages';
15+
// @ts-expect-error Untyped local
1116
import { setAssets } from '../../state/actions/assets';
1217
import { ExportApp } from './export';
1318

@@ -18,7 +23,13 @@ export const routes = [
1823
{
1924
name: 'exportWorkpad',
2025
path: '/pdf/:id/page/:page',
21-
action: (dispatch) => async ({ params, router }) => {
26+
action: (dispatch: Dispatch) => async ({
27+
params,
28+
// @ts-expect-error Fix when Router is typed.
29+
router,
30+
}: {
31+
params: { id: string; page: string };
32+
}) => {
2233
// load workpad if given a new id via url param
2334
const fetchedWorkpad = await workpadService.get(params.id);
2435
const pageNumber = parseInt(params.page, 10);

x-pack/plugins/canvas/public/apps/home/home_app/home_app.js renamed to x-pack/plugins/canvas/public/apps/home/home_app/home_app.component.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import React from 'react';
7+
import React, { FC } from 'react';
88
import { EuiPage, EuiPageBody, EuiPageContent } from '@elastic/eui';
9+
// @ts-expect-error untyped local
910
import { WorkpadManager } from '../../../components/workpad_manager';
11+
// @ts-expect-error untyped local
1012
import { setDocTitle } from '../../../lib/doc_title';
1113

12-
export const HomeApp = ({ onLoad = () => {} }) => {
14+
interface Props {
15+
onLoad: () => void;
16+
}
17+
18+
export const HomeApp: FC<Props> = ({ onLoad = () => {} }) => {
1319
onLoad();
1420
setDocTitle('Canvas');
1521
return (

x-pack/plugins/canvas/public/apps/home/home_app/index.js renamed to x-pack/plugins/canvas/public/apps/home/home_app/home_app.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66

77
import { connect } from 'react-redux';
88
import { resetWorkpad } from '../../../state/actions/workpad';
9-
import { HomeApp as Component } from './home_app';
9+
import { HomeApp as Component } from './home_app.component';
1010

11-
const mapDispatchToProps = (dispatch) => ({
11+
export const HomeApp = connect(null, (dispatch) => ({
1212
onLoad() {
1313
dispatch(resetWorkpad());
1414
},
15-
});
16-
17-
export const HomeApp = connect(null, mapDispatchToProps)(Component);
15+
}))(Component);

0 commit comments

Comments
 (0)