Commit de456f8b by Torkel Ödegaard

wip: solid progress on redux -> angular location bridge update

parent 6efe9da1
import { initNav } from './navModel'; import { initNav } from './navModel';
import { updateLocation } from './location';
export { initNav }; export { initNav, updateLocation };
import { LocationUpdate } from 'app/types';
export type Action = UpdateLocationAction;
export interface UpdateLocationAction {
type: 'UPDATE_LOCATION';
payload: LocationUpdate;
}
export const updateLocation = (location: LocationUpdate): UpdateLocationAction => ({
type: 'UPDATE_LOCATION',
payload: location,
});
import navModel from './navModel'; import navModel from './navModel';
import location from './location';
export default { export default {
navModel, navModel,
location,
}; };
import { Action } from 'app/core/actions/location';
import { LocationState, UrlQueryMap } from 'app/types';
import { toUrlParams } from 'app/core/utils/url';
export const initialState: LocationState = {
url: '',
path: '',
query: {},
routeParams: {},
};
function renderUrl(path: string, query: UrlQueryMap): string {
if (Object.keys(query).length > 0) {
path += '?' + toUrlParams(query);
}
return path;
}
const routerReducer = (state = initialState, action: Action): LocationState => {
switch (action.type) {
case 'UPDATE_LOCATION': {
const { path, query, routeParams } = action.payload;
return {
url: renderUrl(path || state.path, query),
path: path || state.path,
query: query || state.query,
routeParams: routeParams || state.routeParams,
};
}
}
return state;
};
export default routerReducer;
import coreModule from 'app/core/core_module'; import coreModule from 'app/core/core_module';
import appEvents from 'app/core/app_events'; import appEvents from 'app/core/app_events';
import { store } from 'app/stores/store'; import { store } from 'app/stores/store';
import { store as reduxStore } from 'app/stores/configureStore';
import { reaction } from 'mobx'; import { reaction } from 'mobx';
import locationUtil from 'app/core/utils/location_util'; import locationUtil from 'app/core/utils/location_util';
import { updateLocation } from 'app/core/actions';
// Services that handles angular -> mobx store sync & other react <-> angular sync // Services that handles angular -> mobx store sync & other react <-> angular sync
export class BridgeSrv { export class BridgeSrv {
...@@ -19,12 +21,30 @@ export class BridgeSrv { ...@@ -19,12 +21,30 @@ export class BridgeSrv {
if (store.view.currentUrl !== angularUrl) { if (store.view.currentUrl !== angularUrl) {
store.view.updatePathAndQuery(this.$location.path(), this.$location.search(), this.$route.current.params); store.view.updatePathAndQuery(this.$location.path(), this.$location.search(), this.$route.current.params);
} }
const state = reduxStore.getState();
if (state.location.url !== angularUrl) {
reduxStore.dispatch(
updateLocation({
path: this.$location.path(),
query: this.$location.search(),
routeParams: this.$route.current.params,
})
);
}
}); });
this.$rootScope.$on('$routeChangeSuccess', (evt, data) => { this.$rootScope.$on('$routeChangeSuccess', (evt, data) => {
store.view.updatePathAndQuery(this.$location.path(), this.$location.search(), this.$route.current.params); store.view.updatePathAndQuery(this.$location.path(), this.$location.search(), this.$route.current.params);
reduxStore.dispatch(
updateLocation({
path: this.$location.path(),
query: this.$location.search(),
routeParams: this.$route.current.params,
})
);
}); });
// listen for mobx store changes and update angular
reaction( reaction(
() => store.view.currentUrl, () => store.view.currentUrl,
currentUrl => { currentUrl => {
...@@ -39,6 +59,19 @@ export class BridgeSrv { ...@@ -39,6 +59,19 @@ export class BridgeSrv {
} }
); );
// Listen for changes in redux location -> update angular location
reduxStore.subscribe(() => {
const state = reduxStore.getState();
const angularUrl = this.$location.url();
const url = locationUtil.stripBaseFromUrl(state.location.url);
if (angularUrl !== url) {
this.$timeout(() => {
this.$location.url(url);
});
console.log('store updating angular $location.url', url);
}
});
appEvents.on('location-change', payload => { appEvents.on('location-change', payload => {
const urlWithoutBase = locationUtil.stripBaseFromUrl(payload.href); const urlWithoutBase = locationUtil.stripBaseFromUrl(payload.href);
if (this.fullPageReloadRoutes.indexOf(urlWithoutBase) > -1) { if (this.fullPageReloadRoutes.indexOf(urlWithoutBase) > -1) {
......
...@@ -5,11 +5,13 @@ import classNames from 'classnames'; ...@@ -5,11 +5,13 @@ import classNames from 'classnames';
import PageHeader from 'app/core/components/PageHeader/PageHeader'; import PageHeader from 'app/core/components/PageHeader/PageHeader';
import appEvents from 'app/core/app_events'; import appEvents from 'app/core/app_events';
import Highlighter from 'react-highlight-words'; import Highlighter from 'react-highlight-words';
import { initNav } from 'app/core/actions'; import { initNav, updateLocation } from 'app/core/actions';
import { ContainerProps } from 'app/types'; import { ContainerProps } from 'app/types';
import { getAlertRules, AlertRule } from '../apis'; import { getAlertRules, AlertRule } from '../apis';
interface Props extends ContainerProps {} interface Props extends ContainerProps {
updateLocation: typeof updateLocation;
}
interface State { interface State {
rules: AlertRule[]; rules: AlertRule[];
...@@ -44,7 +46,9 @@ export class AlertRuleList extends PureComponent<Props, State> { ...@@ -44,7 +46,9 @@ export class AlertRuleList extends PureComponent<Props, State> {
} }
onStateFilterChanged = evt => { onStateFilterChanged = evt => {
// this.props.view.updateQuery({ state: evt.target.value }); this.props.updateLocation({
query: { state: evt.target.value },
});
// this.fetchRules(); // this.fetchRules();
}; };
...@@ -113,9 +117,7 @@ export class AlertRuleList extends PureComponent<Props, State> { ...@@ -113,9 +117,7 @@ export class AlertRuleList extends PureComponent<Props, State> {
<section> <section>
<ol className="alert-rule-list"> <ol className="alert-rule-list">
{rules.map(rule => ( {rules.map(rule => <AlertRuleItem rule={rule} key={rule.id} search={search} />)}
<AlertRuleItem rule={rule} key={rule.id} search={search} />
))}
</ol> </ol>
</section> </section>
</div> </div>
...@@ -204,6 +206,7 @@ const mapStateToProps = state => ({ ...@@ -204,6 +206,7 @@ const mapStateToProps = state => ({
const mapDispatchToProps = { const mapDispatchToProps = {
initNav, initNav,
updateLocation,
}; };
export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(AlertRuleList)); export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(AlertRuleList));
import { NavModel, NavModelItem } from './navModel'; import { NavModel, NavModelItem } from './navModel';
import { ContainerProps } from './container'; import { ContainerProps } from './container';
import { LocationState, LocationUpdate, UrlQueryMap, UrlQueryValue } from './location';
export { NavModel, NavModelItem, ContainerProps }; export { NavModel, NavModelItem, ContainerProps, LocationState, LocationUpdate, UrlQueryValue, UrlQueryMap };
export interface LocationUpdate {
path?: string;
query?: UrlQueryMap;
routeParams?: UrlQueryMap;
}
export interface LocationState {
url: string;
path: string;
query: UrlQueryMap;
routeParams: UrlQueryMap;
}
export type UrlQueryValue = string | number | boolean | string[] | number[] | boolean[];
export type UrlQueryMap = { [s: string]: UrlQueryValue };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment