@@ -48,38 +48,71 @@ defmodule Plausible.CrmExtensions do
4848 ]
4949 end
5050
51- def javascripts ( % { assigns: % { context: context } } )
52- when context in [ "sites" , "billing" ] do
51+ def javascripts ( % { assigns: % { context: "billing" , resource: "enterprise_plan" , changeset: % { } } } ) do
5352 [
5453 Phoenix.HTML . raw ( """
5554 <script type="text/javascript">
56- (() => {
57- const publicField = document.querySelector("#kaffy-search-field")
58- const searchForm = document.querySelector("#kaffy-filters-form")
59- const searchField = document.querySelector("#kaffy-filter-search")
55+ (async () => {
56+ const CHECK_INTERVAL = 300
57+ const userIdField = document.querySelector("#enterprise_plan_user_id")
58+ const userIdLabel = document.querySelector("label[for=enterprise_plan_user_id]")
59+ const dataList = document.createElement("datalist")
60+ dataList.id = "user-choices"
61+ userIdField.after(dataList)
62+ userIdField.setAttribute("list", "user-choices")
63+ userIdField.setAttribute("type", "text")
64+ const labelSpan = document.createElement("span")
65+ userIdLabel.appendChild(labelSpan)
66+
67+ let updateAction;
68+
69+ const updateLabel = async (id) => {
70+ id = Number(id)
71+
72+ if (!isNaN(id) && id > 0) {
73+ const response = await fetch(`/crm/billing/search/user-by-id/${id}`)
74+ labelSpan.innerHTML = ` <i>(${await response.text()})</i>`
75+ }
76+ }
6077
61- if (publicField && searchForm && searchField) {
62- publicField.name = "#{ @ custom_search } "
63- searchField.name = "#{ @ custom_search } "
78+ const updateSearch = async () => {
79+ const search = userIdField.value
6480
65- const params = new URLSearchParams(window.location.search)
66- publicField.value = params.get("#{ @ custom_search } ")
81+ updateLabel(search)
6782
68- const searchInput = document.createElement("input")
69- searchInput.name = "search"
70- searchInput.type = "hidden"
71- searchInput.value = ""
83+ const response = await fetch("/crm/billing/search/user", {
84+ headers: { "Content-Type": "application/json" },
85+ method: "POST",
86+ body: JSON.stringify({ search: search })
87+ })
7288
73- searchForm.appendChild(searchInput)
89+ const list = await response.json()
90+
91+ const options =
92+ list.map(([label, value]) => {
93+ const option = document.createElement("option")
94+ option.setAttribute("label", label)
95+ option.textContent = value
96+
97+ return option
98+ })
99+
100+ dataList.replaceChildren(...options)
74101 }
102+
103+ updateLabel(userIdField.value)
104+
105+ userIdField.addEventListener("input", async (e) => {
106+ if (updateAction) {
107+ clearTimeout(updateAction)
108+ updateAction = null
109+ }
110+
111+ updateAction = setTimeout(() => updateSearch(), CHECK_INTERVAL)
112+ })
75113 })()
76114 </script>
77- """ )
78- ]
79- end
80-
81- def javascripts ( % { assigns: % { context: "billing" , resource: "enterprise_plan" , changeset: % { } } } ) do
82- [
115+ """ ) ,
83116 Phoenix.HTML . raw ( """
84117 <script type="text/javascript">
85118 (() => {
@@ -153,6 +186,36 @@ defmodule Plausible.CrmExtensions do
153186 """ )
154187 ]
155188 end
189+
190+ def javascripts ( % { assigns: % { context: context } } )
191+ when context in [ "sites" , "billing" ] do
192+ [
193+ Phoenix.HTML . raw ( """
194+ <script type="text/javascript">
195+ (() => {
196+ const publicField = document.querySelector("#kaffy-search-field")
197+ const searchForm = document.querySelector("#kaffy-filters-form")
198+ const searchField = document.querySelector("#kaffy-filter-search")
199+
200+ if (publicField && searchForm && searchField) {
201+ publicField.name = "#{ @ custom_search } "
202+ searchField.name = "#{ @ custom_search } "
203+
204+ const params = new URLSearchParams(window.location.search)
205+ publicField.value = params.get("#{ @ custom_search } ")
206+
207+ const searchInput = document.createElement("input")
208+ searchInput.name = "search"
209+ searchInput.type = "hidden"
210+ searchInput.value = ""
211+
212+ searchForm.appendChild(searchInput)
213+ }
214+ })()
215+ </script>
216+ """ )
217+ ]
218+ end
156219 end
157220
158221 def javascripts ( _ ) do
0 commit comments