Browse Source

url: fix for boolean querystring parameters

Without this fix then the querystring looks like ?abool=true which causes
a mismatch with the angular routing. This results in a redirect and messes
up the browser history and back button.
Daniel Lee 8 năm trước cách đây
mục cha
commit
816c4d2340

+ 5 - 1
public/app/core/utils/url.ts

@@ -12,7 +12,11 @@ export function toUrlParams(a) {
 
   let add = function(k, v) {
     v = typeof v === 'function' ? v() : v === null ? '' : v === undefined ? '' : v;
-    s[s.length] = encodeURIComponent(k) + '=' + encodeURIComponent(v);
+    if (typeof v !== 'boolean') {
+      s[s.length] = encodeURIComponent(k) + '=' + encodeURIComponent(v);
+    } else {
+      s[s.length] = encodeURIComponent(k);
+    }
   };
 
   let buildParams = function(prefix, obj) {

+ 6 - 0
public/app/stores/ViewStore/ViewStore.jest.ts

@@ -23,4 +23,10 @@ describe('ViewStore', () => {
     expect(toJS(store.query.get('values'))).toMatchObject(['A', 'B']);
     expect(store.currentUrl).toBe('/hello?values=A&values=B');
   });
+
+  it('Query can contain boolean', () => {
+    store.updatePathAndQuery('/hello', { abool: true });
+    expect(toJS(store.query.get('abool'))).toBe(true);
+    expect(store.currentUrl).toBe('/hello?abool');
+  });
 });