In-page addition of datapoints to triggers

This commit is contained in:
Magnus Åhall 2024-05-30 14:33:43 +02:00
parent 68abb894a6
commit adab2ab67d
2 changed files with 49 additions and 5 deletions

34
main.go
View File

@ -21,6 +21,7 @@ import (
"net/http"
"os"
"path"
"slices"
"sort"
"strconv"
"time"
@ -138,6 +139,7 @@ func main() { // {{{
service.Register("/triggers", false, false, pageTriggers)
service.Register("/trigger/edit/{id}", false, false, pageTriggerEdit)
service.Register("/trigger/edit/{id}/{sectionID}", false, false, pageTriggerEdit)
service.Register("/trigger/addDatapoint/{id}/{datapointName}", false, false, pageTriggerDatapointAdd)
service.Register("/trigger/update/{id}", false, false, pageTriggerUpdate)
service.Register("/trigger/run/{id}", false, false, pageTriggerRun)
@ -725,6 +727,38 @@ func pageTriggerEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { //
page.Render(w)
} // }}}
func pageTriggerDatapointAdd(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
triggerID := r.PathValue("id")
dpName := r.PathValue("datapointName")
id, err := strconv.Atoi(triggerID)
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
var trigger Trigger
if id > 0 {
trigger, err = TriggerRetrieve(id)
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
}
if !slices.Contains(trigger.Datapoints, dpName) {
trigger.Datapoints = append(trigger.Datapoints, dpName)
}
err = trigger.Update()
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
j, _ := json.Marshal(struct{ OK bool }{OK: true})
w.Header().Add("Content-Type", "application/json")
w.Write(j)
} // }}}
func pageTriggerUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)

View File

@ -66,7 +66,7 @@ export class UI {
})
.catch(err => alert(err))
}//}}}
chooseDatapoint() {//{{{
async chooseDatapoint() {//{{{
const dlg = document.getElementById('dlg-datapoints')
const datapoint = document.getElementById('datapoint').value
const dp = this.datapoints.find(dp => dp.Name == datapoint)
@ -77,8 +77,10 @@ export class UI {
}
this.trigger.addDatapoint(dp)
dlg.close()
this.render()
.then(() => {
dlg.close()
this.render()
})
}//}}}
deleteDatapoint(name) {//{{{
if (!confirm(`Delete ${name}?`)) {
@ -147,7 +149,15 @@ export class Trigger {
})
.catch(err => alert(err))
}//}}}
addDatapoint(dp) {//{{{
this.datapoints[dp.Name] = dp
async addDatapoint(dp) {//{{{
return fetch(`/trigger/addDatapoint/${this.id}/${dp.Name}`)
.then(data => data.json())
.then(json => {
if (!json.OK) {
alert(json.Error)
return
}
this.datapoints[dp.Name] = dp
})
}//}}}
}