This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/components/group-membership-button.js.es6

69 lines
1.8 KiB
JavaScript

import { default as computed } from 'ember-addons/ember-computed-decorators';
import { popupAjaxError } from 'discourse/lib/ajax-error';
import DiscourseURL from 'discourse/lib/url';
export default Ember.Component.extend({
loading: false,
@computed("model.public")
canJoinGroup(publicGroup) {
return publicGroup;
},
@computed("model.is_group_user", "model.id", "groupUserIds")
userIsGroupUser(isGroupUser, groupId, groupUserIds) {
if (isGroupUser !== undefined) {
return isGroupUser;
} else {
return !!groupUserIds && groupUserIds.includes(groupId);
}
},
_showLoginModal() {
this.sendAction('showLogin');
$.cookie('destination_url', window.location.href);
},
actions: {
joinGroup() {
if (this.currentUser) {
this.set('updatingMembership', true);
const model = this.get('model');
model.addMembers(this.currentUser.get('username')).then(() => {
model.set('is_group_user', true);
}).catch(popupAjaxError).finally(() => {
this.set('updatingMembership', false);
});
} else {
this._showLoginModal();
}
},
leaveGroup() {
this.set('updatingMembership', true);
const model = this.get('model');
model.removeMember(this.currentUser).then(() => {
model.set('is_group_user', false);
}).catch(popupAjaxError).finally(() => {
this.set('updatingMembership', false);
});
},
requestMembership() {
if (this.currentUser) {
this.set('loading', true);
this.get('model').requestMembership().then(result => {
DiscourseURL.routeTo(result.relative_url);
}).catch(popupAjaxError).finally(() => {
this.set('loading', false);
});
} else {
this._showLoginModal();
}
}
}
});