diff --git a/client/utils/code-uri.js b/client/utils/code-uri.js new file mode 100644 index 0000000000..3622e5843c --- /dev/null +++ b/client/utils/code-uri.js @@ -0,0 +1,64 @@ +import flow from 'lodash/flow'; +import { decodeFcc } from '../../common/utils/encode-decode'; + +const queryRegex = /^(\?|#\?)/; +export function legacyIsInQuery(query, decode) { + let decoded; + try { + decoded = decode(query); + } catch (err) { + return false; + } + if (!decoded || typeof decoded.split !== 'function') { + return false; + } + return decoded + .replace(queryRegex, '') + .split('&') + .reduce(function(found, param) { + var key = param.split('=')[0]; + if (key === 'solution') { + return true; + } + return found; + }, false); +} + +export function getKeyInQuery(query, keyToFind = '') { + return query + .split('&') + .reduce((oldValue, param) => { + const key = param.split('=')[0]; + const value = param + .split('=') + .slice(1) + .join('='); + + if (key === keyToFind) { + return value; + } + return oldValue; + }, null); +} + +export function getLegacySolutionFromQuery(query = '', decode) { + return flow( + getKeyInQuery, + decode, + decodeFcc + )(query, 'solution'); +} + +export function getCodeUri({ location, decodeURIComponent }) { + let query; + if ( + location.search && + legacyIsInQuery(location.search, decodeURIComponent) + ) { + query = location.search.replace(/^\?/, ''); + } else { + return null; + } + + return getLegacySolutionFromQuery(query, decodeURIComponent); +} diff --git a/common/app/components/Nav/Points-Nav-Item.jsx b/common/app/components/Nav/Points-Nav-Item.jsx index 633a237cd2..a738c6da33 100644 --- a/common/app/components/Nav/Points-Nav-Item.jsx +++ b/common/app/components/Nav/Points-Nav-Item.jsx @@ -7,8 +7,8 @@ export default React.createClass({ 'aria-controls': React.PropTypes.string, className: React.PropTypes.string, href: React.PropTypes.string, - onClick: React.PropTypes.func.isRequired, - points: React.PropTypes.func, + onClick: React.PropTypes.func, + points: React.PropTypes.number, title: React.PropTypes.node }, diff --git a/common/app/routes/challenges/utils.js b/common/app/routes/challenges/utils.js index 8eda75aabc..9b2355e133 100644 --- a/common/app/routes/challenges/utils.js +++ b/common/app/routes/challenges/utils.js @@ -1,42 +1,17 @@ -import { compose } from 'redux'; +import flow from 'lodash/flow'; import { bonfire, html, js } from '../../utils/challengeTypes'; +import { decodeScriptTags } from '../../../utils/encode-decode'; import protect from '../../utils/empty-protector'; -export function encodeScriptTags(value) { - return value - .replace(/'); -} - -export function encodeFormAction(value) { - return value.replace( - /