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_server(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_server(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