From a2fb6428d55715807200820d390e2efb0fc4a5d8 Mon Sep 17 00:00:00 2001 From: jpellizzari Date: Fri, 13 Jan 2017 11:33:27 -0800 Subject: [PATCH] wip --- .../utils/__tests__/router-utils-test.js | 14 +++++++++++++ client/app/scripts/utils/router-utils.js | 20 +++++++++++++++++++ client/package.json | 5 ++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/client/app/scripts/utils/__tests__/router-utils-test.js b/client/app/scripts/utils/__tests__/router-utils-test.js index e69de29bb..7cec45988 100644 --- a/client/app/scripts/utils/__tests__/router-utils-test.js +++ b/client/app/scripts/utils/__tests__/router-utils-test.js @@ -0,0 +1,14 @@ +import { translateUrlParams } from '../router-utils'; + +describe('router-utils', () => { + it('translates a query to a view state', () => { + // const dispatch = createSpy(); + // const state = Object.assign({}, initialState, { + // nodes: [{node_1: 'some_id'}] + // }); + // window.location.search = '?node=node_1'; + // const page = getRouter(dispatch, state); + // expect(page).toBeTruthy(); + translateUrlParams('?key=value&otherkey=othervalue', []); + }); +}); diff --git a/client/app/scripts/utils/router-utils.js b/client/app/scripts/utils/router-utils.js index ffc4d85f1..f3f035464 100644 --- a/client/app/scripts/utils/router-utils.js +++ b/client/app/scripts/utils/router-utils.js @@ -1,7 +1,10 @@ import page from 'page'; +import reduce from 'lodash/reduce'; +import trimStart from 'lodash/trimStart'; import { route } from '../actions/app-actions'; import { storageGet, storageSet } from './storage-utils'; +import { searchTopology } from './search-utils'; // // page.js won't match the routes below if ":state" has a slash in it, so replace those before we @@ -83,10 +86,27 @@ export function updateRoute(getState) { } } +export function translateUrlParams(paramString, nodes) { + // ?key=value&otherkey=othervalue + let state; + const pairs = trimStart(paramString, '?').split('&'); + const params = reduce(pairs, (result, pair) => { + const [k, v] = pair.split('='); + result[k] = v; + return result; + }, {}); + if (params.node) { + state = searchTopology(nodes, { query: params.node }); + } + + return state; +} + export function getRouter(dispatch, initialState) { // strip any trailing '/'s. page.base(window.location.pathname.replace(/\/$/, '')); + console.log(translateUrlParams(window.location.search)); page('/', () => { // recover from storage state on empty URL diff --git a/client/package.json b/client/package.json index 2cd9eaa02..7c3dda389 100644 --- a/client/package.json +++ b/client/package.json @@ -58,6 +58,7 @@ "eslint-plugin-import": "2.2.0", "eslint-plugin-jsx-a11y": "2.2.3", "eslint-plugin-react": "6.8.0", + "expect": "^1.20.2", "extract-text-webpack-plugin": "1.0.1", "file-loader": "0.9.0", "font-awesome-webpack": "0.0.4", @@ -99,7 +100,9 @@ "loadreport": "npm run noprobe && sleep 1 && curl -X POST -H \"Content-Type: application/json\" http://$BACKEND_HOST/api/report -d" }, "jest": { - "transform": {".*": "/node_modules/babel-jest"}, + "transform": { + ".*": "/node_modules/babel-jest" + }, "setupFiles": [ "/test/support/localStorage.js" ],