<commit_before>import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import { Template } from 'meteor/templating';

import SaveAfterLogin from '/imports/ui/lib/save-after-login.js';
import { MaySubscribe } from '/imports/utils/course-role-utils.js';

import '/imports/ui/components/buttons/buttons.js';

import './course-roles.html';

Template.courseRole.created = function() {
	this.busy(false);
	this.enrolling = new ReactiveVar(false);
	this.showFirstSteps = new ReactiveVar(false);
};

Template.courseRole.helpers({
	showFirstSteps: () => Template.instance().showFirstSteps.get(),

	enrolling: function() { return Template.instance().enrolling.get(); },

	roleSubscribe: function() {
		let role = this.type;
		if (role == 'participant') role = 'interested';

		return 'roles.' + role + '.subscribe';
	},

	roleSubscribed: function() {
		let role = this.type;
		if (role == 'participant') role = 'interested';

		return 'roles.' + role + '.subscribed';
	},

	maySubscribe: function(role) {
		var operator = Meteor.userId();

		// Show the participation buttons even when not logged-in.
		// fun HACK: if we pass an arbitrary string instead of falsy
		// the MaySubscribe() will return true if the user could subscribe
		// if they were logged-in. Plain abuse of MaySubscribe().
		if (!operator) operator = 'unlogged';

		return MaySubscribe(operator, this.course, operator, role);
	}
});

Template.courseRole.events({
	'click .js-role-enroll-btn'(event, instance) {
		event.preventDefault();
		instance.enrolling.set(true);
	},

	'click .js-role-subscribe-btn'(event, instance) {
		event.preventDefault();
		const comment = instance.$('.js-comment').val();
		instance.busy('enrolling');
		SaveAfterLogin(instance, mf('loginAction.enroll', 'Login and enroll'), () => {
			Meteor.call('add_role', this.course._id, Meteor.userId(), this.roletype.type, (err) => {
				if (err) {
					console.error(err);
				} else {
					this.showFirstSteps.set(true);
					instance.busy(false);
					instance.enrolling.set(false);
					Meteor.call('change_comment', this.course._id, comment, err => {
						instance.busy(false);
						if (err) {
							console.error(err);
						} else {
							instance.enrolling.set(false);
						}
					});
				}
			});
		});
	},

	'click .js-role-enroll-cancel': function (e, template) {
		template.enrolling.set(false);
		return false;
	},

	'click .js-role-unsubscribe-btn': function () {
		Meteor.call('remove_role', this.course._id, Meteor.userId(), this.roletype.type);
		return false;
	},<commit_msg>fix setting user comment<commit_after>import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import { Template } from 'meteor/templating';

import SaveAfterLogin from '/imports/ui/lib/save-after-login.js';
import { MaySubscribe } from '/imports/utils/course-role-utils.js';

import '/imports/ui/components/buttons/buttons.js';

import './course-roles.html';

Template.courseRole.created = function() {
	this.busy(false);
	this.enrolling = new ReactiveVar(false);
	this.showFirstSteps = new ReactiveVar(false);
};

Template.courseRole.helpers({
	showFirstSteps: () => Template.instance().showFirstSteps.get(),

	enrolling: function() { return Template.instance().enrolling.get(); },

	roleSubscribe: function() {
		let role = this.type;
		if (role == 'participant') role = 'interested';

		return 'roles.' + role + '.subscribe';
	},

	roleSubscribed: function() {
		let role = this.type;
		if (role == 'participant') role = 'interested';

		return 'roles.' + role + '.subscribed';
	},

	maySubscribe: function(role) {
		var operator = Meteor.userId();

		// Show the participation buttons even when not logged-in.
		// fun HACK: if we pass an arbitrary string instead of falsy
		// the MaySubscribe() will return true if the user could subscribe
		// if they were logged-in. Plain abuse of MaySubscribe().
		if (!operator) operator = 'unlogged';

		return MaySubscribe(operator, this.course, operator, role);
	}
});

Template.courseRole.events({
	'click .js-role-enroll-btn'(event, instance) {
		event.preventDefault();
		instance.enrolling.set(true);
	},

	'click .js-role-subscribe-btn'(event, instance) {
		event.preventDefault();
		const comment = instance.$('.js-comment').val();
		instance.busy('enrolling');
		SaveAfterLogin(instance, mf('loginAction.enroll', 'Login and enroll'), () => {
			Meteor.call('add_role', this.course._id, Meteor.userId(), this.roletype.type, (err) => {
				if (err) {
					console.error(err);
				} else {
					instance.showFirstSteps.set(true);
					instance.busy(false);
					instance.enrolling.set(false);
					Meteor.call('change_comment', this.course._id, comment, err => {
						instance.busy(false);
						if (err) {
							console.error(err);
						} else {
							instance.enrolling.set(false);
						}
					});
				}
			});
		});
	},

	'click .js-role-enroll-cancel': function (e, template) {
		template.enrolling.set(false);
		return false;
	},

	'click .js-role-unsubscribe-btn': function () {
		Meteor.call('remove_role', this.course._id, Meteor.userId(), this.roletype.type);
		return false;
	},
{'fraction_non_alphanumeric': 0.16371767450904143, 'fraction_numerical': 0.0, 'mean_word_length': 3.8345864661654137, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '2397249', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1655, 'n_words': 357}