Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ defmodule BikeBrigadeWeb.CommunityFridgeLive.FormComponent do
{:noreply,
socket
|> put_flash(:info, "Community fridge updated successfully")
|> push_navigate(to: socket.assigns.navigate)}
|> push_patch(to: socket.assigns.navigate)}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
Expand All @@ -83,7 +83,7 @@ defmodule BikeBrigadeWeb.CommunityFridgeLive.FormComponent do
{:noreply,
socket
|> put_flash(:info, "Community fridge created successfully")
|> push_navigate(to: socket.assigns.navigate)}
|> push_patch(to: socket.assigns.navigate)}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
Expand Down
26 changes: 22 additions & 4 deletions lib/bike_brigade_web/live/community_fridge_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,44 @@ defmodule BikeBrigadeWeb.CommunityFridgeLive.Index do
|> assign(:community_fridge, nil)
|> assign(:community_fridges, Locations.list_community_fridges())
|> assign(:map_layers, [])
|> assign(:map_coords, @default_coords)}
|> assign(:map_coords, @default_coords)
|> assign(:mode, :list)}
end

@impl Phoenix.LiveView
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end

@impl Phoenix.LiveView
def handle_event("set_mode", %{"mode" => mode}, socket) do
socket = assign(socket, :mode, String.to_existing_atom(mode))

socket =
if socket.assigns.mode == :map do
push_event(socket, "leaflet:redraw_map", %{recenter: false})
else
socket
end

{:noreply, socket}
end

defp apply_action(socket, :index, _params) do
community_fridges = Locations.list_community_fridges()

socket
|> assign(:page_title, "Community Fridges")
|> assign(:community_fridge, nil)
|> assign(:map_layers, fridge_markers(socket.assigns.community_fridges))
|> assign(:map_coords, map_center(socket.assigns.community_fridges))
|> assign(:community_fridges, community_fridges)
|> assign(:map_layers, fridge_markers(community_fridges))
|> assign(:map_coords, map_center(community_fridges))
end

defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Fridge")
|> assign(:community_fridge, %CommunityFridge{})
|> assign(:community_fridge, %CommunityFridge{location: nil})
end

defp apply_action(socket, :edit, %{"id" => id}) do
Expand Down
83 changes: 53 additions & 30 deletions lib/bike_brigade_web/live/community_fridge_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,64 @@
Community fridges are neighbourhood food-sharing locations. Dispatchers can manage fridge details here.
</:subtitle>
</.header>
<.map
id="community-fridges-map"
coords={@map_coords}
initial_layers={@map_layers}
class="w-full h-64 mb-4"
/>

<div class="flex justify-end">
<div class="flex justify-end items-center gap-2 mb-2">
<div class="inline-flex rounded-md shadow-sm">
<button
phx-click={JS.push("set_mode", value: %{mode: :list})}
type="button"
class={"#{if @mode == :list, do: "bg-gray-300", else: "bg-white"} relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-700 border border-gray-300 rounded-l-md hover:bg-gray-200 focus:z-10 focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500"}
>
<Heroicons.table_cells mini class="w-5 h-5 mr-1" /> List
</button>
<button
phx-click={JS.push("set_mode", value: %{mode: :map})}
type="button"
class={"#{if @mode == :map, do: "bg-gray-300", else: "bg-white"} relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-700 border border-gray-300 rounded-r-md hover:bg-gray-200 focus:z-10 focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500"}
>
Map <Heroicons.map mini class="w-5 h-5 ml-1" />
</button>
</div>
<.button patch={~p"/community_fridges/new"}>
New Community Fridge
</.button>
</div>
<.table id="community_fridges" rows={@community_fridges}>
<:col :let={community_fridge} label="Name">
{community_fridge.name}
</:col>
<:col :let={community_fridge} label="Address">
{community_fridge.location}
</:col>
<:col :let={community_fridge} label="Description">
{community_fridge.description}
</:col>
<:col :let={community_fridge} label="Pair Preferred">
{if community_fridge.pair_preferred, do: "Yes", else: "No"}
</:col>
<:col :let={community_fridge} label="Active">
{if community_fridge.active, do: "Yes", else: "No"}
</:col>

<:action :let={community_fridge}>
<.link patch={~p"/community_fridges/#{community_fridge.id}/edit"} class="link">
Edit
</.link>
</:action>
</.table>
<%= if @mode == :map do %>
<div class="min-w-full bg-white rounded-lg shadow">
<div class="p-1 h-[80vh]">
<.map
id="community-fridges-map"
coords={@map_coords}
initial_layers={@map_layers}
class="h-full"
/>
</div>
</div>
<% else %>
<.table id="community_fridges" rows={@community_fridges}>
<:col :let={community_fridge} label="Name">
{community_fridge.name}
</:col>
<:col :let={community_fridge} label="Address">
{community_fridge.location}
</:col>
<:col :let={community_fridge} label="Description">
{community_fridge.description}
</:col>
<:col :let={community_fridge} label="Pair Preferred">
{if community_fridge.pair_preferred, do: "Yes", else: "No"}
</:col>
<:col :let={community_fridge} label="Active">
{if community_fridge.active, do: "Yes", else: "No"}
</:col>

<:action :let={community_fridge}>
<.link patch={~p"/community_fridges/#{community_fridge.id}/edit"} class="link">
Edit
</.link>
</:action>
</.table>
<% end %>

<.modal
:if={@live_action in [:new, :edit]}
Expand Down