Skip to content

Commit

Permalink
Dependent containers re-initialization using eventHub
Browse files Browse the repository at this point in the history
  • Loading branch information
contributeless committed Apr 6, 2021
1 parent 55c4a35 commit 8cd9755
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
15 changes: 8 additions & 7 deletions src/OV.React/src/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Loader } from './views/Loader';
import { ServiceContainer } from './state/ServiceContainer';
import { SettingsContainer } from './state/SettingsContainer';
import { ErrorNotificationList } from './views/ErrorNotificationList';
import EventHub from './services/EventHub';
import { EventTypes } from './models/EventTypes';

interface AppProps {

Expand All @@ -29,14 +31,13 @@ export class App extends React.Component<AppProps, AppState> {
}

async componentDidMount() {
this.settingsContainer.subscribe(async () => {
if (this.settingsContainer.isSettingsValid() && !this.settingsContainer.state.isSettingsOpened) {
await this.filterContainer.initialize();
await this.oplogContainer.initialize();
}
})

await this.settingsContainer.initialize();
await this.filterContainer.initialize(this.settingsContainer.isSettingsValid());
await this.oplogContainer.initialize();

EventHub.on(EventTypes.CONNECTION_STRING_CHANGED, async () => {
await this.filterContainer.reloadPrefillInfo();
});
}

render() {
Expand Down
3 changes: 2 additions & 1 deletion src/OV.React/src/src/models/EventTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export enum EventTypes {
FETCH_ERROR = "FETCH_ERROR"
FETCH_ERROR = "FETCH_ERROR",
CONNECTION_STRING_CHANGED = "CONNECTION_STRING_CHANGED",
}
27 changes: 24 additions & 3 deletions src/OV.React/src/src/state/OplogFilterContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ export class OplogFilterContainer extends BaseContainer<OplogFilterContainerStat
super(serviceContainer);
}

initialize = async (): Promise<void> => {
const prefillResponse = await this.makeRequest(() => OplogService.prefill());
initialize = async (isConnectionStringPresent: boolean): Promise<void> => {
if(isConnectionStringPresent){
await this.reloadPrefillInfo();
}

const favouriteFilters = FilterService.loadFavouriteFilters()
?.map(x => {
return {
Expand All @@ -53,7 +56,6 @@ export class OplogFilterContainer extends BaseContainer<OplogFilterContainerStat
const searchFilter = FilterService.loadSearchFilter() ?? this.state.searchFilter;

await this.setState({
databaseOptions: prefillResponse.databases,
favouriteFilters: favouriteFilters ?? [],
searchFilter: {
...searchFilter,
Expand All @@ -65,6 +67,25 @@ export class OplogFilterContainer extends BaseContainer<OplogFilterContainerStat
this.subscribe(this.onStateChange)
};

reloadPrefillInfo = async () => {
const prefillResponse = await this.makeRequest(() => OplogService.prefill());
let databases: DatabasePrefillModel[] = prefillResponse.databases;

await this.setState({
databaseOptions: databases,
searchFilter: {
collection: "",
database: "",
endDate: null,
startDate: null,
filterId: "",
recordId: ""
}
});

await this.onSearchFilterChange();
}

onStateChange = ()=> {
FilterService.saveFavouriteFilters(this.state.favouriteFilters);
FilterService.saveSearchFilter(this.state.searchFilter);
Expand Down
10 changes: 10 additions & 0 deletions src/OV.React/src/src/state/SettingsContainer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { EventTypes } from "../models/EventTypes";
import { SettingsModel } from "../models/SettingsModel";
import { ConfigService } from "../services/ConfigService";
import EventHub from "../services/EventHub";
import { BaseContainer } from "./BaseContainer";
import { ServiceContainer } from "./ServiceContainer";

Expand Down Expand Up @@ -55,12 +57,20 @@ export class SettingsContainer extends BaseContainer<SettingsContainerState> {
connectionString: model.connectionString
}));

const isConnectionStringChanged = result.connectionString != this.state.settings.connectionString
&& result.isConfigured === true;

await this.setState({
isConfigured: result.isConfigured,
settings: {
connectionString: result.connectionString ?? ""
},
isSettingsOpened: result.isConfigured === false
});

if(isConnectionStringChanged) {
EventHub.emit(EventTypes.CONNECTION_STRING_CHANGED, {});
}

}
}

0 comments on commit 8cd9755

Please sign in to comment.