silverstripe-cms/client/src/state/anchorSelector/AnchorSelectorReducer.js
Dylan 8ba444332a FIX pull up anchors that have been added for linking to
When editing a page an author may insert anchors into their content (via
TinyMCE), and wish to link to those anchors from another point in that
page. E.g. creating a table of contents, a "jump to top" link at the
base of the content, etc.

Previously this required the page to first be saved as a draft, then
reloaded before the anchors would show in the list to link to. Now they
are dynamically pulled from the content entered for a more fluid editing
experience.
2019-08-15 19:26:37 +12:00

53 lines
1.4 KiB
JavaScript

import deepFreeze from 'deep-freeze-strict';
import ACTION_TYPES from './AnchorSelectorActionTypes';
import anchorSelectorStates from './AnchorSelectorStates';
/**
* Default state
*/
const initialState = deepFreeze({ pages: [] });
export default function anchorSelectorReducer(state = initialState, action = null) {
/**
* Update details for the page
*
* @param {String} loadingState - State flag for this page
* @param {Array} anchors - Anchors for this page
* @return {Object} Updated state
*/
const updatePage = (loadingState, anchors) => {
const id = action.payload.pageId;
return deepFreeze({
pages: [
...state.pages.filter(next => next.id !== id),
{
id,
loadingState,
anchors,
},
].sort((left, right) => (left.id - right.id)),
});
};
// Update page status
switch (action.type) {
case ACTION_TYPES.ANCHORSELECTOR_UPDATING: {
return updatePage(anchorSelectorStates.UPDATING, []);
}
case ACTION_TYPES.ANCHORSELECTOR_UPDATED: {
const { anchors, cacheResult } = action.payload;
const { SUCCESS, DIRTY } = anchorSelectorStates;
const newSelectorLoadingState = cacheResult ? SUCCESS : DIRTY;
return updatePage(newSelectorLoadingState, anchors);
}
case ACTION_TYPES.ANCHORSELECTOR_UPDATE_FAILED: {
return updatePage(anchorSelectorStates.FAILED, []);
}
default:
return state;
}
}