|
5 | 5 | import HeadTitle from '$lib/common/HeadTitle.svelte'; |
6 | 6 | import CardAgent from './card-agent.svelte'; |
7 | 7 | import LoadingToComplete from '$lib/common/LoadingToComplete.svelte'; |
8 | | - import { createAgent, getAgents } from '$lib/services/agent-service.js'; |
| 8 | + import { createAgent, getAgentLabels, getAgents } from '$lib/services/agent-service.js'; |
9 | 9 | import { myInfo } from '$lib/services/auth-service'; |
10 | 10 | import PlainPagination from '$lib/common/PlainPagination.svelte'; |
11 | 11 | import { _ } from 'svelte-i18n' |
|
43 | 43 | /** @type {any} */ |
44 | 44 | let unsubscriber; |
45 | 45 |
|
46 | | - let agentTypeOptions = Object.entries(AgentType).map(([k, v]) => ( |
47 | | - { key: k, value: v } |
| 46 | + const agentTypeOptions = Object.entries(AgentType).map(([k, v]) => ( |
| 47 | + { key: v, value: v } |
48 | 48 | )); |
49 | 49 |
|
| 50 | + /** @type {{ key: string, value: string }[]} */ |
| 51 | + let agentLabelOptions = []; |
| 52 | +
|
50 | 53 | /** @type {string[]} */ |
51 | 54 | let selectedAgentTypes = []; |
| 55 | + /** @type {string[]} */ |
| 56 | + let selectedAgentLabels = []; |
52 | 57 |
|
53 | 58 | onMount(async () => { |
54 | 59 | user = await myInfo(); |
55 | 60 | getPagedAgents(); |
| 61 | + getAgentLabelOptions(); |
56 | 62 |
|
57 | 63 | unsubscriber = globalEventStore.subscribe((/** @type {import('$commonTypes').GlobalEvent} */ event) => { |
58 | 64 | if (event.name !== GlobalEvent.Search) return; |
|
82 | 88 | }); |
83 | 89 | } |
84 | 90 |
|
| 91 | + function getAgentLabelOptions() { |
| 92 | + return getAgentLabels().then(res => { |
| 93 | + agentLabelOptions = res?.map(x => ({ key: x, value: x })) || []; |
| 94 | + }).catch(() => { |
| 95 | + agentLabelOptions = []; |
| 96 | + }); |
| 97 | + } |
| 98 | +
|
85 | 99 | function createNewAgent() { |
86 | 100 | // @ts-ignore |
87 | 101 | Swal.fire({ |
|
147 | 161 |
|
148 | 162 | getPagedAgents(); |
149 | 163 | } |
150 | | -
|
151 | 164 | |
152 | | - /** |
153 | | - * @param {any} e |
154 | | - */ |
| 165 | + /** @param {any} e */ |
155 | 166 | function selectAgentTypeOption(e) { |
156 | 167 | // @ts-ignore |
157 | | - selectedAgentTypes = e.detail.selecteds?.map(x => x.value) || []; |
| 168 | + selectedAgentTypes = e.detail.selecteds?.map(x => x.key) || []; |
| 169 | + } |
| 170 | +
|
| 171 | + /** @param {any} e */ |
| 172 | + function selectAgentLabelOption(e) { |
| 173 | + // @ts-ignore |
| 174 | + selectedAgentLabels = e.detail.selecteds?.map(x => x.key) || []; |
| 175 | + } |
| 176 | +
|
| 177 | + function search() { |
| 178 | + refreshFilter(); |
| 179 | + initFilterPager(); |
| 180 | + getPagedAgents(); |
| 181 | + } |
| 182 | +
|
| 183 | + function reset() { |
| 184 | + selectedAgentTypes = []; |
| 185 | + selectedAgentLabels = []; |
158 | 186 | } |
159 | 187 |
|
160 | | - function searchAgents() { |
| 188 | + function refreshFilter() { |
161 | 189 | filter = { |
162 | 190 | ...filter, |
163 | 191 | types: selectedAgentTypes?.length > 0 ? selectedAgentTypes : null, |
| 192 | + labels: selectedAgentLabels?.length > 0 ? selectedAgentLabels : null, |
164 | 193 | pager: initFilter.pager |
165 | 194 | }; |
166 | | - getPagedAgents(); |
| 195 | + } |
| 196 | +
|
| 197 | + function initFilterPager() { |
| 198 | + filter = { |
| 199 | + ...filter, |
| 200 | + pager: { page: firstPage, size: pageSize, count: 0 }, |
| 201 | + }; |
167 | 202 | } |
168 | 203 | </script> |
169 | 204 |
|
|
182 | 217 | <div class="agent-filter"> |
183 | 218 | <MultiSelect |
184 | 219 | tag={'agent-label-select'} |
185 | | - placeholder={'Select agent labels'} |
| 220 | + placeholder={'Select labels'} |
186 | 221 | selectedText={'labels'} |
187 | | - options={[]} |
188 | | - on:select={e => {}} |
| 222 | + searchMode |
| 223 | + selectedKeys={selectedAgentLabels} |
| 224 | + options={agentLabelOptions} |
| 225 | + on:select={e => selectAgentLabelOption(e)} |
189 | 226 | /> |
190 | 227 | <MultiSelect |
191 | 228 | tag={'agent-type-select'} |
192 | | - placeholder={'Select agent types'} |
| 229 | + placeholder={'Select types'} |
193 | 230 | selectedText={'types'} |
| 231 | + selectedKeys={selectedAgentTypes} |
194 | 232 | options={agentTypeOptions} |
195 | 233 | on:select={e => selectAgentTypeOption(e)} |
196 | 234 | /> |
197 | 235 | <Button |
198 | | - class="btn btn-light" |
199 | | - on:click={(e) => searchAgents()} |
| 236 | + class="btn btn-info" |
| 237 | + data-bs-toggle="tooltip" |
| 238 | + data-bs-placement="bottom" |
| 239 | + title="Search" |
| 240 | + on:click={(e) => search()} |
200 | 241 | > |
201 | 242 | <i class="mdi mdi-magnify" /> |
202 | 243 | </Button> |
| 244 | + <Button |
| 245 | + class="btn btn-light" |
| 246 | + data-bs-toggle="tooltip" |
| 247 | + data-bs-placement="bottom" |
| 248 | + title="Reset filters" |
| 249 | + on:click={(e) => reset()} |
| 250 | + > |
| 251 | + <i class="mdi mdi-restore" /> |
| 252 | + </Button> |
203 | 253 | </div> |
204 | 254 | </div> |
205 | 255 |
|
|
0 commit comments