Implement client side enable, disable, edit

master
bel 2020-03-15 20:14:03 +00:00
parent b7bfee96a3
commit 19d4b645b8
8 changed files with 63 additions and 18 deletions

View File

@ -10,19 +10,15 @@ tbody tr:nth-child(2n+1) {
background-color: #161f27;
}
#upserts {
#upsert {
width: 100%;
}
#upserts > textarea {
#upsert > textarea {
width: 100%;
resize: vertical;
}
#upserts > input[name="id"] {
display: none;
}
#jobs > tbody > tr > td {
padding: 0;
}

View File

@ -8,8 +8,9 @@
</h1>
<details>
<summary>Upsert Job</summary>
<form id="upserts" action="#" method="get" onsubmit="upserts(); return false;">
<form id="upsert" action="#" method="get" onsubmit="upsert(); return false;">
<input type="text" name="id" placeholder="id"/>
<label><input type="checkbox" name="disabled"/> Disabled</label>
<select name="language" required>
<option value="bash" selected>bash</option>
</select>

View File

@ -12,11 +12,11 @@ function http(method, remote, callback, body) {
xmlhttp.send(body);
}
function upserts() {
function upsert() {
function cb(body, status) {
console.log(status, body)
}
http("POST", "/upserts", cb, jsonifyForm("upserts"))
http("POST", "/api/job/upsert", cb, jsonifyForm("upsert"))
}
function jsonifyForm(id) {
@ -33,8 +33,6 @@ function init() {
jobs.forEach(function(job) {
var s = format(job)
inject(s)
console.log("job=", job)
console.log("s=", s)
})
}
function format(job) {
@ -55,12 +53,12 @@ function init() {
btns.forEach(function(e) {
buttons += `
<span>
<input type="button" onclick="${e.name}(this);" value="&#${e.icon};" alt="${e.name}" title="${e.name}"/>
<input type="button" onclick="job${e.name}(this);" value="&#${e.icon};" alt="${e.name}" title="${e.name}" job="${btoa(JSON.stringify(job))}"/>
</span>
`
})
return `<tr><td><details>
<summary name="${job.id}"}">
<summary name="${job.id}">
<span>${job.title}</span>
<span>${passing}</span>
${buttons}
@ -87,7 +85,50 @@ function init() {
var table = document.getElementById("jobs").getElementsByTagName("tbody")[0]
table.innerHTML += s
}
http("GET", "/list", cb, null)
http("GET", "/api/job/list", cb, null)
}
init()
function jobdisable(input) {
jobmodify(input)
getField("disabled").checked = true
}
function jobenable(input) {
jobmodify(input)
getField("disabled").checked = false
}
function jobmodify(input) {
var form = getForm()
var job = jobFromInput(input)
var fields = ["id", "language", "cron", "script", "disabled"]
fields.forEach(function(field) {
var e = getField(field)
e.checked = job[field]
e.value = job[field]
})
var details = form.parentElement
details.setAttribute("open", "")
}
function jobdelete(input) {
http("DELETE", "/api/job/delete", cb, null)
}
function jobFromInput(input) {
var b64 = input.getAttribute("job")
var json = atob(b64)
return JSON.parse(json)
}
function getForm() {
return document.getElementById("upsert")
}
function getField(name) {
var form = getForm()
var matches = form.elements[name]
return matches
}

View File

@ -25,6 +25,7 @@ type Job struct {
LastOutput string
LastRuntime time.Duration
LastRun time.Time
Disabled bool
}
func NewJob(runner Runner, schedule, raw string) (*Job, error) {
@ -100,6 +101,7 @@ func (j *Job) Decode(b []byte) error {
k.LastOutput = j.LastOutput
k.LastRuntime = j.LastRuntime
k.LastRun = j.LastRun
k.Disabled = j.Disabled
*j = *k
}
return err

View File

@ -89,6 +89,9 @@ func TestJobEncodeDecode(t *testing.T) {
if k.Name != j.Name {
t.Error(k.Name, "vs", j.Name)
}
if k.Disabled != j.Disabled {
t.Error(k.Disabled, "vs", j.Disabled)
}
if k.Title != j.Title {
t.Error(k.Title, "vs", j.Title)
}

View File

@ -33,6 +33,7 @@ func (s *Server) list(w http.ResponseWriter, r *http.Request) {
if err != nil {
panic(err)
}
out[i]["disabled"] = j.Disabled
out[i]["id"] = j.Name
out[i]["title"] = j.Title
out[i]["cron"] = j.Schedule

View File

@ -13,11 +13,11 @@ func (s *Server) Routes() error {
handler http.HandlerFunc
}{
{
path: fmt.Sprintf("/upserts"),
handler: s.gzip(s.authenticate(s.upserts)),
path: fmt.Sprintf("/api/job/upsert"),
handler: s.gzip(s.authenticate(s.upsert)),
},
{
path: fmt.Sprintf("/list"),
path: fmt.Sprintf("/api/job/list"),
handler: s.gzip(s.authenticate(s.list)),
},
{

View File

@ -19,6 +19,7 @@ type upsertRequest struct {
Language string `json:"language"`
Cron string `json:"cron"`
Script string `json:"script"`
Disabled bool `json:"disabled"`
}
func newUpsertRequest(r io.Reader) (upsertRequest, error) {
@ -51,7 +52,7 @@ func (u *upsertRequest) validate() error {
return nil
}
func (s *Server) upserts(w http.ResponseWriter, r *http.Request) {
func (s *Server) upsert(w http.ResponseWriter, r *http.Request) {
upsert, err := newUpsertRequest(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)