2017-02-20 18:15:36 +02:00
|
|
|
import isEmpty from 'lodash.isempty';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { Switch, Route } from 'react-router-dom';
|
|
|
|
import React from 'react';
|
2016-12-15 16:10:36 +02:00
|
|
|
|
2017-02-20 18:15:36 +02:00
|
|
|
import NotFound from '@containers/not-found';
|
|
|
|
import PropTypes from '@root/prop-types';
|
|
|
|
import Redirect from '@components/redirect';
|
|
|
|
import { orgByIdSelector, orgSectionsSelector } from '@state/selectors';
|
|
|
|
import NewProject from '@containers/new-project';
|
|
|
|
import PeopleSection from './people';
|
|
|
|
import SettingsSection from './settings';
|
|
|
|
import ProjectSection from './projects';
|
2017-01-30 19:22:01 +02:00
|
|
|
|
2016-12-16 14:28:27 +02:00
|
|
|
const SectionComponents = {
|
2017-02-20 18:15:36 +02:00
|
|
|
people: PeopleSection,
|
|
|
|
settings: SettingsSection,
|
|
|
|
projects: ProjectSection
|
2016-12-16 14:28:27 +02:00
|
|
|
};
|
2016-12-15 16:10:36 +02:00
|
|
|
|
2016-12-14 00:09:04 +02:00
|
|
|
const Org = ({
|
2016-12-15 16:10:36 +02:00
|
|
|
org = {},
|
2016-12-16 14:28:27 +02:00
|
|
|
sections = []
|
2016-12-14 00:09:04 +02:00
|
|
|
}) => {
|
2016-12-15 16:10:36 +02:00
|
|
|
if (isEmpty(org)) {
|
|
|
|
return (
|
|
|
|
<NotFound />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-20 21:06:02 +02:00
|
|
|
const missMatch = !sections.length ? null : (
|
2017-02-06 20:27:10 +02:00
|
|
|
<Route
|
|
|
|
component={Redirect(`/${org.id}/${sections[0]}`)}
|
|
|
|
exact
|
|
|
|
path={`/${org.id}`}
|
|
|
|
/>
|
2016-12-20 21:06:02 +02:00
|
|
|
);
|
2016-12-16 14:28:27 +02:00
|
|
|
|
|
|
|
const navMatches = sections.map((name) => (
|
2017-02-06 20:27:10 +02:00
|
|
|
<Route
|
2016-12-16 14:28:27 +02:00
|
|
|
component={SectionComponents[name]}
|
|
|
|
key={name}
|
2017-02-06 20:27:10 +02:00
|
|
|
path={`/:org/${name}`}
|
2016-12-16 14:28:27 +02:00
|
|
|
/>
|
|
|
|
));
|
|
|
|
|
2017-01-30 19:22:01 +02:00
|
|
|
navMatches.push(
|
2017-02-06 20:27:10 +02:00
|
|
|
<Route
|
2017-01-30 19:22:01 +02:00
|
|
|
component={NewProject}
|
|
|
|
key='new-project'
|
2017-02-06 20:27:10 +02:00
|
|
|
path={'/:org/new-project'}
|
2017-01-30 19:22:01 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2016-12-14 00:09:04 +02:00
|
|
|
return (
|
2017-02-06 20:27:10 +02:00
|
|
|
<Switch>
|
2016-12-16 14:28:27 +02:00
|
|
|
{missMatch}
|
2017-02-06 20:27:10 +02:00
|
|
|
{navMatches}
|
|
|
|
</Switch>
|
2016-12-14 00:09:04 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Org.propTypes = {
|
2017-01-03 00:32:29 +02:00
|
|
|
org: PropTypes.org,
|
|
|
|
sections: PropTypes.sections
|
2016-12-14 00:09:04 +02:00
|
|
|
};
|
|
|
|
|
2016-12-15 16:10:36 +02:00
|
|
|
const mapStateToProps = (state, ownProps) => ({
|
2017-02-06 20:27:10 +02:00
|
|
|
org: orgByIdSelector(ownProps.match.params.org)(state),
|
|
|
|
sections: orgSectionsSelector(ownProps.match.params.org)(state)
|
2016-12-15 16:10:36 +02:00
|
|
|
});
|
|
|
|
|
2017-02-20 18:15:36 +02:00
|
|
|
export default connect(
|
|
|
|
mapStateToProps
|
|
|
|
)(Org);
|