diff --git a/src/constants/action_types.js b/src/constants/action_types.js index 3a91dc7..b01704c 100644 --- a/src/constants/action_types.js +++ b/src/constants/action_types.js @@ -127,3 +127,6 @@ export const SYNC_APPLY_STARTED = 'SYNC_APPLY_STARTED'; export const SYNC_APPLY_COMPLETED = 'SYNC_APPLY_COMPLETED'; export const SYNC_APPLY_FAILED = 'SYNC_APPLY_FAILED'; export const SYNC_RESET = 'SYNC_RESET'; + +// Lbry.tv +export const UPDATE_UPLOAD_PROGRESS = 'UPDATE_UPLOAD_PROGRESS'; diff --git a/src/index.js b/src/index.js index 616bd84..81dad29 100644 --- a/src/index.js +++ b/src/index.js @@ -75,6 +75,7 @@ export { doResetSync, doSyncEncryptAndDecrypt, } from 'redux/actions/sync'; +export { doUpdateUploadProgress } from 'redux/actions/lbrytv'; // reducers export { authReducer } from 'redux/reducers/auth'; @@ -87,6 +88,7 @@ export { filteredReducer } from 'redux/reducers/filtered'; export { homepageReducer } from 'redux/reducers/homepage'; export { statsReducer } from 'redux/reducers/stats'; export { syncReducer } from 'redux/reducers/sync'; +export { lbrytvReducer } from 'redux/reducers/lbrytv'; // selectors export { selectAuthToken, selectIsAuthenticating } from 'redux/selectors/auth'; @@ -188,3 +190,4 @@ export { selectHashChanged, selectSyncApplyErrorMessage, } from 'redux/selectors/sync'; +export { selectCurrentUploads, selectUploadCount } from 'redux/selectors/lbrytv'; diff --git a/src/redux/actions/lbrytv.js b/src/redux/actions/lbrytv.js new file mode 100644 index 0000000..1bcce69 --- /dev/null +++ b/src/redux/actions/lbrytv.js @@ -0,0 +1,12 @@ +// @flow +import * as ACTIONS from 'constants/action_types'; + +export const doUpdateUploadProgress = ( + progress: string, + params: { [key: string]: any }, + xhr: any +) => (dispatch: Dispatch) => + dispatch({ + type: ACTIONS.UPDATE_UPLOAD_PROGRESS, + data: { progress, params, xhr }, + }); diff --git a/src/redux/reducers/lbrytv.js b/src/redux/reducers/lbrytv.js new file mode 100644 index 0000000..c28a685 --- /dev/null +++ b/src/redux/reducers/lbrytv.js @@ -0,0 +1,62 @@ +// @flow +import * as ACTIONS from 'constants/action_types'; + +/* +test mock: + currentUploads: { + 'test#upload': { + progress: 50, + params: { + name: 'steve', + thumbnail_url: 'https://dev2.spee.ch/4/KMNtoSZ009fawGz59VG8PrID.jpeg', + }, + }, + }, + */ + +export type Params = { + channel?: string, + name: string, + thumbnail_url: ?string, + title: ?string, +}; + +export type UploadItem = { + progess: string, + params: Params, + xhr?: any, +}; + +export type TvState = { + currentUploads: { [key: string]: UploadItem }, +}; + +const reducers = {}; + +const defaultState: TvState = { + currentUploads: {}, +}; + +reducers[ACTIONS.UPDATE_UPLOAD_PROGRESS] = (state: TvState, action) => { + const { progress, params, xhr } = action.data; + const key = params.channel ? `${params.name}#${params.channel}` : `${params.name}#anonymous`; + let currentUploads; + if (!progress) { + currentUploads = Object.assign({}, state.currentUploads); + Object.keys(currentUploads).forEach(k => { + if (k === key) { + delete currentUploads[key]; + } + }); + } else { + currentUploads = Object.assign({}, state.currentUploads); + currentUploads[key] = { progress, params, xhr }; + } + return { ...state, currentUploads }; +}; + +export function lbrytvReducer(state = defaultState, action) { + const handler = reducers[action.type]; + if (handler) return handler(state, action); + return state; +} diff --git a/src/redux/selectors/lbrytv.js b/src/redux/selectors/lbrytv.js new file mode 100644 index 0000000..58d3c1b --- /dev/null +++ b/src/redux/selectors/lbrytv.js @@ -0,0 +1,10 @@ +import { createSelector } from 'reselect'; + +const selectState = state => state.lbrytv || {}; + +export const selectCurrentUploads = createSelector(selectState, state => state.currentUploads); + +export const selectUploadCount = createSelector( + selectCurrentUploads, + currentUploads => currentUploads && Object.keys(currentUploads).length +);