Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
feat(autoplay): Add autoplay as url parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-heimbuch committed Nov 4, 2017
1 parent 6c55fe3 commit 7352579
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 24 deletions.
18 changes: 13 additions & 5 deletions src/embed/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import browser from 'detect-browser'

import { findNode, createNode, appendNode, tag } from 'utils/dom'
import requestConfig from 'utils/request'
import { params } from 'utils/url'
import { urlParameters } from 'utils/url'

import { iframeResizer } from 'iframe-resizer'
// eslint-disable-next-line
Expand Down Expand Up @@ -86,12 +86,19 @@ const renderPlayer = anchor => player => {
.then(getPodloveStore)
}

const mergeConfig = (episode) =>
const getConfig = (episode) =>
Bluebird.resolve(episode)
// If the config is a string, lets assume that this will point to the remote config json
.then(config => isString(config) ? requestConfig(config) : config)
// Load parameters from url
.then(config => Object.assign({}, config, params))

const dispatchUrlParameters = store => {
store.dispatch({
type: 'SET_URL_PARAMS',
payload: urlParameters
})

return store
}

// Config Node
const configNode = (config = {}) => tag('script', `window.PODLOVE = ${JSON.stringify(config)}`)
Expand All @@ -111,7 +118,7 @@ const playerEntry = tag('PodlovePlayer')
window.podlovePlayer = (selector, episode) => {
const anchor = typeof selector === 'string' ? head(findNode(selector)) : selector

return mergeConfig(episode)
return getConfig(episode)
.then(config => Bluebird.all([
playerEntry,
configNode(config),
Expand All @@ -122,4 +129,5 @@ window.podlovePlayer = (selector, episode) => {
]))
.then(result => result.join(''))
.then(renderPlayer(anchor))
.then(dispatchUrlParameters)
}
7 changes: 3 additions & 4 deletions src/embed/share.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'babel-polyfill'
import { params } from 'utils/url'
import { urlParameters } from 'utils/url'
import remoteConfig from 'utils/request'

import app from '../app'

remoteConfig(params.episode)
.then(config => Object.assign({}, config, params))
.then(config => Object.assign({}, config, {display: 'embed'}))
remoteConfig(urlParameters.episode)
.then(config => ({ ...config, display: 'embed' }))
.then(app)
2 changes: 2 additions & 0 deletions src/store/effects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import idleEffects from './idle'
import quantileEffects from './quantiles'
import chapterEffects from './chapters'
import volumeEffects from './volume'
import urlEffects from './url'

import storage from 'utils/storage'
import keyhandler from 'utils/keyboard'
Expand All @@ -27,5 +28,6 @@ export default store => {
volumeEffects(store, action)
componentsEffects(store, action)
playerEffects(store, action)
urlEffects(store, action)
}
}
14 changes: 5 additions & 9 deletions src/store/effects/storage.episode.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,23 @@ export default (storage, store, action) => {
break
case 'SET_PLAYTIME':
case 'UPDATE_PLAYTIME':
if (!podloveStorage) {
return
}

podloveStorage.set('playtime', action.payload)
podloveStorage && podloveStorage.set('playtime', action.payload)
break
case 'TOGGLE_TAB':
const tabs = get(store.getState(), 'tabs', {})
podloveStorage.set('tabs', tabs)
podloveStorage && podloveStorage.set('tabs', tabs)
break
case 'SET_QUANTILE':
const quantiles = get(store.getState(), 'quantiles', [])
podloveStorage.set('quantiles', quantiles)
podloveStorage && podloveStorage.set('quantiles', quantiles)
break
case 'SET_VOLUME':
const currentVolume = get(store.getState(), 'volume', 1)
podloveStorage.set('volume', currentVolume)
podloveStorage && podloveStorage.set('volume', currentVolume)
break
case 'SET_RATE':
const currentRate = get(store.getState(), 'rate', 1)
podloveStorage.set('rate', currentRate)
podloveStorage && podloveStorage.set('rate', currentRate)
break
}
}
10 changes: 6 additions & 4 deletions src/store/effects/storage.episode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ test(`storageEffects: it sets the quantiles on INIT if stored`, t => {
})
})

test(`storageEffects: it doesn't sets the playtime on INIT if not stored`, t => {
getStub = sinon.stub().returns(undefined)
test(`storageEffects: it doesn't sets state on INIT if nothing is stored`, t => {
storage = sinon.stub().returns({
set: setStub,
get: sinon.stub().returns(undefined)
})

storageEffects(storage, store, {
type: 'INIT',
Expand All @@ -124,8 +127,7 @@ test(`storageEffects: it doesn't sets the playtime on INIT if not stored`, t =>
}
})

t.truthy(storage.called)
t.falsy(store.called)
t.falsy(store.dispatch.called)
})

test(`storageEffects: it persists the playtime on SET_PLAYTIME`, t => {
Expand Down
16 changes: 16 additions & 0 deletions src/store/effects/storage.live.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,19 @@ test(`storageEffects: it persists the tabs on TOGGLE_TAB`, t => {
settings: true
})
})

test(`storageEffects: it doesn't sets state on INIT if nothing is stored`, t => {
storage = sinon.stub().returns({
set: setStub,
get: sinon.stub().returns(undefined)
})

storageEffects(storage, store, {
type: 'INIT',
payload: {
foo: 'bar'
}
})

t.falsy(store.dispatch.called)
})
16 changes: 16 additions & 0 deletions src/store/effects/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import actions from '../actions'

export default (store, { type, payload }) => {
switch (type) {
case 'SET_URL_PARAMS':
if (payload.playtime) {
store.dispatch(actions.setPlaytime(payload.playtime))
store.dispatch(actions.idle())
}

if (payload.autoplay) {
store.dispatch(actions.play())
}
break
}
}
43 changes: 43 additions & 0 deletions src/store/effects/url.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import test from 'ava'
import sinon from 'sinon'

import urlEffects from './url'

let store

test.beforeEach(t => {
store = {
dispatch: sinon.stub()
}
})

test(`storageEffects: it dispatches autoplay on SET_URL_PARAMS`, t => {
urlEffects(store, {
type: 'SET_URL_PARAMS',
payload: {
autoplay: true
}
})

t.deepEqual(store.dispatch.getCall(0).args[0], {
type: 'UI_PLAY'
})
})

test(`storageEffects: it dispatches playtime on SET_URL_PARAMS`, t => {
urlEffects(store, {
type: 'SET_URL_PARAMS',
payload: {
playtime: 10
}
})

t.deepEqual(store.dispatch.getCall(0).args[0], {
type: 'SET_PLAYTIME',
payload: 10
})

t.deepEqual(store.dispatch.getCall(1).args[0], {
type: 'IDLE'
})
})
8 changes: 6 additions & 2 deletions src/utils/url.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import queryString from 'query-string'
import { timeToSeconds } from 'utils/time'

const locationParams = queryString.parse(window.location.search)
export const locationParams = queryString.parse(window.location.search)

const parseParameters = parameters => {
const parsed = {}
Expand All @@ -14,10 +14,14 @@ const parseParameters = parameters => {
parsed.episode = parameters.episode
}

if (parameters.autoplay) {
parsed.autoplay = true
}

return parsed
}

export const params = Object.assign({}, parseParameters(locationParams))
export const urlParameters = {...parseParameters(locationParams)}

export const addQueryParameter = (url, additionalParameters = {}) => {
const parser = document.createElement('a')
Expand Down

0 comments on commit 7352579

Please sign in to comment.