56 lines
1.7 KiB
Elixir
56 lines
1.7 KiB
Elixir
defmodule OutlookWeb.ArticleLive.New do
|
|
use OutlookWeb, :live_view
|
|
|
|
import OutlookWeb.ArticleLive.NewComponents
|
|
|
|
alias Outlook.{Articles,Authors,HtmlPreparations}
|
|
alias Articles.{Article,RawHtmlInput}
|
|
|
|
require Logger
|
|
|
|
@impl true
|
|
def mount(_params, _session, socket) do
|
|
{:ok,
|
|
socket
|
|
|> assign(:page_title, "New Article")
|
|
|> assign(:article, %Article{})
|
|
|> assign(:raw_html_input, %RawHtmlInput{})
|
|
|> assign(:changeset, Articles.change_raw_html_input(%RawHtmlInput{}))
|
|
|> assign(:selected_els, [])
|
|
|> assign(:step, :import_raw_html)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_params(%{"author_id" => author_id}, _, socket) do
|
|
author = Authors.get_author!(author_id)
|
|
{:noreply,
|
|
socket
|
|
|> assign(:author, author)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("validate_raw_html_input", %{"raw_html_input" => raw_html_input_params}, socket) do
|
|
changeset = validate_raw_html_input(raw_html_input_params, socket)
|
|
{:noreply, assign(socket, :changeset, changeset)}
|
|
end
|
|
|
|
def handle_event("convert_raw_html_input", %{"raw_html_input" => raw_html_input_params}, socket) do
|
|
changeset = validate_raw_html_input(raw_html_input_params, socket)
|
|
case changeset.valid? do
|
|
true ->
|
|
{:noreply,
|
|
socket
|
|
|> assign(:raw_internal_tree, HtmlPreparations.convert_raw_html_input(raw_html_input_params["content"]))
|
|
|> assign(:step, :review_raw_internaltree)}
|
|
false ->
|
|
{:noreply, assign(socket, :changeset, changeset)}
|
|
end
|
|
end
|
|
|
|
defp validate_raw_html_input(raw_html_input_params, socket) do
|
|
socket.assigns.raw_html_input
|
|
|> Articles.change_raw_html_input(raw_html_input_params)
|
|
|> Map.put(:action, :validate)
|
|
end
|
|
end
|