refract: delete event

This commit is contained in:
cv
2021-05-12 20:57:30 +02:00
parent 9c3aa6d386
commit 608ed8d5bd
2 changed files with 60 additions and 28 deletions
+54 -27
View File
@@ -49,7 +49,7 @@ class EventTimer extends Timer {
presenterNext: null, presenterNext: null,
}; };
selectedEvent = null; selectedEventIndex = null;
selectedEventId = null; selectedEventId = null;
nextEventId = null; nextEventId = null;
selectedPublicEventId = null; selectedPublicEventId = null;
@@ -411,7 +411,7 @@ class EventTimer extends Timer {
this.selectedEventId === id && this._startedAt != null this.selectedEventId === id && this._startedAt != null
? 'reload' ? 'reload'
: 'load'; : 'load';
this.loadEvent(this.selectedEvent, type); this.loadEvent(this.selectedEventIndex, type);
} else if ('title' in e || 'subtitle' in e || 'presenter') { } else if ('title' in e || 'subtitle' in e || 'presenter') {
// TODO: should be more selective on the need to load titles // TODO: should be more selective on the need to load titles
this._loadTitlesNext(); this._loadTitlesNext();
@@ -424,15 +424,39 @@ class EventTimer extends Timer {
this.broadcastState(); this.broadcastState();
} }
deleteId(eventId) {
// find object in events
const eventIndex = this._eventlist.findIndex((e) => e.id === eventId);
if (eventIndex === -1) return;
// delete event and update count
this._eventlist.splice(eventIndex, 1);
this.numEvents = this._eventlist.length;
// reload data if necessary
if (eventId === this.selectedEventId) {
this.unload();
return;
}
// update selected event index
this.selectedEventIndex = this._eventlist.findIndex(
(e) => e.id === eventId
);
// reload titles if necessary
if (eventId === this.nextEventId || eventId === this.nextPublicEventId) {
this._loadTitlesNext();
} else if (eventId === this.selectedPublicEventId) {
this._loadTitlesNow();
}
this.broadcastState();
}
loadEventById(eventId) { loadEventById(eventId) {
let eventIndex = this._eventlist.findIndex((e) => e.id === eventId); let eventIndex = this._eventlist.findIndex((e) => e.id === eventId);
console.log(
eventId,
this.selectedEventId,
eventIndex === this.selectedEventId
);
if (eventIndex === -1) return; if (eventIndex === -1) return;
this.pause(); this.pause();
this.loadEvent(eventIndex); this.loadEvent(eventIndex);
@@ -442,11 +466,13 @@ class EventTimer extends Timer {
// Loads a given event // Loads a given event
// load timers // load timers
// load selectedEvent // load selectedEventIndex
// load titles // load titles
loadEvent(eventIndex, type = 'load') { loadEvent(eventIndex, type = 'load') {
const e = this._eventlist[eventIndex]; const e = this._eventlist[eventIndex];
if (e == null) return;
const start = e.timeStart == null || e.timeStart === '' ? 0 : e.timeStart; const start = e.timeStart == null || e.timeStart === '' ? 0 : e.timeStart;
let end = e.timeEnd == null || e.timeEnd === '' ? 0 : e.timeEnd; let end = e.timeEnd == null || e.timeEnd === '' ? 0 : e.timeEnd;
// in case the end is earlier than start, we assume is the day after // in case the end is earlier than start, we assume is the day after
@@ -459,14 +485,14 @@ class EventTimer extends Timer {
this.duration = end - start; this.duration = end - start;
this.current = this.duration; this.current = this.duration;
this.selectedEvent = eventIndex; this.selectedEventIndex = eventIndex;
this.selectedEventId = e.id; this.selectedEventId = e.id;
} else if (type === 'reload') { } else if (type === 'reload') {
const now = this._getCurrentTime(); const now = this._getCurrentTime();
const elapsed = this.getElapsed(); const elapsed = this.getElapsed();
this.duration = end - start; this.duration = end - start;
this.selectedEvent = eventIndex; this.selectedEventIndex = eventIndex;
this._finishAt = now + (this.duration - elapsed); this._finishAt = now + (this.duration - elapsed);
} }
@@ -478,7 +504,7 @@ class EventTimer extends Timer {
} }
_loadTitlesNow() { _loadTitlesNow() {
const e = this._eventlist[this.selectedEvent]; const e = this._eventlist[this.selectedEventIndex];
if (e == null) return; if (e == null) return;
// private title is always current // private title is always current
@@ -501,10 +527,10 @@ class EventTimer extends Timer {
this.selectedPublicEventId = null; this.selectedPublicEventId = null;
// if there is nothing before, return // if there is nothing before, return
if (this.selectedEvent === 0) return; if (this.selectedEventIndex === 0) return;
// iterate backwards to find it // iterate backwards to find it
for (let i = this.selectedEvent; i >= 0; i--) { for (let i = this.selectedEventIndex; i >= 0; i--) {
if ( if (
this._eventlist[i].type === 'event' && this._eventlist[i].type === 'event' &&
this._eventlist[i].isPublic this._eventlist[i].isPublic
@@ -521,7 +547,7 @@ class EventTimer extends Timer {
_loadTitlesNext() { _loadTitlesNext() {
// maybe there is nothing to load // maybe there is nothing to load
if (this.selectedEvent == null) return; if (this.selectedEventIndex == null) return;
// assume there is no next event // assume there is no next event
this.titles.titleNext = null; this.titles.titleNext = null;
@@ -534,11 +560,11 @@ class EventTimer extends Timer {
this.titlesPublic.presenterNext = null; this.titlesPublic.presenterNext = null;
this.nextPublicEventId = null; this.nextPublicEventId = null;
if (this.selectedEvent < this.numEvents - 1) { if (this.selectedEventIndex < this.numEvents - 1) {
let nextPublic = false; let nextPublic = false;
let nextPrivate = false; let nextPrivate = false;
for (let i = this.selectedEvent + 1; i < this.numEvents; i++) { for (let i = this.selectedEventIndex + 1; i < this.numEvents; i++) {
// check that is the right type // check that is the right type
if (this._eventlist[i].type === 'event') { if (this._eventlist[i].type === 'event') {
// if we have not set private // if we have not set private
@@ -585,7 +611,7 @@ class EventTimer extends Timer {
presenterNext: null, presenterNext: null,
}; };
this.selectedEvent = null; this.selectedEventIndex = null;
this.selectedEventId = null; this.selectedEventId = null;
this.nextEventId = null; this.nextEventId = null;
this.selectedPublicEventId = null; this.selectedPublicEventId = null;
@@ -606,7 +632,7 @@ class EventTimer extends Timer {
Events Events
------------------------------ ------------------------------
numEvents = ${this.numEvents} numEvents = ${this.numEvents}
selectedEvent = ${this.selectedEvent} selectedEventIndex = ${this.selectedEventIndex}
selectedEventId = ${this.selectedEventId} selectedEventId = ${this.selectedEventId}
nextEventId = ${this.nextEventId} nextEventId = ${this.nextEventId}
selectedPublicEventId = ${this.selectedPublicEventId} selectedPublicEventId = ${this.selectedPublicEventId}
@@ -701,13 +727,14 @@ class EventTimer extends Timer {
if (this.numEvents < 1) return; if (this.numEvents < 1) return;
// if there is no event running, go to first // if there is no event running, go to first
if (this.selectedEvent == null) { if (this.selectedEventIndex == null) {
this.loadEvent(0); this.loadEvent(0);
return; return;
} }
const gotoEvent = this.selectedEvent > 0 ? this.selectedEvent - 1 : 0; const gotoEvent =
this.selectedEventIndex > 0 ? this.selectedEventIndex - 1 : 0;
if (gotoEvent === this.selectedEvent) return; if (gotoEvent === this.selectedEventIndex) return;
this.loadEvent(gotoEvent); this.loadEvent(gotoEvent);
// change playstate // change playstate
@@ -719,17 +746,17 @@ class EventTimer extends Timer {
if (this.numEvents < 1) return; if (this.numEvents < 1) return;
// if there is no event running, go to first // if there is no event running, go to first
if (this.selectedEvent == null) { if (this.selectedEventIndex == null) {
this.loadEvent(0); this.loadEvent(0);
return; return;
} }
const gotoEvent = const gotoEvent =
this.selectedEvent < this.numEvents - 1 this.selectedEventIndex < this.numEvents - 1
? this.selectedEvent + 1 ? this.selectedEventIndex + 1
: this.numEvents - 1; : this.numEvents - 1;
if (gotoEvent === this.selectedEvent) return; if (gotoEvent === this.selectedEventIndex) return;
this.loadEvent(gotoEvent); this.loadEvent(gotoEvent);
// change playstate // change playstate
@@ -749,7 +776,7 @@ class EventTimer extends Timer {
reload() { reload() {
// reload data // reload data
this.loadEvent(this.selectedEvent); this.loadEvent(this.selectedEventIndex);
// reset playstate // reset playstate
this.pause(); this.pause();
+6 -1
View File
@@ -61,6 +61,11 @@ function _updateTimersSingle(id, entry) {
global.timer.updateSingleEvent(id, entry); global.timer.updateSingleEvent(id, entry);
} }
// Delete a single entry in timer object
function _deleteTimerId(entryId) {
global.timer.deleteId(entryId);
}
// Create controller for GET request to '/events' // Create controller for GET request to '/events'
// Returns - // Returns -
exports.eventsGetAll = async (req, res) => { exports.eventsGetAll = async (req, res) => {
@@ -281,7 +286,7 @@ exports.eventsDelete = async (req, res) => {
_removeById(req.params.eventId); _removeById(req.params.eventId);
// update timer // update timer
_updateTimers(); _deleteTimerId(req.params.eventId);
res.sendStatus(201); res.sendStatus(201);
} catch (error) { } catch (error) {