Skip to content

Commit

Permalink
refactor(): use @dhis2/analytics in place of @dhis2/d2-ui-analytics (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardo authored Jun 27, 2019
1 parent 5cfecd9 commit c9f5c93
Show file tree
Hide file tree
Showing 28 changed files with 119 additions and 103 deletions.
3 changes: 1 addition & 2 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"webpack-bundle-analyzer": "^3.0.3"
},
"dependencies": {
"@dhis2/analytics": "2.1.1",
"@dhis2/d2-i18n": "^1.0.3",
"@dhis2/d2-ui-analytics": "^1.0.5",
"@dhis2/d2-ui-core": "6.1.0",
"@dhis2/d2-ui-file-menu": "6.1.0",
"@dhis2/d2-ui-interpretations": "6.1.0",
Expand All @@ -46,7 +46,6 @@
"chalk": "2.4.1",
"css-loader": "0.28.7",
"d2": "31.2.1",
"d2-charts-api": "33.0.1",
"d2-manifest": "^1.0.0",
"dotenv": "6.0.0",
"dotenv-expand": "4.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/api/analytics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DIMENSION_ID_PERIOD } from '@dhis2/d2-ui-analytics';
import { DIMENSION_ID_PERIOD } from '@dhis2/analytics';
import { getInstance } from 'd2';

export const apiDownloadImage = async (type, formData) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { connect } from 'react-redux';
import Button from '@material-ui/core/Button';
import MenuItem from '@material-ui/core/Button';
import { withStyles } from '@material-ui/core/styles';
import { AXIS_NAME_COLUMNS } from '@dhis2/d2-ui-analytics';
import { AXIS_NAME_COLUMNS } from '@dhis2/analytics';

import UpdateButton from '../../../UpdateButton/UpdateButton';
import Menu from './Menu';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
DIMENSION_ID_PERIOD,
DIMENSION_ID_ORGUNIT,
FIXED_DIMENSIONS,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

import HideButton from './HideButton';
import AddToLayoutButton from './AddToLayoutButton/AddToLayoutButton';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
DIMENSION_ID_DATA,
DIMENSION_ID_PERIOD,
DIMENSION_ID_ORGUNIT,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

import { DialogManager } from '../DialogManager';

Expand All @@ -18,7 +18,7 @@ jest.mock('@material-ui/core/DialogActions', () => props => (
<div id="mock-mui-dialog-actions">{props.children}</div>
));

jest.mock('@dhis2/d2-ui-analytics', () => {
jest.mock('@dhis2/analytics', () => {
const dataId = 'dx';
const periodId = 'pe';
const ouId = 'ou';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { DIMENSION_ID_PERIOD, DimensionsPanel } from '@dhis2/d2-ui-analytics';
import { DIMENSION_ID_PERIOD, DimensionsPanel } from '@dhis2/analytics';

import DialogManager from './Dialogs/DialogManager';
import DimensionOptions from './DimensionOptions/DimensionOptions';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { shallow } from 'enzyme';
import { DimensionsPanel } from '@dhis2/d2-ui-analytics';
import { DimensionsPanel } from '@dhis2/analytics';

import { Dimensions } from '../DimensionsPanel';

Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/components/Layout/Chip.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { connect } from 'react-redux';
import i18n from '@dhis2/d2-i18n';
import { FIXED_DIMENSIONS } from '@dhis2/d2-ui-analytics';
import { FIXED_DIMENSIONS } from '@dhis2/analytics';

import Menu from './Menu';
import Tooltip from './Tooltip';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AXIS_NAME_COLUMNS,
AXIS_NAMES,
DIMENSION_ID_DATA,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

import Chip from '../Chip';
import { sGetUi } from '../../../reducers/ui';
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/components/Layout/PieLayout/PieLayout.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { AXIS_NAME_COLUMNS, AXIS_NAME_FILTERS } from '@dhis2/d2-ui-analytics';
import { AXIS_NAME_COLUMNS, AXIS_NAME_FILTERS } from '@dhis2/analytics';

import DefaultAxis from '../DefaultLayout/DefaultAxis';
import defaultAxisStyles from '../DefaultLayout/styles/DefaultAxis.style';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { connect } from 'react-redux';
import { AXIS_NAME_FILTERS } from '@dhis2/d2-ui-analytics';
import { AXIS_NAME_FILTERS } from '@dhis2/analytics';

import DefaultAxis from '../DefaultLayout/DefaultAxis';
import defaultLayoutStyles from '../DefaultLayout/styles/DefaultLayout.style';
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/modules/__tests__/current.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
DIMENSION_ID_DATA,
DIMENSION_ID_PERIOD,
DIMENSION_ID_ORGUNIT,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

import { getAxesFromUi } from '../current';

Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/modules/__tests__/layoutAdapters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
DIMENSION_ID_DATA,
DIMENSION_ID_PERIOD,
DIMENSION_ID_ORGUNIT,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

import { pieLayoutAdapter, yearOverYearLayoutAdapter } from '../layoutAdapters';

Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/modules/current.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
DIMENSION_ID_DATA,
DIMENSION_ID_PERIOD,
dimensionCreate,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

import options from './options';
import {} from './layout';
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/modules/currentAnalyticalObject.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
DIMENSION_ID_ORGUNIT,
layoutGetAxisNameDimensionIdsObject,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

import { getInverseLayout } from './layout';

Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/modules/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
AXIS_NAME_COLUMNS,
AXIS_NAME_ROWS,
AXIS_NAME_FILTERS,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

// Names for dnd sources
export const SOURCE_DIMENSIONS = 'dimensions';
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/modules/layoutAdapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
AXIS_NAME_ROWS,
AXIS_NAME_FILTERS,
DIMENSION_ID_PERIOD,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

// Transform from ui.layout to pie layout format
export const pieLayoutAdapter = layout => {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/modules/layoutValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
FIXED_DIMENSIONS,
dimensionIsValid,
layoutGetDimension,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

import {
YEAR_OVER_YEAR_LINE,
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/modules/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
DIMENSION_ID_ORGUNIT,
layoutGetAxisNameDimensionIdsObject,
layoutGetDimensionIdItemIdsObject,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

import {
YEAR_OVER_YEAR_LINE,
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/reducers/__tests__/current.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
DIMENSION_ID_DATA,
DIMENSION_ID_PERIOD,
DIMENSION_ID_ORGUNIT,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

import options from '../../modules/options';
import reducer, {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/reducers/__tests__/dimensions.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FIXED_DIMENSIONS } from '@dhis2/d2-ui-analytics';
import { FIXED_DIMENSIONS } from '@dhis2/analytics';

import reducer, { SET_DIMENSIONS } from '../dimensions';

Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/reducers/__tests__/ui.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
AXIS_NAME_COLUMNS,
AXIS_NAME_ROWS,
AXIS_NAME_FILTERS,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

import * as ui from '../ui';
import { BAR } from '../../modules/chartTypes';
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/reducers/dimensions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FIXED_DIMENSIONS as DEFAULT_DIMENSIONS } from '@dhis2/d2-ui-analytics';
import { FIXED_DIMENSIONS as DEFAULT_DIMENSIONS } from '@dhis2/analytics';

export const SET_DIMENSIONS = 'SET_DIMENSIONS';
export const SET_SELECTED_DIMENSION = 'SET_SELECTED_DIMENSION';
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/reducers/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
DIMENSION_ID_ORGUNIT,
AXIS_NAME_COLUMNS,
AXIS_NAME_ROWS,
} from '@dhis2/d2-ui-analytics';
} from '@dhis2/analytics';

import { getFilteredLayout, getSwapModObj } from '../modules/layout';
import { getOptionsForUi } from '../modules/options';
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"main": "./build/index.js",
"license": "BSD-3-Clause",
"dependencies": {
"@dhis2/analytics": "2.1.1",
"@material-ui/core": "^3.1.2",
"d2-charts-api": "33.0.1",
"lodash-es": "^4.17.11",
"react": "^16.6.0",
"react-dom": "^16.6.0"
Expand Down
19 changes: 11 additions & 8 deletions packages/plugin/src/ChartPlugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import isEqual from 'lodash-es/isEqual';
import { createChart } from 'd2-charts-api';
import { createVisualization } from '@dhis2/analytics';

import { apiFetchVisualization } from './api/visualization';
import {
Expand All @@ -20,7 +20,7 @@ class ChartPlugin extends Component {

this.canvasRef = React.createRef();

this.recreateChart = Function.prototype;
this.recreateVisualization = Function.prototype;

this.state = {
isLoading: true,
Expand All @@ -43,8 +43,11 @@ class ChartPlugin extends Component {
}

// id set by DV app, style works in dashboards
if (this.props.id !== prevProps.id || !isEqual(this.props.style, prevProps.style)) {
this.recreateChart(0); // disable animation
if (
this.props.id !== prevProps.id ||
!isEqual(this.props.style, prevProps.style)
) {
this.recreateVisualization(0); // disable animation
return;
}
}
Expand Down Expand Up @@ -134,8 +137,8 @@ class ChartPlugin extends Component {
onResponsesReceived(responses);
}

this.recreateChart = animation => {
const chartConfig = createChart(
this.recreateVisualization = animation => {
const visualizationConfig = createVisualization(
responses,
visualization,
this.canvasRef.current,
Expand All @@ -146,14 +149,14 @@ class ChartPlugin extends Component {
);

onChartGenerated(
chartConfig.chart.getSVGForExport({
visualizationConfig.visualization.getSVGForExport({
sourceHeight: 768,
sourceWidth: 1024,
})
);
};

this.recreateChart();
this.recreateVisualization();

this.setState({ isLoading: false });
} catch (error) {
Expand Down
26 changes: 14 additions & 12 deletions packages/plugin/src/__tests__/ChartPlugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React from 'react';
import { shallow } from 'enzyme';
import LoadingMask from '../widgets/LoadingMask';
import ChartPlugin from '../ChartPlugin';
import * as chartsApi from 'd2-charts-api';
import * as analytics from '@dhis2/analytics';
import * as api from '../api/analytics';
import * as apiViz from '../api/visualization';
import * as options from '../modules/options';
import { YEAR_OVER_YEAR_LINE, COLUMN } from '../modules/chartTypes';

jest.mock('d2-charts-api');
jest.mock('@dhis2/analytics');

const dxMock = {
dimension: 'dx',
Expand Down Expand Up @@ -83,8 +83,8 @@ class MockYoYAnalyticsResponse {
}
}

const createChartMock = {
chart: {
const createVisualizationMock = {
visualization: {
getSVGForExport: () => '<svg />',
},
};
Expand Down Expand Up @@ -135,9 +135,11 @@ describe('ChartPlugin', () => {
).toBeTruthy();
});

describe('createChart success', () => {
describe('createVisualization success', () => {
beforeEach(() => {
chartsApi.createChart = jest.fn().mockReturnValue(createChartMock);
analytics.createVisualization = jest
.fn()
.mockReturnValue(createVisualizationMock);
});

it('renders a div', done => {
Expand All @@ -154,11 +156,11 @@ describe('ChartPlugin', () => {
done();
});

it('calls createChart', done => {
it('calls createVisualization', done => {
canvas();

setTimeout(() => {
expect(chartsApi.createChart).toHaveBeenCalled();
expect(analytics.createVisualization).toHaveBeenCalled();
done();
});
});
Expand Down Expand Up @@ -220,7 +222,7 @@ describe('ChartPlugin', () => {
setTimeout(() => {
expect(props.onChartGenerated).toHaveBeenCalled();
expect(props.onChartGenerated).toHaveBeenCalledWith(
createChartMock.chart.getSVGForExport()
createVisualizationMock.visualization.getSVGForExport()
);
done();
});
Expand Down Expand Up @@ -284,18 +286,18 @@ describe('ChartPlugin', () => {
});
});

it('provides extra options to createChart', done => {
it('provides extra options to createVisualization', done => {
canvas();

setTimeout(() => {
expect(chartsApi.createChart).toHaveBeenCalled();
expect(analytics.createVisualization).toHaveBeenCalled();

const expectedExtraOptions = {
yearlySeries: mockYoYSeriesLabels,
xAxisLabels: ['period 1', 'period 2'],
};

expect(chartsApi.createChart.mock.calls[0][3]).toEqual({
expect(analytics.createVisualization.mock.calls[0][3]).toEqual({
animation: undefined,
dashboard: false,
...expectedExtraOptions,
Expand Down
Loading

0 comments on commit c9f5c93

Please sign in to comment.