DEVELOPMENT ENVIRONMENT

~liljamo/emerwen-web

ref: 4e555b1e3309881de4969794eff19e5275430069 emerwen-web/internal/components/partials.templ -rw-r--r-- 4.1 KiB
4e555b1eJonni Liljamo feat: UI for new/patch/delete worker/target 11 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
119
120
121
122
123
124
125
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>
}