Skip to content

Commit bf68622

Browse files
chore: Deduplicate session lock/unlock code (#4708)
1 parent 6feed0e commit bf68622

File tree

2 files changed

+25
-48
lines changed

2 files changed

+25
-48
lines changed

app/controllers/events/view/sessions/list.js

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ export default class extends Controller.extend(EmberTableControllerMixin) {
9696
extraValuePaths : ['isLocked'],
9797
cellComponent : 'ui-table/cell/events/view/sessions/cell-lock-session',
9898
actions : {
99-
unlockSession : this.unlockSession.bind(this),
100-
lockSession : this.lockSession.bind(this)
99+
lockSession: this.lockSession.bind(this)
101100
}
102101
}
103102
];
@@ -137,51 +136,29 @@ export default class extends Controller.extend(EmberTableControllerMixin) {
137136
}
138137

139138
@action
140-
lockSession(session_id) {
141-
let session = this.store.peekRecord('session', session_id, { backgroundReload: false });
142-
session.set('isLocked', true);
143-
this.set('isLoading', true);
144-
session.save()
145-
.then(() => {
146-
this.notify.success(this.l10n.t('Session has been locked successfully.'),
147-
{
148-
id: 'session_locked'
149-
});
150-
this.refreshModel.bind(this)();
151-
})
152-
.catch(() => {
153-
this.notify.error(this.l10n.t('An unexpected error has occurred.'),
154-
{
155-
id: 'session_lock_error'
156-
});
157-
})
158-
.finally(() => {
159-
this.set('isLoading', false);
160-
});
161-
}
162-
163-
@action
164-
unlockSession(session_id) {
165-
let session = this.store.peekRecord('session', session_id, { backgroundReload: false });
166-
session.set('isLocked', false);
139+
async lockSession(session_id, lock) {
140+
const session = this.store.peekRecord('session', session_id, { backgroundReload: false });
141+
const { isLocked } = session;
142+
session.set('isLocked', lock);
167143
this.set('isLoading', true);
168-
session.save()
169-
.then(() => {
170-
this.notify.success(this.l10n.t('Session has been unlocked successfully.'),
171-
{
172-
id: 'session_unlock'
173-
});
174-
this.refreshModel.bind(this)();
175-
})
176-
.catch(() => {
177-
this.notify.error(this.l10n.t('An unexpected error has occurred.'),
178-
{
179-
id: 'session_unexpected_unlock'
180-
});
181-
})
182-
.finally(() => {
183-
this.set('isLoading', false);
184-
});
144+
const lockMessage = lock ? 'locked' : 'unlocked';
145+
try {
146+
await session.save();
147+
this.notify.success(this.l10n.t(`Session has been ${ lockMessage } successfully.`),
148+
{
149+
id: 'session_lock'
150+
});
151+
this.refreshModel.bind(this)();
152+
} catch (e) {
153+
session.set('isLocked', isLocked);
154+
console.error('Error while changing session lock in organizer session list', e);
155+
this.notify.error(this.l10n.t('An unexpected error has occurred.'),
156+
{
157+
id: 'session_unexpected_lock'
158+
});
159+
} finally {
160+
this.set('isLoading', false);
161+
}
185162
}
186163

187164
@action
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{{#if this.extraRecords.isLocked}}
2-
<UiPopup @content={{t "Unlock Session"}} @class="ui basic button" @click={{action this.props.actions.unlockSession this.record}} @position="left center">
2+
<UiPopup @content={{t "Unlock Session"}} @class="ui basic button" @click={{action this.props.actions.lockSession this.record false}} @position="left center">
33
<i class="lock icon"></i>
44
</UiPopup>
55
{{else}}
6-
<UiPopup @content={{t "Lock Session"}} @class="ui basic button" @click={{action this.props.actions.lockSession this.record}} @position="left center">
6+
<UiPopup @content={{t "Lock Session"}} @class="ui basic button" @click={{action this.props.actions.lockSession this.record true}} @position="left center">
77
<i class="unlock icon"></i>
88
</UiPopup>
99
{{/if}}

0 commit comments

Comments
 (0)