Commit 50d6e34e authored by Florent Chehab's avatar Florent Chehab

Fixed todos and cleaning

parent 16553345
......@@ -12,7 +12,7 @@ import Badge from "@material-ui/core/Badge";
export default function MyBadge(props) {
if (props.badgeContent) {
return (
<Badge {...props} >
<Badge badgeContent={props.badgeContent} color={props.color} max={5} >
{props.children}
</Badge>
);
......
......@@ -182,7 +182,6 @@ class Editor extends Component {
this.props.openFullScreenDialog(this.renderEditor());
}
// TODO is this needed with redesign
// To handle moderation, we use the editor in "hacky" manner
// with the `dataToSave` props, which triggers a save.
const { dataToSave } = this.props;
......
......@@ -81,16 +81,14 @@ class Form extends Component {
/**
* Function to build all the errors from the fields of the form.
*
* TODO change setup with new custom error
*
* @returns
* @memberof Form
*/
fieldsHaveError() {
const messages = this.getFields()
.map(({ field }) => field.getError().messages)
.filter(messages => messages.length > 0);
return new CustomError(messages);
return CustomError.superCombine(
this.getFields()
.map(({ field }) => field.getError())
);
}
/**
......@@ -116,7 +114,7 @@ class Form extends Component {
* @memberof Form
*/
hasFormLevelErrors() {
return CustomError([]); // default: no errors
return new CustomError([]); // default: no errors
}
......
......@@ -30,7 +30,7 @@ import getActions from "../../../api/getActions";
/**
* Component to display the previous versions of module
*
* TODO: use with CustomComponentForAPI
* TODO: check if we can use CustomComponentForAPI
*
* @class History
* @extends {Component}
......@@ -123,7 +123,7 @@ class History extends Component {
* @memberof History
*/
closeHistory() {
this.props.handleHistoryWasClosed();
this.props.closeHistoryPanel();
this.props.closeFullScreenDialog();
this.props.resetVersions();
}
......@@ -186,14 +186,14 @@ class History extends Component {
// className={classes.mobileStepper}
nextButton={
<Button color="secondary" size="small" onClick={() => this.handleGoBy(1)} disabled={activeStep >= maxSteps - 1}>
Version suivante
Version précédente
{theme.direction === "rtl" ? <KeyboardArrowLeft /> : <KeyboardArrowRight />}
</Button>
}
backButton={
<Button color="secondary" size="small" onClick={() => this.handleGoBy(-1)} disabled={activeStep === 0}>
{theme.direction === "rtl" ? <KeyboardArrowRight /> : <KeyboardArrowLeft />}
Version précédente
Version suivante
</Button>
}
/>
......@@ -216,7 +216,8 @@ class History extends Component {
*/
renderVersionInfo(rawModelData) {
let dateInfo = <em>(Information non connue.)</em>;
const { updated_on } = rawModelData;
const { updated_on } = rawModelData,
versions = this.getVersions();
if (updated_on) {
const data = dateTimeStrToStr(updated_on);
dateInfo = `${data.date} à ${data.time}`;
......@@ -224,12 +225,12 @@ class History extends Component {
return (
<div>
<Typography variant='caption' align='center'>Les versions successives d'un même utilisateur ne sont pas enregistrés (dans de tels cas, seul la dernière est conservée).</Typography>
<Typography variant='h6' align='center'>Version n°{this.state.versionInView + 1} du {dateInfo}</Typography>
<Typography variant='h6' align='center'>Version n°{versions.length - this.state.versionInView} du {dateInfo}</Typography>
<Button
variant='outlined'
color="primary"
className={this.props.classes.editButton}
onClick={() => this.props.handleRestoreVersion(rawModelData)}
onClick={() => this.props.handleEditFromVersion(rawModelData)}
>
Éditer à partir de cette version
</Button>
......@@ -246,9 +247,9 @@ class History extends Component {
History.propTypes = {
classes: PropTypes.object.isRequired,
handleRestoreVersion: PropTypes.func.isRequired,
handleEditFromVersion: PropTypes.func.isRequired,
readVersions: PropTypes.func.isRequired,
handleHistoryWasClosed: PropTypes.func.isRequired,
closeHistoryPanel: PropTypes.func.isRequired,
resetVersions: PropTypes.func.isRequired,
open: PropTypes.bool.isRequired,
versions: PropTypes.object.isRequired,
......@@ -262,7 +263,7 @@ History.propTypes = {
History.defaultProps = {
open: false,
// eslint-disable-next-line no-console
handleHistoryWasClosed: () => console.error("Dev forgot something..."),
closeHistoryPanel: () => console.error("Dev forgot something..."),
};
......
......@@ -40,7 +40,7 @@ class ModuleWrapper extends CustomComponentForAPI {
alert: { open: false }
};
handleOpenEditor = (force = false) => {
openEditorPanel = (force = false) => {
if (force || this.props.rawModelData.pending_moderation.length == 0) {
this.setState({ editorOpen: true });
} else {
......@@ -56,7 +56,7 @@ class ModuleWrapper extends CustomComponentForAPI {
if (agree) {
this.handleOpenPendingModeration();
} else {
this.handleOpenEditor(true);
this.openEditorPanel(true);
}
},
multilineButtons: true,
......@@ -76,7 +76,7 @@ class ModuleWrapper extends CustomComponentForAPI {
this.setState({ alert: { open: false } });
}
handleHistoryWasClosed = () => {
closeHistoryPanel = () => {
this.setState({ historyOpen: false });
};
......@@ -88,7 +88,7 @@ class ModuleWrapper extends CustomComponentForAPI {
this.setState({ pendingModerationOpen: true });
}
handleClosePendingModeration = () => {
closePendingModerationPanel = () => {
this.setState({ pendingModerationOpen: false });
}
......@@ -99,8 +99,8 @@ class ModuleWrapper extends CustomComponentForAPI {
rawModelDataForEditor: rawModelData,
forceSave: this.userCanModerate(),
});
this.handleClosePendingModeration();
this.handleOpenEditor(true);
this.closePendingModerationPanel();
this.openEditorPanel(true);
}
handleEditCurrent = () => {
......@@ -108,16 +108,18 @@ class ModuleWrapper extends CustomComponentForAPI {
rawModelDataForEditor: this.props.rawModelData,
forceSave: false,
});
this.handleOpenEditor();
this.openEditorPanel();
}
handleRestoreVersion = (rawModelData) => {
handleEditFromVersion = (rawModelData) => {
this.setState({
rawModelDataForEditor: rawModelData,
forceSave: true,
});
this.handleHistoryWasClosed();
this.handleOpenEditor();
this.closeHistoryPanel();
this.openEditorPanel();
}
handleApproveModeration = (modelDataInModeration) => {
......@@ -168,7 +170,7 @@ class ModuleWrapper extends CustomComponentForAPI {
modelData={this.props.parseRawModelData(this.state.rawModelDataForEditor)}
outsideData={this.props.outsideData}
userData={this.props.userData}
forceSave={this.state.forceSave}
forceSave={this.state.forceSave} // don't take into account that the form hasn't change, it's normal
dataToSave={this.state.dataToSave}
__apiAttr={this.props.__apiAttr}
/>
......@@ -176,13 +178,13 @@ class ModuleWrapper extends CustomComponentForAPI {
renderer={this}
modelInfo={{ contentTypeId: rawModelData.content_type_id, id: rawModelData.id }}
open={this.state.historyOpen}
handleHistoryWasClosed={this.handleHistoryWasClosed}
handleRestoreVersion={this.handleRestoreVersion}
closeHistoryPanel={this.closeHistoryPanel}
handleEditFromVersion={this.handleEditFromVersion}
/>
<PendingModeration
renderer={this}
open={this.state.pendingModerationOpen}
handleClosePendingModeration={this.handleClosePendingModeration}
closePendingModerationPanel={this.closePendingModerationPanel}
handleEditPendingModeration={this.handleEditPendingModeration}
handleApproveModeration={this.handleApproveModeration}
userCanModerate={userCanModerate}
......
......@@ -52,7 +52,6 @@ class ScholarshipForm extends Form {
label: "Fréquence de la bourse",
options: frequencyOptions,
fieldMapping: "frequency",
required: true, //TODO remove
})}
{this.renderMarkdownField({
label: "Autres avantages",
......
......@@ -63,15 +63,8 @@ class UsefulLinksField extends Field {
}
serializeFromField() {
// TODO CLEAN
const usefulLinks = this.state.value;
let emptyIndexes = Array();
usefulLinks.map((el, idx) => {
if (el.url == "" && el.description == "") {
emptyIndexes.push(idx);
}
});
return usefulLinks.filter((s, idx) => !(idx in emptyIndexes));
return usefulLinks.filter(el => !(el.url === "" && el.description === ""));
}
updateUsefulLinks(newUsefulLinks) {
......
......@@ -17,20 +17,19 @@ import Tooltip from "@material-ui/core/Tooltip";
export default function renderFirstRow(userCanModerate) {
const { classes, theme } = this.props;
const { rawModelData } = this.props;
const { classes, theme, rawModelData } = this.props,
nbVersions = Math.max(0, rawModelData.nb_versions - 1),
{ pending_moderation } = rawModelData;
const nbVersions = Math.max(0, rawModelData.nb_versions - 1);
const { pending_moderation } = rawModelData;
let nbPendingModeration = 0;
if (pending_moderation) {
nbPendingModeration = pending_moderation.length;
}
const readOnly = rawModelData.model_config.read_only;
const { versionTooltip, versionClass } = getVersionTooltipAndClass(nbVersions);
const { moderTooltip, moderClass } = getModerationTooltipAndClass(nbPendingModeration, readOnly);
const { editTooltip, editClass } = getEditTooltipAndClass(readOnly, userCanModerate);
const readOnly = rawModelData.model_config.read_only,
{ versionTooltip, versionClass } = getVersionTooltipAndClass(nbVersions),
{ moderTooltip, moderClass } = getModerationTooltipAndClass(nbPendingModeration, readOnly),
{ editTooltip, editClass } = getEditTooltipAndClass(readOnly, userCanModerate);
return (
<Grid container spacing={8}>
......@@ -42,7 +41,7 @@ export default function renderFirstRow(userCanModerate) {
<Tooltip title={moderTooltip} placement="top">
<div style={{ display: "inline-block" }}> {/* Needed to fire events for the tooltip when below is disabled! when below is disabled!! */}
<MyBadge classes={{ badge: classes.badge }} badgeContent={nbPendingModeration} color="secondary">
<MyBadge className={classes.badge} badgeContent={nbPendingModeration} color="secondary">
<IconButton aria-label="Modération" disabled={moderClass == "disabled" || moderClass == "green"} onClick={this.handleOpenPendingModeration} className={classes.button}>
<VerifiedUserIcon className={classes[moderClass]} />
</IconButton>
......@@ -60,7 +59,7 @@ export default function renderFirstRow(userCanModerate) {
<Tooltip title={versionTooltip} placement="top">
<div style={{ display: "inline-block" }}> {/* Needed to fire events for the tooltip when below is disabled!! */}
<MyBadge classes={{ badge: classes.badge }} badgeContent={nbVersions} color="secondary">
<MyBadge className={classes.badge} badgeContent={nbVersions} color="secondary">
<IconButton aria-label="Restorer" disabled={versionClass == "disabled"} className={classes.button} onClick={this.handleOpenHistory}>
<SettingsBackRestoreIcon className={classes[versionClass]} />
</IconButton>
......
......@@ -11,9 +11,6 @@ import NumberField from "./fields/NumberField";
import store from "../../../store/index";
import { getLatestRead } from "../../../api/utils";
import __map from "lodash/map";
export default {
/**
* For field mixins that handle custom props, we need to make use of getReferenceAndValue too
......@@ -110,7 +107,7 @@ export default {
renderUniversitiesField() {
const { outsideData } = this.props,
universities = __map(outsideData.universities, // TODO __map required ?
universities = outsideData.universities.map(
(univ) => { return { label: univ.name, value: univ.id, disabled: false }; }
);
......@@ -125,7 +122,7 @@ export default {
renderCountriesField() {
const { outsideData } = this.props,
countries = __map(outsideData.countries, // TODO __map required ?
countries = outsideData.countries.map(
(country) => { return { label: country.name, value: country.id, disabled: false }; }
);
......@@ -148,7 +145,7 @@ export default {
renderCurrencyField(props) {
const { outsideData } = this.props;
const currencies = __map(outsideData.currencies,
const currencies = outsideData.currencies.map(
(c) => { return { label: c.code, value: c.code, disabled: false }; }
);
......
......@@ -27,4 +27,19 @@ export default class CustomError {
combine(other) {
return new CustomError(Array.prototype.concat(this.messages, other.messages));
}
/**
* Combines an array of CustomError
*
* @static
* @param {Array[CustomError]} arrOfCustomErrors
* @returns
* @memberof CustomError
*/
static superCombine(arrOfCustomErrors) {
return new CustomError(
Array.prototype.concat(
arrOfCustomErrors
.flatMap(el => el.messages)));
}
}
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