Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions packages/services-store/src/actions/kalturaPlayerAPIv7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* @license
*
* Copyright IBM Corp. 2020, 2022
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { ThunkAction } from 'redux-thunk';
import KalturaPlayerAPI from '@carbon/ibmdotcom-services/es/services/KalturaPlayerV7/KalturaPlayer.js';
import {
MediaData,
MEDIA_PLAYER_API_ACTION,
MediaPlayerAPIState,
} from '../types/kalturaPlayerAPIv7';

/**
* @param mediaId A language.
* @param request The promise of the REST call for media data of the given language that is in progress.
* @returns A Redux action to set the state that the REST call for media data for the given language that is in progress.
* @private
*/
export function setRequestMediaDataInProgress(
mediaId: string,
request: Promise<MediaData>
) {
return {
type: MEDIA_PLAYER_API_ACTION.SET_REQUEST_MEDIA_DATA_IN_PROGRESS,
mediaId,
request,
};
}

/**
* @param mediaId A language.
* @param error An error from the REST call for media data of the given language.
* @returns A Redux action to set the state that the REST call for media data for the given language failed.
* @private
*/
export function setErrorRequestMediaData(mediaId: string, error: Error) {
return {
type: MEDIA_PLAYER_API_ACTION.SET_ERROR_REQUEST_MEDIA_DATA,
mediaId,
error,
};
}

/**
* @param mediaId A language.
* @param mediaData The media data from the REST call.
* @returns A Redux action to set the given media data.
*/
export function setMediaData(mediaId: string, mediaData: MediaData) {
return {
type: MEDIA_PLAYER_API_ACTION.SET_MEDIA_DATA,
mediaId,
mediaData,
};
}

/**
* A Redux action to work with `MediaPlayerAPI`.
*/
export type MediaPlayerAPIActions =
| ReturnType<typeof setRequestMediaDataInProgress>
| ReturnType<typeof setErrorRequestMediaData>
| ReturnType<typeof setMediaData>;

/**
* @returns A Redux action that sends a REST call for media data.
*/
export function loadMediaData(
mediaId: string
): ThunkAction<
Promise<MediaData>,
{ mediaPlayerAPI: MediaPlayerAPIState },
void,
MediaPlayerAPIActions
> {
return async (dispatch, getState) => {
const { requestsMediaData = {} } = getState().mediaPlayerAPI ?? {};
const { [mediaId]: requestMediaData } = requestsMediaData;
if (requestMediaData) {
return requestMediaData;
}
const promiseVideoData: Promise<MediaData> = KalturaPlayerAPI.api(mediaId);
dispatch(setRequestMediaDataInProgress(mediaId, promiseVideoData));
try {
dispatch(setMediaData(mediaId, await promiseVideoData));
} catch (error) {
dispatch(setErrorRequestMediaData(mediaId, error as Error));
}
return promiseVideoData;
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* @license
*
* Copyright IBM Corp. 2020, 2022
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import {
MEDIA_PLAYER_API_ACTION,
MediaData,
MediaPlayerAPIState,
} from '../../types/kalturaPlayerAPIv7';
import { MediaPlayerAPIActions } from '../../actions/kalturaPlayerAPIv7';
import convertValue from '../../../tests/utils/convert-value';
import reducer from '../kalturaPlayerAPIv7';

const mockMediaData: Partial<MediaData> = {
name: 'name-foo',
description: 'description-foo',
duration: 120,
};

describe('Redux reducers for `KalturaPlayerAPIv7`', () => {
it('should return the state unmodified for unknown action', () => {
const state = {
mediaData: {
'video-id-foo': mockMediaData as MediaData,
},
};
expect(reducer(state, {} as MediaPlayerAPIActions)).toEqual(state);
});

it('should support starting the spinner for loading video data', () => {
const request = Promise.resolve(mockMediaData as MediaData);
expect(
convertValue(
reducer({} as MediaPlayerAPIState, {
type: MEDIA_PLAYER_API_ACTION.SET_REQUEST_MEDIA_DATA_IN_PROGRESS,
mediaId: 'video-id-foo',
request,
})
)
).toEqual({
requestsMediaDataInProgress: {
'video-id-foo': true,
},
requestsMediaData: {
'video-id-foo': 'PROMISE',
},
});
});

it('should support setting error in loading video data', () => {
expect(
convertValue(
reducer({} as MediaPlayerAPIState, {
type: MEDIA_PLAYER_API_ACTION.SET_ERROR_REQUEST_MEDIA_DATA,
mediaId: 'video-id-foo',
error: new Error('error-getvideodata'),
})
)
).toEqual({
requestsMediaDataInProgress: {
'video-id-foo': false,
},
errorsRequestMediaData: {
'video-id-foo': 'error-getvideodata',
},
});
});

it('should support setting loaded video data', () => {
expect(
convertValue(
reducer({} as MediaPlayerAPIState, {
type: MEDIA_PLAYER_API_ACTION.SET_MEDIA_DATA,
mediaId: 'video-id-foo',
mediaData: mockMediaData as MediaData,
})
)
).toEqual({
requestsMediaDataInProgress: {
'video-id-foo': false,
},
requestsMediaData: {
'video-id-foo': 'PROMISE',
},
mediaData: {
'video-id-foo': mockMediaData,
},
});
});
});
85 changes: 85 additions & 0 deletions packages/services-store/src/reducers/kalturaPlayerAPIv7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* @license
*
* Copyright IBM Corp. 2020, 2022
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import {
MEDIA_PLAYER_API_ACTION,
MediaPlayerAPIState,
} from '../types/kalturaPlayerAPIv7';
import {
MediaPlayerAPIActions,
setRequestMediaDataInProgress,
setErrorRequestMediaData,
setMediaData,
} from '../actions/kalturaPlayerAPIv7';

/**
* @param state The state for media player API.
* @param action The action.
* @returns The new state for media player API.
*/
export default function reducer(
state: MediaPlayerAPIState = {},
action: MediaPlayerAPIActions
): MediaPlayerAPIState {
switch (action.type) {
case MEDIA_PLAYER_API_ACTION.SET_REQUEST_MEDIA_DATA_IN_PROGRESS: {
const { mediaId, request } = action as ReturnType<
typeof setRequestMediaDataInProgress
>;
return {
...state,
requestsMediaDataInProgress: {
...(state.requestsMediaDataInProgress || {}),
[mediaId]: true,
},
requestsMediaData: {
...(state.requestsMediaData || {}),
[mediaId]: request,
},
};
}
case MEDIA_PLAYER_API_ACTION.SET_ERROR_REQUEST_MEDIA_DATA: {
const { mediaId, error } = action as ReturnType<
typeof setErrorRequestMediaData
>;
return {
...state,
requestsMediaDataInProgress: {
...(state.requestsMediaDataInProgress || {}),
[mediaId]: false,
},
errorsRequestMediaData: {
...(state.errorsRequestMediaData || {}),
[mediaId]: error,
},
};
}
case MEDIA_PLAYER_API_ACTION.SET_MEDIA_DATA: {
const { mediaId, mediaData } = action as ReturnType<typeof setMediaData>;
return {
...state,
// If application sets language data without making a REST call, mark the request as resolved already
requestsMediaDataInProgress: {
...(state.requestsMediaDataInProgress || {}),
[mediaId]: false,
},
requestsMediaData: {
...(state.requestsMediaData || {}),
[mediaId]: Promise.resolve(mediaData),
},
mediaData: {
...(state.mediaData || {}),
[mediaId]: mediaData,
},
};
}
default:
return state;
}
}
78 changes: 78 additions & 0 deletions packages/services-store/src/types/kalturaPlayerAPIv7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @license
*
* Copyright IBM Corp. 2020, 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* The media data for ibm.com sites
*/
export interface MediaData {
/**
* The media id.
*/
id: string;

/**
* The media description.
*/
description: string;

/**
* The media duration.
*/
duration: number;

/**
* The media name.
*/
name: string;
}

/**
* The Redux action ID for `MediaPlayerAPI`.
*/
export enum MEDIA_PLAYER_API_ACTION {
/**
* One to set the state that the REST call for media data that is in progress.
*/
SET_REQUEST_MEDIA_DATA_IN_PROGRESS = 'SET_REQUEST_MEDIA_DATA_IN_PROGRESS',

/**
* One to set the state that the REST call for media data failed.
*/
SET_ERROR_REQUEST_MEDIA_DATA = 'SET_ERROR_REQUEST_MEDIA_DATA',

/**
* One to set the given media data.
*/
SET_MEDIA_DATA = 'SET_MEDIA_DATA',
}

/**
* A Redux substate for `MediaPlayerAPI`.
*/
export interface MediaPlayerAPIState {
/**
* The media data, keyed by the media ID.
*/
mediaData?: { [mediaId: string]: MediaData };

/**
* The requests for the media data, keyed by the media ID.
*/
requestsMediaData?: { [mediaId: string]: Promise<MediaData> };

/**
* The status of whether requests for the media data are in progress, keyed by the media ID.
*/
requestsMediaDataInProgress?: { [mediaId: string]: boolean };

/**
* The errors from the requests for the media data, keyed by the media ID.
*/
errorsRequestMediaData?: { [mediaId: string]: Error };
}
Loading
Loading