package components
import "git.src.quest/~liljamo/emerwen-proto/go/proto/shared"
import "fmt"
import "strings"
func getMethodString(target *shared.Target) string {
switch target.Method.(type) {
case *shared.Target_Ping:
return "ping"
case *shared.Target_Get:
return "get"
default:
return "invalid"
}
}
templ PartialWorkers(workers []*shared.Worker) {
<div class="flex flex-col gap-2">
for _, worker := range workers {
<div class="flex flex-col bg-slate-100 p-2">
<div class="flex items-center gap-1">
<form class="flex items-center w-full" hx-confirm="update worker name?" hx-patch="/api/worker">
<input hidden name="id" value={ worker.Id }/>
<input class="border w-full" type="text" placeholder="..." name="name" value={ worker.Name }/>
<input type="submit" hidden/>
</form>
@trash("/api/worker", worker.Id, "delete worker?")
</div>
@smallDetails("id", worker.Id)
<div class="flex items-center gap-1">
<div class="min-w-max">auth token: </div>
<input class="border w-full" disabled type="password" value={ worker.AuthToken }
id={ fmt.Sprintf("auth_key_%s", worker.Id) }
/>
@eyeToggle(fmt.Sprintf("auth_key_%s", worker.Id))
</div>
<details>
<summary class="select-none">targets</summary>
<div class="flex flex-col gap-1 p-2">
@newTarget(worker.Id)
for _, target := range worker.Targets {
@editTarget(target)
}
</div>
</details>
</div>
}
</div>
}
script selectNewTargetMethod(worker_id string) {
var value = document.getElementById(worker_id + "_new_target_method_select").value;
var get_options = document.getElementById(worker_id + "_new_target_get_options");
if (value === "get") {
get_options.style.display = "block";
} else {
get_options.style.display = "none";
}
}
templ newTarget(worker_id string) {
<details>
<summary class="select-none">new target</summary>
<form class="flex flex-col p-2 gap-1" hx-post="/api/target">
<input hidden name="worker_id" value={ worker_id }/>
<label>name</label>
<input class="border" type="text" placeholder="..." name="name"/>
<label>addr</label>
<input class="border" type="text" placeholder="..." name="addr"/>
<label>interval (ms)</label>
<input class="border" type="text" placeholder="..." name="interval"/>
<div>
<label>method</label>
<select onchange={ selectNewTargetMethod(worker_id) } id={ worker_id + "_new_target_method_select" } name="method">
<option value="ping">ping</option>
<option value="get">get</option>
</select>
</div>
<div id={ worker_id + "_new_target_get_options"} style="display: none;">
<label>ok codes</label>
<input class="border" type="text" placeholder="..." name="ok_codes"/>
</div>
<button class="border p-1" type="submit">add</button>
</form>
</details>
}
templ editTarget(target *shared.Target) {
<form class="flex flex-col bg-slate-200 p-2" hx-confirm="update target?" hx-patch="/api/target">
<div class="flex items-center gap-1">
<input class="border w-full" type="text" placeholder="..." name="name" value={ target.Name }/>
@trash("/api/target", target.Id, "delete target?")
</div>
@smallDetails("id", target.Id)
<div class="flex items-center gap-1">
<label>addr:</label>
<input class="border w-full" type="text" placeholder="..." name="addr" value={ target.Addr }/>
</div>
<div class="flex items-center gap-1">
<label class="min-w-max">interval (ms):</label>
<input class="border w-full" type="number" placeholder="..." name="interval" value={ fmt.Sprint(target.Interval) }/>
</div>
switch m := target.Method.(type) {
case *shared.Target_Ping:
<div>method: ping</div>
case *shared.Target_Get:
<div>method: get</div>
<div class="flex items-center gap-1">
<label class="min-w-max">ok codes:</label>
<input class="border w-full" type="text" placeholder="..." name="ok_codes"
value={ strings.Trim(fmt.Sprint(m.Get.OkCodes), "[]") }/>
</div>
default:
<div>invalid</div>
}
<div class="flex flex-row-reverse">
<input hidden name="id" value={ target.Id }/>
@buttonSubmit()
</div>
</form>
}