Skip to content

Commit 9cdec0f

Browse files
authored
Merge pull request #1404 from alexpelan/alexpelan/1358
Preserve experimental flag (and only experimental) after refresh
2 parents ad6c826 + 353e748 commit 9cdec0f

File tree

3 files changed

+85
-15
lines changed

3 files changed

+85
-15
lines changed

src/components/Workspace.jsx

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import get from 'lodash/get';
1010
import partial from 'lodash/partial';
1111
import map from 'lodash/map';
1212
import {t} from 'i18next';
13-
import qs from 'qs';
1413
import classnames from 'classnames';
1514
import {getNodeWidth, getNodeWidths} from '../util/resize';
15+
import {getQueryParameters, setQueryParameters} from '../util/queryParams';
1616
import {dehydrateProject, rehydrateProject} from '../clients/localStorage';
1717

1818
import {
@@ -76,21 +76,13 @@ class Workspace extends React.Component {
7676
}
7777

7878
componentWillMount() {
79-
let gistId = null;
80-
let snapshotKey = null;
81-
let isExperimental = false;
82-
if (location.search) {
83-
const query = qs.parse(location.search.slice(1));
84-
if (query.gist) {
85-
gistId = query.gist;
86-
}
87-
if (query.snapshot) {
88-
snapshotKey = query.snapshot;
89-
}
90-
isExperimental = Object.keys(query).includes('experimental');
91-
}
79+
const {
80+
gistId,
81+
snapshotKey,
82+
isExperimental,
83+
} = getQueryParameters(location.search);
9284
const rehydratedProject = rehydrateProject();
93-
history.replaceState({}, '', location.pathname);
85+
setQueryParameters({isExperimental});
9486
this.props.dispatch(applicationLoaded({
9587
snapshotKey,
9688
gistId,

src/util/queryParams.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import qs from 'qs';
2+
3+
export function getQueryParameters(queryString) {
4+
let gistId = null;
5+
let snapshotKey = null;
6+
let isExperimental = false;
7+
if (queryString) {
8+
const query = qs.parse(queryString.slice(1));
9+
if (query.gist) {
10+
gistId = query.gist;
11+
}
12+
if (query.snapshot) {
13+
snapshotKey = query.snapshot;
14+
}
15+
isExperimental = Object.keys(query).includes('experimental');
16+
}
17+
return {
18+
gistId,
19+
snapshotKey,
20+
isExperimental,
21+
};
22+
}
23+
24+
export function setQueryParameters(params) {
25+
if (params.isExperimental) {
26+
history.replaceState({}, '', '?experimental');
27+
} else {
28+
history.replaceState({}, '', location.pathname);
29+
}
30+
}

test/unit/util/queryParams.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import test from 'tape';
2+
import {getQueryParameters} from '../../../src/util/queryParams';
3+
4+
5+
test('empty querystring', (assert) => {
6+
const {gistId, snapshotKey, isExperimental} = getQueryParameters('');
7+
assert.isEqual(gistId, null);
8+
assert.isEqual(snapshotKey, null);
9+
assert.isEqual(isExperimental, false);
10+
assert.end();
11+
});
12+
13+
test('snapshot url', (assert) => {
14+
const {
15+
gistId,
16+
snapshotKey,
17+
isExperimental,
18+
} = getQueryParameters('?snapshot=90283083-5951-4af8-bc12-2fe889ea5e4a');
19+
assert.isEqual(gistId, null);
20+
assert.isEqual(snapshotKey, '90283083-5951-4af8-bc12-2fe889ea5e4a');
21+
assert.isEqual(isExperimental, false);
22+
assert.end();
23+
});
24+
25+
26+
test('experimental url', (assert) => {
27+
const {
28+
gistId,
29+
snapshotKey,
30+
isExperimental,
31+
} = getQueryParameters('?experimental');
32+
assert.isEqual(gistId, null);
33+
assert.isEqual(snapshotKey, null);
34+
assert.isEqual(isExperimental, true);
35+
assert.end();
36+
});
37+
38+
test('gist url', (assert) => {
39+
const {
40+
gistId,
41+
snapshotKey,
42+
isExperimental,
43+
} = getQueryParameters('?gist=223f7869fa7dea6089dffd72a38c3286');
44+
assert.isEqual(gistId, '223f7869fa7dea6089dffd72a38c3286');
45+
assert.isEqual(snapshotKey, null);
46+
assert.isEqual(isExperimental, false);
47+
assert.end();
48+
});

0 commit comments

Comments
 (0)