Added trigger deletion

This commit is contained in:
Magnus Åhall 2024-06-01 09:18:56 +02:00
parent 9ceca8600f
commit ef3f89cb2c
7 changed files with 191 additions and 48 deletions

20
main.go
View File

@ -143,6 +143,7 @@ func main() { // {{{
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)
service.Register("/trigger/delete/{id}", false, false, actionTriggerDelete)
service.Register("/configuration", false, false, pageConfiguration)
service.Register("/entry/{datapoint}", false, false, entryDatapoint)
@ -695,7 +696,7 @@ func triggerCreate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{
}
resp := struct {
OK bool
OK bool
Trigger Trigger
}{
true,
@ -862,6 +863,23 @@ func pageTriggerRun(w http.ResponseWriter, r *http.Request, _ *session.T) { // {
w.Header().Add("Content-Type", "application/json")
w.Write(j)
} // }}}
func actionTriggerDelete(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
err = TriggerDelete(id)
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
w.Header().Add("Location", "/triggers")
w.WriteHeader(302)
} // }}}
func pageConfiguration(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{
areas, err := AreaRetrieve()

View File

@ -201,19 +201,6 @@ label {
#areas .area .section > .name {
font-weight: 800;
}
#areas .area .section .triggers .trigger {
display: grid;
grid-template-columns: min-content 1fr;
grid-gap: 8px;
align-items: center;
margin-top: 8px;
}
#areas .area .section .triggers .trigger img {
height: 16px;
}
#areas .area .section .triggers .trigger .label {
color: inherit;
}
dialog {
background: #202020;
border: 1px solid #606060;

117
static/css/triggers.css Normal file
View File

@ -0,0 +1,117 @@
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
*:focus {
outline: none;
}
[onClick] {
cursor: pointer;
}
html,
body {
margin: 0;
padding: 0;
}
body {
background: #282828;
font-family: sans-serif;
font-weight: 300;
color: #d5c4a1;
font-size: 11pt;
}
h1,
h2 {
margin-bottom: 4px;
}
h1:first-child,
h2:first-child {
margin-top: 0px;
}
h1 {
font-size: 1.5em;
color: #fb4934;
font-weight: 800;
}
h2 {
font-size: 1.25em;
color: #b8bb26;
font-weight: 800;
}
a {
color: #3f9da1;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
b {
font-weight: 800;
}
input[type="text"],
textarea,
select {
font-family: monospace;
background: #202020;
color: #d5c4a1;
padding: 4px 8px;
border: none;
font-size: 1em;
line-height: 1.5em;
}
button {
background: #202020;
color: #d5c4a1;
padding: 8px 32px;
border: 1px solid #535353;
font-size: 1em;
height: 3em;
}
button:focus {
background: #333;
}
.line {
grid-column: 1 / -1;
border-bottom: 1px solid #4e4e4e;
}
span.date {
color: #d5c4a1;
font-weight: 800;
}
span.time {
font-size: 0.9em;
color: #d5c4a1;
}
span.seconds {
display: none;
}
label {
user-select: none;
}
.description {
border: 1px solid #737373;
color: #3f9da1;
background: #202020;
padding: 4px 8px;
margin-top: 8px;
white-space: nowrap;
width: min-content;
border-radius: 8px;
}
#areas .area .section .triggers .trigger {
display: grid;
grid-template-columns: min-content 1fr min-content;
grid-gap: 8px;
align-items: center;
margin-top: 8px;
}
#areas .area .section .triggers .trigger img {
height: 16px;
}
#areas .area .section .triggers .trigger .label {
color: inherit;
}

View File

@ -117,24 +117,6 @@
&>.name {
font-weight: @bold;
}
.triggers {
.trigger {
display: grid;
grid-template-columns: min-content 1fr;
grid-gap: 8px;
align-items: center;
margin-top: 8px;
img {
height: 16px;
}
.label {
color: inherit;
}
}
}
}
}
}

26
static/less/triggers.less Normal file
View File

@ -0,0 +1,26 @@
@import 'theme.less';
#areas {
.area {
.section {
.triggers {
.trigger {
display: grid;
grid-template-columns: min-content 1fr min-content;
grid-gap: 8px;
align-items: center;
margin-top: 8px;
img {
height: 16px;
}
.label {
color: inherit;
}
}
}
}
}
}

View File

@ -2,7 +2,7 @@ package main
import (
// External
we "git.gibonuddevalla.se/go/wrappederror"
werr "git.gibonuddevalla.se/go/wrappederror"
"github.com/expr-lang/expr"
"github.com/lib/pq"
@ -67,7 +67,7 @@ func TriggersRetrieve() (areas []Area, err error) { // {{{
var jsonData []byte
err = row.Scan(&jsonData)
if err != nil {
err = we.Wrap(err)
err = werr.Wrap(err)
return
}
@ -77,7 +77,7 @@ func TriggersRetrieve() (areas []Area, err error) { // {{{
err = json.Unmarshal(jsonData, &areas)
if err != nil {
err = we.Wrap(err)
err = werr.Wrap(err)
return
}
@ -97,13 +97,13 @@ func TriggersRetrieveByDatapoint(datapointName string) (triggers []Trigger, err
var data []byte
err = row.Scan(&data)
if err != nil {
err = we.Wrap(err).WithData(datapointName)
err = werr.Wrap(err).WithData(datapointName)
return
}
err = json.Unmarshal(data, &triggers)
if err != nil {
err = we.Wrap(err).WithData(datapointName)
err = werr.Wrap(err).WithData(datapointName)
return
}
@ -114,7 +114,7 @@ func TriggerRetrieve(id int) (trigger Trigger, err error) { // {{{
var jsonData []byte
err = row.Scan(&jsonData)
if err != nil {
err = we.Wrap(err)
err = werr.Wrap(err)
return
}
@ -158,7 +158,7 @@ func (t *Trigger) Update() (err error) { // {{{
)
err = row.Scan(&t.ID)
if err != nil {
err = we.Wrap(err).WithData(
err = werr.Wrap(err).WithData(
struct {
SectionID int
Name string
@ -191,7 +191,7 @@ func (t *Trigger) Update() (err error) { // {{{
}
if pqErr, ok := err.(*pq.Error); ok {
err = we.Wrap(err).WithData(
err = werr.Wrap(err).WithData(
struct {
Trigger *Trigger
PostgresCode pq.ErrorCode
@ -202,7 +202,14 @@ func (t *Trigger) Update() (err error) { // {{{
pqErr.Code.Name(),
})
} else if err != nil {
err = we.Wrap(err).WithData(t)
err = werr.Wrap(err).WithData(t)
}
return
} // }}}
func TriggerDelete(id int) (err error) { // {{{
_, err = service.Db.Conn.Exec(`DELETE FROM public.trigger WHERE id=$1`, id)
if err != nil {
return werr.Wrap(err).WithData(id)
}
return
} // }}}
@ -213,7 +220,7 @@ func (t *Trigger) Run() (output any, err error) { // {{{
var dp Datapoint
dp, err = DatapointRetrieve(0, dpname)
if err != nil {
err = we.Wrap(err)
err = werr.Wrap(err)
return
}
datapoints[dpname] = dp

View File

@ -2,6 +2,7 @@
{{ block "page_label" . }}{{end}}
{{ $version := .VERSION }}
<link rel="stylesheet" type="text/css" href="/css/{{ .VERSION }}/triggers.css">
<script type="text/javascript">
function createTrigger(sectionID) {
@ -24,6 +25,12 @@
location.href = `/trigger/edit/${json.Trigger.ID}`
})
}
function deleteTrigger(triggerID, name) {
if (!confirm(`Sure you want to delete '${name}'?`))
return
location.href = `/trigger/delete/${triggerID}`
}
</script>
<div id="areas">
@ -42,12 +49,11 @@
{{ if eq .Name "" }}
{{ continue }}
{{ end }}
<a href="/trigger/edit/{{ .ID }}">
<div class="trigger">
<img src="/images/{{ $version }}/triggers.svg">
<div class="label">{{ .Name }}</div>
</div>
</a>
<div class="trigger">
<img src="/images/{{ $version }}/triggers.svg">
<div class="label"><a href="/trigger/edit/{{ .ID }}">{{ .Name }}</a></div>
<img src="/images/{{ $version }}/delete.svg" onclick="deleteTrigger({{ .ID }}, '{{ .Name }}')">
</div>
{{ end }}
</div>
</div>