noahsettersten
commited on
Commit
•
3280f20
1
Parent(s):
aeeeee4
feat: Search for codes by code or description
Browse files
lib/medical_transcription/coding.ex
CHANGED
@@ -35,10 +35,14 @@ defmodule MedicalTranscription.Coding do
|
|
35 |
num_rows >= 14_567
|
36 |
end
|
37 |
|
38 |
-
def
|
39 |
-
|
40 |
-
|
41 |
-
Repo.all(
|
|
|
|
|
|
|
|
|
42 |
end
|
43 |
|
44 |
@doc """
|
|
|
35 |
num_rows >= 14_567
|
36 |
end
|
37 |
|
38 |
+
def search_for_code_vector(""), do: []
|
39 |
+
|
40 |
+
def search_for_code_vector(text) when is_binary(text) do
|
41 |
+
Repo.all(
|
42 |
+
from v in CodeVector,
|
43 |
+
where: ilike(v.code, ^"%#{text}%") or ilike(v.description, ^"%#{text}%"),
|
44 |
+
limit: 5
|
45 |
+
)
|
46 |
end
|
47 |
|
48 |
@doc """
|
lib/medical_transcription_web/components/code_select.ex
CHANGED
@@ -10,37 +10,44 @@ defmodule MedicalTranscriptionWeb.Components.CodeSelect do
|
|
10 |
def mount(socket) do
|
11 |
socket =
|
12 |
socket
|
|
|
13 |
|> assign(:codes, [])
|
14 |
|> assign(:selected_code, nil)
|
15 |
|
16 |
{:ok, socket}
|
17 |
end
|
18 |
|
19 |
-
@impl Phoenix.LiveComponent
|
20 |
-
def update_many(assigns_sockets) do
|
21 |
-
codes = lookup_codes()
|
22 |
-
|
23 |
-
Enum.map(assigns_sockets, fn {_assigns, socket} ->
|
24 |
-
assign(socket, :codes, codes)
|
25 |
-
end)
|
26 |
-
end
|
27 |
-
|
28 |
@impl Phoenix.LiveComponent
|
29 |
def render(assigns) do
|
30 |
~H"""
|
31 |
<div>
|
32 |
<%= if is_nil(@selected_code) do %>
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
<% else %>
|
45 |
<p class="leading-normal font-bold text-type-black-primary uppercase">
|
46 |
User-selected code
|
@@ -60,13 +67,16 @@ defmodule MedicalTranscriptionWeb.Components.CodeSelect do
|
|
60 |
end
|
61 |
|
62 |
@impl Phoenix.LiveComponent
|
63 |
-
def handle_event("
|
64 |
-
|
65 |
|
66 |
-
{:noreply, assign(socket, :
|
67 |
end
|
68 |
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
71 |
end
|
72 |
end
|
|
|
10 |
def mount(socket) do
|
11 |
socket =
|
12 |
socket
|
13 |
+
|> assign(:form, to_form(%{"search_term" => ""}))
|
14 |
|> assign(:codes, [])
|
15 |
|> assign(:selected_code, nil)
|
16 |
|
17 |
{:ok, socket}
|
18 |
end
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
@impl Phoenix.LiveComponent
|
21 |
def render(assigns) do
|
22 |
~H"""
|
23 |
<div>
|
24 |
<%= if is_nil(@selected_code) do %>
|
25 |
+
<.form for={@form} phx-change="suggest-code" phx-target={@myself}>
|
26 |
+
<.input
|
27 |
+
type="text"
|
28 |
+
field={@form[:search_term]}
|
29 |
+
class="w-full rounded"
|
30 |
+
style={if length(@codes) > 0, do: "border-radius: 0.25rem 0.25rem 0 0 !important;"}
|
31 |
+
placeholder="Search for another code..."
|
32 |
+
phx-change="suggest-code"
|
33 |
+
phx-target={@myself}
|
34 |
+
/>
|
35 |
+
</.form>
|
36 |
+
|
37 |
+
<%= if length(@codes) > 0 do %>
|
38 |
+
<div class="border-l border-r border-b border-zinc-400">
|
39 |
+
<%= for code <- @codes do %>
|
40 |
+
<p
|
41 |
+
class="px-2 py-1 text-sm border-b border-zinc-100 hover:bg-blue-200"
|
42 |
+
phx-click="choose-code"
|
43 |
+
phx-value-code={code.code}
|
44 |
+
phx-target={@myself}
|
45 |
+
>
|
46 |
+
<%= code.code %>: <%= code.description %>
|
47 |
+
</p>
|
48 |
+
<% end %>
|
49 |
+
</div>
|
50 |
+
<% end %>
|
51 |
<% else %>
|
52 |
<p class="leading-normal font-bold text-type-black-primary uppercase">
|
53 |
User-selected code
|
|
|
67 |
end
|
68 |
|
69 |
@impl Phoenix.LiveComponent
|
70 |
+
def handle_event("suggest-code", %{"search_term" => text}, socket) do
|
71 |
+
suggested_codes = MedicalTranscription.Coding.search_for_code_vector(text)
|
72 |
|
73 |
+
{:noreply, assign(socket, :codes, suggested_codes)}
|
74 |
end
|
75 |
|
76 |
+
@impl Phoenix.LiveComponent
|
77 |
+
def handle_event("choose-code", %{"code" => code}, socket) do
|
78 |
+
selected_code = Enum.find(socket.assigns.codes, &(&1.code == code))
|
79 |
+
|
80 |
+
{:noreply, assign(socket, :selected_code, selected_code)}
|
81 |
end
|
82 |
end
|