defmodule CartosnapAppWeb.TripLive.Show do
@moduledoc """
One trip: everyone's photos in it on a map that fills the width, with the
photos themselves beside it, as on `/map`. Hovering a photo lifts its pin.
The trip is an album its people fill together: the owner and the members
they've invited each add their own photos (Add photos uploads straight
into it). The people panel shows who's in and lets anyone in the trip
invite more (by address, a suggestion from their other trips, or a link);
the owner removes people, members can leave. The owner also shares the album by
public link (`CartosnapAppWeb.ShareLive`).
With areas on (the owner's toggle; `CartosnapApp.Trips.Areas`), the
album is split by where the photos were taken: each area is ringed and
named on the map, and picking one (on the map or in the strip above it;
`?area=<n>`) narrows the photos and the map to it. Names are looked up
in the background the first time a place is seen.
Everything here follows the trip's topic (`Trips.topic/1`): photos added,
changed or taken out by anyone, and people coming and going.
"""
use CartosnapAppWeb, :live_view
import CartosnapAppWeb.PhotoComponents
import CartosnapAppWeb.Components.PhotoMap
alias CartosnapApp.Accounts
alias CartosnapApp.Photos.Photo
alias CartosnapApp.Trips
alias CartosnapApp.Trips.Areas
alias CartosnapAppWeb.Components.PhotoMap
alias CartosnapAppWeb.TripLive
@impl true
def mount(%{"id" => id}, _session, socket) do
scope = socket.assigns.current_scope
trip = Trips.get_trip!(scope, id)
if connected?(socket), do: Trips.subscribe(trip)
{:ok,
socket
|> assign(:page_title, trip.name)
|> assign(:trip, trip)
|> assign(:owner?, Trips.owner?(scope, trip))
|> assign(:panel, nil)
|> assign(:new_invite_url, nil)
|> assign(:area_id, nil)
|> assign(:area_names, %{})
|> assign(:naming, MapSet.new())
|> assign(:asked, MapSet.new())
|> load_people()}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, socket |> assign(:area_id, params["area"]) |> load_photos()}
end
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope} active={:trips} wide>
<div class="flex h-[calc(100dvh-4rem)] flex-col">
<div class="flex flex-wrap items-center justify-between gap-3 border-b border-base-300 px-4 py-3 sm:px-6">
<div class="flex min-w-0 items-center gap-2">
<.link
navigate={~p"/trips"}
class="btn btn-ghost btn-sm btn-square"
id="back-to-trips"
aria-label="Trips"
>
<.icon name="hero-arrow-left-micro" class="size-4" />
</.link>
<div class="min-w-0">
<h1 class="truncate text-lg font-bold tracking-tight">{@trip.name}</h1>
<p class="text-xs text-base-content/60">
{TripLive.Index.date_range(@trip)} · {@count} {if @count == 1,
do: "photo",
else: "photos"}
<span :if={!@owner?}>· {owner_name(@people)}'s trip</span>
</p>
</div>
</div>
<div class="flex flex-wrap items-center gap-1">
<.link
navigate={~p"/upload?#{[trip: @trip.id]}"}
class="btn btn-primary btn-sm"
id="add-photos"
>
<.icon name="hero-arrow-up-tray-micro" class="size-4" /> Add photos
</.link>
<button
type="button"
id="toggle-people"
phx-click="panel"
phx-value-panel="people"
aria-expanded={to_string(@panel == :people)}
class={["btn btn-ghost btn-sm gap-1.5", @panel == :people && "btn-active"]}
>
<.icon name="hero-user-group-micro" class="size-4" />
<span class="flex -space-x-1.5">
<span
:for={person <- Enum.take(@people, 4)}
class="grid size-5 place-items-center rounded-full bg-neutral text-[9px] font-bold text-neutral-content ring-2 ring-base-100"
title={Accounts.display_name(person.user)}
>
{person.user |> Accounts.display_name() |> String.first() |> String.upcase()}
</span>
</span>
{length(@people)}
</button>
<%= if @owner? do %>
<button
type="button"
id="toggle-areas"
phx-click="toggle-areas"
aria-pressed={to_string(@trip.areas)}
title={
if @trip.areas,
do: "Areas on: the album is split by where photos were taken",
else: "Split the album into areas by where photos were taken"
}
class={[
"btn btn-sm gap-1.5",
if(@trip.areas, do: "btn-primary btn-soft", else: "btn-ghost")
]}
>
<.icon name="hero-map-micro" class="size-4" /> Areas
</button>
<button
type="button"
id="toggle-share"
phx-click="panel"
phx-value-panel="share"
aria-expanded={to_string(@panel == :share)}
class={[
"btn btn-sm gap-1.5",
if(@trip.share_token, do: "btn-primary btn-soft", else: "btn-ghost")
]}
>
<.icon name="hero-share-micro" class="size-4" />
{if @trip.share_token, do: "Shared", else: "Share"}
</button>
<button
type="button"
id="toggle-public"
phx-click="panel"
phx-value-panel="public"
aria-expanded={to_string(@panel == :public)}
class={[
"btn btn-sm gap-1.5",
if(@trip.public, do: "btn-success btn-soft", else: "btn-ghost")
]}
>
<.icon
name={if @trip.public, do: "hero-globe-alt-micro", else: "hero-lock-closed-micro"}
class="size-4"
/>
{if @trip.public, do: "Public", else: "Private"}
</button>
<.link navigate={~p"/trips/#{@trip}/edit"} class="btn btn-ghost btn-sm" id="edit-trip">
<.icon name="hero-pencil-square-micro" class="size-4" /> Edit
</.link>
<button
type="button"
id="delete-trip"
phx-click="delete"
data-confirm="Delete this trip? The photos stay; they're just no longer filed under it."
class="btn btn-ghost btn-sm text-error"
>
<.icon name="hero-trash-micro" class="size-4" /> Delete
</button>
<% end %>
</div>
</div>
<.people_panel
:if={@panel == :people}
people={@people}
owner?={@owner?}
me={@current_scope.user.id}
invites={@invites}
new_invite_url={@new_invite_url}
invite_form={@invite_form}
suggestions={@suggestions}
scope={@current_scope}
trip={@trip}
/>
<.share_panel :if={@panel == :share and @owner?} trip={@trip} />
<.public_panel :if={@panel == :public and @owner?} trip={@trip} />
<nav
:if={@areas != []}
id="trip-areas"
aria-label="Areas"
class="flex items-center gap-1.5 overflow-x-auto border-b border-base-300 px-4 py-2 sm:px-6"
>
<.link
patch={~p"/trips/#{@trip}"}
id="area-all"
aria-current={is_nil(@area) && "true"}
class={[
"btn btn-xs shrink-0 gap-1 rounded-full",
if(is_nil(@area), do: "btn-primary", else: "btn-ghost")
]}
>
All <span class="opacity-60">{@count}</span>
</.link>
<.link
:for={area <- @areas}
patch={~p"/trips/#{@trip}?#{[area: area.id]}"}
id={"area-#{area.id}"}
aria-current={@area && @area.id == area.id && "true"}
class={[
"btn btn-xs shrink-0 gap-1 rounded-full font-medium transition",
if(@area && @area.id == area.id, do: "btn-primary", else: "btn-ghost")
]}
>
<span class={[
"grid size-4 place-items-center rounded-full text-[9px] font-bold",
if(@area && @area.id == area.id,
do: "bg-primary-content/20",
else: "bg-base-300"
)
]}>
{area.index}
</span>
{area.name}
<span class="opacity-60">{area.count}</span>
</.link>
<span
:if={MapSet.size(@naming) > 0}
id="areas-naming"
class="loading loading-dots loading-xs shrink-0 opacity-50"
aria-label="Naming areas"
></span>
</nav>
<div class="grid min-h-0 flex-1 grid-rows-[minmax(0,1fr)_auto] lg:grid-cols-[minmax(0,1fr)_22rem] lg:grid-rows-1">
<div class="relative min-h-72">
<.photo_map
id="trip-map"
points={@points}
focus_from="#trip-photos"
areas={PhotoMap.areas(if @area, do: [@area], else: @areas)}
area_event="select-area"
class="h-full"
rounded={false}
empty_label={
if @count == 0,
do: "No photos in this trip yet",
else: "None of these photos has a location"
}
label={"Map of #{@trip.name}"}
/>
</div>
<aside class="flex max-h-[40dvh] min-h-0 flex-col border-t border-base-300 bg-base-100 lg:max-h-none lg:border-l lg:border-t-0">
<div
id="trip-photos"
phx-update="stream"
class="grid min-h-0 flex-1 auto-rows-min grid-cols-3 gap-1.5 overflow-y-auto p-3 lg:grid-cols-2"
>
<div
id="trip-photos-empty"
class="col-span-full hidden py-10 text-center text-sm text-base-content/60 only:block"
>
No photos in this trip yet.
<.link navigate={~p"/upload?#{[trip: @trip.id]}"} class="link link-primary">Add some</.link>
</div>
<.link
:for={{dom_id, photo} <- @streams.photos}
id={dom_id}
navigate={~p"/photos/#{photo}?#{[trip: @trip.id]}"}
data-photo-id={photo.id}
class="group relative block overflow-hidden rounded-field"
>
<.photo_image
id={"#{dom_id}-img"}
photo={photo}
class="aspect-square transition duration-300 group-hover:scale-105"
/>
<.promoted_badge photo={photo} class="absolute right-1 top-1 size-5" />
<.shape_badge photo={photo} class="absolute bottom-1 right-1" />
<span
:if={photo.user != @current_scope.user.id}
class="absolute left-1 top-1 rounded-full bg-black/55 px-1.5 py-0.5 text-[10px] font-medium text-white backdrop-blur"
title={"Added by #{@names[photo.user]}"}
>
{@names[photo.user]}
</span>
<span class="pointer-events-none absolute inset-x-0 bottom-0 truncate bg-gradient-to-t from-black/70 to-transparent px-1.5 pb-1 pt-4 text-[10px] text-white opacity-0 transition group-hover:opacity-100">
{Photo.title(photo)}
</span>
</.link>
</div>
<p
:if={@unmapped > 0}
id="trip-unmapped"
class="border-t border-base-300 px-4 py-2 text-xs text-base-content/60"
>
{@unmapped} {if @unmapped == 1, do: "photo has", else: "photos have"} no location.
</p>
</aside>
</div>
</div>
</Layouts.app>
"""
end
attr :people, :list, required: true
attr :owner?, :boolean, required: true
attr :me, :string, required: true
attr :invites, :list, required: true
attr :new_invite_url, :string, default: nil
attr :invite_form, :any, required: true
attr :suggestions, :list, required: true
attr :scope, :any, required: true
attr :trip, :map, required: true
defp people_panel(assigns) do
~H"""
<div id="people-panel" class="border-b border-base-300 bg-base-200/60 px-4 py-4 sm:px-6">
<div class="grid gap-5 md:grid-cols-[minmax(0,1fr)_minmax(0,1fr)]">
<div>
<h2 class="mb-2 text-xs font-semibold uppercase tracking-wider text-base-content/60">
In this trip
</h2>
<ul class="space-y-1">
<li
:for={person <- @people}
id={"person-#{person.user.id}"}
class="flex items-center justify-between gap-2 rounded-field px-2 py-1 hover:bg-base-100"
>
<span class="flex min-w-0 items-center gap-2">
<span class="grid size-7 shrink-0 place-items-center rounded-full bg-neutral text-xs font-bold text-neutral-content">
{person.user |> Accounts.display_name() |> String.first() |> String.upcase()}
</span>
<span class="truncate text-sm">{Accounts.display_name(person.user)}</span>
<span :if={person.owner?} class="badge badge-warning badge-soft badge-sm">owner</span>
<span :if={person.user.id == @me} class="text-xs text-base-content/50">(you)</span>
</span>
<button
:if={@owner? and not person.owner?}
type="button"
id={"remove-#{person.user.id}"}
phx-click="remove-member"
phx-value-id={person.user.id}
data-confirm={"Remove #{Accounts.display_name(person.user)}? Their photos leave this album with them."}
class="btn btn-ghost btn-xs text-error"
>
Remove
</button>
<button
:if={person.user.id == @me and not person.owner?}
type="button"
id="leave-trip"
phx-click="leave"
data-confirm="Leave this trip? Your photos leave its album with you."
class="btn btn-ghost btn-xs text-error"
>
Leave
</button>
</li>
</ul>
</div>
<div id="invites" class="space-y-3">
<h2 class="text-xs font-semibold uppercase tracking-wider text-base-content/60">
Invite friends
</h2>
<p class="text-sm text-base-content/70">
Members add their own photos to this album. Each invite lets one person in and lasts a week.
</p>
<.form
for={@invite_form}
id="invite-form"
phx-submit="invite"
phx-change="validate-invite"
class="flex items-start gap-2"
>
<div class="min-w-0 flex-1">
<.input
field={@invite_form[:email]}
type="email"
placeholder="friend@example.com"
autocomplete="off"
class="input input-sm w-full"
/>
</div>
<button type="submit" class="btn btn-primary btn-sm mt-1" phx-disable-with="Sending…">
<.icon name="hero-paper-airplane-micro" class="size-4" /> Send
</button>
</.form>
<div :if={@suggestions != []} id="invite-suggestions" class="flex flex-wrap gap-1.5">
<span class="self-center text-xs text-base-content/60">From your other trips:</span>
<button
:for={suggestion <- Enum.take(@suggestions, 6)}
type="button"
phx-click="invite"
phx-value-email={suggestion.email}
class="btn btn-xs btn-soft gap-1"
title={"Invite #{suggestion.email}"}
>
<.icon name="hero-plus-micro" class="size-3" />
{if suggestion.user, do: Accounts.display_name(suggestion.user), else: suggestion.email}
</button>
</div>
<div>
<button
type="button"
id="create-invite"
phx-click="invite-link"
class="btn btn-ghost btn-sm"
>
<.icon name="hero-link-micro" class="size-4" />
{if @new_invite_url, do: "Another link", else: "Or make a link to send yourself"}
</button>
<%= if @new_invite_url do %>
<div class="join mt-1 w-full">
<input
id="invite-link"
type="text"
readonly
value={@new_invite_url}
class="input input-sm join-item min-w-0 flex-1 font-mono text-xs"
phx-click={JS.dispatch("phx:copy")}
aria-label="Invite link"
/>
<button
type="button"
class="btn btn-sm btn-primary join-item"
id="copy-invite-link"
phx-click={JS.dispatch("phx:copy", to: "#invite-link")}
>
<.icon name="hero-clipboard-document-micro" class="size-4" /> Copy
</button>
</div>
<p class="mt-1 text-xs text-warning">Copy it now: it won't be shown again.</p>
<% end %>
</div>
<ul :if={@invites != []} class="space-y-1 text-xs" id="pending-invites">
<li
:for={invite <- @invites}
id={"invite-#{invite.id}"}
class="flex flex-wrap items-center gap-x-2 gap-y-0.5 rounded-field px-2 py-1 hover:bg-base-100"
>
<.icon
name={if invite.email, do: "hero-envelope-micro", else: "hero-link-micro"}
class="size-3.5 opacity-60"
/>
<span class="font-medium">{invite.email || "Link"}</span>
<span class="text-base-content/60">
from {Accounts.display_name(invite.created_by)} · expires {Calendar.strftime(
invite.expires_at,
"%b %-d"
)}
</span>
<span :if={Trips.manages_invite?(@scope, @trip, invite)} class="ml-auto flex gap-1">
<button
:if={invite.email}
type="button"
phx-click="resend-invite"
phx-value-id={invite.id}
class="btn btn-ghost btn-xs"
>
Resend
</button>
<button
type="button"
phx-click="revoke-invite"
phx-value-id={invite.id}
class="btn btn-ghost btn-xs text-error"
>
Revoke
</button>
</span>
</li>
</ul>
</div>
</div>
</div>
"""
end
attr :trip, :map, required: true
# Public: anyone who comes across it. Apart from the share link, which is
# for the people it's sent to.
defp public_panel(assigns) do
assigns = assign(assigns, :link, url(~p"/t/#{assigns.trip.id}"))
~H"""
<div id="public-panel" class="border-b border-base-300 bg-base-200/60 px-4 py-4 sm:px-6">
<%= if @trip.public do %>
<div class="flex flex-wrap items-center gap-3">
<div class="join min-w-0 flex-1 basis-80">
<input
id="public-link"
type="text"
readonly
value={@link}
class="input input-sm join-item min-w-0 flex-1 font-mono text-xs"
phx-click={JS.dispatch("phx:copy")}
aria-label="Public address"
/>
<button
type="button"
id="copy-public-link"
class="btn btn-sm btn-success join-item"
phx-click={JS.dispatch("phx:copy", to: "#public-link")}
>
<.icon name="hero-clipboard-document-micro" class="size-4" /> Copy
</button>
<.link href={@link} target="_blank" class="btn btn-sm join-item" id="open-public-link">
<.icon name="hero-arrow-top-right-on-square-micro" class="size-4" />
</.link>
</div>
<label class="label cursor-pointer gap-2 text-sm">
<input
type="checkbox"
id="public-promoted-only"
class="toggle toggle-sm toggle-warning"
checked={@trip.public_promoted_only}
phx-click="toggle-public-promoted"
/> Promoted photos only
</label>
<button
type="button"
id="make-private"
phx-click="set-public"
phx-value-to="false"
class="btn btn-ghost btn-sm"
>
<.icon name="hero-lock-closed-micro" class="size-4" /> Make private
</button>
</div>
<p class="mt-2 text-xs text-base-content/60">
Listed on the front page for anyone, signed in or not. Your share link (if any) is separate and still only for the people you sent it to.
</p>
<% else %>
<div class="flex flex-wrap items-center justify-between gap-3">
<p class="text-sm text-base-content/70">
Make this trip public to list it on the front page, where anyone who comes across it can page through the photos and see on the map where they were taken.
</p>
<button
type="button"
id="make-public"
phx-click="set-public"
phx-value-to="true"
data-confirm="Make this trip public? Anyone will be able to see its photos and where they were taken."
class="btn btn-success btn-sm"
>
<.icon name="hero-globe-alt-micro" class="size-4" /> Make public
</button>
</div>
<% end %>
</div>
"""
end
attr :trip, :map, required: true
defp share_panel(assigns) do
assigns =
assign(assigns, :link, assigns.trip.share_token && url(~p"/s/#{assigns.trip.share_token}"))
~H"""
<div id="share-panel" class="border-b border-base-300 bg-base-200/60 px-4 py-4 sm:px-6">
<%= if @link do %>
<div class="flex flex-wrap items-center gap-3">
<div class="join min-w-0 flex-1 basis-80">
<input
id="share-link"
type="text"
readonly
value={@link}
class="input input-sm join-item min-w-0 flex-1 font-mono text-xs"
phx-click={JS.dispatch("phx:copy")}
aria-label="Share link"
/>
<button
type="button"
id="copy-share-link"
class="btn btn-sm btn-primary join-item"
phx-click={
JS.dispatch("phx:copy", to: "#share-link")
|> JS.show(
to: "#share-copied",
transition: {"transition-opacity duration-200", "opacity-0", "opacity-100"}
)
|> JS.hide(
to: "#share-copied",
time: 1500,
transition: {"transition-opacity duration-1000", "opacity-100", "opacity-0"}
)
}
>
<.icon name="hero-clipboard-document-micro" class="size-4" /> Copy
</button>
<.link href={@link} target="_blank" class="btn btn-sm join-item" id="open-share-link">
<.icon name="hero-arrow-top-right-on-square-micro" class="size-4" />
</.link>
</div>
<span id="share-copied" class="hidden text-xs font-medium text-success">Copied</span>
<label class="label cursor-pointer gap-2 text-sm">
<input
type="checkbox"
id="share-promoted-only"
class="toggle toggle-sm toggle-warning"
checked={@trip.share_promoted_only}
phx-click="toggle-share-promoted"
/> Promoted photos only
</label>
<div class="flex gap-1">
<button
type="button"
id="rotate-share-link"
phx-click="rotate-share"
data-confirm="Make a new link? The current one stops working, and anyone viewing through it is sent away."
class="btn btn-ghost btn-sm"
>
New link
</button>
<button
type="button"
id="stop-sharing"
phx-click="stop-sharing"
data-confirm="Stop sharing this trip? Anyone viewing it is sent away."
class="btn btn-ghost btn-sm text-error"
>
Stop sharing
</button>
</div>
</div>
<p class="mt-2 text-xs text-base-content/60">
Only for the people you send it to: it isn't listed anywhere. Open it yourself to host: viewers follow along as you page through.
</p>
<% else %>
<div class="flex flex-wrap items-center justify-between gap-3">
<p class="text-sm text-base-content/70">
Share this trip by a private link, for the people you send it to: they page through the photos and the map, no account needed, and follow along live when you host. It isn't listed anywhere.
</p>
<button type="button" id="start-sharing" phx-click="share" class="btn btn-primary btn-sm">
<.icon name="hero-link-micro" class="size-4" /> Create share link
</button>
</div>
<% end %>
</div>
"""
end
defp owner_name(people) do
case Enum.find(people, & &1.owner?) do
%{user: user} -> Accounts.display_name(user)
nil -> "someone"
end
end
@impl true
def handle_event("panel", %{"panel" => panel}, socket) when panel in ~w(people share public) do
panel = String.to_existing_atom(panel)
{:noreply, update(socket, :panel, &if(&1 == panel, do: nil, else: panel))}
end
def handle_event("select-area", %{"id" => id}, socket) do
params = if socket.assigns.area && socket.assigns.area.id == id, do: [], else: [area: id]
{:noreply, push_patch(socket, to: ~p"/trips/#{socket.assigns.trip}?#{params}")}
end
def handle_event("toggle-areas", _params, socket) do
trip = socket.assigns.trip
{:ok, trip} = Trips.set_areas(socket.assigns.current_scope, trip, !trip.areas)
{:noreply, socket |> assign(:trip, trip) |> load_photos()}
end
def handle_event("validate-invite", %{"invite" => params}, socket) do
{:noreply, assign(socket, :invite_form, to_form(params, as: :invite))}
end
# By address: from the form, or a suggestion's one click.
def handle_event("invite", params, socket) do
email = params["email"] || get_in(params, ["invite", "email"])
scope = socket.assigns.current_scope
case Trips.create_invite(scope, socket.assigns.trip, email, &invite_url/1) do
{:ok, invite} when is_binary(invite.email) ->
{:noreply,
socket
|> put_flash(:info, "Invite sent to #{invite.email}")
|> assign(:invite_form, to_form(%{"email" => ""}, as: :invite))
|> load_people()}
{:ok, _link_only} ->
{:noreply, put_flash(socket, :error, "Enter an email address, or make a link instead")}
{:error, reason} ->
{:noreply, socket |> invite_error(reason, email) |> load_people()}
end
end
def handle_event("invite-link", _params, socket) do
{:ok, invite} = Trips.create_invite(socket.assigns.current_scope, socket.assigns.trip)
{:noreply,
socket
|> assign(:new_invite_url, invite_url(invite.token))
|> load_people()}
end
def handle_event("resend-invite", %{"id" => id}, socket) do
case Trips.resend_invite(socket.assigns.current_scope, socket.assigns.trip, id, &invite_url/1) do
{:ok, invite} ->
{:noreply, socket |> put_flash(:info, "Sent again to #{invite.email}") |> load_people()}
{:error, _} ->
{:noreply, socket}
end
end
def handle_event("revoke-invite", %{"id" => id}, socket) do
_ = Trips.revoke_invite(socket.assigns.current_scope, socket.assigns.trip, id)
{:noreply, socket |> assign(:new_invite_url, nil) |> load_people()}
end
def handle_event("remove-member", %{"id" => user_id}, socket) do
:ok = Trips.remove_member(socket.assigns.current_scope, socket.assigns.trip, user_id)
{:noreply, socket |> load_people() |> load_photos()}
end
def handle_event("leave", _params, socket) do
:ok = Trips.leave_trip(socket.assigns.current_scope, socket.assigns.trip)
{:noreply,
socket
|> put_flash(:info, "You've left #{socket.assigns.trip.name}")
|> push_navigate(to: ~p"/trips")}
end
def handle_event("share", _params, socket) do
{:ok, trip} = Trips.share_trip(socket.assigns.current_scope, socket.assigns.trip)
{:noreply, assign(socket, :trip, trip)}
end
def handle_event("rotate-share", _params, socket) do
{:ok, trip} = Trips.rotate_share_link(socket.assigns.current_scope, socket.assigns.trip)
{:noreply,
socket
|> assign(:trip, trip)
|> put_flash(:info, "New link made; the old one no longer works")}
end
def handle_event("stop-sharing", _params, socket) do
{:ok, trip} = Trips.stop_sharing(socket.assigns.current_scope, socket.assigns.trip)
{:noreply, socket |> assign(:trip, trip) |> put_flash(:info, "This trip is no longer shared")}
end
def handle_event("set-public", %{"to" => to}, socket) do
{:ok, trip} =
Trips.set_public(socket.assigns.current_scope, socket.assigns.trip, to == "true")
message =
if trip.public, do: "Anyone can see this trip now", else: "This trip is private again"
{:noreply, socket |> assign(:trip, trip) |> put_flash(:info, message)}
end
def handle_event("toggle-public-promoted", _params, socket) do
trip = socket.assigns.trip
{:ok, trip} =
Trips.set_public_promoted_only(
socket.assigns.current_scope,
trip,
!trip.public_promoted_only
)
{:noreply, assign(socket, :trip, trip)}
end
def handle_event("toggle-share-promoted", _params, socket) do
trip = socket.assigns.trip
{:ok, trip} =
Trips.set_share_promoted_only(socket.assigns.current_scope, trip, !trip.share_promoted_only)
{:noreply, assign(socket, :trip, trip)}
end
def handle_event("delete", _params, socket) do
{:ok, _} = Trips.delete_trip(socket.assigns.current_scope, socket.assigns.trip)
{:noreply,
socket
|> put_flash(:info, "Trip deleted")
|> push_navigate(to: ~p"/trips")}
end
@impl true
def handle_info({event, _photo}, socket)
when event in [:photo_created, :photo_updated, :photo_deleted] do
{:noreply, load_photos(socket)}
end
def handle_info({:areas_changed, on?}, socket) do
{:noreply, socket |> update(:trip, &%{&1 | areas: on?}) |> load_photos()}
end
def handle_info(:album_changed, socket),
do: {:noreply, socket |> load_people() |> load_photos()}
def handle_info({:member_removed, user_id}, socket) do
if user_id == socket.assigns.current_scope.user.id do
{:noreply,
socket
|> put_flash(:info, "You're no longer in #{socket.assigns.trip.name}")
|> push_navigate(to: ~p"/trips")}
else
{:noreply, socket |> load_people() |> load_photos()}
end
end
def handle_info(:trip_deleted, socket) do
{:noreply,
socket
|> put_flash(:info, "#{socket.assigns.trip.name} was deleted")
|> push_navigate(to: ~p"/trips")}
end
def handle_info(_message, socket), do: {:noreply, socket}
@impl true
def handle_async(:area_names, {:ok, {asked, names}}, socket) do
{:noreply,
socket
|> update(:area_names, &Map.merge(&1, names))
|> update(:naming, &MapSet.difference(&1, asked))
|> load_photos()}
end
# The lookup crashed: leave the numbers, and ask again on the next change.
def handle_async(:area_names, {:exit, _reason}, socket),
do: {:noreply, assign(socket, :naming, MapSet.new())}
defp invite_url(token), do: url(~p"/trips/join/#{token}")
defp invite_error(socket, :already_member, email),
do: put_flash(socket, :error, "#{email} is already in this trip")
defp invite_error(socket, :already_invited, email),
do: put_flash(socket, :error, "#{email} already has an invite waiting; you can resend it")
defp invite_error(socket, %Ecto.Changeset{} = changeset, email) do
assign(
socket,
:invite_form,
to_form(%{changeset | action: :insert}, as: :invite) |> keep_email(email)
)
end
defp invite_error(socket, _reason, _email),
do: put_flash(socket, :error, "Couldn't send that invite")
# The changeset's form is for the invite schema; keep what was typed.
defp keep_email(form, email), do: %{form | params: %{"email" => email}}
defp load_people(socket) do
trip = socket.assigns.trip
scope = socket.assigns.current_scope
socket
|> assign(:people, Trips.list_people(trip))
|> assign(:invites, Trips.list_pending_invites(trip))
|> assign(:suggestions, Trips.suggested_invitees(scope, trip))
|> assign_new(:invite_form, fn -> to_form(%{"email" => ""}, as: :invite) end)
end
defp load_photos(socket) do
photos = Trips.list_album(socket.assigns.trip)
socket = assign_areas(socket, photos)
area = socket.assigns.area
shown =
if area, do: Enum.filter(photos, &MapSet.member?(area.photo_ids, &1.id)), else: photos
points = PhotoMap.points(shown)
socket
|> assign(:count, length(photos))
|> assign(:points, points)
|> assign(:unmapped, if(area, do: 0, else: length(photos) - length(points)))
|> assign(:names, photos |> Enum.map(& &1.user) |> Accounts.display_names())
|> stream(:photos, shown, reset: true)
end
# Splits the album into areas (when the trip has them on), names them
# from what's known, and looks up the rest in the background.
defp assign_areas(socket, photos) do
areas = if socket.assigns.trip.areas, do: Areas.split(photos), else: []
names = Map.merge(Areas.known_names(areas), socket.assigns.area_names)
areas = Areas.name(areas, names)
socket
|> assign(:areas, areas)
|> assign(:area, Enum.find(areas, &(&1.id == socket.assigns.area_id)))
|> assign(:area_names, names)
|> look_up_area_names(Areas.unnamed(areas, names))
end
# One lookup at a time, and each place asked about once per visit, so a
# geocoder that's down doesn't get asked over and over.
defp look_up_area_names(socket, unnamed) do
unnamed = Enum.reject(unnamed, &MapSet.member?(socket.assigns.asked, &1.key))
keys = MapSet.new(unnamed, & &1.key)
if unnamed != [] and connected?(socket) and MapSet.size(socket.assigns.naming) == 0 do
socket
|> assign(:naming, keys)
|> update(:asked, &MapSet.union(&1, keys))
|> start_async(:area_names, fn -> {keys, Areas.look_up_names(unnamed)} end)
else
socket
end
end
end
neiam
/cartosnap_ex
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
35.0 KiB · text
History
e569b6d