Files
grafana/public/app/stores/ViewStore/ViewStore.ts
Johannes Schill 8abef88b94 mobx: poc in using each store as individual prop on the react containers (#10414)
* mobx: poc in using each store as individual prop on the react containers

* prettier test

* fix: end the war between prettier vs tslint.

* mobx: Move stores into their own folders

* mobx: Refactor the AlertRule into its own file and add a helper-file

* mobx: Move NavItem out of NavStore and remove lodash dependancy

* mobx: Move ResultItem and SearchResultSection models out of the SearchStore

* mobx: ServerStatsStore rename .tsx => .ts. And move ServerStat-model to its own file.

* mobx: Remove lodash and jquery dependancy from ViewStore

* mobx: Remove issue with double question mark
2018-01-03 20:11:07 +01:00

47 lines
1.1 KiB
TypeScript

import { types } from 'mobx-state-tree';
const QueryValueType = types.union(types.string, types.boolean, types.number);
const urlParameterize = queryObj => {
const keys = Object.keys(queryObj);
const newQuery = keys.reduce((acc: string, key: string, idx: number) => {
const preChar = idx === 0 ? '?' : '&';
return acc + preChar + key + '=' + queryObj[key];
}, '');
return newQuery;
};
export const ViewStore = types
.model({
path: types.string,
query: types.map(QueryValueType),
})
.views(self => ({
get currentUrl() {
let path = self.path;
if (self.query.size) {
path += urlParameterize(self.query.toJS());
}
return path;
},
}))
.actions(self => {
function updateQuery(query: any) {
self.query.clear();
for (let key of Object.keys(query)) {
self.query.set(key, query[key]);
}
}
function updatePathAndQuery(path: string, query: any) {
self.path = path;
updateQuery(query);
}
return {
updateQuery,
updatePathAndQuery,
};
});