poex/lib/poex_web/live/pad/pad_live.ex

37 lines
1.0 KiB
Elixir

defmodule PoexWeb.PadLive do
alias Poex.Pads.{Document, DocumentDynamicSupervisor}
alias Poex.Repo
alias Poex.Utils
use PoexWeb, :live_view
def mount(%{"id" => "new"}, _session, socket) do
# Create a new PadDocument
{:ok, new_document} =
Document.changeset(%Document{}, %{title: "Untitled"})
|> Repo.insert()
DocumentDynamicSupervisor.start_document_supervisor(new_document)
# Redirect to the new document with its ID
{:ok, push_navigate(socket, to: ~p"/pad/#{new_document.id}", replace: true)}
end
def mount(%{"id" => id}, _session, socket) do
%{
id: id,
title: title,
contents: contents
} = document = Repo.get!(Document, id)
DocumentDynamicSupervisor.start_document_supervisor(document)
# init editor and assigns with latest state from doc
{:ok, assign(socket, id: id, title: title, contents: contents |> Utils.atomize_keys())}
end
def handle_info(:new_document, socket) do
{:noreply, push_navigate(socket, to: "/pad/new")}
end
end