feat(elixir): QR code generate & download function
This commit is contained in:
@@ -154,6 +154,28 @@ defmodule ComponentsElixirWeb.StorageLocationsLive do
|
||||
{:noreply, assign(socket, :scanned_codes, [])}
|
||||
end
|
||||
|
||||
def handle_event("download_qr", %{"id" => id}, socket) do
|
||||
case Inventory.get_storage_location!(id) do
|
||||
location ->
|
||||
case QRCode.generate_qr_image(location) do
|
||||
{:ok, png_data} ->
|
||||
filename = "#{location.name |> String.replace(" ", "_")}_QR.png"
|
||||
|
||||
# Send file download to browser
|
||||
{:noreply,
|
||||
socket
|
||||
|> push_event("download_file", %{
|
||||
filename: filename,
|
||||
data: Base.encode64(png_data),
|
||||
mime_type: "image/png"
|
||||
})}
|
||||
|
||||
{:error, _reason} ->
|
||||
{:noreply, put_flash(socket, :error, "Failed to generate QR code")}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp reload_storage_locations(socket) do
|
||||
storage_locations = list_storage_locations()
|
||||
assign(socket, :storage_locations, storage_locations)
|
||||
@@ -208,6 +230,10 @@ defmodule ComponentsElixirWeb.StorageLocationsLive do
|
||||
Inventory.count_components_in_storage_location(location_id)
|
||||
end
|
||||
|
||||
defp get_qr_image_url(location) do
|
||||
QRCode.get_qr_image_url(location)
|
||||
end
|
||||
|
||||
# Component for rendering individual storage location items with QR code support
|
||||
defp location_item(assigns) do
|
||||
# Calculate margin based on depth (0 = no margin, 1+ = incremental margin)
|
||||
@@ -243,46 +269,71 @@ defmodule ComponentsElixirWeb.StorageLocationsLive do
|
||||
~H"""
|
||||
<div class={[@border_class, @depth > 0 && "mt-4"]} style={"margin-left: #{@margin_left}px"}>
|
||||
<div class="flex items-center justify-between">
|
||||
<div class="flex-1">
|
||||
<div class="flex items-center">
|
||||
<.icon name={@icon_name} class={"#{@icon_size} #{if @depth == 0, do: "text-blue-500", else: "text-gray-400"} mr-3"} />
|
||||
<div>
|
||||
<%= if @title_tag == "h3" do %>
|
||||
<h3 class={"#{@text_size} font-medium #{if @depth == 0, do: "text-gray-900", else: "text-gray-700"}"}>{@location.name}</h3>
|
||||
<% else %>
|
||||
<h4 class={"#{@text_size} font-medium #{if @depth == 0, do: "text-gray-900", else: "text-gray-700"}"}>{@location.name}</h4>
|
||||
<div class="flex items-center flex-1 space-x-4">
|
||||
<.icon name={@icon_name} class={"#{@icon_size} #{if @depth == 0, do: "text-blue-500", else: "text-gray-400"}"} />
|
||||
<div class="flex-1">
|
||||
<%= if @title_tag == "h3" do %>
|
||||
<h3 class={"#{@text_size} font-medium #{if @depth == 0, do: "text-gray-900", else: "text-gray-700"}"}>{@location.name}</h3>
|
||||
<% else %>
|
||||
<h4 class={"#{@text_size} font-medium #{if @depth == 0, do: "text-gray-900", else: "text-gray-700"}"}>{@location.name}</h4>
|
||||
<% end %>
|
||||
<%= if @location.description do %>
|
||||
<p class="text-sm text-gray-500 mt-1">{@location.description}</p>
|
||||
<% end %>
|
||||
<div class="flex items-center space-x-2 mt-1">
|
||||
<p class="text-xs text-gray-400">
|
||||
{count_components_in_location(@location.id)} components
|
||||
</p>
|
||||
<%= if @location.qr_code do %>
|
||||
<span class="text-xs bg-gray-100 text-gray-600 px-2 py-1 rounded">
|
||||
QR: {@location.qr_code}
|
||||
</span>
|
||||
<% end %>
|
||||
<%= if @location.description do %>
|
||||
<p class="text-sm text-gray-500 mt-1">{@location.description}</p>
|
||||
<% end %>
|
||||
<div class="flex items-center space-x-2 mt-1">
|
||||
<p class="text-xs text-gray-400">
|
||||
{count_components_in_location(@location.id)} components
|
||||
</p>
|
||||
<%= if @location.qr_code do %>
|
||||
<span class="text-xs bg-gray-100 text-gray-600 px-2 py-1 rounded">
|
||||
QR: {@location.qr_code}
|
||||
</span>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<%= if @location.qr_code do %>
|
||||
<div class="flex items-center space-x-3">
|
||||
<%= if get_qr_image_url(@location) do %>
|
||||
<div class="qr-code-container flex-shrink-0">
|
||||
<img
|
||||
src={get_qr_image_url(@location)}
|
||||
alt={"QR Code for #{@location.name}"}
|
||||
class="w-16 h-16 border border-gray-200 rounded bg-white"
|
||||
onerror="this.style.display='none'"
|
||||
/>
|
||||
</div>
|
||||
<% else %>
|
||||
<div class="w-16 h-16 border border-gray-200 rounded bg-gray-50 flex items-center justify-center flex-shrink-0">
|
||||
<.icon name="hero-qr-code" class="w-8 h-8 text-gray-400" />
|
||||
</div>
|
||||
<% end %>
|
||||
<button
|
||||
phx-click="download_qr"
|
||||
phx-value-id={@location.id}
|
||||
class="inline-flex items-center px-3 py-1.5 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 flex-shrink-0"
|
||||
title="Download QR Code"
|
||||
>
|
||||
<.icon name="hero-arrow-down-tray" class="w-4 h-4 mr-1.5" />
|
||||
Download
|
||||
</button>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
<div class="flex items-center space-x-2">
|
||||
<div class="flex items-center space-x-2 ml-4">
|
||||
<button
|
||||
phx-click="show_edit_form"
|
||||
phx-value-id={@location.id}
|
||||
class={"inline-flex items-center #{@button_size} border border-transparent rounded-full shadow-sm text-white bg-blue-600 hover:bg-blue-700"}
|
||||
class="inline-flex items-center p-2 border border-transparent rounded-full shadow-sm text-white bg-blue-600 hover:bg-blue-700"
|
||||
>
|
||||
<.icon name="hero-pencil" class={@icon_size} />
|
||||
<.icon name="hero-pencil" class="w-4 h-4" />
|
||||
</button>
|
||||
<button
|
||||
phx-click="delete_location"
|
||||
phx-value-id={@location.id}
|
||||
data-confirm="Are you sure you want to delete this storage location? This action cannot be undone."
|
||||
class={"inline-flex items-center #{@button_size} border border-transparent rounded-full shadow-sm text-white bg-red-600 hover:bg-red-700"}
|
||||
class="inline-flex items-center p-2 border border-transparent rounded-full shadow-sm text-white bg-red-600 hover:bg-red-700"
|
||||
>
|
||||
<.icon name="hero-trash" class={@icon_size} />
|
||||
<.icon name="hero-trash" class="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user