New import info flash (#630)

* adding a new info flash on imported notebooks

* changing the imported notebook error flashes to warning level

* Update lib/livebook_web/live/session_helpers.ex

Co-authored-by: José Valim <jose.valim@gmail.com>

* adding unit tests for the flash messages and fixing put_import_flash/1 call location

* changing name of put_import_flash_messages/2 to put_import_warnings

* Update lib/livebook_web/live/home_live.ex

* formating code

Co-authored-by: José Valim <jose.valim@gmail.com>
Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
This commit is contained in:
Matheus Cumpian 2021-10-21 18:21:54 -03:00 committed by GitHub
parent 887a4ad0d1
commit c6d7c81ee9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 10 deletions

View file

@ -257,7 +257,7 @@ defmodule LivebookWeb.HomeLive do
images_dir = Session.images_dir_for_notebook(file) images_dir = Session.images_dir_for_notebook(file)
socket socket
|> put_import_flash_messages(messages) |> put_import_warnings(messages)
|> create_session( |> create_session(
notebook: notebook, notebook: notebook,
copy_images_from: images_dir, copy_images_from: images_dir,
@ -278,7 +278,7 @@ defmodule LivebookWeb.HomeLive do
case import_notebook(file) do case import_notebook(file) do
{:ok, {notebook, messages}} -> {:ok, {notebook, messages}} ->
socket socket
|> put_import_flash_messages(messages) |> put_import_warnings(messages)
|> create_session(notebook: notebook, file: file, origin: {:file, file}) |> create_session(notebook: notebook, file: file, origin: {:file, file})
{:error, error} -> {:error, error} ->
@ -408,7 +408,15 @@ defmodule LivebookWeb.HomeLive do
defp import_content(socket, content, session_opts) do defp import_content(socket, content, session_opts) do
{notebook, messages} = Livebook.LiveMarkdown.Import.notebook_from_markdown(content) {notebook, messages} = Livebook.LiveMarkdown.Import.notebook_from_markdown(content)
socket = put_import_flash_messages(socket, messages)
socket =
socket
|> put_import_warnings(messages)
|> put_flash(
:info,
"You have imported a notebook, no code has been executed so far. You should read and evaluate code as needed."
)
session_opts = Keyword.merge(session_opts, notebook: notebook) session_opts = Keyword.merge(session_opts, notebook: notebook)
create_session(socket, session_opts) create_session(socket, session_opts)
end end

View file

@ -30,15 +30,15 @@ defmodule LivebookWeb.SessionHelpers do
@doc """ @doc """
Formats the given list of notebook import messages and puts Formats the given list of notebook import messages and puts
into the info flash. into the warning flash.
""" """
@spec put_import_flash_messages(Phoenix.LiveView.Socket.t(), list(String.t())) :: @spec put_import_warnings(Phoenix.LiveView.Socket.t(), list(String.t())) ::
Phoenix.LiveView.Socket.t() Phoenix.LiveView.Socket.t()
def put_import_flash_messages(socket, messages) def put_import_warnings(socket, messages)
def put_import_flash_messages(socket, []), do: socket def put_import_warnings(socket, []), do: socket
def put_import_flash_messages(socket, messages) do def put_import_warnings(socket, messages) do
list = list =
messages messages
|> Enum.map(fn message -> ["- ", message] end) |> Enum.map(fn message -> ["- ", message] end)
@ -49,6 +49,6 @@ defmodule LivebookWeb.SessionHelpers do
"We found problems while importing the file and tried to autofix them:\n" | list "We found problems while importing the file and tried to autofix them:\n" | list
]) ])
put_flash(socket, :info, flash) put_flash(socket, :warning, flash)
end end
end end

View file

@ -872,7 +872,7 @@ defmodule LivebookWeb.SessionLive do
{file, notebook} = file_and_notebook(fork?, origin, notebook) {file, notebook} = file_and_notebook(fork?, origin, notebook)
socket socket
|> put_import_flash_messages(messages) |> put_import_warnings(messages)
|> create_session(notebook: notebook, origin: origin, file: file) |> create_session(notebook: notebook, origin: origin, file: file)
{:error, message} -> {:error, message} ->

View file

@ -9,6 +9,15 @@
</div> </div>
<% end %> <% end %>
<%= if live_flash(@flash, :warning) do %>
<div class="flex items-center space-x-2 rounded-lg px-4 py-2 bg-yellow-100 text-yellow-600 hover:opacity-75 cursor-pointer" role="alert"
phx-click="lv:clear-flash"
phx-value-key="warning">
<.remix_icon icon="error-warning-line" class="text-2xl" />
<span class="whitespace-pre-wrap"><%= live_flash(@flash, :warning) %></span>
</div>
<% end %>
<%= if live_flash(@flash, :error) do %> <%= if live_flash(@flash, :error) do %>
<div class="flex items-center space-x-2 rounded-lg px-4 py-2 bg-red-100 text-red-400 hover:opacity-75 cursor-pointer" role="alert" <div class="flex items-center space-x-2 rounded-lg px-4 py-2 bg-red-100 text-red-400 hover:opacity-75 cursor-pointer" role="alert"
phx-click="lv:clear-flash" phx-click="lv:clear-flash"

View file

@ -206,6 +206,43 @@ defmodule LivebookWeb.HomeLiveTest do
{:ok, view, _} = live(conn, "/sessions/#{id}") {:ok, view, _} = live(conn, "/sessions/#{id}")
assert render(view) =~ "My notebook" assert render(view) =~ "My notebook"
end end
test "should show info flash with information about the imported notebook", %{conn: conn} do
{:ok, view, _} = live(conn, "/home/import/content")
notebook_content = """
# My notebook
"""
view
|> element("form", "Import")
|> render_submit(%{data: %{content: notebook_content}})
{_path, flash} = assert_redirect(view)
assert flash["info"] =~
"You have imported a notebook, no code has been executed so far. You should read and evaluate code as needed."
end
test "should show warning flash when the imported notebook have errors", %{conn: conn} do
{:ok, view, _} = live(conn, "/home/import/content")
# Notebook with 3 headers
notebook_content = """
# My notebook
# My notebook
# My notebook
"""
view
|> element("form", "Import")
|> render_submit(%{data: %{content: notebook_content}})
{_path, flash} = assert_redirect(view)
assert flash["warning"] =~
"We found problems while importing the file and tried to autofix them:\n- Downgrading all headings, because 3 instances of heading 1 were found"
end
end end
describe "public import endpoint" do describe "public import endpoint" do