Commit e9c7e960 authored by Florent Chehab's avatar Florent Chehab

Moved stuff to MyComponent

parent 064c5099
......@@ -34,6 +34,15 @@ class MyComponent extends Component {
return res;
}
getUnivCityAndCountry(univId) {
const univMainCampus = this.findMainCampus(univId);
const cities = this.getFetchedData("cities");
const countries = this.getFetchedData("countries");
const city = cities[univMainCampus.city];
const country = countries[city.country];
return { city, country }
}
findMainCampus(univId) {
const mainCampuses = this.getFetchedData('mainCampuses');
for (let main_campus_pk in mainCampuses) {
......
import React from 'react';
import PropTypes from 'prop-types';
import withStyles from '@material-ui/core/styles/withStyles';
import compose from 'recompose/compose';
import { connect } from "react-redux";
import Editor from '../shared/Editor';
import editorStyle from '../shared/editorStyle';
import MultiSelectField from '../shared/fields/MultiSelectField';
import __map from 'lodash/map';
import getMapStateToPropsForEditor from '../shared/editorFunctions/getMapStateToPropsForEditor';
import getMapDispatchToPropsForEditor from '../shared/editorFunctions/getMapDispatchToPropsForEditor';
import {
countryDriElSaveData,
countryDriElSavingHasError
} from '../../../generated/actions';
const styles = theme => ({
...editorStyle(theme)
});
class CountryDriEditor extends Editor {
renderEditor() {
const { modelData } = this.props;
const { outsideData } = this.props;
const countries = __map(outsideData.countries,
(country) => { return { label: country.name, value: country.id, disabled: false } }
)
return (
<div>
{this.renderTitleField()}
{this.renderImportanceLevelField()}
<MultiSelectField label={"Pays concernés"}
required={true}
value={modelData.countries}
options={countries}
formManager={this}
fieldMapping={'countries'}
/>
{this.renderCommentField()}
{this.renderUsefulLinksField()}
</div>
)
}
}
CountryDriEditor.propTypes = {
modelData: PropTypes.object.isRequired,
};
export default compose(
withStyles(styles, { withTheme: true }),
connect(
getMapStateToPropsForEditor('countryDriEl'),
getMapDispatchToPropsForEditor(countryDriElSaveData, countryDriElSavingHasError)
)
)(CountryDriEditor);
\ No newline at end of file
import React from 'react';
import PropTypes from 'prop-types';
import withStyles from '@material-ui/core/styles/withStyles';
import compose from 'recompose/compose';
import { connect } from "react-redux";
import __pick from 'lodash/pick';
import Markdown from '../../shared/Markdown';
import MyComponent from '../../MyComponent';
import GenericModule from '../shared/GenericModule';
import GenericGroupModule from '../shared/GenericGroupModule';
import CountryDriEditor from '../editors/CountryDriEditor';
import {
citiesFetchData,
mainCampusesFetchData,
countriesFetchData,
countryDriFetchData,
countryDriInvalidated,
} from '../../../generated/actions';
const styles = theme => ({
});
function renderCore(rawModelData, classes, outsideData) {
const { comment } = rawModelData;
return (
<Markdown source={comment} />
)
}
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 MyComponent {
ignoreInvalidation = true;
componentWillUnmount() {
this.props.invalidateData();
}
myComponentDidUpdate() {
if (this.props.countryDri.invalidated && !this.props.countryDri.isLoading) {
this.props.fetchData.countryDri(this.props.countryId);
}
}
myRender() {
const countryDriItems = this.getFetchedData('countryDri');
const countries = this.getFetchedData('countries')
const { classes } = this.props;
const outsideData = {
countries
}
return (
<GenericGroupModule
groupTitle={"Informations émanant de la DRI"}
endPoint={"countryDri"}
editor={CountryDriEditor}
invalidateGroup={this.props.invalidateData}
propsForEditor={{
modelData: { countries: [this.props.countryId], importance_level: '-' },
parseRawModelData: parseRawModelData,
outsideData: outsideData,
__apiAttr: this.props.countryId,
}}
>
{
countryDriItems.map((rawModelData) => (
<GenericModule
visible={this.props.visible}
buildTitle={(modelData) => modelData.title}
rawModelData={rawModelData}
parseRawModelData={parseRawModelData}
editor={CountryDriEditor}
renderCore={renderCore}
coreClasses={classes}
outsideData={outsideData}
moduleInGroupInfos={{ isInGroup: true, invalidateGroup: () => this.props.invalidateData() }}
__apiAttr={this.props.countryId}
/>
))
}
</GenericGroupModule >
)
}
__apiAttr = { countryDri: "countryId" };
}
CountryDri.propTypes = {
classes: PropTypes.object.isRequired,
theme: PropTypes.object.isRequired,
countryId: PropTypes.string.isRequired
};
const mapStateToProps = (state) => {
return {
countryDri: state.countryDri,
countries: state.countries,
cities: state.cities,
mainCampuses: state.mainCampuses
};
};
const mapDispatchToProps = (dispatch) => {
return {
fetchData: {
countryDri: (countryId) => dispatch(countryDriFetchData(countryId)),
countries: () => dispatch(countriesFetchData()),
cities: () => dispatch(citiesFetchData()),
mainCampuses: () => dispatch(mainCampusesFetchData()),
},
invalidateData: () => dispatch(countryDriInvalidated(true))
};
};
export default compose(
withStyles(styles, { withTheme: true }),
connect(mapStateToProps, mapDispatchToProps)
)(CountryDri);
\ No newline at end of file
......@@ -95,14 +95,11 @@ class UniversityGeneral extends MyComponent {
myRender() {
const univInfos = this.getFetchedData('universitiesEl');
const { classes } = this.props;
const univMainCampus = this.findMainCampus(this.props.univId);
const { cities, countries } = this.getAllFetchedData();
const cityModel = cities[univMainCampus.city];
const countryModel = countries[cityModel.country];
const { city, country } = this.getUnivCityAndCountry(this.props.univId);
const outsideData = {
city: cityModel.name,
country: countryModel.name
city: city.name,
country: country.name
}
return (
......
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