Commit 9ea5c94b authored by Florent Chehab's avatar Florent Chehab

Added __apiParam to CustomComponentForApi and History use this class now

parent f744905f
Pipeline #36194 passed with stages
in 4 minutes and 55 seconds
...@@ -48,6 +48,10 @@ class CustomComponentForAPI extends Component { ...@@ -48,6 +48,10 @@ class CustomComponentForAPI extends Component {
// mapping should be : props_key => other_props_that contains the attribute to use // mapping should be : props_key => other_props_that contains the attribute to use
__apiAttr = null; __apiAttr = null;
// __apiParam should be an object
// mapping the prop that needs to be fetched to the parameter to be passed to the read function
__apiParam = null;
constructor(props) { constructor(props) {
super(props); super(props);
...@@ -175,6 +179,8 @@ class CustomComponentForAPI extends Component { ...@@ -175,6 +179,8 @@ class CustomComponentForAPI extends Component {
performReadFromApi(propName) { performReadFromApi(propName) {
if (this.__apiAttr && this.__apiAttr[propName]) { if (this.__apiAttr && this.__apiAttr[propName]) {
this.props.api[propName](this.props[this.__apiAttr[propName]]); this.props.api[propName](this.props[this.__apiAttr[propName]]);
} else if (this.__apiParam && this.__apiParam[propName]) {
this.props.api[propName](this.__apiParam[propName]);
} else { } else {
this.props.api[propName](); this.props.api[propName]();
} }
......
import React, { Component } from "react"; import React from "react";
import { openFullScreenDialog, closeFullScreenDialog } from "../../../../redux/actions/fullScreenDialog"; import { openFullScreenDialog, closeFullScreenDialog } from "../../../../redux/actions/fullScreenDialog";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
...@@ -22,6 +22,7 @@ import Loading from "../../../common/Loading"; ...@@ -22,6 +22,7 @@ import Loading from "../../../common/Loading";
import dateTimeStrToStr from "../../../../utils/dateTimeStrToStr"; import dateTimeStrToStr from "../../../../utils/dateTimeStrToStr";
import editorStyle from "../../editors/common/editorStyle"; import editorStyle from "../../editors/common/editorStyle";
import CustomComponentForAPI from "../../../common/CustomComponentForAPI";
import getActions from "../../../../redux/api/getActions"; import getActions from "../../../../redux/api/getActions";
...@@ -30,17 +31,25 @@ import getActions from "../../../../redux/api/getActions"; ...@@ -30,17 +31,25 @@ import getActions from "../../../../redux/api/getActions";
/** /**
* Component to display the previous versions of module * Component to display the previous versions of module
* *
* TODO: check if we can use CustomComponentForAPI
*
* @class History * @class History
* @extends {Component} * @extends {Component}
*/ */
class History extends Component { class History extends CustomComponentForAPI {
// Store the version that is being viewed // Store the version that is being viewed
state = { state = {
versionInView: 1 versionInView: 1
} }
constructor(props) {
super(props);
const { contentTypeId, id } = props.modelInfo;
// set the __apiAttr here so that we can read the correct data
this.__apiParam = {
versions: { contentTypeId, id }
};
}
/** /**
* Move to next or previous version with step * Move to next or previous version with step
* *
...@@ -58,45 +67,7 @@ class History extends Component { ...@@ -58,45 +67,7 @@ class History extends Component {
* @memberof History * @memberof History
*/ */
getVersions() { getVersions() {
if (!this.props.versions.readSucceeded.readAt) { return this.getLatestReadData("versions").map((el) => { return el.data; });
return [];
} else {
return this.props.versions.readSucceeded.data
.map((el) => { return el.data; }); // get the data inside the version
}
}
/**
* Get the information about the model from the props.
*
* @returns
* @memberof History
*/
getContentTypeAndId() {
const { contentTypeId, id } = this.props.modelInfo;
return { contentTypeId, id };
}
/**
* Checks whether the versioned data is ready or not.
*
* @returns
* @memberof History
*/
dataIsReady() {
if (this.props.versions.isInvalidated) {
return false;
}
const { contentTypeId, id } = this.getContentTypeAndId(),
versions = this.getVersions();
if (versions.length === 0) {
return false;
}
const lastVersion = versions[0];
return lastVersion.content_type_id == contentTypeId
&& lastVersion.id == id;
} }
/** /**
...@@ -106,6 +77,7 @@ class History extends Component { ...@@ -106,6 +77,7 @@ class History extends Component {
* @memberof Editor * @memberof Editor
*/ */
componentDidMount() { componentDidMount() {
super.componentDidMount();
this.forceUpdate(); this.forceUpdate();
} }
...@@ -114,12 +86,8 @@ class History extends Component { ...@@ -114,12 +86,8 @@ class History extends Component {
* *
* @memberof History * @memberof History
*/ */
componentDidUpdate() { componentDidUpdate(prevProps, prevState, snapshot) {
// Load the data as necessary super.componentDidUpdate(prevProps, prevState, snapshot);
if (!this.dataIsReady() && !this.props.versions.isLoading) {
const { contentTypeId, id } = this.getContentTypeAndId();
this.props.readVersions(contentTypeId, id);
}
this.props.openFullScreenDialog(this.renderPanel()); this.props.openFullScreenDialog(this.renderPanel());
} }
...@@ -141,6 +109,10 @@ class History extends Component { ...@@ -141,6 +109,10 @@ class History extends Component {
* @memberof History * @memberof History
*/ */
renderPanel() { renderPanel() {
if (!this.allApiDataIsReady()) {
return <Loading />;
}
const { classes } = this.props; const { classes } = this.props;
return ( return (
<> <>
...@@ -168,13 +140,6 @@ class History extends Component { ...@@ -168,13 +140,6 @@ class History extends Component {
* @memberof History * @memberof History
*/ */
renderHistory() { renderHistory() {
if (this.props.versions.readFailed.failed) {
return <p>Nous sommes désolé, une erreur est survenue lors du chargement de l'historique</p>;
}
if (!this.dataIsReady()) {
return <Loading />;
}
const { theme } = this.props, const { theme } = this.props,
versions = this.getVersions(), versions = this.getVersions(),
activeStep = this.state.versionInView, activeStep = this.state.versionInView,
...@@ -245,8 +210,17 @@ class History extends Component { ...@@ -245,8 +210,17 @@ class History extends Component {
); );
} }
// Dumb render function for the component TODO remove ? /**
* @override
*
* @returns
* @memberof History
*/
render() { render() {
if (!this.allApiDataIsReady()) {
return <></>;
}
return <></>; return <></>;
} }
} }
...@@ -254,7 +228,6 @@ class History extends Component { ...@@ -254,7 +228,6 @@ class History extends Component {
History.propTypes = { History.propTypes = {
classes: PropTypes.object.isRequired, classes: PropTypes.object.isRequired,
editFromVersion: PropTypes.func.isRequired, editFromVersion: PropTypes.func.isRequired,
readVersions: PropTypes.func.isRequired,
closeHistoryPanel: PropTypes.func.isRequired, closeHistoryPanel: PropTypes.func.isRequired,
resetVersions: PropTypes.func.isRequired, resetVersions: PropTypes.func.isRequired,
versions: PropTypes.object.isRequired, versions: PropTypes.object.isRequired,
...@@ -279,7 +252,9 @@ const mapStateToProps = (state) => { ...@@ -279,7 +252,9 @@ const mapStateToProps = (state) => {
const mapDispatchToProps = (dispatch) => { const mapDispatchToProps = (dispatch) => {
return { return {
readVersions: (contentTypeId, id) => dispatch(getActions("versions").readSpecific(`${contentTypeId}/${id}`)), api: {
versions: (param) => dispatch(getActions("versions").readSpecific(`${param.contentTypeId}/${param.id}`)),
},
resetVersions: () => dispatch(getActions("versions").setInvalidatedSpecific(true)), resetVersions: () => dispatch(getActions("versions").setInvalidatedSpecific(true)),
openFullScreenDialog: (innerNodes) => dispatch(openFullScreenDialog(innerNodes)), openFullScreenDialog: (innerNodes) => dispatch(openFullScreenDialog(innerNodes)),
closeFullScreenDialog: () => dispatch(closeFullScreenDialog()), closeFullScreenDialog: () => dispatch(closeFullScreenDialog()),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment