Skip to content

Commit

Permalink
feat: api tweaks mostly with index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
rayzhou-bit committed Nov 15, 2023
1 parent e74c90f commit 3d8d5b0
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 110 deletions.
24 changes: 14 additions & 10 deletions src/editors/containers/LibraryContentEditor/LibrarySelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,30 @@ export const LibrarySelector = ({
// redux
libraries,
loadLibrary,
selectedLibrary,
onSelectLibrary,
selectedLibraryId,
settings,
unloadLibrary,
}) => {
const {
initializeLibrary,
onSelectedLibraryChange,
setSelectedLibrary,
title,
} = useLibrarySelectorHook({
libraries,
loadLibrary,
selectedLibrary,
selectedLibraryId,
settings,
studioEndpointUrl,
unloadLibrary,
});

if (!!selectedLibraryId) {
initializeLibrary(selectedLibraryId);
}

onSelectedLibraryChange(selectedLibraryId);

return (
<div className='mb-3'>
{libraries
Expand All @@ -43,11 +51,11 @@ export const LibrarySelector = ({
{title}
</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item onClick={() => onSelectLibrary({ selectedLibrary: null })}>
<Dropdown.Item onClick={() => setSelectedLibrary(null)}>
<FormattedMessage {...messages.librarySelectorDropdownDefault} />
</Dropdown.Item>
{libraries.map((library, index) => (
<Dropdown.Item onClick={() => onSelectLibrary({ selectedLibrary: index })}>
<Dropdown.Item onClick={() => setSelectedLibrary(index)}>
{library.display_name}
</Dropdown.Item>
))}
Expand All @@ -65,7 +73,6 @@ export const LibrarySelector = ({

LibrarySelector.defaultProps = {
libraries: [],
selectedLibrary: 0,
settings: {},
};

Expand All @@ -74,21 +81,18 @@ LibrarySelector.propTypes = {
// redux
libraries: PropTypes.array,
loadLibrary: PropTypes.func.isRequired,
selectedLibrary: PropTypes.number,
onSelectLibrary: PropTypes.func.isRequired,
settings: PropTypes.object,
unloadLibrary: PropTypes.func.isRequired,
};

export const mapStateToProps = (state) => ({
libraries: selectors.libraries(state),
selectedLibrary: selectors.selectedLibrary(state),
selectedLibraryId: selectors.selectedLibraryId(state),
settings: selectors.settings(state),
});

export const mapDispatchToProps = {
loadLibrary: actions.loadLibrary,
onSelectLibrary: actions.onSelectLibrary,
unloadLibrary: actions.unloadLibrary,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ describe('LibrarySelector',()=>{
studioEndpointUrl: 'eXaMplE.com',
libraries: mocklibraries,
loadLibrary: jest.fn(),
selectedLibrary: 0,
onSelectLibrary: jest.fn(),
settings: {
[mocklibraries[0].library_key]: {
value: 'SoMethIng'
Expand Down Expand Up @@ -68,35 +66,7 @@ describe('LibrarySelector',()=>{
const {container, queryByTestId} = renderComponent({
...props,
libraries: null,
selectedLibrary: null,
});
expect(queryByTestId('dropdown')).toBeFalsy();
});

it('sets the default option to be selected if selectedLibrary is null.',()=>{
const {container, queryByTestId, queryByText} = renderComponent({
...props,
selectedLibrary: null,
});
expect(queryByTestId('dropdown')).toBeTruthy();
expect(queryByText('Select a library')).toBeTruthy();
});
it('Clicking The default option calls onSelectLibrary with value null and calls unloadlibrary',()=>{
const {container, queryByTestId, queryByText} = renderComponent({
...props,
});
fireEvent.click(container.querySelector("#library-selector"));
fireEvent.click(queryByText('FormattedMessage'));
expect(props.onSelectLibrary).toHaveBeenCalledWith({selectedLibrary: null});
expect(props.unloadLibrary).toHaveBeenCalled();
});
it('Clicking any other option in dropdown calls onSelectLibrary with value equal to its index and calls loadlibrary',()=>{
const {container, queryByTestId, queryByText} = renderComponent({
...props,
});
fireEvent.click(container.querySelector("#library-selector"));
fireEvent.click(queryByText(mocklibraries[1].display_name));
expect(props.onSelectLibrary).toHaveBeenCalledWith({selectedLibrary: 1});
expect(props.loadLibrary).toHaveBeenCalled();
});
})
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ export const LibrarySettings = ({
onCountChange,
onModeChange,
onShowResetChange,
selectedLibrary,
selectedLibraryId,
settings,
}) => {
if (selectedLibrary === null) return <></>;
if (selectedLibraryId === null) return <></>;

return (
<div className='col'>
Expand Down Expand Up @@ -77,7 +76,6 @@ export const LibrarySettings = ({
};

export const mapStateToProps = (state) => ({
selectedLibrary: selectors.selectedLibrary(state),
selectedLibraryId: selectors.selectedLibraryId(state),
settings: selectors.settings(state),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('LibrarySettings Component', () => {
onCountChange: jest.fn(),
onModeChange: jest.fn(),
onShowResetChange: jest.fn(),
selectedLibrary: 'sOme vAluE',
selectedLibraryId: 0,
settings: { 0: { mode: 'random', count: 5, showReset: true } },
}
Expand Down
43 changes: 33 additions & 10 deletions src/editors/containers/LibraryContentEditor/data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { StrictDict } from '../../../utils';

const initialState = {
libraries: [],
selectedLibrary: null,
selectedLibraryId: null,
selectedLibraryVersion: null,
settings: {
Expand All @@ -31,18 +30,46 @@ const library = createSlice({
name: 'library',
initialState,
reducers: {
initialize: (state, { payload }) => ({
// initialize: (state, { payload }) => ({
// ...state,
// libraries: payload.libraries,
// selectedLibraryId: payload.selectedLibraryId,
// selectedLibraryVersion: payload.selectedLibraryVersion,
// settings: {
// ...state.settings,
// [payload.selectedLibraryId]: payload.settings,
// },
// blocksInSelectedLibrary: payload.blocksInSelectedLibrary,
// }),
initializeFromBlockValue: (state, { payload }) => ({
...state,
selectedLibraryId: payload.selectedLibraryId,
settings: payload.settings,
}),
loadLibraryList: (state, { payload }) => ({
...state,
libraries: payload.libraries,
selectedLibrary: payload.selectedLibrary,
}),
setLibraryId: (state, { payload }) => ({
...state,
selectedLibraryId: payload.selectedLibraryId,
selectedLibraryVersion: payload.selectedLibraryVersion,
}),
setLibraryVersion: (state, { payload }) => ({
...state,
selectedLibraryVersion: payload.version,
}),
setLibraryBlocks: (state, { payload }) => ({
...state,
blocksInSelectedLibrary: payload.blocks,
}),
setLibrarySettings: (state, { payload }) => ({
...state,
settings: {
...state.settings,
[payload.selectedLibraryId]: payload.settings,
[payload.id]: payload.settings,
},
blocksInSelectedLibrary: payload.blocksInSelectedLibrary,
}),

loadLibrary: (state, { payload }) => ({
...state,
selectedLibraryId: payload.id,
Expand All @@ -59,10 +86,6 @@ const library = createSlice({
selectedLibraryVersion: null,
blocksInSelectedLibrary: [],
}),
onSelectLibrary: (state, { payload }) => ({
...state,
selectedLibrary: payload.selectedLibrary,
}),
onModeChange: (state, { payload }) => ({
...state,
settings: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe('app reducer', () => {
const libId = 'anOther lIb Id';
const data = {
libraries: 'soMe LibS',
selectedLibrary: 'a lIb id',
selectedLibraryId: libId,
selectedLibraryVersion: 'a lib veRsioN (oFteN an Int)',
settings: {
Expand Down Expand Up @@ -67,13 +66,6 @@ describe('app reducer', () => {
blocksInSelectedLibrary: [],
});
});
it(' onSelectLibrary sets the selected library from data.selectedLibrary', () => {
const selectedlib = { selectedLibrary: 'sOmE LiB' };
expect(reducer(testingState, actions.onSelectLibrary(selectedlib))).toEqual({
...testingState,
selectedLibrary: selectedlib.selectedLibrary,
});
});
});
describe('Library Settings Updates', () => {
const testSettingsChanges = {
Expand Down
62 changes: 62 additions & 0 deletions src/editors/containers/LibraryContentEditor/data/requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { StrictDict } from '../../../utils';
import { RequestKeys } from '../../../data/constants/requests';
import { networkRequest } from '../../../data/redux/thunkActions/requests';

/* eslint-disable import/no-cycle */
import { actions, selectors } from '../../../data/redux';
import api from './api';

/**
* Tracked fetchContentStore api method.
* Tracked to the `fetchContentStore` request key.
* @param {[func]} onSuccess - onSuccess method ((response) => { ... })
* @param {[func]} onFailure - onFailure method ((error) => { ... })
*/
export const fetchContentStore = ({ ...rest }) => (dispatch, getState) => {
dispatch(networkRequest({
requestKey: RequestKeys.fetchContentStore,
promise: api.fetchContentStore({
studioEndpointUrl: selectors.app.studioEndpointUrl(getState()),
}),
...rest,
}));
};

/**
* Tracked fetchLibraryProperty api method.
* Tracked to the `fetchLibraryProperty` request key.
* @param {[func]} onSuccess - onSuccess method ((response) => { ... })
* @param {[func]} onFailure - onFailure method ((error) => { ... })
*/
export const fetchLibraryProperty = ({ libraryId, ...rest }) => (dispatch, getState) => {
dispatch(networkRequest({
requestKey: RequestKeys.fetchLibraryProperty,
promise: api.fetchLibraryProperty({
studioEndpointUrl: selectors.app.studioEndpointUrl(getState()),
libraryId,
}),
...rest,
}));
};

/**
* Tracked fetchLibraryContent api method.
* Tracked to the `fetchLibraryContent` request key.
* @param {[func]} onSuccess - onSuccess method ((response) => { ... })
* @param {[func]} onFailure - onFailure method ((error) => { ... })
*/
export const fetchLibraryContent = ({ libraryId, ...rest }) => (dispatch, getState) => {
dispatch(networkRequest({
requestKey: RequestKeys.fetchLibraryProperty,
promise: api.fetchLibraryProperty({
studioEndpointUrl: selectors.app.studioEndpointUrl(getState()),
libraryId,
}),
...rest,
}));
};

export default StrictDict({
fetchContentStore,
fetchLibraryProperty,
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const mkSimpleSelector = (cb) => createSelector([module.libraryState], cb);

export const simpleSelectors = {
libraries: mkSimpleSelector(library => library.libraries),
selectedLibrary: mkSimpleSelector(library => library.selectedLibrary),
selectedLibraryId: mkSimpleSelector(library => library.selectedLibraryId),
selectedLibraryVersion: mkSimpleSelector(library => library.selectedLibraryVersion),
settings: mkSimpleSelector(library => library.settings),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe('Library Selectors', () => {
describe('simple selectors link their values from app store', () => {
[
simpleKeys.libraries,
simpleKeys.selectedLibrary,
simpleKeys.selectedLibraryId,
simpleKeys.selectedLibraryVersion,
simpleKeys.settings,
Expand Down
Loading

0 comments on commit 3d8d5b0

Please sign in to comment.