Skip to content

Commit 55f5e9e

Browse files
authored
chore(compass-import-export): Convert import-export to a global plugin instead of a scoped one COMPASS-5537 COMPASS-4911 COMPASS-5143 (#2835)
* chore(compass-import-export): Convert import-export to a global plugin instead of a scoped one * chore(import-export): Clean-up import-finished event usage
1 parent b42ba3a commit 55f5e9e

File tree

19 files changed

+198
-465
lines changed

19 files changed

+198
-465
lines changed

packages/compass-aggregations/src/stores/store.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,6 @@ const configureStore = (options = {}) => {
193193
const localAppRegistry = options.localAppRegistry;
194194
setLocalAppRegistry(store, localAppRegistry);
195195

196-
/**
197-
* When the collection is changed, update the store.
198-
*/
199-
localAppRegistry.on('import-finished', () => {
200-
refreshInput(store);
201-
});
202-
203196
/**
204197
* Refresh documents on data refresh.
205198
*/
@@ -229,6 +222,13 @@ const configureStore = (options = {}) => {
229222
refreshInput(store);
230223
});
231224

225+
globalAppRegistry.on('import-finished', ({ ns }) => {
226+
const { namespace } = store.getState();
227+
if (ns === namespace) {
228+
refreshInput(store);
229+
}
230+
});
231+
232232
/**
233233
* Set the environment.
234234
*/

packages/compass-collection/src/stores/store.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,33 @@ store.onActivated = (appRegistry) => {
139139
}
140140
});
141141

142+
ipc.on('compass:open-export', () => {
143+
const state = store.getState();
144+
if (state.tabs) {
145+
const activeTab = state.tabs.find((tab) => tab.isActive === true);
146+
if (activeTab) {
147+
const crudStore = activeTab.localAppRegistry.getStore('CRUD.Store');
148+
const { query: crudQuery, count } = crudStore.state;
149+
const { filter, limit, skip } = crudQuery;
150+
appRegistry.emit('open-export', {
151+
namespace: activeTab.namespace,
152+
query: { filter, limit, skip },
153+
count,
154+
});
155+
}
156+
}
157+
});
158+
159+
ipc.on('compass:open-import', () => {
160+
const state = store.getState();
161+
if (state.tabs) {
162+
const activeTab = state.tabs.find((tab) => tab.isActive === true);
163+
if (activeTab) {
164+
appRegistry.emit('open-import', { namespace: activeTab.namespace });
165+
}
166+
}
167+
});
168+
142169
/**
143170
* Set the app registry to use later.
144171
*/

packages/compass-crud/src/stores/crud-store.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -717,16 +717,22 @@ const configureStore = (options = {}) => {
717717
* Emits a global app registry event the plugin listens to.
718718
*/
719719
openImportFileDialog() {
720-
this.localAppRegistry.emit('open-import');
720+
this.globalAppRegistry.emit('open-import', { namespace: this.state.ns });
721721
},
722722

723723
/**
724724
* Open an export file dialog from compass-import-export-plugin.
725725
* Emits a global app registry event the plugin listens to.
726726
*/
727727
openExportFileDialog() {
728-
// Pass the doc count to the export modal so we can avoid re-counting.
729-
this.localAppRegistry.emit('open-export', this.state.count);
728+
// Only three query fields that export modal will handle
729+
const { filter, limit, skip } = this.state.query;
730+
this.globalAppRegistry.emit('open-export', {
731+
namespace: this.state.ns,
732+
query: { filter, limit, skip },
733+
// Pass the doc count to the export modal so we can avoid re-counting.
734+
count: this.state.count
735+
});
730736
},
731737

732738
/**
@@ -1228,7 +1234,6 @@ const configureStore = (options = {}) => {
12281234
const localAppRegistry = options.localAppRegistry;
12291235

12301236
localAppRegistry.on('query-changed', store.onQueryChanged.bind(store));
1231-
localAppRegistry.on('import-finished', store.refreshDocuments.bind(store));
12321237
localAppRegistry.on('refresh-data', store.refreshDocuments.bind(store));
12331238

12341239
setLocalAppRegistry(store, options.localAppRegistry);
@@ -1241,10 +1246,17 @@ const configureStore = (options = {}) => {
12411246
globalAppRegistry.on('instance-created', ({ instance }) => {
12421247
store.onInstanceCreated(instance);
12431248
});
1249+
12441250
globalAppRegistry.on('refresh-data', () => {
12451251
store.refreshDocuments();
12461252
});
12471253

1254+
globalAppRegistry.on('import-finished', ({ ns }) => {
1255+
if (ns === store.state.ns) {
1256+
store.refreshDocuments();
1257+
}
1258+
});
1259+
12481260
setGlobalAppRegistry(store, globalAppRegistry);
12491261
}
12501262

packages/compass-import-export/src/components/export-modal/export-modal.jsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,6 @@ class ExportModal extends PureComponent {
386386
* @returns {Object} The mapped properties.
387387
*/
388388
const mapStateToProps = (state) => {
389-
const exportCount = state.exportData.count;
390-
const rawCount = state.stats.rawDocumentCount;
391-
392-
// 0 is a valid number of documents
393-
const count = typeof exportCount === 'number' ? exportCount : rawCount;
394-
395389
return {
396390
ns: state.ns,
397391
error: state.exportData.error,
@@ -405,7 +399,8 @@ const mapStateToProps = (state) => {
405399
exportStep: state.exportData.exportStep,
406400
isFullCollection: state.exportData.isFullCollection,
407401
exportedDocsCount: state.exportData.exportedDocsCount,
408-
count
402+
// 0 is a valid number of documents, only ignore null or undefined
403+
count: state.exportData.count ?? null
409404
};
410405
};
411406

packages/compass-import-export/src/components/export-modal/export-modal.spec.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ import React from 'react';
22
import { mount } from 'enzyme';
33
import { expect } from 'chai';
44

5-
import configureExportstore from '../../stores/export-store';
5+
import store from '../../stores/export-store';
66
import ExportModal from './export-modal';
77

8-
function renderModal(store) {
8+
function renderModal() {
99
return mount(
1010
<ExportModal store={store}/>
1111
);
1212
}
1313

1414
describe('ExportModal Component', function() {
15-
let store;
1615
let state;
1716

1817
beforeEach(function() {
19-
store = configureExportstore();
2018
state = store.getState();
2119

2220
// all the state for all export stores is the same state until we make a copy here
@@ -26,23 +24,23 @@ describe('ExportModal Component', function() {
2624

2725
it('should render zero results', function() {
2826
state.exportData.count = 0;
29-
const component = renderModal(store);
27+
const component = renderModal();
3028
const elements = component.find('[data-test-id="export-with-filters-label"]');
3129
expect(elements).to.have.lengthOf(1);
3230
expect(elements.first().text()).to.equal('Export query with filters — 0 results (Recommended)');
3331
});
3432

3533
it('should render 5 results', function() {
3634
state.exportData.count = 5;
37-
const component = renderModal(store);
35+
const component = renderModal();
3836
const elements = component.find('[data-test-id="export-with-filters-label"]');
3937
expect(elements).to.have.lengthOf(1);
4038
expect(elements.first().text()).to.equal('Export query with filters — 5 results (Recommended)');
4139
});
4240

4341
it('should render 5 results', function() {
4442
state.exportData.count = null;
45-
const component = renderModal(store);
43+
const component = renderModal();
4644
const elements = component.find('[data-test-id="export-with-filters-label"]');
4745
expect(elements).to.have.lengthOf(1);
4846
expect(elements.first().text()).to.equal('Export query with filters (Recommended)');

packages/compass-import-export/src/export-plugin.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
import React, { Component } from 'react';
2-
import PropTypes from 'prop-types';
32
import { Provider } from 'react-redux';
43
import ExportModal from './components/export-modal';
4+
import exportStore from './stores/export-store';
55

66
class ExportPlugin extends Component {
7-
static displayName = 'ExportPlugin';
8-
static propTypes = {
9-
store: PropTypes.object.isRequired
10-
}
11-
127
/**
138
* Connect the Plugin to the store and render.
149
*
1510
* @returns {React.Component} The rendered component.
1611
*/
1712
render() {
1813
return (
19-
<Provider store={this.props.store}>
14+
<Provider store={exportStore}>
2015
<ExportModal />
2116
</Provider>
2217
);

packages/compass-import-export/src/import-plugin.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
import React, { Component } from 'react';
2-
import PropTypes from 'prop-types';
32
import { Provider } from 'react-redux';
43
import ImportModal from './components/import-modal';
4+
import importStore from './stores/import-store';
55

66
class ImportPlugin extends Component {
7-
static displayName = 'ImportPlugin';
8-
static propTypes = {
9-
store: PropTypes.object.isRequired
10-
}
11-
127
/**
138
* Connect the Plugin to the store and render.
149
*
1510
* @returns {React.Component} The rendered component.
1611
*/
1712
render() {
1813
return (
19-
<Provider store={this.props.store}>
14+
<Provider store={importStore}>
2015
<ImportModal />
2116
</Provider>
2217
);
Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import Plugin from './plugin';
22
import ImportPlugin from './import-plugin';
33
import ExportPlugin from './export-plugin';
4-
import configureExportStore from './stores/export-store';
5-
import configureImportStore from './stores/import-store';
4+
import exportStore from './stores/export-store';
5+
import importStore from './stores/import-store';
66

77
/**
88
* The import plugin.
99
*/
1010
const IMPORT_ROLE = {
1111
name: 'Import',
1212
component: ImportPlugin,
13-
configureStore: configureImportStore,
14-
configureActions: () => {},
15-
storeName: 'Import.Store'
1613
};
1714

1815
/**
@@ -21,36 +18,30 @@ const IMPORT_ROLE = {
2118
const EXPORT_ROLE = {
2219
name: 'Export',
2320
component: ExportPlugin,
24-
configureStore: configureExportStore,
25-
configureActions: () => {},
26-
storeName: 'Export.Store'
2721
};
2822

2923
/**
3024
* Activate all the components in the Import Export package.
3125
* @param {Object} appRegistry - The Hadron appRegisrty to activate this plugin with.
3226
**/
3327
function activate(appRegistry) {
34-
appRegistry.registerRole('Collection.ScopedModal', IMPORT_ROLE);
35-
appRegistry.registerRole('Collection.ScopedModal', EXPORT_ROLE);
28+
appRegistry.registerRole('Global.Modal', EXPORT_ROLE);
29+
appRegistry.registerStore('ExportModal.Store', exportStore);
30+
appRegistry.registerRole('Global.Modal', IMPORT_ROLE);
31+
appRegistry.registerStore('ImportModal.Store', importStore);
3632
}
3733

3834
/**
3935
* Deactivate all the components in the Import Export package.
4036
* @param {Object} appRegistry - The Hadron appRegisrty to deactivate this plugin with.
4137
**/
4238
function deactivate(appRegistry) {
43-
appRegistry.deregisterRole('Collection.ScopedModal', IMPORT_ROLE);
44-
appRegistry.deregisterRole('Collection.ScopedModal', EXPORT_ROLE);
39+
appRegistry.deregisterRole('Global.Modal', EXPORT_ROLE);
40+
appRegistry.deregisterStore('ExportModal.Store', exportStore);
41+
appRegistry.deregisterRole('Global.Modal', IMPORT_ROLE);
42+
appRegistry.deregisterStore('ImportModal.Store', importStore);
4543
}
4644

4745
export default Plugin;
48-
export {
49-
activate,
50-
deactivate,
51-
ImportPlugin,
52-
ExportPlugin,
53-
configureExportStore,
54-
configureImportStore
55-
};
46+
export { activate, deactivate, ImportPlugin, ExportPlugin };
5647
export { default as metadata } from '../package.json';

packages/compass-import-export/src/modules/compass/app-registry.js

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

packages/compass-import-export/src/modules/compass/app-registry.spec.js

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

0 commit comments

Comments
 (0)