Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Harmonize channel-related actions #9697

Merged
merged 4 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 16 additions & 104 deletions packages/rocketchat-channel-settings/client/views/channelSettings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import toastr from 'toastr';
import s from 'underscore.string';
import { RocketChat, RoomSettingsEnum } from 'meteor/rocketchat:lib';
import { call, erase, hide, leave, RocketChat, RoomSettingsEnum } from 'meteor/rocketchat:lib';
const common = {
canLeaveRoom() {
const { cl: canLeave, t: roomType } = Template.instance().room;
Expand All @@ -25,18 +25,6 @@ const common = {
return room.t === 'd';
}
};
const call = (method, ...params) => {
return new Promise((resolve, reject) => {
Meteor.call(method, ...params, (err, result)=> {
if (err) {
handleError(err);
return reject(err);
}
return resolve(result);
});
});
};


Template.channelSettingsEditing.events({
'input .js-input'(e) {
Expand All @@ -46,11 +34,11 @@ Template.channelSettingsEditing.events({
this.value.set(e.currentTarget.checked);
},
'click .js-reset'(e, t) {
const {settings} = t;
const { settings } = t;
Object.keys(settings).forEach(key => settings[key].value.set(settings[key].default.get()));
},
async 'click .js-save'(e, t) {
const {settings} = t;
const { settings } = t;
Object.keys(settings).forEach(async name => {
const setting = settings[name];
const value = setting.value.get();
Expand Down Expand Up @@ -201,7 +189,7 @@ Template.channelSettingsEditing.onCreated(function() {
};
if (room['default']) {
if (RocketChat.authz.hasRole(Meteor.userId(), 'admin')) {
return new Promise((resolve, reject)=> {
return new Promise((resolve, reject) => {
modal.open({
title: t('Room_default_change_to_private_will_be_default_no_more'),
type: 'warning',
Expand Down Expand Up @@ -269,7 +257,7 @@ Template.channelSettingsEditing.onCreated(function() {
return RocketChat.authz.hasAtLeastOnePermission(['archive-room', 'unarchive-room'], room._id);
},
save(value) {
return new Promise((resolve, reject)=>{
return new Promise((resolve, reject) => {
modal.open({
title: t('Are_you_sure'),
type: 'warning',
Expand Down Expand Up @@ -347,7 +335,7 @@ Template.channelSettingsEditing.onCreated(function() {
};
Object.keys(this.settings).forEach(key => {
const setting = this.settings[key];
const def =setting.getValue ? setting.getValue(this.room): this.room[key];
const def = setting.getValue ? setting.getValue(this.room) : this.room[key];
setting.default = new ReactiveVar(def || false);
setting.value = new ReactiveVar(def || false);
});
Expand All @@ -368,7 +356,7 @@ Template.channelSettingsEditing.helpers({
return this.value.get();// ? '' : 'checked';
},
modified(text = '') {
const {settings} = Template.instance();
const { settings } = Template.instance();
return !Object.keys(settings).some(key => settings[key].default.get() !== settings[key].value.get()) ? text : '';
},
equal(text = '', text2 = '', ret = '*') {
Expand All @@ -385,7 +373,7 @@ Template.channelSettingsEditing.helpers({
return 'hashtag';
case 'l':
return 'livechat';
default :
default:
return null;
}
},
Expand Down Expand Up @@ -420,96 +408,20 @@ Template.channelSettings.events({
t.editing.set(true);
},
'click .js-leave'(e, instance) {
let warnText;
const { rid, name, t : type } = instance.room;
switch (type) {
case 'c': warnText = 'Leave_Room_Warning'; break;
case 'p': warnText = 'Leave_Group_Warning'; break;
case 'd': warnText = 'Leave_Private_Warning'; break;
case 'l': warnText = 'Leave_Livechat_Warning'; break;
}

modal.open({
title: t('Are_you_sure'),
text: warnText ? t(warnText, name) : '',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_leave_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: true,
html: false
}, async function() {
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName()) && (Session.get('openedRoom') === rid)) {
FlowRouter.go('home');
}

await call('leaveRoom', rid);

if (rid === Session.get('openedRoom')) {
Session.delete('openedRoom');
}

});
const { name, t: type } = instance.room;
const rid = instance.room._id;
leave(type, rid, name);
},
'click .js-hide'(e, instance) {
let warnText;
const { rid, name, t: type } = instance.room;
switch (type) {
case 'c': warnText = 'Hide_Room_Warning'; break;
case 'p': warnText = 'Hide_Group_Warning'; break;
case 'd': warnText = 'Hide_Private_Warning'; break;
case 'l': warnText = 'Hide_Livechat_Warning'; break;
}

modal.open({
title: t('Are_you_sure'),
text: warnText ? t(warnText, name) : '',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_hide_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: true,
html: false
}, async function() {
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName()) && (Session.get('openedRoom') === rid)) {
FlowRouter.go('home');
}

await call('hideRoom', rid);

if (rid === Session.get('openedRoom')) {
Session.delete('openedRoom');
}

});
const { name, t: type } = instance.room;
const rid = instance.room._id;
hide(type, rid, name);
},
'click .js-cancel'(e, t) {
t.editing.set(false);
},
'click .js-delete'() {
return modal.open({
title: t('Are_you_sure'),
text: t('Delete_Room_Warning'),
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_delete_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: false,
html: false
}, () => {
call('eraseRoom', this.rid).then(() => {
modal.open({
title: t('Deleted'),
text: t('Room_has_been_deleted'),
type: 'success',
timer: 2000,
showConfirmButton: false
});
});
});
return erase(this.rid);
}
});

Expand Down Expand Up @@ -558,7 +470,7 @@ Template.channelSettingsInfo.helpers({
return 'hashtag';
case 'l':
return 'livechat';
default :
default:
return null;
}
}
Expand Down
93 changes: 93 additions & 0 deletions packages/rocketchat-lib/client/lib/ChannelActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { call, UiTextContext } from 'meteor/rocketchat:lib';

export function hide(type, rid, name) {
const warnText = RocketChat.roomTypes.roomTypes[type].getUiText(UiTextContext.HIDE_WARNING);

modal.open({
title: t('Are_you_sure'),
text: warnText ? t(warnText, name) : '',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_hide_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: true,
html: false
}, function() {
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName()) && (Session.get('openedRoom') === rid)) {
FlowRouter.go('home');
}

Meteor.call('hideRoom', rid, function(err) {
if (err) {
handleError(err);
} else if (rid === Session.get('openedRoom')) {
Session.delete('openedRoom');
}
});
});

return false;
}

export function leave(type, rid, name) {
const warnText = RocketChat.roomTypes.roomTypes[type].getUiText(UiTextContext.LEAVE_WARNING);

modal.open({
title: t('Are_you_sure'),
text: warnText ? t(warnText, name) : '',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_leave_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: false,
html: false
}, function(isConfirm) {
if (isConfirm) {
Meteor.call('leaveRoom', rid, function(err) {
if (err) {
modal.open({
title: t('Warning'),
text: handleError(err, false),
type: 'warning',
html: false
});
} else {
modal.close();
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName()) && (Session.get('openedRoom') === rid)) {
FlowRouter.go('home');
}

RoomManager.close(rid);
}
});
}
});

return false;
}

export function erase(rid) {
modal.open({
title: t('Are_you_sure'),
text: t('Delete_Room_Warning'),
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_delete_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: false,
html: false
}, () => {
call('eraseRoom', rid).then(() => {
modal.open({
title: t('Deleted'),
text: t('Room_has_been_deleted'),
type: 'success',
timer: 2000,
showConfirmButton: false
});
});
});
}
17 changes: 17 additions & 0 deletions packages/rocketchat-lib/client/lib/callMethod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Wraps a Meteor method into a Promise.
* This is particularly useful for creating information dialogs after execution of a Meteor method
* @param {The Meteor method to be calls} method
* @param {the method's parameters} params
*/
export const call = (method, ...params) => {
return new Promise((resolve, reject) => {
Meteor.call(method, ...params, (err, result) => {
if (err) {
handleError(err);
return reject(err);
}
return resolve(result);
});
});
};
7 changes: 6 additions & 1 deletion packages/rocketchat-lib/client/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@

import { RocketChatTabBar } from './RocketChatTabBar';
import { RoomSettingsEnum, RoomTypeConfig, RoomTypeRouteConfig, UiTextContext } from '../../lib/RoomTypeConfig';

import { hide, leave, erase } from './ChannelActions';
import { call } from './callMethod';

export {
call,
erase,
hide,
leave,
RocketChatTabBar,
RoomSettingsEnum,
RoomTypeConfig,
Expand Down
Loading