Adding filter bar
[aai/sparky-fe.git] / src / utils / Routes.js
1 import {encrypt } from './Crypto.js';
2 import { decrypt } from '../utils/Crypto.js';
3
4
5 export function changeUrlAddress(pathObj, historyObj) {
6   let toGo = '/' + pathObj.route;
7   // left global search to act the same as before for our 2 static views for now
8   // until we decide to change those 2 views too to act like extensible views.
9   if (pathObj.route === 'schema' || pathObj.route === 'vnfSearch') {
10     if (pathObj.filterValues && pathObj.filterValues.length > 0) {
11       let filterList = [];
12       for (let index in pathObj.filterValues) {
13         if (pathObj.filterValues[index].filterValue !== undefined) {
14           filterList.push(pathObj.filterValues[index].filterId +
15             '=' +
16             pathObj.filterValues[index].filterValue);
17         } else {
18           filterList.push(pathObj.filterValues[index].filterId + '=');
19         }
20       }
21       toGo = toGo + '/' + filterList.toString();
22     } else {
23       toGo = toGo + '/' + pathObj.hashId;
24     }
25   } else {
26     toGo += '/' + encrypt(JSON.stringify(pathObj));
27   }
28   historyObj.push(toGo, {lastRoute: pathObj.route});
29 }
30
31 export function buildRouteObjWithHash(routePath, routeHash) {
32   return {
33     route: routePath,
34     hashId: routeHash
35   };
36 }
37
38
39 export function decryptParamsForView(params) {
40   let jsonParam = {};
41
42   function isJson(str) {
43     try {
44       JSON.parse(str);
45     } catch (e) {
46       return false;
47     }
48     return true;
49   };
50
51   let stringParams;
52
53   try {
54     stringParams = decrypt(params);
55   } catch(e) {
56     //add inline message in next story
57     //happens when user changes the url.
58   }
59
60   if(!isJson(stringParams)) {
61     return jsonParam;
62   }
63
64   jsonParam = JSON.parse(stringParams);
65   return jsonParam;
66 }
67
68 export function buildRouteObjWithFilters(routePath, routeFiltersObj) {
69   let filterValues = [];
70   if (routeFiltersObj !== undefined) {
71     for (let id in routeFiltersObj) {
72       if (routeFiltersObj[id] !== undefined) {
73         filterValues.push(
74           {
75             'filterId': id,
76             'filterValue': routeFiltersObj[id]
77           }
78         );
79       } else {
80         filterValues.push(
81           {
82             'filterId': id,
83             'filterValue': ''
84           }
85         );
86       }
87     }
88   }
89
90   return {
91     route: routePath,
92     filterValues: filterValues
93   };
94 }