Skip to content

Commit

Permalink
only show available participants #33
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbeier committed Apr 28, 2017
1 parent 324ce50 commit 9013e61
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions client/components/MissionForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export default React.createClass({
<VolunteerGroupSelect
key={assignment.volunteer}
assignment={assignment}
mission={this.state.mission}
onChange={onChange(assignment.volunteer)}
onRemove={onRemove(assignment.volunteer)}
/>
Expand Down
35 changes: 29 additions & 6 deletions client/components/VolunteerGroupSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default React.createClass({

propTypes: {
assignment: React.PropTypes.object.isRequired,
mission: React.PropTypes.object.isRequired,
onChange: React.PropTypes.func,
onRemove: React.PropTypes.func,
},
Expand All @@ -35,17 +36,39 @@ export default React.createClass({

getInitialState() {
const group = (this.context.volunteers[this.props.assignment.volunteer] || {}).group;
const volunteers = this.getVolunteersByGroup(group);
return { group, volunteers };
return { group };
},

getVolunteersByGroup(group) {
return _.filter(this.context.volunteers, volunteer => group === volunteer.group);
componentDidMount() {
this.setSuitableVolunteers();
},

componentWillReceiveProps(nextProps) {
this.setSuitableVolunteers(nextProps.mission);
},

setSuitableVolunteers(mission = this.props.mission) {
const group = this.state.group;
const now = new Date();
const start = new Date(mission.start);
const end = new Date(mission.end);

const isAvailable = av => (
new Date(av.confirmationTill) >= now &&
new Date(av.from) <= start &&
new Date(av.till) >= end
);

const volunteers = _.filter(this.context.volunteers, volunteer =>
group === volunteer.group &&
volunteer.availabilities.length &&
volunteer.availabilities.find(isAvailable));

this.setState({ volunteers });
},

changeGroup({ value }) {
const volunteers = this.getVolunteersByGroup(value);
this.setState({ group: value, volunteers });
this.setState({ group: value }, this.setSuitableVolunteers);
},

changeName({ value }) {
Expand Down

0 comments on commit 9013e61

Please sign in to comment.