Skip to content

Commit

Permalink
API Standardise endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jul 29, 2024
1 parent cfb57e7 commit bfd1140
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 374 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

94 changes: 49 additions & 45 deletions client/src/components/HistoryViewer/HistoryViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Griddle from 'griddle-react';
import historyViewerConfig from 'containers/HistoryViewer/HistoryViewerConfig';
import i18n from 'i18n';
import { inject } from 'lib/Injector';
import backend from 'lib/Backend';
import Config from 'lib/Config';
import Loading from 'components/Loading/Loading';
import {
setCurrentPage,
Expand All @@ -22,7 +24,7 @@ import PropTypes from 'prop-types';

/**
* The HistoryViewer component is abstract, and requires an Injector component
* to be connected providing the GraphQL query implementation for the appropriate
* to be connected providing the query implementation for the appropriate
* DataObject type
*/
class HistoryViewer extends Component {
Expand All @@ -32,6 +34,17 @@ class HistoryViewer extends Component {
this.handleSetPage = this.handleSetPage.bind(this);
this.handleNextPage = this.handleNextPage.bind(this);
this.handlePrevPage = this.handlePrevPage.bind(this);
this.handleAfterRevert = this.handleAfterRevert.bind(this);
this.refreshVersionData = this.refreshVersionData.bind(this);

this.state = {
versions: [],
totalCount: 0,
};
}

componentDidMount() {
this.refreshVersionData();
}

/**
Expand All @@ -41,16 +54,11 @@ class HistoryViewer extends Component {
* @param {object} prevProps
*/
componentDidUpdate(prevProps) {
if (!this.props.actions || !this.props.actions.versions) {
if (this.state.versions.length === 0) {
return;
}

const { page: prevPage } = prevProps;
const { page: currentPage } = this.props;
const { actions: { versions } } = this.props;

if (prevPage !== currentPage && typeof versions.goToPage === 'function') {
versions.goToPage(currentPage);
if (prevProps.page !== this.props.page) {
this.refreshVersionData();
}
}

Expand All @@ -66,15 +74,21 @@ class HistoryViewer extends Component {
}

/**
* Returns the result of the GraphQL version history query
*
* @returns {Array}
* Refetch version data from the "read" endpoint
*/
getVersions() {
const { versions } = this.props;
return (versions && versions.versions && versions.versions.nodes)
? versions.versions.nodes
: [];
refreshVersionData() {
const sectionConfig = Config.getSection('SilverStripe\\VersionedAdmin\\Controllers\\HistoryViewerController');
const endpoint = sectionConfig.endpoints.read;
const recordClass = this.props.recordClass;
const url = `${endpoint}/${this.props.recordId}?recordClass=${recordClass}&page=${this.props.page}`;
backend.get(url)
.then(response => response.json())
.then(responseJson => {
this.setState({
versions: responseJson.versions,
totalCount: responseJson.pageInfo.totalCount,
});
});
}

/**
Expand Down Expand Up @@ -111,7 +125,7 @@ class HistoryViewer extends Component {
}

// Look for one in the list of available versions
const latestDraftVersion = this.getVersions()
const latestDraftVersion = this.state.versions
.filter(version => version.latestDraftVersion === true);

if (latestDraftVersion.length) {
Expand Down Expand Up @@ -170,6 +184,13 @@ class HistoryViewer extends Component {
this.handleSetPage(page);
}

/**
* Handler for after reverting
*/
handleAfterRevert() {
this.refreshVersionData();
}

/**
* Handler for decrementing the set page
*/
Expand All @@ -190,7 +211,7 @@ class HistoryViewer extends Component {
* @returns {boolean}
*/
compareModeAvailable() {
return this.getVersions().length > 1;
return this.state.versions.length > 1;
}

/**
Expand Down Expand Up @@ -241,6 +262,8 @@ class HistoryViewer extends Component {
compare,
compareModeAvailable: this.compareModeAvailable(),
previewState,
recordClass: this.props.recordClass,
onAfterRevert: this.handleAfterRevert
};

return (
Expand All @@ -263,23 +286,20 @@ class HistoryViewer extends Component {
* @returns {XML|null}
*/
renderPagination() {
const { limit, page, versions } = this.props;
const { limit, page } = this.props;
const { versions } = this.state;
const totalCount = this.state.totalCount;

if (!versions) {
if (versions.length === 0) {
return null;
}

const totalVersions = versions.versions
? versions.versions.pageInfo.totalCount
: 0;

if (totalVersions <= limit) {
if (totalCount <= limit) {
return null;
}

const props = {
setPage: this.handleSetPage,
maxPage: Math.ceil(totalVersions / limit),
maxPage: Math.ceil(totalCount / limit),
next: this.handleNextPage,
nextText: i18n._t('HistoryViewer.NEXT', 'Next'),
previous: this.handlePrevPage,
Expand Down Expand Up @@ -345,7 +365,7 @@ class HistoryViewer extends Component {
<div className={isInGridField ? '' : 'panel panel--padded panel--scrollable'}>
{this.renderComparisonSelectionList()}
<ListComponent
versions={this.getVersions()}
versions={this.state.versions}
showHeader={!compare || (compare && !hasVersionFrom)}
compareModeAvailable={this.compareModeAvailable()}
/>
Expand Down Expand Up @@ -403,14 +423,6 @@ HistoryViewer.propTypes = {
isRevertable: PropTypes.bool,
VersionDetailComponent: PropTypes.elementType.isRequired,
CompareWarningComponent: PropTypes.elementType.isRequired,
versions: PropTypes.shape({
versions: PropTypes.shape({
pageInfo: PropTypes.shape({
totalCount: PropTypes.number,
}),
nodes: PropTypes.arrayOf(versionType),
}),
}),
page: PropTypes.number,
schemaUrl: PropTypes.string,
previewState: PropTypes.oneOf(['edit', 'preview', 'split']),
Expand All @@ -427,14 +439,6 @@ HistoryViewer.defaultProps = {
isInGridField: false,
isPreviewable: false,
schemaUrl: '',
versions: {
versions: {
pageInfo: {
totalCount: 0,
},
nodes: [],
},
},
};

function mapStateToProps(state) {
Expand Down
27 changes: 20 additions & 7 deletions client/src/components/HistoryViewer/HistoryViewerToolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { compose } from 'redux';
import { connect } from 'react-redux';
import i18n from 'i18n';
import { inject } from 'lib/Injector';
import backend from 'lib/Backend';
import Config from 'lib/Config';
import { addMessage, showList } from 'state/historyviewer/HistoryViewerActions';

class HistoryViewerToolbar extends Component {
Expand All @@ -22,12 +24,21 @@ class HistoryViewerToolbar extends Component {
* @returns Promise
*/
handleRevert() {
const { actions: { revertToVersion }, onAfterRevert, recordId, versionId } = this.props;

this.setState({ isReverting: true });

const handler = typeof onAfterRevert === 'function' ? onAfterRevert : () => {};
return revertToVersion(recordId, versionId, 'DRAFT', 'DRAFT').then(() => handler(versionId));
const { onAfterRevert, recordId, versionId, recordClass } = this.props;
const sectionConfig = Config.getSection('SilverStripe\\VersionedAdmin\\Controllers\\HistoryViewerController');
const url = sectionConfig.endpoints.revert;
this.setState({
isReverting: true,
});
backend.post(url, {
id: recordId,
toVersion: versionId,
recordClass,
})
.then(() => {
this.props.showToolbarSuccessMessage(versionId);
onAfterRevert();
});
}

render() {
Expand Down Expand Up @@ -83,17 +94,19 @@ HistoryViewerToolbar.propTypes = {
onAfterRevert: PropTypes.func,
recordId: PropTypes.number.isRequired,
versionId: PropTypes.number.isRequired,
recordClass: PropTypes.string.isRequired,
};

HistoryViewerToolbar.defaultProps = {
isLatestVersion: false,
isPreviewable: false,
isRevertable: false,
showToolbarSuccessMessage: () => {},
};

function mapDispatchToProps(dispatch) {
return {
onAfterRevert(versionId) {
showToolbarSuccessMessage(versionId) {
dispatch(addMessage(
i18n.sprintf(
i18n._t('HistoryViewerToolbar.REVERTED_MESSAGE', 'Successfully reverted to version %s'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ class HistoryViewerVersionDetail extends PureComponent {
isLatestVersion={isLatestVersion}
recordId={recordId}
versionId={version.version}
recordClass={this.props.recordClass}
isPreviewable={this.isPreviewable()}
isRevertable={isRevertable}
onAfterRevert={this.props.onAfterRevert}
/>
);
}
Expand Down Expand Up @@ -216,6 +218,7 @@ HistoryViewerVersionDetail.propTypes = {
ListComponent: PropTypes.elementType.isRequired,
PreviewComponent: PropTypes.elementType,
recordId: PropTypes.number.isRequired,
recordClass: PropTypes.string.isRequired,
schemaUrl: PropTypes.string.isRequired,
ToolbarComponent: PropTypes.elementType.isRequired,
version: versionType,
Expand All @@ -227,6 +230,7 @@ HistoryViewerVersionDetail.propTypes = {
PropTypes.bool,
]),
previewState: PropTypes.oneOf(['edit', 'preview', 'split']),
onAfterRevert: PropTypes.func.isRequired,
};

HistoryViewerVersionDetail.defaultProps = {
Expand Down
Loading

0 comments on commit bfd1140

Please sign in to comment.