DEVELOPMENT ENVIRONMENT

~liljamo/emerwen-web

ref: 797037fe5da9b632166a17b9e7f57ec2ae0d35e3 emerwen-web/internal/components/partials.templ -rw-r--r-- 4.1 KiB
797037feJonni Liljamo feat: conform to new protocol changes 22 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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>
}