import url from 'url' ; import { ActionTypes } from "./actionTypes" ; import { ActionTypes as AppActionTypes } from '../actionTypes' ; import { Bundle } from "../linker" ; export const initialStateExamples = { fullExamplesUrl : '' } ; export default function ( bundle ) { bundle . addReducer ( AppActionTypes . AppInit , ( state ) => { state . examples = updateExamplesState ( state , { } ) ; } ) ; bundle . addReducer ( ActionTypes . PlatformChanged , ( state ) => { state . examples = updateExamplesState ( state , state . examples ) ; } ) ; } function updateExamplesState ( state , examples ) { const { callbackUrl , examplesUrl , platform , language } = state . options ; if ( ! examplesUrl ) { return false ; } let fullCallbackUrl = url . parse ( callbackUrl , true ) ; delete fullCallbackUrl . search ; delete fullCallbackUrl . query [ 'source' ] ; delete fullCallbackUrl . query [ 'platform' ] ; fullCallbackUrl . query [ 'language' ] = language ; fullCallbackUrl = url . format ( fullCallbackUrl ) ; let fullExamplesUrl = url . parse ( examplesUrl , true ) ; fullExamplesUrl . query [ 'target' ] = '_self' ; fullExamplesUrl . query [ 'tags' ] = platform ; fullExamplesUrl . query [ 'lang' ] = language . replace ( /_.*$/ , '' ) ; fullExamplesUrl . query [ 'callback' ] = fullCallbackUrl ; fullExamplesUrl = url . format ( fullExamplesUrl ) ; return { ... examples , fullExamplesUrl } ; }