Skip to content

Commit

Permalink
Merge pull request #304 from lookit/add-child-hash-and-response-to-url
Browse files Browse the repository at this point in the history
Add child hash and response to exit url
  • Loading branch information
okaycj committed Jun 21, 2022
2 parents 46ba974 + 926521a commit 48f1c7d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/components/exp-player/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import layout from './template';

import FullScreen from '../../mixins/full-screen';
import ExperimentParser from '../../utils/parse-experiment';
import { addSearchParams } from '../../utils/add-search-params';

let {
$
Expand Down Expand Up @@ -192,7 +193,11 @@ export default Ember.Component.extend(FullScreen, {
},

_exit() {
this.get('session').save().then(() => window.location = this.get('experiment.exitURL') || '/');
const exitUrl = this.get('experiment.exitURL');
const hashChildId = this.get('session.hash_child_id');
const responseId = this.get('session.id');

this.get('session').save().then(() => window.location = addSearchParams(exitUrl, responseId, hashChildId) );
},

actions: {
Expand Down
3 changes: 2 additions & 1 deletion app/models/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export default DS.Model.extend({
study: DS.belongsTo('study'),
demographicSnapshot: DS.belongsTo('demographic'),
createdOn: DS.attr('date'),
isPreview: DS.attr('boolean', {defaultValue: false})
isPreview: DS.attr('boolean', {defaultValue: false}),
hash_child_id: DS.attr('string')
});
25 changes: 25 additions & 0 deletions app/utils/add-search-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const addSearchParams = function(exitUrl, responseId, hashChildId){
try {
// Parse URL and search params
const url = new URL(exitUrl);
const params = new URLSearchParams(url.search);

// Set child and response values
// params.set('child', this.get('session.hash_child_id'));
// params.set('response', this.get('session.id'))
params.set('child', hashChildId);
params.set('response', responseId);

// Set these changes to the URL
url.search = params;

// Return updated URL
return url.toString();

} catch (TypeError) {
// If the provided URL can't be parsed, return root.
return '/'
}
};

export { addSearchParams };
12 changes: 12 additions & 0 deletions tests/unit/utils/add-search-params-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { module, test} from 'qunit';
import { addSearchParams } from '../../../utils/add-search-params';

module('Unit | Utility | add search params');

test('Test add search params function', function(assert) {
assert.expect(3);
assert.equal(addSearchParams('https://lookit.mit.edu', 'repsonse-id', 'hash-child-id'), 'https://lookit.mit.edu/?child=hash-child-id&response=repsonse-id');
assert.equal(addSearchParams('not-a-url', 'repsonse-id', 'hash-child-id'), '/');
assert.equal(addSearchParams(undefined, 'repsonse-id', 'hash-child-id'), '/');
});

0 comments on commit 48f1c7d

Please sign in to comment.