import React from 'react';
import PropTypes from 'prop-types';
import { Waypoint } from 'react-waypoint';
import { createSubscribedElement } from '../UnstatedUtils';
import GrowiRenderer from '../../util/GrowiRenderer';
import AppContainer from '../../services/AppContainer';
import RevisionRenderer from './RevisionRenderer';
/**
* Load data from server and render RevisionBody component
*/
class RevisionLoader extends React.Component {
constructor(props) {
super(props);
this.logger = require('@alias/logger')('growi:Page:RevisionLoader');
this.state = {
markdown: '',
isLoading: false,
isLoaded: false,
error: null,
};
this.loadData = this.loadData.bind(this);
this.onWaypointChange = this.onWaypointChange.bind(this);
}
componentWillMount() {
if (!this.props.lazy) {
this.loadData();
}
}
async loadData() {
if (!this.state.isLoaded && !this.state.isLoading) {
this.setState({ isLoading: true });
}
const requestData = {
page_id: this.props.pageId,
revision_id: this.props.revisionId,
};
// load data with REST API
try {
const res = await this.props.appContainer.apiGet('/revisions.get', requestData);
this.setState({
markdown: res.revision.body,
error: null,
});
if (this.props.onRevisionLoaded != null) {
this.props.onRevisionLoaded(res.revision);
}
}
catch (error) {
this.setState({ error });
}
finally {
this.setState({ isLoaded: true, isLoading: false });
}
}
onWaypointChange(event) {
if (event.currentPosition === Waypoint.above || event.currentPosition === Waypoint.inside) {
this.loadData();
}
}
render() {
// ----- before load -----
if (this.props.lazy && !this.state.isLoaded) {
return (