From 5689754f22967efc3f98daab8c3101900c2fa1e1 Mon Sep 17 00:00:00 2001 From: Florent Chehab Date: Fri, 14 Sep 2018 22:23:12 +0200 Subject: [PATCH] Better generalization --- .../editors/UniversityGeneralEditor.js | 31 ++++------------- .../editors/UniversitySemestersDatesEditor.js | 34 +++++-------------- .../components/university/shared/Editor.js | 1 - .../getMapDispatchToPropsForEditor.js | 8 +++++ .../getMapStateToPropsForEditor.js | 15 ++++++++ 5 files changed, 38 insertions(+), 51 deletions(-) create mode 100644 frontend/src/components/university/shared/editorFunctions/getMapDispatchToPropsForEditor.js create mode 100644 frontend/src/components/university/shared/editorFunctions/getMapStateToPropsForEditor.js diff --git a/frontend/src/components/university/editors/UniversityGeneralEditor.js b/frontend/src/components/university/editors/UniversityGeneralEditor.js index 35cf956a..e12dd306 100644 --- a/frontend/src/components/university/editors/UniversityGeneralEditor.js +++ b/frontend/src/components/university/editors/UniversityGeneralEditor.js @@ -9,6 +9,9 @@ import editorStyle from '../shared/editorStyle'; import TextField from '../shared/fields/TextField'; +import getMapStateToPropsForEditor from '../shared/editorFunctions/getMapStateToPropsForEditor'; +import getMapDispatchToPropsForEditor from '../shared/editorFunctions/getMapDispatchToPropsForEditor'; + import { universitiesElSaveData, universitiesElSavingHasError @@ -57,35 +60,15 @@ class UniversityGeneral extends Editor { } - UniversityGeneral.propTypes = { modelData: PropTypes.object.isRequired, }; -const mapStateToProps = (state) => { - let lastUpdateTime = null; - const tmp = state.universitiesEl.fetched; - if (tmp.fetchedAt) { - lastUpdateTime = tmp.data.updated_on; - } - - return { - savingHasError: state.universitiesEl.savingHasError, - lastSaveTime: state.universitiesEl.savedAt, - lastUpdateTime, - }; -}; - -const mapDispatchToProps = (dispatch) => { - return { - saveData: (data) => dispatch(universitiesElSaveData(data)), - clearSaveError: () => dispatch(universitiesElSavingHasError(false)) - }; -}; - - export default compose( withStyles(styles, { withTheme: true }), - connect(mapStateToProps, mapDispatchToProps) + connect( + getMapStateToPropsForEditor('universitiesEl'), + getMapDispatchToPropsForEditor(universitiesElSaveData, universitiesElSavingHasError) + ) )(UniversityGeneral); \ No newline at end of file diff --git a/frontend/src/components/university/editors/UniversitySemestersDatesEditor.js b/frontend/src/components/university/editors/UniversitySemestersDatesEditor.js index 1998a1d5..e38958bd 100644 --- a/frontend/src/components/university/editors/UniversitySemestersDatesEditor.js +++ b/frontend/src/components/university/editors/UniversitySemestersDatesEditor.js @@ -12,6 +12,9 @@ import MarkdownField from '../shared/fields/MarkdownField'; import dateStrToDate from '../../../utils/dateStrToDate'; +import getMapStateToPropsForEditor from '../shared/editorFunctions/getMapStateToPropsForEditor'; +import getMapDispatchToPropsForEditor from '../shared/editorFunctions/getMapDispatchToPropsForEditor'; + import { universitiesSemestersDatesElSaveData, universitiesSemestersDatesElSavingHasError @@ -30,7 +33,7 @@ class UniversitySemestersDatesEditor extends Editor { let messages = Array(); const formData = this.getDataFromFields(); const { autumn_begin, autumn_end, spring_begin, spring_end } = formData; - + if (onlyOneIsNull(autumn_begin, autumn_end)) { messages.push("Si une date est saisie pour le semestre d'automne, l'autre doit l'être aussi."); } @@ -91,36 +94,15 @@ class UniversitySemestersDatesEditor extends Editor { } } - - UniversitySemestersDatesEditor.propTypes = { modelData: PropTypes.object.isRequired, }; -const mapStateToProps = (state) => { - let lastUpdateTime = null; - const tmp = state.universitiesSemestersDatesEl.fetched; - if (tmp.fetchedAt) { - lastUpdateTime = tmp.data.updated_on; - } - - return { - savingHasError: state.universitiesSemestersDatesEl.savingHasError, - lastSaveTime: state.universitiesSemestersDatesEl.savedAt, - lastUpdateTime, - }; -}; - -const mapDispatchToProps = (dispatch) => { - return { - saveData: (data) => dispatch(universitiesSemestersDatesElSaveData(data)), - clearSaveError: () => dispatch(universitiesSemestersDatesElSavingHasError(false)) - }; -}; - - export default compose( withStyles(styles, { withTheme: true }), - connect(mapStateToProps, mapDispatchToProps) + connect( + getMapStateToPropsForEditor('universitiesSemestersDatesEl'), + getMapDispatchToPropsForEditor(universitiesSemestersDatesElSaveData, universitiesSemestersDatesElSavingHasError) + ) )(UniversitySemestersDatesEditor); \ No newline at end of file diff --git a/frontend/src/components/university/shared/Editor.js b/frontend/src/components/university/shared/Editor.js index 29c61ce0..0f7a43cb 100644 --- a/frontend/src/components/university/shared/Editor.js +++ b/frontend/src/components/university/shared/Editor.js @@ -163,7 +163,6 @@ class Editor extends MyComponent { if (this.state.lastSaveTime < this.props.lastSaveTime) { //saving data was successfull - console.log("state", this.state.lastSaveTime, "props", this.props.lastSaveTime) // We check if data was moderated let message = "Les données ont été enregistrées avec succès !"; let lastUpdateTime = this.state.lastUpdateTime; diff --git a/frontend/src/components/university/shared/editorFunctions/getMapDispatchToPropsForEditor.js b/frontend/src/components/university/shared/editorFunctions/getMapDispatchToPropsForEditor.js new file mode 100644 index 00000000..0c804397 --- /dev/null +++ b/frontend/src/components/university/shared/editorFunctions/getMapDispatchToPropsForEditor.js @@ -0,0 +1,8 @@ +export default function getMapDispatchToPropsForEditor(saveDataAction, savingHasErrorAction) { + return (dispatch) => { + return { + saveData: (data) => dispatch(saveDataAction(data)), + clearSaveError: () => dispatch(savingHasErrorAction(false)) + }; + } +} \ No newline at end of file diff --git a/frontend/src/components/university/shared/editorFunctions/getMapStateToPropsForEditor.js b/frontend/src/components/university/shared/editorFunctions/getMapStateToPropsForEditor.js new file mode 100644 index 00000000..c03c9a22 --- /dev/null +++ b/frontend/src/components/university/shared/editorFunctions/getMapStateToPropsForEditor.js @@ -0,0 +1,15 @@ +export default function getMapStateToPropsForEditor(elKey) { + return (state) => { + let lastUpdateTime = null; + const tmp = state[elKey].fetched; + if (tmp.fetchedAt) { + lastUpdateTime = tmp.data.updated_on; + } + + return { + savingHasError: state[elKey].savingHasError, + lastSaveTime: state[elKey].savedAt, + lastUpdateTime, + }; + }; +} \ No newline at end of file -- GitLab