package components
import "git.src.quest/~liljamo/emerwen-proto/go/proto/shared"
import "fmt"
import "strings"
func getMethodString(target *shared.Target) string {
return shared.Method_name[int32(target.Method)]
}
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.WorkerId }/>
<input class="border w-full" type="text" placeholder="..." name="name" value={ worker.Name }/>
<input type="submit" hidden/>
</form>
@trash("/api/worker", worker.WorkerId, "delete worker?")
</div>
@smallDetails("id", worker.WorkerId)
<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.WorkerId) }
/>
@eyeToggle(fmt.Sprintf("auth_key_%s", worker.WorkerId))
</div>
<details>
<summary class="select-none">targets</summary>
<div class="flex flex-col gap-1 p-2">
@newTarget(worker.WorkerId)
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.TargetId, "delete target?")
</div>
@smallDetails("id", target.TargetId)
<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 target.Method {
case shared.Method_PING:
<div>method: ping</div>
case shared.Method_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(target.OkCodes), "[]") }/>
</div>
default:
<div>invalid</div>
}
<div class="flex flex-row-reverse">
<input hidden name="id" value={ target.TargetId }/>
@buttonSubmit()
</div>
</form>
}