Commit 137f3e7a authored by Florent Chehab's avatar Florent Chehab

Infer parseRawModelData instead of writting it.

And bug fixes
parent 6cd8ddc9
......@@ -85,12 +85,12 @@ export default class SmartActions {
* @param {string} apiEndPoint
* @param {function} isSavingSpecific
* @param {function} saveSpecificSucceeded
* @param {function} failedSpecific
* @param {function} saveSpecificFailed
* @param {function} [onSuccessCallback = (newData) => {}] CallBack called if the update was successful besides calling the saveSpecificSucceeded action
* @returns {function} Function that takes the `dispatch` function of redux as argument.
* @memberof SmartActions
*/
_ElSaveData(data, apiEndPoint, isSavingSpecific, saveSpecificSucceeded, failedSpecific, onSuccessCallback = () => { }) {
_ElSaveData(data, apiEndPoint, isSavingSpecific, saveSpecificSucceeded, saveSpecificFailed, onSuccessCallback = () => { }) {
return (dispatch) => {
let method = "POST";
let pk = "";
......@@ -138,8 +138,8 @@ export default class SmartActions {
}
})
.then((errorContent) => {
if (typeof errorContent != "undefined") {
dispatch(failedSpecific(true, { message: errorStatusText, content: errorContent }));
if (typeof errorContent !== "undefined") {
dispatch(saveSpecificFailed(true, { message: errorStatusText, content: errorContent }));
dispatch(isSavingSpecific(false));
}
});
......
......@@ -36,7 +36,7 @@ class CountryDriForm extends Form {
class CountryDriEditor extends Editor {
renderForm() {
return <CountryDriForm
modelData={this.props.modelData}
modelData={this.props.rawModelData}
outsideData={this.props.outsideData}
ref={this.formRef}
/>;
......@@ -44,7 +44,7 @@ class CountryDriEditor extends Editor {
}
CountryDriEditor.propTypes = {
modelData: PropTypes.object.isRequired,
outsideData: PropTypes.object.isRequired,
};
......
......@@ -29,7 +29,7 @@ class CountryScholarshipEditor extends Editor {
renderForm() {
return (
<CountryScholarshipForm
modelData={this.props.modelData}
modelData={this.props.rawModelData}
outsideData={this.props.outsideData}
ref={this.formRef}
/>
......@@ -37,10 +37,7 @@ class CountryScholarshipEditor extends Editor {
}
}
CountryScholarshipEditor.propTypes = {
modelData: PropTypes.object.isRequired,
outsideData: PropTypes.object.isRequired,
};
......
......@@ -34,7 +34,7 @@ class UniversityDriEditor extends Editor {
renderForm() {
return <UniversityDriForm
modelData={this.props.modelData}
modelData={this.props.rawModelData}
outsideData={this.props.outsideData}
ref={this.formRef}
/>;
......@@ -42,7 +42,6 @@ class UniversityDriEditor extends Editor {
}
UniversityDriEditor.propTypes = {
modelData: PropTypes.object.isRequired,
outsideData: PropTypes.object.isRequired,
};
......
......@@ -51,7 +51,7 @@ class UniversityGeneralForm extends Form {
class UniversityGeneralEditor extends Editor {
renderForm() {
return <UniversityGeneralForm
modelData={this.props.modelData}
modelData={this.props.rawModelData}
ref={this.formRef}
/>;
}
......
......@@ -25,10 +25,12 @@ class UniversityScholarshipForm extends ScholarshipForm {
}
class UniversityScholarshipEditor extends Editor {
extraFieldMappings = ["university"];
renderForm() {
return (
<UniversityScholarshipForm
modelData={this.props.modelData}
modelData={this.props.rawModelData}
outsideData={this.props.outsideData}
ref={this.formRef}
/>
......@@ -37,7 +39,6 @@ class UniversityScholarshipEditor extends Editor {
}
UniversityScholarshipEditor.propTypes = {
modelData: PropTypes.object.isRequired,
outsideData: PropTypes.object.isRequired,
};
......
......@@ -77,20 +77,18 @@ function onlyOneIsNull(a, b) {
}
class UniversitySemestersDatesEditor extends Editor {
extraFieldMappings = ["university"];
renderForm() {
return (
<UniversitySemestersDatesForm
modelData={this.props.modelData}
modelData={this.props.rawModelData}
ref={this.formRef}
/>
);
}
}
UniversitySemestersDatesEditor.propTypes = {
modelData: PropTypes.object.isRequired,
};
export default compose(
withSnackbar,
......
......@@ -28,24 +28,6 @@ function renderCore(rawModelData, classes, outsideData) {
);
}
function parseRawModelData(rawModelData) {
// reverse serialization
const modelData = __pick(rawModelData,
[
"id",
"title",
"importance_level",
"countries",
"comment",
"useful_links",
// "obj_moderation_level",
]);
return modelData;
}
class CountryDri extends Module {
customRender() {
const countryDriItems = this.getReadData("countryDri");
......@@ -62,8 +44,7 @@ class CountryDri extends Module {
editor={CountryDriEditor}
invalidateGroup={this.props.invalidateData}
propsForEditor={{
modelData: { countries: [this.props.countryId], importance_level: "-" },
parseRawModelData: parseRawModelData,
rawModelData: { countries: [this.props.countryId], importance_level: "-" },
outsideData: outsideData,
__apiAttr: this.props.countryId,
}}
......@@ -74,7 +55,6 @@ class CountryDri extends Module {
key={idx}
buildTitle={(modelData) => modelData.title}
rawModelData={rawModelData}
parseRawModelData={parseRawModelData}
editor={CountryDriEditor}
renderCore={renderCore}
coreClasses={classes}
......
......@@ -41,29 +41,6 @@ function renderCore(rawModelData, classes, outsideData) {
}
function parseRawModelData(rawModelData) {
// reverse serialization
const modelData = __pick(rawModelData,
[
"id",
"title",
"type",
"currency",
"frequency",
"amount_min",
"amount_max",
// "importance_level",
"countries",
"comment",
"other_advantages",
"useful_links",
"obj_moderation_level",
]);
return modelData;
}
class CountryScholarships extends Module {
customRender() {
const countryScholarshipsItems = this.getReadData("countryScholarships");
......@@ -81,8 +58,7 @@ class CountryScholarships extends Module {
editor={CountryScholarshipEditor}
invalidateGroup={this.props.invalidateData}
propsForEditor={{
modelData: { countries: [this.props.countryId], importance_level: "-", currency: "EUR", obj_moderation_level: 0 },
parseRawModelData: parseRawModelData,
rawModelData: { countries: [this.props.countryId], importance_level: "-", currency: "EUR", obj_moderation_level: 0 },
outsideData: outsideData,
__apiAttr: this.props.countryId,
}}
......@@ -93,7 +69,6 @@ class CountryScholarships extends Module {
key={idx}
buildTitle={(modelData) => modelData.title}
rawModelData={rawModelData}
parseRawModelData={parseRawModelData}
editor={CountryScholarshipEditor}
renderCore={renderCore}
coreClasses={classes}
......
......@@ -31,24 +31,6 @@ function renderCore(rawModelData, classes, outsideData) {
}
function parseRawModelData(rawModelData) {
// reverse serialization
const univInfos = rawModelData;
const modelData = __pick(univInfos,
[
"id",
"title",
"importance_level",
"universities",
"comment",
"useful_links",
// "obj_moderation_level",
]);
return modelData;
}
class UniversityDri extends Module {
customRender() {
......@@ -64,8 +46,7 @@ class UniversityDri extends Module {
editor={UniversityDriEditor}
invalidateGroup={this.props.invalidateData}
propsForEditor={{
modelData: { universities: [this.props.univId], importance_level: "-" },
parseRawModelData: parseRawModelData,
rawModelData: { universities: [this.props.univId], importance_level: "-" },
outsideData: outsideData,
__apiAttr: this.props.univId,
}}
......@@ -76,7 +57,6 @@ class UniversityDri extends Module {
key={rawModelData.id} // use the id of the model to prevent useless unmount
buildTitle={(modelData) => modelData.title}
rawModelData={rawModelData}
parseRawModelData={parseRawModelData}
editor={UniversityDriEditor}
renderCore={renderCore}
coreClasses={classes}
......
......@@ -57,26 +57,6 @@ function renderCore(rawModelData, classes, outsideData) {
}
function parseRawModelData(rawModelData) {
// reverse serialization
const univInfos = rawModelData;
const modelData = __pick(univInfos,
[
"name",
"acronym",
"logo",
"website",
// "useful_links",
// "comment",
"university",
// "obj_moderation_level",
"id"
]);
return modelData;
}
class UniversityGeneral extends Module {
customRender() {
const univInfos = this.getReadData("university");
......@@ -90,7 +70,6 @@ class UniversityGeneral extends Module {
<ModuleWrapper
buildTitle={() => "Présentation"}
rawModelData={univInfos}
parseRawModelData={parseRawModelData}
editor={UniversityGeneralEditor}
renderCore={renderCore}
coreClasses={classes}
......
......@@ -19,8 +19,8 @@ const styles = theme => ({
});
function renderCore(rawModelData, classes, outsideData) {
const { comment, frequency, currency, type } = rawModelData;
const amountMin = rawModelData.amount_min,
const { comment, frequency, currency, type } = rawModelData,
amountMin = rawModelData.amount_min,
amountMax = rawModelData.amount_max,
otherAdvantages = rawModelData.other_advantages;
......@@ -38,30 +38,6 @@ function renderCore(rawModelData, classes, outsideData) {
);
}
function parseRawModelData(rawModelData) {
// reverse serialization
const modelData = __pick(rawModelData,
[
"id",
"title",
"type",
"currency",
"frequency",
"amount_min",
"amount_max",
// "importance_level",
"universities",
"comment",
"other_advantages",
"useful_links",
"obj_moderation_level",
]);
return modelData;
}
class UniversityScholarships extends Module {
customRender() {
const univScholarshipsItems = this.getReadData("universityScholarships");
......@@ -79,8 +55,7 @@ class UniversityScholarships extends Module {
editor={UniversityScholarshipEditor}
invalidateGroup={this.props.invalidateData}
propsForEditor={{
modelData: { universities: [this.props.univId], importance_level: "-", currency: "EUR", obj_moderation_level: 0 },
parseRawModelData: parseRawModelData,
rawModelData: { universities: [this.props.univId], importance_level: "-", currency: "EUR", obj_moderation_level: 0 },
outsideData: outsideData,
__apiAttr: this.props.univId,
}}
......@@ -91,7 +66,6 @@ class UniversityScholarships extends Module {
key={idx}
buildTitle={(modelData) => modelData.title}
rawModelData={rawModelData}
parseRawModelData={parseRawModelData}
editor={UniversityScholarshipEditor}
renderCore={renderCore}
coreClasses={classes}
......
......@@ -103,26 +103,6 @@ function renderCore(rawModelData, classes) {
}
function parseRawModelData(rawModelData) {
// reverse serialization
const semestersDates = rawModelData;
const modelData = __pick(semestersDates,
[
"autumn_begin",
"autumn_end",
"spring_begin",
"spring_end",
"useful_links",
"comment",
"university",
"obj_moderation_level",
"id"
]);
return modelData;
}
class UniversitySemestersDates extends Module {
customRender() {
......@@ -132,7 +112,6 @@ class UniversitySemestersDates extends Module {
<ModuleWrapper
buildTitle={() => "Date des semestres"}
rawModelData={semestersDates}
parseRawModelData={parseRawModelData}
editor={UniversitySemestersDatesEditor}
renderCore={renderCore}
coreClasses={classes}
......
......@@ -35,7 +35,7 @@ class Alert extends React.Component {
{this.props.infoText}
</Button>
:
<>
<div>
<Button
onClick={() => { this.props.handleClose(); this.props.handleResponse(false); }}
color='secondary'
......@@ -52,7 +52,7 @@ class Alert extends React.Component {
>
{this.props.agreeText}
</Button>
</>
</div>
}
</DialogActions>
</>
......
......@@ -20,7 +20,7 @@ import Form from "./Form";
* class BlablaEditor extends Editor {
* renderForm() {
* return <blablaForm
* modelData={this.props.modelData}
* modelData={this.props.rawModelData}
* ref={this.formRef}
* />;
* }
......@@ -45,6 +45,41 @@ class Editor extends Component {
// reference to the form that the editor should contain
formRef = React.createRef();
/**
* Extra attributes that should be retrieved in rawModelData when parsing
* I.e. the fields that are not present in the form but still need to be there
*
* @memberof Editor
*/
extraFieldMappings = []
/**
* Function that extracts the modelData from the raw one.
*
* Basically we extract all field that can be edited in the form associated with the editor.
* Plut the id.
*
* @returns
* @memberof Editor
*/
parseRawModelData() {
let out = {};
this.getForm()
.getFields()
.forEach(({ fieldMapping }) => out[fieldMapping] = this.props.rawModelData[fieldMapping]);
this.extraFieldMappings.
forEach(fieldMapping => out[fieldMapping] = this.props.rawModelData[fieldMapping]);
if ("id" in this.props.rawModelData) {
const { id } = this.props.rawModelData;
out.id = id;
}
return out;
}
/**
* Returns the form instance associated with the editor
*
......@@ -85,7 +120,7 @@ class Editor extends Component {
// Copy the model data and copy above the data from the form
// So that we don't forget anything.
this.performSave(
Object.assign({}, this.props.modelData, this.getFormData())
Object.assign({}, this.parseRawModelData(this.props.rawModelData), this.getFormData())
);
} else {
this.notifyNoChangesDetected();
......@@ -195,7 +230,7 @@ class Editor extends Component {
// we make to notify if saving to the server has errors
const { savingHasError } = this.props;
if (savingHasError.failed && !this.state.alert.open) {
this.alertSaveFailed(savingHasError.error.content.toString());
this.alertSaveFailed(JSON.stringify(savingHasError.error, null, 2));
}
}
......@@ -335,7 +370,7 @@ Editor.propTypes = {
lastSaveTime: PropTypes.number,
forceSave: PropTypes.bool.isRequired,
__apiAttr: PropTypes.oneOf([PropTypes.number, PropTypes.string, ""]),
modelData: PropTypes.object.isRequired,
rawModelData: PropTypes.object.isRequired,
open: PropTypes.bool.isRequired, // should the editor be opened
handleEditorWasClosed: PropTypes.func.isRequired,
dataToSave: PropTypes.object,
......
......@@ -165,7 +165,7 @@ class ModuleWrapper extends CustomComponentForAPI {
<this.props.editor
open={this.state.editorOpen}
handleEditorWasClosed={this.handleEditorWasClosed}
modelData={this.props.parseRawModelData(this.state.rawModelDataForEditor)}
rawModelData={this.state.rawModelDataForEditor}
outsideData={this.props.outsideData}
userData={this.props.userData}
forceSave={this.state.forceSave} // don't take into account that the form hasn't change, it's normal
......@@ -209,7 +209,6 @@ ModuleWrapper.propTypes = {
rawModelData: PropTypes.object.isRequired,
buildTitle: PropTypes.func.isRequired,
renderCore: PropTypes.func.isRequired,
parseRawModelData: PropTypes.func.isRequired,
coreClasses: PropTypes.object.isRequired,
outsideData: PropTypes.object,
userData: PropTypes.object.isRequired,
......
......@@ -16,13 +16,13 @@ class ScholarshipForm extends Form {
hasFormLevelErrors() {
let messages = Array();
const formData = this.getDataFromFields();
const { amount_min, amount_max } = formData;
const formData = this.getDataFromFields(),
{ amount_min, amount_max } = formData;
if (amount_min !== null && amount_max !== null && amount_max < amount_min) {
messages.push("La logique voudrait que la borne supérieure de la bourse soit... supérieure à la borne inférieure.");
}
return CustomError(messages);
return new CustomError(messages);
}
render() {
......
......@@ -20,10 +20,18 @@ export default function getMapStateToPropsForEditor(elKey) {
lastUpdateTimeInModel = latestRead.data.updated_on;
}
// simple way to retreive the save error
const updateFailed = state.api[propName].updateFailed,
createFailed = state.api[propName].createFailed;
let savingHasError = updateFailed;
if (createFailed.failed) {
savingHasError = createFailed;
}
return {
savingHasError: state.api[propName].updateFailed,
lastSaveTime: state.api[propName].updateSucceeded.readAt,
lastUpdateTimeInModel,
savingHasError,
lastUpdateTimeInModel
};
};
}
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