From b0b4d5e26a5b88933e8d76ad63507d37d1899d8c Mon Sep 17 00:00:00 2001 From: Levko Kravets Date: Thu, 24 Jan 2019 16:24:58 +0200 Subject: [PATCH] Convert Angular services to CommonJS-style and use them in React components instead of injecting (#3331) * Refine Auth service: remove dead code and fix race condition * Export services in CommonJS style * Refine Users, Events and OfflineListener services * Refactor Notifications service - rewrite to CommonJS * Replace Angular service injection with imports in React components * Fix Footer tests * Events service -> recordEvent function * CR1 --- client/app/components/AutocompleteToggle.jsx | 2 +- client/app/components/DateInput.jsx | 5 +- client/app/components/DateRangeInput.jsx | 5 +- client/app/components/DateTimeInput.jsx | 5 +- client/app/components/DateTimeRangeInput.jsx | 5 +- client/app/components/Footer.jsx | 17 +-- client/app/components/Footer.test.js | 15 +-- client/app/components/ParameterValueInput.jsx | 55 ++-------- .../components/QueryBasedParameterInput.jsx | 4 +- client/app/components/QueryEditor.jsx | 20 ++-- .../dashboards/AddTextboxDialog.jsx | 6 +- .../components/dashboards/AddWidgetDialog.jsx | 17 +-- .../EditParameterMappingsDialog.jsx | 8 +- .../components/dynamic-form/DynamicForm.jsx | 2 +- .../tags-control/DashboardTagsControl.jsx | 2 +- .../tags-control/QueryTagsControl.jsx | 2 +- .../components/tags-control/TagsControl.jsx | 3 +- client/app/index.js | 3 +- client/app/pages/queries/view.js | 2 +- client/app/services/alert-dialog.js | 15 +-- client/app/services/alert-subscription.js | 14 ++- client/app/services/alert.js | 15 +-- client/app/services/auth.js | 98 ++++++++--------- client/app/services/dashboard.js | 11 +- client/app/services/data-source.js | 11 +- client/app/services/destination.js | 16 +-- client/app/services/events.js | 6 +- client/app/services/getTags.js | 2 +- client/app/services/group.js | 14 ++- client/app/services/http.js | 11 -- client/app/services/keyboard-shortcuts.js | 10 +- client/app/services/ng.js | 16 +++ client/app/services/notifications.js | 102 ++++++++---------- client/app/services/offline-listener.js | 36 +++---- client/app/services/organization-status.js | 10 +- client/app/services/query-snippet.js | 10 +- client/app/services/query.js | 39 ++++--- client/app/{lib => services}/recordEvent.js | 6 +- client/app/services/toastr.js | 10 -- client/app/services/user.js | 24 +++-- client/app/services/widget.js | 20 ++-- 41 files changed, 319 insertions(+), 355 deletions(-) delete mode 100644 client/app/services/http.js create mode 100644 client/app/services/ng.js rename client/app/{lib => services}/recordEvent.js (79%) delete mode 100644 client/app/services/toastr.js diff --git a/client/app/components/AutocompleteToggle.jsx b/client/app/components/AutocompleteToggle.jsx index e74e0a9047..5a4c9bff20 100644 --- a/client/app/components/AutocompleteToggle.jsx +++ b/client/app/components/AutocompleteToggle.jsx @@ -2,7 +2,7 @@ import React from 'react'; import Tooltip from 'antd/lib/tooltip'; import PropTypes from 'prop-types'; import '@/redash-font/style.less'; -import recordEvent from '@/lib/recordEvent'; +import recordEvent from '@/services/recordEvent'; export default function AutocompleteToggle({ state, disabled, onToggle }) { let tooltipMessage = 'Live Autocomplete Enabled'; diff --git a/client/app/components/DateInput.jsx b/client/app/components/DateInput.jsx index 695a2a1997..ff7812ed8e 100644 --- a/client/app/components/DateInput.jsx +++ b/client/app/components/DateInput.jsx @@ -3,12 +3,11 @@ import React from 'react'; import PropTypes from 'prop-types'; import { react2angular } from 'react2angular'; import DatePicker from 'antd/lib/date-picker'; +import { clientConfig } from '@/services/auth'; export function DateInput({ value, onSelect, - // eslint-disable-next-line react/prop-types - clientConfig, className, }) { const format = clientConfig.dateFormat || 'YYYY-MM-DD'; @@ -46,7 +45,7 @@ DateInput.defaultProps = { }; export default function init(ngModule) { - ngModule.component('dateInput', react2angular(DateInput, null, ['clientConfig'])); + ngModule.component('dateInput', react2angular(DateInput)); } init.init = true; diff --git a/client/app/components/DateRangeInput.jsx b/client/app/components/DateRangeInput.jsx index 1b1454bbb5..d8ccef48f7 100644 --- a/client/app/components/DateRangeInput.jsx +++ b/client/app/components/DateRangeInput.jsx @@ -4,14 +4,13 @@ import React from 'react'; import PropTypes from 'prop-types'; import { react2angular } from 'react2angular'; import DatePicker from 'antd/lib/date-picker'; +import { clientConfig } from '@/services/auth'; const { RangePicker } = DatePicker; export function DateRangeInput({ value, onSelect, - // eslint-disable-next-line react/prop-types - clientConfig, className, }) { const format = clientConfig.dateFormat || 'YYYY-MM-DD'; @@ -53,7 +52,7 @@ DateRangeInput.defaultProps = { }; export default function init(ngModule) { - ngModule.component('dateRangeInput', react2angular(DateRangeInput, null, ['clientConfig'])); + ngModule.component('dateRangeInput', react2angular(DateRangeInput)); } init.init = true; diff --git a/client/app/components/DateTimeInput.jsx b/client/app/components/DateTimeInput.jsx index d95fd60884..92fb81f104 100644 --- a/client/app/components/DateTimeInput.jsx +++ b/client/app/components/DateTimeInput.jsx @@ -3,13 +3,12 @@ import React from 'react'; import PropTypes from 'prop-types'; import { react2angular } from 'react2angular'; import DatePicker from 'antd/lib/date-picker'; +import { clientConfig } from '@/services/auth'; export function DateTimeInput({ value, withSeconds, onSelect, - // eslint-disable-next-line react/prop-types - clientConfig, className, }) { const format = (clientConfig.dateFormat || 'YYYY-MM-DD') + @@ -51,7 +50,7 @@ DateTimeInput.defaultProps = { }; export default function init(ngModule) { - ngModule.component('dateTimeInput', react2angular(DateTimeInput, null, ['clientConfig'])); + ngModule.component('dateTimeInput', react2angular(DateTimeInput)); } init.init = true; diff --git a/client/app/components/DateTimeRangeInput.jsx b/client/app/components/DateTimeRangeInput.jsx index d0fa5ecb39..51f6c62ddc 100644 --- a/client/app/components/DateTimeRangeInput.jsx +++ b/client/app/components/DateTimeRangeInput.jsx @@ -4,6 +4,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import { react2angular } from 'react2angular'; import DatePicker from 'antd/lib/date-picker'; +import { clientConfig } from '@/services/auth'; const { RangePicker } = DatePicker; @@ -11,8 +12,6 @@ export function DateTimeRangeInput({ value, withSeconds, onSelect, - // eslint-disable-next-line react/prop-types - clientConfig, className, }) { const format = (clientConfig.dateFormat || 'YYYY-MM-DD') + @@ -58,7 +57,7 @@ DateTimeRangeInput.defaultProps = { }; export default function init(ngModule) { - ngModule.component('dateTimeRangeInput', react2angular(DateTimeRangeInput, null, ['clientConfig'])); + ngModule.component('dateTimeRangeInput', react2angular(DateTimeRangeInput)); } init.init = true; diff --git a/client/app/components/Footer.jsx b/client/app/components/Footer.jsx index bd1f06efda..7e45562fe1 100644 --- a/client/app/components/Footer.jsx +++ b/client/app/components/Footer.jsx @@ -1,11 +1,10 @@ import React from 'react'; -import PropTypes from 'prop-types'; - import { react2angular } from 'react2angular'; +import { clientConfig, currentUser } from '@/services/auth'; import frontendVersion from '../version.json'; -export function Footer({ clientConfig, currentUser }) { +export function Footer() { const backendVersion = clientConfig.version; const newVersionAvailable = clientConfig.newVersionAvailable && currentUser.isAdmin; const separator = ' \u2022 '; @@ -31,18 +30,8 @@ export function Footer({ clientConfig, currentUser }) { ); } -Footer.propTypes = { - clientConfig: PropTypes.shape({ - version: PropTypes.string, - newVersionAvailable: PropTypes.bool, - }).isRequired, - currentUser: PropTypes.shape({ - isAdmin: PropTypes.bool, - }).isRequired, -}; - export default function init(ngModule) { - ngModule.component('footer', react2angular(Footer, [], ['clientConfig', 'currentUser'])); + ngModule.component('footer', react2angular(Footer)); } init.init = true; diff --git a/client/app/components/Footer.test.js b/client/app/components/Footer.test.js index 81a157ebfc..519a9e5732 100644 --- a/client/app/components/Footer.test.js +++ b/client/app/components/Footer.test.js @@ -1,16 +1,19 @@ +import { extend } from 'lodash'; import React from 'react'; import renderer from 'react-test-renderer'; +import { clientConfig, currentUser } from '../services/auth'; import { Footer } from './Footer'; test('Footer renders', () => { - const clientConfig = { + // TODO: Properly mock this + extend(clientConfig, { version: '5.0.1', newVersionAvailable: true, - }; - const currentUser = { - isAdmin: true, - }; - const component = renderer.create(