<commit_before>"use strict";
EC.CreateUnit = React.createClass({
	propTypes: {
		data: React.PropTypes.object.isRequired,
		// createUnit: {
		// 		stage: 1,
		// 		options: {
		// 			classrooms: []
		// 		},
		// 		model: {
		// 			name: null,
		// 			selectedActivities: []
		// 		}
		actions: React.PropTypes.object.isRequired,
		analytics: React.PropTypes.object.isRequired
	},

	getInitialState: function () {
		return {
			selectedActivities : [],
			selectedClassrooms: [],
			dueDates: {}
		}
	},

	getStage: function () {
		return this.props.data.stage;
	},

	getSelectedActivities: function () {
		return this.props.data.model.selectedActivities;
	},

	getClassrooms: function () {
		return this.props.data.options.classrooms;
	},

	getUnitName: function () {
		return this.props.data.model.name;
	},

	assignActivityDueDate: function(activity, dueDate) {
		console.log('assign acitivyt due date in EC.CreateUnit', {activity: activity, dueDate: dueDate})
		var dueDates = this.state.dueDates;
		dueDates[activity.id] = dueDate;
		this.setState({dueDates: dueDates});
	},

	toggleClassroomSelection: function(classroom, flag) {
		var classrooms = this.getClassrooms();
		var updated = _.map(classrooms, function (c) {
			if (c.classroom.id == classroom.id) {
				if (c.students.length == 0) {
					c.emptyClassroomSelected = (this.toggleEmptyClassroomSelected(c));
				} else {
					var selected = _.where(c.students, {isSelected: true});
					if (selected.length == c.students.length) {
						var updatedStudents = _.map(c.students, function (s) {
							s.isSelected = false;
							return s;
						});
					} else {
						var updatedStudents = _.map(c.students, function (s) {
							s.isSelected = true;
							return s;
						});
					}
					c.students = updatedStudents;
				}
			}
			return c;
		}, this);
		this.setState({classrooms: updated});
	},

	toggleStudentSelection: function(student, classroom, flag) {
		var updated = _.map(this.getClassrooms(), function (c) {
			if (c.classroom.id == classroom.id) {
				var updated_students = _.map(c.students, function (s) {
					if (s.id == student.id) {
						s.isSelected = flag;
					}
					return s;
				});
				c.students = updated_students;
			}
			return c;
		}, this);
		this.setState({classrooms: updated});
	},

	updateUnitName: function (unitName) {
		this.props.actions.update({name: unitName})
	},

	clickContinue: function () {
		this.props.analytics.track('click Continue in lesson planner');
		this.props.actions.toggleStage(2);
		this.resetWindowPosition();
	},

	resetWindowPosition: function () {
		window.scrollTo(500, 0);
	},

	finish: function() {
		$.ajax({
			type: 'POST',
			url: '/teachers/units',
			data: this.formatCreateRequestData(),
			success: this.onCreateSuccess,
		});
	},

	formatCreateRequestData: function() {
		var classroomPostData = _.select(this.getClassrooms(), function (c) {
			var includeClassroom, selectedStudents;
			if (this.emptyClassroomSelected(c)) {
				includeClassroom = true;
			} else {
				selectedStudents = _.where(c.students, {isSelected: true});
				includeClassroom = selectedStudents.length > 0;
			}
			return includeClassroom;
		}, this);

		classroomPostData = _.map(classroomPostData, function (c) {
			var selectedStudentIds;
			var selectedStudents = _.where(c.students, {isSelected: true});
			if (selectedStudents.length == c.students.length) {
				selectedStudentIds = [];
			} else {
				selectedStudentIds = _.map(selectedStudents, function (s) {return s.id});
			}
			return {id: c.classroom.id, student_ids: selectedStudentIds};
		});

		var that = this;
		var _findDueDate = function (id) {
			_.reduce(that.state.dueDates, function (acc, v, k) {
				if (v == id) acc = k
				return acc
			}, null)
		}
		var sas = this.getSelectedActivities()

		var activityPostData = _.map(sas, function (sa) {
			return {
				id: sa.id,
				due_date: _findDueDate(sa.id)
			}
		})

		var x = {
			unit: {
				name: this.getUnitName(),
				classrooms: classroomPostData,
				activities: activityPostData
			}
		};
		return x;
	},

	onCreateSuccess: function(response) {
		this.props.actions.toggleTab('manageUnits');
	},

	isUnitNameSelected: function () {
		return ((this.getUnitName() != null) && (this.getUnitName() != ''));
	},

	determineIfEnoughInputProvidedToContinue: function () {
		var a = this.isUnitNameSelected();
		var b = (this.getSelectedActivities().length > 0);
		return (a && b);
	},

	emptyClassroomSelected: function (c) {
		var val = (c.emptyClassroomSelected === true);
		return val;
	},
<commit_msg>fix dueDate bug<commit_after>"use strict";
EC.CreateUnit = React.createClass({
	propTypes: {
		data: React.PropTypes.object.isRequired,
		// createUnit: {
		// 		stage: 1,
		// 		options: {
		// 			classrooms: []
		// 		},
		// 		model: {
		// 			name: null,
		// 			selectedActivities: []
		// 		}
		actions: React.PropTypes.object.isRequired,
		analytics: React.PropTypes.object.isRequired
	},

	getInitialState: function () {
		return {
			selectedActivities : [],
			selectedClassrooms: [],
			dueDates: {}
		}
	},

	getStage: function () {
		return this.props.data.stage;
	},

	getSelectedActivities: function () {
		return this.props.data.model.selectedActivities;
	},

	getClassrooms: function () {
		return this.props.data.options.classrooms;
	},

	getUnitName: function () {
		return this.props.data.model.name;
	},

	assignActivityDueDate: function(activity, dueDate) {
		console.log('assign acitivyt due date in EC.CreateUnit', {activity: activity, dueDate: dueDate})
		var dueDates = this.state.dueDates;
		dueDates[activity.id] = dueDate;
		this.setState({dueDates: dueDates});
	},

	toggleClassroomSelection: function(classroom, flag) {
		var classrooms = this.getClassrooms();
		var updated = _.map(classrooms, function (c) {
			if (c.classroom.id == classroom.id) {
				if (c.students.length == 0) {
					c.emptyClassroomSelected = (this.toggleEmptyClassroomSelected(c));
				} else {
					var selected = _.where(c.students, {isSelected: true});
					if (selected.length == c.students.length) {
						var updatedStudents = _.map(c.students, function (s) {
							s.isSelected = false;
							return s;
						});
					} else {
						var updatedStudents = _.map(c.students, function (s) {
							s.isSelected = true;
							return s;
						});
					}
					c.students = updatedStudents;
				}
			}
			return c;
		}, this);
		this.setState({classrooms: updated});
	},

	toggleStudentSelection: function(student, classroom, flag) {
		var updated = _.map(this.getClassrooms(), function (c) {
			if (c.classroom.id == classroom.id) {
				var updated_students = _.map(c.students, function (s) {
					if (s.id == student.id) {
						s.isSelected = flag;
					}
					return s;
				});
				c.students = updated_students;
			}
			return c;
		}, this);
		this.setState({classrooms: updated});
	},

	updateUnitName: function (unitName) {
		this.props.actions.update({name: unitName})
	},

	clickContinue: function () {
		this.props.analytics.track('click Continue in lesson planner');
		this.props.actions.toggleStage(2);
		this.resetWindowPosition();
	},

	resetWindowPosition: function () {
		window.scrollTo(500, 0);
	},

	finish: function() {
		$.ajax({
			type: 'POST',
			url: '/teachers/units',
			data: this.formatCreateRequestData(),
			success: this.onCreateSuccess,
		});
	},

	formatCreateRequestData: function() {
		var classroomPostData = _.select(this.getClassrooms(), function (c) {
			var includeClassroom, selectedStudents;
			if (this.emptyClassroomSelected(c)) {
				includeClassroom = true;
			} else {
				selectedStudents = _.where(c.students, {isSelected: true});
				includeClassroom = selectedStudents.length > 0;
			}
			return includeClassroom;
		}, this);

		classroomPostData = _.map(classroomPostData, function (c) {
			var selectedStudentIds;
			var selectedStudents = _.where(c.students, {isSelected: true});
			if (selectedStudents.length == c.students.length) {
				selectedStudentIds = [];
			} else {
				selectedStudentIds = _.map(selectedStudents, function (s) {return s.id});
			}
			return {id: c.classroom.id, student_ids: selectedStudentIds};
		});

		var sas = this.getSelectedActivities()

		var activityPostData = _.map(sas, function (sa) {
			return {
				id: sa.id,
				due_date: this.state.dueDates[sa.id]
			}
		}, this)

		var x = {
			unit: {
				name: this.getUnitName(),
				classrooms: classroomPostData,
				activities: activityPostData
			}
		};
		return x;
	},

	onCreateSuccess: function(response) {
		this.props.actions.toggleTab('manageUnits');
	},

	isUnitNameSelected: function () {
		return ((this.getUnitName() != null) && (this.getUnitName() != ''));
	},

	determineIfEnoughInputProvidedToContinue: function () {
		var a = this.isUnitNameSelected();
		var b = (this.getSelectedActivities().length > 0);
		return (a && b);
	},

	emptyClassroomSelected: function (c) {
		var val = (c.emptyClassroomSelected === true);
		return val;
	},

{'fraction_non_alphanumeric': 0.14647761527297087, 'fraction_numerical': 0.002009601429049905, 'mean_word_length': 3.346433770014556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3019692', 'n_tokens_mistral': 3561, 'n_tokens_neox': 2975, 'n_words': 587}