From 063c8892bc1be55c942b5e7da5d317d4f7172130 Mon Sep 17 00:00:00 2001 From: Sergey Petushkov Date: Thu, 9 Nov 2023 17:03:34 +0100 Subject: [PATCH] chore(aggregations): refactor create-view to use service injection --- package-lock.json | 5 +- packages/compass-aggregations/package.json | 5 +- ...e-view-modal.jsx => create-view-modal.tsx} | 85 ++++---- .../create-view-modal/{index.js => index.ts} | 0 packages/compass-aggregations/src/index.ts | 18 +- .../src/modules/create-view/error.js | 57 ----- .../src/modules/create-view/index.js | 169 --------------- .../{index.spec.js => index.spec.ts} | 24 +-- .../src/modules/create-view/index.ts | 202 ++++++++++++++++++ .../src/modules/create-view/is-duplicating.js | 26 --- .../src/modules/create-view/is-running.js | 26 --- .../src/modules/create-view/is-visible.js | 15 -- .../src/modules/create-view/name.js | 22 -- .../src/modules/create-view/pipeline.js | 22 -- .../src/modules/create-view/reset.js | 13 -- .../src/modules/create-view/source.js | 22 -- .../src/stores/create-view.js | 40 ---- ...reate-view.spec.js => create-view.spec.ts} | 19 +- .../src/stores/create-view.ts | 54 +++++ packages/compass-home/src/components/home.tsx | 2 +- 20 files changed, 335 insertions(+), 491 deletions(-) rename packages/compass-aggregations/src/components/create-view-modal/{create-view-modal.jsx => create-view-modal.tsx} (57%) rename packages/compass-aggregations/src/components/create-view-modal/{index.js => index.ts} (100%) delete mode 100644 packages/compass-aggregations/src/modules/create-view/error.js delete mode 100644 packages/compass-aggregations/src/modules/create-view/index.js rename packages/compass-aggregations/src/modules/create-view/{index.spec.js => index.spec.ts} (70%) create mode 100644 packages/compass-aggregations/src/modules/create-view/index.ts delete mode 100644 packages/compass-aggregations/src/modules/create-view/is-duplicating.js delete mode 100644 packages/compass-aggregations/src/modules/create-view/is-running.js delete mode 100644 packages/compass-aggregations/src/modules/create-view/is-visible.js delete mode 100644 packages/compass-aggregations/src/modules/create-view/name.js delete mode 100644 packages/compass-aggregations/src/modules/create-view/pipeline.js delete mode 100644 packages/compass-aggregations/src/modules/create-view/reset.js delete mode 100644 packages/compass-aggregations/src/modules/create-view/source.js delete mode 100644 packages/compass-aggregations/src/stores/create-view.js rename packages/compass-aggregations/src/stores/{create-view.spec.js => create-view.spec.ts} (75%) create mode 100644 packages/compass-aggregations/src/stores/create-view.ts diff --git a/package-lock.json b/package-lock.json index b0906a3e6e2..ecdcd4db5f2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -43940,7 +43940,8 @@ "compass-preferences-model": "^2.15.6", "hadron-app-registry": "^9.0.13", "hadron-document": "^8.4.3", - "hadron-type-checker": "^7.1.0" + "hadron-type-checker": "^7.1.0", + "mongodb-data-service": "^22.15.1" }, "devDependencies": { "@babel/generator": "^7.19.5", @@ -43966,7 +43967,6 @@ "lodash": "^4.17.21", "mocha": "^10.2.0", "mongodb": "^6.0.0", - "mongodb-data-service": "^22.15.1", "mongodb-ns": "^2.4.0", "mongodb-query-parser": "^3.1.3", "mongodb-schema": "^12.0.0", @@ -44000,6 +44000,7 @@ "hadron-app-registry": "^9.0.14", "hadron-document": "^8.4.3", "hadron-type-checker": "^7.1.0", + "mongodb-data-service": "^22.15.1", "react": "^17.0.2" } }, diff --git a/packages/compass-aggregations/package.json b/packages/compass-aggregations/package.json index 947fa613f8e..a4dc2451207 100644 --- a/packages/compass-aggregations/package.json +++ b/packages/compass-aggregations/package.json @@ -54,6 +54,7 @@ "hadron-app-registry": "^9.0.14", "hadron-document": "^8.4.3", "hadron-type-checker": "^7.1.0", + "mongodb-data-service": "^22.15.1", "react": "^17.0.2" }, "devDependencies": { @@ -80,7 +81,6 @@ "lodash": "^4.17.21", "mocha": "^10.2.0", "mongodb": "^6.0.0", - "mongodb-data-service": "^22.15.1", "mongodb-ns": "^2.4.0", "mongodb-query-parser": "^3.1.3", "mongodb-schema": "^12.0.0", @@ -113,7 +113,8 @@ "compass-preferences-model": "^2.15.6", "hadron-app-registry": "^9.0.13", "hadron-document": "^8.4.3", - "hadron-type-checker": "^7.1.0" + "hadron-type-checker": "^7.1.0", + "mongodb-data-service": "^22.15.1" }, "homepage": "https://github.com/mongodb-js/compass", "bugs": { diff --git a/packages/compass-aggregations/src/components/create-view-modal/create-view-modal.jsx b/packages/compass-aggregations/src/components/create-view-modal/create-view-modal.tsx similarity index 57% rename from packages/compass-aggregations/src/components/create-view-modal/create-view-modal.jsx rename to packages/compass-aggregations/src/components/create-view-modal/create-view-modal.tsx index 4f6d7ecaffe..5328c631ed2 100644 --- a/packages/compass-aggregations/src/components/create-view-modal/create-view-modal.jsx +++ b/packages/compass-aggregations/src/components/create-view-modal/create-view-modal.tsx @@ -1,7 +1,5 @@ import React, { PureComponent } from 'react'; -import PropTypes from 'prop-types'; import { connect } from 'react-redux'; - import { Banner, Body, @@ -11,12 +9,14 @@ import { spacing, TextInput, } from '@mongodb-js/compass-components'; - -import { createView } from '../../modules/create-view'; -import { changeViewName } from '../../modules/create-view/name'; -import { toggleIsVisible } from '../../modules/create-view/is-visible'; -import { createLoggerAndTelemetry } from '@mongodb-js/compass-logging'; -const { track } = createLoggerAndTelemetry('COMPASS-AGGREGATIONS-UI'); +import { + createView, + changeViewName, + toggleIsVisible, +} from '../../modules/create-view'; +import type { LoggerAndTelemetry } from '@mongodb-js/compass-logging/provider'; +import { withLoggerAndTelemetry } from '@mongodb-js/compass-logging/provider'; +import type { CreateViewRootState } from '../../stores/create-view'; const progressContainerStyles = css({ display: 'flex', @@ -24,25 +24,21 @@ const progressContainerStyles = css({ alignItems: 'center', }); -class CreateViewModal extends PureComponent { - static displayName = 'CreateViewModalComponent'; - - static propTypes = { - createView: PropTypes.func.isRequired, - - isVisible: PropTypes.bool.isRequired, - toggleIsVisible: PropTypes.func.isRequired, - - name: PropTypes.string, - changeViewName: PropTypes.func.isRequired, - isDuplicating: PropTypes.bool.isRequired, - - source: PropTypes.string.isRequired, - pipeline: PropTypes.array.isRequired, - isRunning: PropTypes.bool.isRequired, - error: PropTypes.object, - }; - +type CreateViewModalProps = { + createView: () => void; + isVisible?: boolean; + toggleIsVisible: (newVal: boolean) => void; + name?: string; + changeViewName: (name: string) => void; + isDuplicating?: boolean; + source?: string; + pipeline?: unknown[]; + isRunning?: boolean; + error: Error | null; + logger: LoggerAndTelemetry; +}; + +class CreateViewModal extends PureComponent { static defaultProps = { name: '', source: '', @@ -52,19 +48,17 @@ class CreateViewModal extends PureComponent { isDuplicating: false, }; - componentDidUpdate(prevProps) { + componentDidUpdate(prevProps: CreateViewModalProps) { if (prevProps.isVisible !== this.props.isVisible && this.props.isVisible) { - track('Screen', { name: 'create_view_modal' }); + this.props.logger.track('Screen', { name: 'create_view_modal' }); } } - onNameChange = (evt) => { - this.props.changeViewName(evt.target.value); + onNameChange = (evt: React.ChangeEvent) => { + this.props.changeViewName(evt.currentTarget.value); }; - onFormSubmit = (evt) => { - evt.preventDefault(); - evt.stopPropagation(); + onFormSubmit = () => { this.props.createView(); }; @@ -74,15 +68,13 @@ class CreateViewModal extends PureComponent { /** * Render the save pipeline component. - * - * @returns {Component} The component. */ render() { return ( ({ +const mapStateToProps = (state: CreateViewRootState) => ({ isRunning: state.isRunning, isVisible: state.isVisible, isDuplicating: state.isDuplicating, @@ -129,11 +117,14 @@ const mapStateToProps = (state) => ({ * Connect the redux store to the component. * (dispatch) */ -const MappedCreateViewModal = connect(mapStateToProps, { - createView, - changeViewName, - toggleIsVisible, -})(CreateViewModal); +const MappedCreateViewModal = withLoggerAndTelemetry( + connect(mapStateToProps, { + createView, + changeViewName, + toggleIsVisible, + })(CreateViewModal), + 'COMPASS-CREATE-VIEW-UI' +); export default MappedCreateViewModal; export { CreateViewModal }; diff --git a/packages/compass-aggregations/src/components/create-view-modal/index.js b/packages/compass-aggregations/src/components/create-view-modal/index.ts similarity index 100% rename from packages/compass-aggregations/src/components/create-view-modal/index.js rename to packages/compass-aggregations/src/components/create-view-modal/index.ts diff --git a/packages/compass-aggregations/src/index.ts b/packages/compass-aggregations/src/index.ts index 954ca2e6ce8..015a6d809b0 100644 --- a/packages/compass-aggregations/src/index.ts +++ b/packages/compass-aggregations/src/index.ts @@ -6,6 +6,8 @@ import { Aggregations } from './components/aggregations'; import { activateCreateViewPlugin } from './stores/create-view'; import StageEditor from './components/stage-editor'; import CreateViewModal from './components/create-view-modal'; +import { dataServiceLocator } from 'mongodb-data-service/provider'; +import { createLoggerAndTelemetryLocator } from '@mongodb-js/compass-logging/provider'; /** * A sample role for the component. @@ -40,11 +42,17 @@ const deactivate = (appRegistry: AppRegistry) => { appRegistry.deregisterRole('Collection.Tab', ROLE); }; -export const CreateViewPlugin = registerHadronPlugin({ - name: 'CreateView', - component: CreateViewModal, - activate: activateCreateViewPlugin, -}); +export const CreateViewPlugin = registerHadronPlugin( + { + name: 'CreateView', + component: CreateViewModal, + activate: activateCreateViewPlugin, + }, + { + dataService: dataServiceLocator as typeof dataServiceLocator<'createView'>, + logger: createLoggerAndTelemetryLocator('COMPASS-CREATE-VIEW-UI'), + } +); export default AggregationsPlugin; export { activate, deactivate, Aggregations, StageEditor, configureStore }; diff --git a/packages/compass-aggregations/src/modules/create-view/error.js b/packages/compass-aggregations/src/modules/create-view/error.js deleted file mode 100644 index 158e9d66f8c..00000000000 --- a/packages/compass-aggregations/src/modules/create-view/error.js +++ /dev/null @@ -1,57 +0,0 @@ -/** - * The action name prefix. - */ -const PREFIX = 'aggregations/create-view/error'; - -/** - * Handle error action name. - */ -export const HANDLE_ERROR = `${PREFIX}/HANDLE_ERROR`; - -/** - * Clear error action name. - */ -export const CLEAR_ERROR = `${PREFIX}/CLEAR_ERROR`; - -/** - * The initial state of the error. - */ -export const INITIAL_STATE = null; - -/** - * Reducer function for handle state changes to errors. - * - * @param {Error} state - The error state. - * @param {Object} action - The action. - * - * @returns {Error} The new state. - */ -export default function reducer(state = INITIAL_STATE, action) { - if (action.type === HANDLE_ERROR) { - return action.error; - } else if (action.type === CLEAR_ERROR) { - return null; - } - return state; -} - -/** - * Handle error action creator. - * - * @param {Error} error - The error. - * - * @returns {Object} The action. - */ -export const handleError = (error) => ({ - type: HANDLE_ERROR, - error: error, -}); - -/** - * Clear error action creator. - * - * @returns {Object} The action. - */ -export const clearError = () => ({ - type: CLEAR_ERROR, -}); diff --git a/packages/compass-aggregations/src/modules/create-view/index.js b/packages/compass-aggregations/src/modules/create-view/index.js deleted file mode 100644 index 23abd56ce23..00000000000 --- a/packages/compass-aggregations/src/modules/create-view/index.js +++ /dev/null @@ -1,169 +0,0 @@ -import { createLoggerAndTelemetry } from '@mongodb-js/compass-logging'; -const { track, debug } = createLoggerAndTelemetry('COMPASS-AGGREGATIONS-UI'); - -import { combineReducers } from 'redux'; -import dataService from '../data-service'; - -import source, { - INITIAL_STATE as SOURCE_INITIAL_STATE, -} from '../create-view/source'; - -import pipeline, { - INITIAL_STATE as PIPELINE_INITIAL_STATE, -} from '../create-view/pipeline'; - -import isRunning, { - toggleIsRunning, - INITIAL_STATE as IS_RUNNING_INITIAL_STATE, -} from '../create-view/is-running'; -import isVisible, { - INITIAL_STATE as IS_VISIBLE_INITIAL_STATE, -} from '../create-view/is-visible'; -import isDuplicating, { - INITIAL_STATE as IS_DUPLICATING_INITIAL_STATE, -} from '../create-view/is-duplicating'; -import name, { INITIAL_STATE as NAME_INITIAL_STATE } from '../create-view/name'; - -import error, { - clearError, - handleError, - INITIAL_STATE as ERROR_INITIAL_STATE, -} from '../create-view/error'; - -import { reset, RESET } from '../create-view/reset'; -import appRegistry from '@mongodb-js/mongodb-redux-common/app-registry'; - -const parseNs = require('mongodb-ns'); - -/** - * Open action name. - */ -const OPEN = 'aggregations/create-view/OPEN'; - -export const INITIAL_STATE = { - isRunning: IS_RUNNING_INITIAL_STATE, - isVisible: IS_VISIBLE_INITIAL_STATE, - isDuplicating: IS_DUPLICATING_INITIAL_STATE, - name: NAME_INITIAL_STATE, - error: ERROR_INITIAL_STATE, - source: SOURCE_INITIAL_STATE, - pipeline: PIPELINE_INITIAL_STATE, -}; - -/** - * The main reducer. - */ -const reducer = combineReducers({ - appRegistry, - isRunning, - isDuplicating, - isVisible, - name, - error, - source, - pipeline, - dataService, -}); - -/** - * The root reducer. - * - * @param {Object} state - The state. - * @param {Object} action - The action. - * - * @returns {Object} The new state. - */ -const rootReducer = (state, action) => { - if (action.type === RESET) { - return { - ...state, - ...INITIAL_STATE, - }; - } else if (action.type === OPEN) { - const newState = { - ...state, - ...INITIAL_STATE, - isVisible: true, - isDuplicating: action.duplicate, - source: action.source, - pipeline: action.pipeline, - }; - - debug('handling open', { newState }); - - return newState; - } - return reducer(state, action); -}; - -export default rootReducer; - -/** - * Stop progress and set the error. - * - * @param {Function} dispatch - The dispatch function. - * @param {Error} err - The error. - * - * @return {Object} The result. - */ -const stopWithError = (dispatch, err) => { - dispatch(toggleIsRunning(false)); - return dispatch(handleError(err)); -}; - -/** - * Open create view action creator. - * - * @param {String} sourceNs - The source namespace for the view. - * @param {Array} sourcePipeline - The pipeline to use for the view. - * @returns {Object} The action. - */ -export const open = (sourceNs, sourcePipeline, duplicate) => ({ - type: OPEN, - source: sourceNs, - pipeline: sourcePipeline, - duplicate: duplicate, -}); - -/** - * The create view action. - * - * @returns {Function} The thunk function. - */ -export const createView = () => { - return async (dispatch, getState, { globalAppRegistry }) => { - debug('creating view!'); - const state = getState(); - const ds = state.dataService.dataService; - - const viewName = state.name; - const viewSource = state.source; - const { database } = parseNs(state.source); - const viewPipeline = state.pipeline; - const options = {}; - - dispatch(clearError()); - - try { - dispatch(toggleIsRunning(true)); - debug( - 'calling data-service.createView', - viewName, - viewSource, - viewPipeline, - options - ); - await ds.createView(viewName, viewSource, viewPipeline, options); - debug('View created!'); - track('Aggregation Saved As View', { num_stages: viewPipeline.length }); - globalAppRegistry.emit( - 'create-view-open-result-namespace', - `${database}.${viewName}` - ); - dispatch(reset()); - } catch (e) { - debug('error creating view', e); - return stopWithError(dispatch, e); - } - }; -}; diff --git a/packages/compass-aggregations/src/modules/create-view/index.spec.js b/packages/compass-aggregations/src/modules/create-view/index.spec.ts similarity index 70% rename from packages/compass-aggregations/src/modules/create-view/index.spec.js rename to packages/compass-aggregations/src/modules/create-view/index.spec.ts index 0472dbd18cb..b841ccce806 100644 --- a/packages/compass-aggregations/src/modules/create-view/index.spec.js +++ b/packages/compass-aggregations/src/modules/create-view/index.spec.ts @@ -1,6 +1,10 @@ -import reducer, { createView, INITIAL_STATE } from '../create-view'; -import { reset } from '../create-view/reset'; -import { CLEAR_ERROR, HANDLE_ERROR } from '../create-view/error'; +import reducer, { + createView, + INITIAL_STATE, + reset, + CLEAR_ERROR, + HANDLE_ERROR, +} from './'; import sinon from 'sinon'; import { expect } from 'chai'; @@ -8,15 +12,9 @@ describe('create view module', function () { describe('#reducer', function () { describe('when an action is provided', function () { describe('when the action is reset', function () { - const dataService = 'data-service'; - it('returns the reset state', function () { - expect( - reducer({ dataService: dataService, appRegistry: 'test' }, reset()) - ).to.deep.equal({ + expect(reducer(INITIAL_STATE, reset())).to.deep.equal({ ...INITIAL_STATE, - dataService: dataService, - appRegistry: 'test', }); }); }); @@ -28,6 +26,7 @@ describe('create view module', function () { describe('when the source is invalid', function () { const dispatchSpy = sinon.spy(); const getState = () => ({ + ...INITIAL_STATE, name: 'myView', source: 'dataService', pipeline: [ @@ -37,12 +36,13 @@ describe('create view module', function () { }, }, ], - dataService: { dataService: 'ds' }, }); before(async function () { await createView()(dispatchSpy, getState, { - globalAppRegistry: { on: sinon.stub() }, + globalAppRegistry: { on: sinon.stub() } as any, + dataService: {} as any, + logger: { debug: sinon.stub(), track: sinon.stub() } as any, }); }); diff --git a/packages/compass-aggregations/src/modules/create-view/index.ts b/packages/compass-aggregations/src/modules/create-view/index.ts new file mode 100644 index 00000000000..a0d1e260f43 --- /dev/null +++ b/packages/compass-aggregations/src/modules/create-view/index.ts @@ -0,0 +1,202 @@ +import type { Reducer } from 'redux'; +import parseNs from 'mongodb-ns'; +import type { Document } from 'bson'; +import type { CreateViewThunkAction } from '../../stores/create-view'; + +export const TOGGLE_IS_RUNNING = + 'aggregations/create-view/is-running/TOGGLE_IS_RUNNING'; + +export const toggleIsRunning = (isRunning: boolean) => ({ + type: TOGGLE_IS_RUNNING, + isRunning: isRunning, +}); + +export const TOGGLE_IS_VISIBLE = + 'aggregations/create-view/is-visible/TOGGLE_IS_VISIBLE'; + +export const toggleIsVisible = (isVisible: boolean) => ({ + type: TOGGLE_IS_VISIBLE, + isVisible: isVisible, +}); + +/** + * Handle error action name. + */ +export const HANDLE_ERROR = `aggregations/create-view/error/HANDLE_ERROR`; + +/** + * Handle error action creator. + */ +export const handleError = (error: Error) => ({ + type: HANDLE_ERROR, + error: error, +}); + +export const CLEAR_ERROR = `aggregations/create-view/error/CLEAR_ERROR`; + +export const clearError = () => ({ + type: CLEAR_ERROR, +}); + +export const CHANGE_VIEW_NAME = 'aggregations/create-view/name/CHANGE_NAME'; + +export const changeViewName = (name: string) => ({ + type: CHANGE_VIEW_NAME, + name: name, +}); + +export const RESET = 'aggregations/create-view/reset'; + +export const reset = () => ({ + type: RESET, +}); + +/** + * Open action name. + */ +const OPEN = 'aggregations/create-view/OPEN'; + +type CreateViewState = { + isRunning: boolean; + isVisible: boolean; + isDuplicating: boolean; + name: string; + error: Error | null; + source: string; + pipeline: unknown[]; +}; + +export const INITIAL_STATE = { + isRunning: false, + isVisible: false, + isDuplicating: false, + name: '', + error: null, + source: '', + pipeline: [], +}; + +/** + * The main reducer. + */ +const reducer: Reducer = (state = INITIAL_STATE, action) => { + if (action.type === RESET) { + return { ...INITIAL_STATE }; + } + if (action.type === OPEN) { + return { + ...state, + ...INITIAL_STATE, + isVisible: true, + isDuplicating: action.duplicate, + source: action.source, + pipeline: action.pipeline, + }; + } + if (action.type === TOGGLE_IS_RUNNING) { + return { + ...state, + isRunning: action.isRunning, + }; + } + if (action.type === TOGGLE_IS_VISIBLE) { + return { + ...state, + isVisible: action.isVisible, + }; + } + if (action.type === CHANGE_VIEW_NAME) { + return { + ...state, + name: action.name, + }; + } + if (action.type === HANDLE_ERROR) { + return { + ...state, + error: action.error, + }; + } + if (action.type === CLEAR_ERROR) { + return { + ...state, + error: null, + }; + } + return state; +}; + +export default reducer; + +/** + * Stop progress and set the error. + */ +const stopWithError = (err: Error): CreateViewThunkAction => { + return (dispatch) => { + dispatch(toggleIsRunning(false)); + dispatch(handleError(err)); + }; +}; + +/** + * Open create view action creator. + */ +export const open = ( + sourceNs: string, + sourcePipeline: unknown[], + duplicate: boolean +) => ({ + type: OPEN, + source: sourceNs, + pipeline: sourcePipeline, + duplicate: duplicate, +}); + +/** + * The create view action. + */ +export const createView = (): CreateViewThunkAction> => { + return async ( + dispatch, + getState, + { globalAppRegistry, dataService, logger: { debug, track } } + ) => { + debug('creating view!'); + const state = getState(); + + const viewName = state.name; + const viewSource = state.source; + const { database } = parseNs(state.source); + const viewPipeline = state.pipeline; + const options = {}; + + dispatch(clearError()); + + try { + dispatch(toggleIsRunning(true)); + debug( + 'calling data-service.createView', + viewName, + viewSource, + viewPipeline, + options + ); + await dataService.createView( + viewName, + viewSource, + viewPipeline as Document[], + options + ); + debug('View created!'); + track('Aggregation Saved As View', { num_stages: viewPipeline.length }); + globalAppRegistry.emit( + 'create-view-open-result-namespace', + `${database}.${viewName}` + ); + dispatch(reset()); + } catch (e) { + debug('error creating view', e); + dispatch(stopWithError(e as Error)); + } + }; +}; diff --git a/packages/compass-aggregations/src/modules/create-view/is-duplicating.js b/packages/compass-aggregations/src/modules/create-view/is-duplicating.js deleted file mode 100644 index 10826cdc7e5..00000000000 --- a/packages/compass-aggregations/src/modules/create-view/is-duplicating.js +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Toggle is duplicating action name. - */ -export const TOGGLE_IS_DUPLICATING = - 'aggregations/create-view/is-duplicating/TOGGLE_IS_DUPLICATING'; - -export const INITIAL_STATE = false; - -export default function reducer(state = INITIAL_STATE, action) { - if (action.type === TOGGLE_IS_DUPLICATING) { - return action.isDuplicating; - } - return state; -} - -/** - * The toggle is duplicating action creator. - * - * @param {Boolean} isDuplicating - Is duplicating. - * - * @returns {Object} The action. - */ -export const toggleIsDuplicating = (isDuplicating) => ({ - type: TOGGLE_IS_DUPLICATING, - isDuplicating: isDuplicating, -}); diff --git a/packages/compass-aggregations/src/modules/create-view/is-running.js b/packages/compass-aggregations/src/modules/create-view/is-running.js deleted file mode 100644 index 0cc1c276eb5..00000000000 --- a/packages/compass-aggregations/src/modules/create-view/is-running.js +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Toggle is running action name. - */ -export const TOGGLE_IS_RUNNING = - 'aggregations/create-view/is-running/TOGGLE_IS_RUNNING'; - -export const INITIAL_STATE = false; - -export default function reducer(state = INITIAL_STATE, action) { - if (action.type === TOGGLE_IS_RUNNING) { - return action.isRunning; - } - return state; -} - -/** - * The toggle is running action creator. - * - * @param {Boolean} isRunning - Is running. - * - * @returns {Object} The action. - */ -export const toggleIsRunning = (isRunning) => ({ - type: TOGGLE_IS_RUNNING, - isRunning: isRunning, -}); diff --git a/packages/compass-aggregations/src/modules/create-view/is-visible.js b/packages/compass-aggregations/src/modules/create-view/is-visible.js deleted file mode 100644 index 3f4b332d125..00000000000 --- a/packages/compass-aggregations/src/modules/create-view/is-visible.js +++ /dev/null @@ -1,15 +0,0 @@ -export const TOGGLE_IS_VISIBLE = - 'aggregations/create-view/is-visible/TOGGLE_IS_VISIBLE'; - -export const INITIAL_STATE = false; - -export default function reducer(state = INITIAL_STATE, action) { - if (action.type === TOGGLE_IS_VISIBLE) { - return action.isVisible; - } - return state; -} -export const toggleIsVisible = (isVisible) => ({ - type: TOGGLE_IS_VISIBLE, - isVisible: isVisible, -}); diff --git a/packages/compass-aggregations/src/modules/create-view/name.js b/packages/compass-aggregations/src/modules/create-view/name.js deleted file mode 100644 index 6c3b3c75e98..00000000000 --- a/packages/compass-aggregations/src/modules/create-view/name.js +++ /dev/null @@ -1,22 +0,0 @@ -export const CHANGE_VIEW_NAME = 'aggregations/create-view/name/CHANGE_NAME'; - -export const INITIAL_STATE = ''; - -export default function reducer(state = INITIAL_STATE, action) { - if (action.type === CHANGE_VIEW_NAME) { - return action.name; - } - return state; -} - -/** - * The change name action creator. - * - * @param {String} name - The view name. - * - * @returns {Object} The action. - */ -export const changeViewName = (name) => ({ - type: CHANGE_VIEW_NAME, - name: name, -}); diff --git a/packages/compass-aggregations/src/modules/create-view/pipeline.js b/packages/compass-aggregations/src/modules/create-view/pipeline.js deleted file mode 100644 index cec3eeeaeef..00000000000 --- a/packages/compass-aggregations/src/modules/create-view/pipeline.js +++ /dev/null @@ -1,22 +0,0 @@ -export const SET_VIEW_PIPELINE = 'aggregations/create-view/pipeline/SET'; - -export const INITIAL_STATE = []; - -export default function reducer(state = INITIAL_STATE, action) { - if (action.type === SET_VIEW_PIPELINE) { - return action.pipeline; - } - return state; -} - -/** - * The set pipeline action creator. - * - * @param {Array} pipeline - The view's pipeline. - * - * @returns {Object} The action. - */ -export const setViewPipeline = (pipeline) => ({ - type: SET_VIEW_PIPELINE, - pipeline: pipeline, -}); diff --git a/packages/compass-aggregations/src/modules/create-view/reset.js b/packages/compass-aggregations/src/modules/create-view/reset.js deleted file mode 100644 index 2eb043968e7..00000000000 --- a/packages/compass-aggregations/src/modules/create-view/reset.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * The reset action name. - */ -export const RESET = 'aggregations/create-view/reset'; - -/** - * Reset the state action. - * - * @return {Object} The action creator. - */ -export const reset = () => ({ - type: RESET, -}); diff --git a/packages/compass-aggregations/src/modules/create-view/source.js b/packages/compass-aggregations/src/modules/create-view/source.js deleted file mode 100644 index 0bdf77d866f..00000000000 --- a/packages/compass-aggregations/src/modules/create-view/source.js +++ /dev/null @@ -1,22 +0,0 @@ -export const SET_VIEW_SOURCE = 'aggregations/create-view/source/SET'; - -export const INITIAL_STATE = ''; - -export default function reducer(state = INITIAL_STATE, action) { - if (action.type === SET_VIEW_SOURCE) { - return action.source; - } - return state; -} - -/** - * The change name action creator. - * - * @param {String} source - The view source namespace. - * - * @returns {Object} The action. - */ -export const setViewSource = (source) => ({ - type: SET_VIEW_SOURCE, - source: source, -}); diff --git a/packages/compass-aggregations/src/stores/create-view.js b/packages/compass-aggregations/src/stores/create-view.js deleted file mode 100644 index bcbe1efaa3f..00000000000 --- a/packages/compass-aggregations/src/stores/create-view.js +++ /dev/null @@ -1,40 +0,0 @@ -import { createStore, applyMiddleware } from 'redux'; -import thunk from 'redux-thunk'; -import { dataServiceConnected } from '../modules/data-service'; -import reducer, { open } from '../modules/create-view'; - -/** - * @param {import('hadron-app-registry').AppRegistry} appRegistry - * @returns {{ store: any, deactivate: () => void }} - */ -export function activateCreateViewPlugin(_, { globalAppRegistry }) { - const store = createStore( - reducer, - applyMiddleware( - thunk.withExtraArgument({ globalAppRegistry: globalAppRegistry }) - ) - ); - - const onDataServiceConnected = (error, dataService) => { - store.dispatch(dataServiceConnected(error, dataService)); - }; - - globalAppRegistry.on('data-service-connected', onDataServiceConnected); - - const onOpenCreateView = (meta) => { - store.dispatch(open(meta.source, meta.pipeline, meta.duplicate ?? false)); - }; - - globalAppRegistry.on('open-create-view', onOpenCreateView); - - return { - store, - deactivate() { - globalAppRegistry.removeListener( - 'data-service-connected', - onDataServiceConnected - ); - globalAppRegistry.removeListener('open-create-view', onOpenCreateView); - }, - }; -} diff --git a/packages/compass-aggregations/src/stores/create-view.spec.js b/packages/compass-aggregations/src/stores/create-view.spec.ts similarity index 75% rename from packages/compass-aggregations/src/stores/create-view.spec.js rename to packages/compass-aggregations/src/stores/create-view.spec.ts index c8ca6ce40c4..bd9c6aa77f5 100644 --- a/packages/compass-aggregations/src/stores/create-view.spec.js +++ b/packages/compass-aggregations/src/stores/create-view.spec.ts @@ -3,21 +3,25 @@ import { activateCreateViewPlugin } from './create-view'; import { expect } from 'chai'; describe('CreateViewStore [Store]', function () { - if (typeof window !== 'undefined' && window?.process?.type === 'renderer') { + if ( + typeof window !== 'undefined' && + (window?.process as any)?.type === 'renderer' + ) { // These tests don't pass in electron environment in Evergreen CI for some // reason, disable for now return; } - let store; - let deactivate; + let store: any; + let deactivate: any; const globalAppRegistry = new AppRegistry(); - const ds = 'data-service'; + const ds = 'data-service' as any; + const logger = {} as any; beforeEach(function () { ({ store, deactivate } = activateCreateViewPlugin( {}, - { globalAppRegistry } + { globalAppRegistry, dataService: ds, logger } )); }); @@ -27,11 +31,6 @@ describe('CreateViewStore [Store]', function () { }); describe('#configureStore', function () { - it('dispatches the data service connected action on data-service-connected event', function () { - globalAppRegistry.emit('data-service-connected', null, ds); - expect(store.getState().dataService.dataService).to.equal(ds); - }); - describe('when open create view is emitted', function () { beforeEach(function () { globalAppRegistry.emit('open-create-view', { diff --git a/packages/compass-aggregations/src/stores/create-view.ts b/packages/compass-aggregations/src/stores/create-view.ts new file mode 100644 index 00000000000..c4ed63efbc8 --- /dev/null +++ b/packages/compass-aggregations/src/stores/create-view.ts @@ -0,0 +1,54 @@ +import type { AnyAction, Action } from 'redux'; +import { createStore, applyMiddleware } from 'redux'; +import type { ThunkAction } from 'redux-thunk'; +import thunk from 'redux-thunk'; +import reducer, { open } from '../modules/create-view'; +import type AppRegistry from 'hadron-app-registry'; +import type { DataService } from 'mongodb-data-service'; +import type { LoggerAndTelemetry } from '@mongodb-js/compass-logging/provider'; + +type CreateViewServices = { + globalAppRegistry: AppRegistry; + dataService: Pick; + logger: LoggerAndTelemetry; +}; + +export function configureStore(services: CreateViewServices) { + return createStore( + reducer, + applyMiddleware(thunk.withExtraArgument(services)) + ); +} + +export type CreateViewRootState = ReturnType< + ReturnType['getState'] +>; + +export type CreateViewThunkAction< + R, + A extends Action = AnyAction +> = ThunkAction; + +export function activateCreateViewPlugin( + _: unknown, + { globalAppRegistry, dataService, logger }: CreateViewServices +) { + const store = configureStore({ globalAppRegistry, dataService, logger }); + + const onOpenCreateView = (meta: { + source: string; + pipeline: any[]; + duplicate?: boolean; + }) => { + store.dispatch(open(meta.source, meta.pipeline, meta.duplicate ?? false)); + }; + + globalAppRegistry.on('open-create-view', onOpenCreateView); + + return { + store, + deactivate(this: void) { + globalAppRegistry.removeListener('open-create-view', onOpenCreateView); + }, + }; +} diff --git a/packages/compass-home/src/components/home.tsx b/packages/compass-home/src/components/home.tsx index fe72cb169bd..450b6797392 100644 --- a/packages/compass-home/src/components/home.tsx +++ b/packages/compass-home/src/components/home.tsx @@ -357,6 +357,7 @@ function Home({ + @@ -381,7 +382,6 @@ function Home({ -