Skip to content

Commit

Permalink
Fix: inputs for rocketchat apps (#10274)
Browse files Browse the repository at this point in the history
* fix inputs for rocketchat apps

* Fix the user connection status being undefined error for rocket.cat on the apps
  • Loading branch information
ggazzo authored and rodrigok committed Apr 3, 2018
1 parent 0afab32 commit 18906b2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 44 deletions.
58 changes: 49 additions & 9 deletions packages/rocketchat-apps/client/admin/appManage.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,54 @@
</label>
<span class="rc-switch__description">{{{parseDescription i18nDescription}}}</span>
</div>
{{else if $eq type 'int'}}
<div class="rc-input">
<label class="rc-input__label">
<div class="rc-input__title">{{_ i18nLabel}}</div>
<div class="rc-input__wrapper">
{{#if multiline}}
<textarea class="rc-input__element" name="{{id}}" rows="4" style="height: auto">{{value}}</textarea>
{{else}}
<input class="rc-input__element" type="number" name="{{id}}" value="{{value}}" placeholder="{{_ i18nPlaceholder}}" />
{{/if}}
</div>
</label>
{{# if i18nDescription}}
<div class="rc-input__description">{{{parseDescription i18nDescription}}}</div>
{{/if}}
{{# if i18nAlert}}
<div class="rc-input__error">
<div class="rc-input__error-icon">
{{> icon block="rc-input__error-icon" icon="warning" classes="rc-input__error-icon-svg"}}
</div>
<div class="rc-input__error-message">{{_ "i18nAlert"}}</div>
</div>
{{/if}}
</div>
{{else if $eq type 'password'}}
<div class="rc-input">
<label class="rc-input__label">
<div class="rc-input__title">{{_ i18nLabel}}</div>
<div class="rc-input__wrapper">
{{#if multiline}}
<textarea class="rc-input__element" name="{{id}}" rows="4" style="height: auto">{{value}}</textarea>
{{else}}
<input class="rc-input__element" type="password" name="{{id}}" value="{{value}}" placeholder="{{_ i18nPlaceholder}}" />
{{/if}}
</div>
</label>
{{# if i18nDescription}}
<div class="rc-input__description">{{{parseDescription i18nDescription}}}</div>
{{/if}}
{{# if i18nAlert}}
<div class="rc-input__error">
<div class="rc-input__error-icon">
{{> icon block="rc-input__error-icon" icon="warning" classes="rc-input__error-icon-svg"}}
</div>
<div class="rc-input__error-message">{{_ "i18nAlert"}}</div>
</div>
{{/if}}
</div>
{{else}}
<div class="input-line double-col">
<label class="setting-label">{{_ i18nLabel}}</label>
Expand All @@ -99,14 +147,6 @@
<input class="input-monitor" type="text" name="{{id}}" value="{{relativeUrl value}}" placeholder="{{_ i18nPlaceholder}}" {{isReadonly}}/>
{{/if}}

{{#if $eq type 'password'}}
<input class="input-monitor" type="password" name="{{id}}" value="{{value}}" placeholder="{{_ i18nPlaceholder}}" />
{{/if}}

{{#if $eq type 'int'}}
<input class="input-monitor" type="number" name="{{id}}" value="{{value}}" placeholder="{{_ i18nPlaceholder}}" />
{{/if}}

{{#if $eq type 'select'}}
<div class="select-arrow">
<i class="icon-down-open secondary-font-color"></i>
Expand Down Expand Up @@ -234,7 +274,7 @@
<div class="rc-apps-container">
<div class="rc-button-group">
<button class="rc-button rc-button--secondary js-cancel">{{ _ "Cancel" }}</button>
<button class="rc-button rc-button--primary js-save" disabled='{{disabled}}'>{{ _ "Save" }}</button>
<button class="rc-button rc-button--primary js-save {{#if saving}} loading{{/if}}" disabled='{{disabled}}'>{{ _ "Save" }}</button>
<button class="rc-button rc-button--cancel js-uninstall">{{ _ "Uninstall" }}</button>
</div>
</div>
Expand Down
84 changes: 50 additions & 34 deletions packages/rocketchat-apps/client/admin/appManage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import s from 'underscore.string';

import { AppEvents } from '../communication';


Template.appManage.onCreated(function() {
const instance = this;
this.id = new ReactiveVar(FlowRouter.getParam('appId'));
Expand All @@ -12,14 +13,16 @@ Template.appManage.onCreated(function() {
this.processingEnabled = new ReactiveVar(false);
this.app = new ReactiveVar({});
this.settings = new ReactiveVar({});
this.loading = new ReactiveVar(false);

const id = this.id.get();

function _morphSettings(settings) {
Object.keys(settings).forEach((k) => {
settings[k].i18nPlaceholder = settings[k].i18nPlaceholder || ' ';
settings[k].value = settings[k].value || settings[k].packageValue;
settings[k].value = settings[k].value !== undefined ? settings[k].value : settings[k].packageValue;
settings[k].oldValue = settings[k].value;
settings[k].hasChanged = false;
});

instance.settings.set(settings);
Expand Down Expand Up @@ -73,17 +76,8 @@ Template.apps.onDestroyed(function() {
Template.appManage.helpers({
disabled() {
const t = Template.instance();

const toSave = Object.keys(t.settings.get()).filter((k) => {
const setting = t.settings.get()[k];
if (setting.hasChanged) {
return true;
}
});

if (toSave.length === 0) {
return true;
}
const settings = t.settings.get();
return !Object.keys(settings).some((k) => settings[k].hasChanged);
},
isReady() {
if (Template.instance().ready) {
Expand Down Expand Up @@ -134,6 +128,9 @@ Template.appManage.helpers({
item.tokens.forEach((t) => item.html = item.html.replace(t.token, t.text));

return item.html;
},
saving() {
return Template.instance().loading.get();
}
});

Expand Down Expand Up @@ -172,42 +169,60 @@ Template.appManage.events({
});
},

'click .js-uninstall': (e, t) => {
'click .js-uninstall': async(e, t) => {
t.ready.set(false);

RocketChat.API.delete(`apps/${ t.id.get() }`).then(() => {
try {
await RocketChat.API.delete(`apps/${ t.id.get() }`);
FlowRouter.go('/admin/apps');
}).catch((err) => {
} catch (err) {
console.warn('Error:', err);
} finally {
t.ready.set(true);
});
}
},

'click .logs': (e, t) => {
FlowRouter.go(`/admin/apps/${ t.id.get() }/logs`);
},

'click .js-save': (e, t) => {
const toSave = [];

Object.keys(t.settings.get()).forEach((k) => {
const setting = t.settings.get()[k];
if (setting.hasChanged) {
toSave.push(setting);
}
});

if (toSave.length === 0) {
console.log('Nothing to save..');
'click .js-save': async(e, t) => {
if (t.loading.get()) {
return;
}
t.loading.set(true);
const settings = t.settings.get();


try {
const toSave = [];
Object.keys(settings).forEach(k => {
const setting = settings[k];
if (setting.hasChanged) {
toSave.push(setting);
}
// return !!setting.hasChanged;
});

RocketChat.API.post(`apps/${ t.id.get() }/settings`, undefined, { settings: toSave }).then((result) => {
if (toSave.length === 0) {
throw 'Nothing to save..';
}
const result = await RocketChat.API.post(`apps/${ t.id.get() }/settings`, undefined, { settings: toSave });
console.log('Updating results:', result);
result.updated.forEach((setting) => {
t.settings.get()[setting.id].oldValue = setting.value;
result.updated.forEach(setting => {
settings[setting.id].value = settings[setting.id].oldValue = setting.value;
});
});
Object.keys(settings).forEach(k => {
const setting = settings[k];
setting.hasChanged = false;
});
t.settings.set(settings);

} catch (e) {
console.log(e);
} finally {
t.loading.set(false);
}

},

'change input[type="checkbox"]': (e, t) => {
Expand All @@ -225,7 +240,7 @@ Template.appManage.events({
}
},

'change .input-monitor, keyup .input-monitor': _.throttle(function(e, t) {
'input input': _.throttle(function(e, t) {
let value = s.trim($(e.target).val());
switch (this.type) {
case 'int':
Expand All @@ -240,6 +255,7 @@ Template.appManage.events({

if (setting.oldValue !== setting.value) {
t.settings.get()[this.id].hasChanged = true;
t.settings.set(t.settings.get());
}
}, 500)
});
2 changes: 1 addition & 1 deletion packages/rocketchat-apps/server/converters/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class AppUsersConverter {
return UserStatusConnection.BUSY;
default:
console.warn(`The user ${ username } (${ userId }) does not have a valid status (offline, online, away, or busy). It is currently: "${ status }"`);
return status === '' ? UserStatusConnection.OFFLINE : status.toUpperCase();
return !status ? UserStatusConnection.OFFLINE : status.toUpperCase();
}
}
}

0 comments on commit 18906b2

Please sign in to comment.