| 
									
										
										
										
											2016-08-14 23:22:34 -07:00
										 |  |  | 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'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 12:10:09 -07:00
										 |  |  | export function getCodeUri(location, decodeURIComponent) { | 
					
						
							| 
									
										
										
										
											2016-08-14 23:22:34 -07:00
										 |  |  |   let query; | 
					
						
							|  |  |  |   if ( | 
					
						
							|  |  |  |     location.search && | 
					
						
							|  |  |  |     legacyIsInQuery(location.search, decodeURIComponent) | 
					
						
							|  |  |  |   ) { | 
					
						
							|  |  |  |     query = location.search.replace(/^\?/, ''); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return null; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return getLegacySolutionFromQuery(query, decodeURIComponent); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-15 12:10:09 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function removeCodeUri(location, history) { | 
					
						
							|  |  |  |   if ( | 
					
						
							| 
									
										
										
										
											2016-08-15 13:34:01 -07:00
										 |  |  |     typeof location.href.split !== 'function' || | 
					
						
							| 
									
										
										
										
											2016-08-15 12:10:09 -07:00
										 |  |  |     typeof history.replaceState !== 'function' | 
					
						
							|  |  |  |   ) { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   history.replaceState( | 
					
						
							|  |  |  |     history.state, | 
					
						
							|  |  |  |     null, | 
					
						
							| 
									
										
										
										
											2016-08-15 13:34:01 -07:00
										 |  |  |     location.href.split('?')[0] | 
					
						
							| 
									
										
										
										
											2016-08-15 12:10:09 -07:00
										 |  |  |   ); | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } |