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 28, 2024
1 parent cfb57e7 commit fa62085
Show file tree
Hide file tree
Showing 13 changed files with 992 additions and 809 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

78 changes: 48 additions & 30 deletions client/src/components/HistoryViewer/HistoryViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 Loading from 'components/Loading/Loading';
import {
setCurrentPage,
Expand All @@ -20,6 +21,8 @@ import ResizeAware from 'components/ResizeAware/ResizeAware';
import * as viewModeActions from 'state/viewMode/ViewModeActions';
import PropTypes from 'prop-types';

const useRPC = true;

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

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

componentDidMount() {
this.refreshVersions();
}

/**
Expand All @@ -41,16 +54,12 @@ 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.refreshVersions();
// versions.goToPage(currentPage);
}
}

Expand All @@ -65,16 +74,26 @@ class HistoryViewer extends Component {
}
}

/**
* Returns the result of the GraphQL version history query
*
* @returns {Array}
*/
getVersions() {
const { versions } = this.props;
return (versions && versions.versions && versions.versions.nodes)
? versions.versions.nodes
: [];
refreshVersions() {
if (useRPC) {
// TODO: hardcoded, use config of some sort
const recordClass = this.props.recordClass;
const url = `/admin/pages/history/api/read/${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,
});
});
} else {
const { versions } = this.props;
const nodes = (versions && versions.versions && versions.versions.nodes)
? versions.versions.nodes
: [];
this.setState({ versions: nodes });
}
}

/**
Expand Down Expand Up @@ -111,7 +130,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 @@ -190,7 +209,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 +260,8 @@ class HistoryViewer extends Component {
compare,
compareModeAvailable: this.compareModeAvailable(),
previewState,
recordClass: this.props.recordClass,
onAfterRevert: () => this.refreshVersions()
};

return (
Expand All @@ -263,23 +284,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 +363,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
19 changes: 15 additions & 4 deletions client/src/components/HistoryViewer/HistoryViewerToolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { compose } from 'redux';
import { connect } from 'react-redux';
import i18n from 'i18n';
import { inject } from 'lib/Injector';
import backend from 'lib/Backend';
import { addMessage, showList } from 'state/historyviewer/HistoryViewerActions';

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

this.setState({ isReverting: true });

const handler = typeof onAfterRevert === 'function' ? onAfterRevert : () => {};
return revertToVersion(recordId, versionId, 'DRAFT', 'DRAFT').then(() => handler(versionId));
// todo hardecoded url - use config passed in via prop (like schemaUrl in HistoryViewerVersionDetail)
const url = '/admin/pages/history/api/revert';
backend.post(url, {
id: recordId,
toVersion: versionId,
recordClass,
})
.then(() => {
this.props.showToolbarSuccessMessage(versionId);
onAfterRevert();
});
}

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

HistoryViewerToolbar.defaultProps = {
Expand All @@ -93,7 +104,7 @@ HistoryViewerToolbar.defaultProps = {

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,13 +230,15 @@ HistoryViewerVersionDetail.propTypes = {
PropTypes.bool,
]),
previewState: PropTypes.oneOf(['edit', 'preview', 'split']),
onAfterRevert: PropTypes.func.isRequired,
};

HistoryViewerVersionDetail.defaultProps = {
isLatestVersion: false,
isPreviewable: false,
isRevertable: false,
compare: false,
onAfterRevert: () => {},
};

export { HistoryViewerVersionDetail as Component };
Expand Down
Loading

0 comments on commit fa62085

Please sign in to comment.