36 lines
946 B
Elixir
36 lines
946 B
Elixir
defmodule PoexWeb.PadLive do
|
|
alias Poex.Pads
|
|
alias Poex.Pads.Document
|
|
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()
|
|
|
|
Poex.Pads.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,
|
|
state: state
|
|
} = Repo.get!(Document, id)
|
|
|
|
# init editor and assigns with latest state from doc
|
|
{:ok, assign(socket, id: id, title: title, state: state |> Utils.atomize_keys())}
|
|
end
|
|
|
|
def handle_info(:new_document, socket) do
|
|
{:noreply, push_navigate(socket, to: "/pad/new")}
|
|
end
|
|
end
|