From d6c05c790389efa21fc8d27f7421998e0ccaf87e Mon Sep 17 00:00:00 2001 From: shahryar tavakkoli Date: Mon, 11 Apr 2022 09:52:42 +0430 Subject: [PATCH] fix cleaning flash message in admin --- apps/mishka_content/lib/blog/post.ex | 5 +++++ .../lib/schema/mishka_content/post.ex | 2 +- apps/mishka_html/assets/js/app.js | 9 +++++++++ .../lib/mishka_html/helpers/live_crud.ex | 4 ++-- .../lib/mishka_html_web/live/admin_notifs.ex | 4 ++-- .../live/components/public/flash_component.ex | 18 +++++++++++++----- .../components/public/list_item_component.ex | 2 +- 7 files changed, 33 insertions(+), 11 deletions(-) diff --git a/apps/mishka_content/lib/blog/post.ex b/apps/mishka_content/lib/blog/post.ex index 403dc803..91c227a1 100644 --- a/apps/mishka_content/lib/blog/post.ex +++ b/apps/mishka_content/lib/blog/post.ex @@ -187,4 +187,9 @@ defmodule MishkaContent.Blog.Post do end def notify_subscribers(params, _), do: params + + def delete_record(id) do + MishkaDatabase.Repo.get(Post, id) + |> MishkaDatabase.Repo.delete() + end end diff --git a/apps/mishka_database/lib/schema/mishka_content/post.ex b/apps/mishka_database/lib/schema/mishka_content/post.ex index 201f6474..db227b4d 100644 --- a/apps/mishka_database/lib/schema/mishka_content/post.ex +++ b/apps/mishka_database/lib/schema/mishka_content/post.ex @@ -49,7 +49,7 @@ defmodule MishkaDatabase.Schema.MishkaContent.Blog.Post do field :show_location, :boolean, null: true belongs_to :blog_categories, MishkaDatabase.Schema.MishkaContent.Blog.Category, foreign_key: :category_id, type: :binary_id - has_many :blog_authors, MishkaDatabase.Schema.MishkaContent.BlogAuthor, foreign_key: :post_id + has_many :blog_authors, MishkaDatabase.Schema.MishkaContent.BlogAuthor, foreign_key: :post_id, on_delete: :nothing has_many :blog_likes, MishkaDatabase.Schema.MishkaContent.BlogLike, foreign_key: :post_id has_many :blog_tags_mappers, MishkaDatabase.Schema.MishkaContent.BlogTagMapper, foreign_key: :post_id, on_delete: :delete_all diff --git a/apps/mishka_html/assets/js/app.js b/apps/mishka_html/assets/js/app.js index bbde598d..07ce9654 100644 --- a/apps/mishka_html/assets/js/app.js +++ b/apps/mishka_html/assets/js/app.js @@ -222,6 +222,15 @@ Hooks.Editor = { } } +Hooks.DeleteFlashMessage = { + mounted() { + this.handleEvent("delete_flash_message", ({id}) => { + const element = document.getElementById(id); + element.remove(); + }); + } +} + let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content") let liveSocket = new LiveSocket("/live", Socket, {hooks: Hooks, params: {_csrf_token: csrfToken}}) diff --git a/apps/mishka_html/lib/mishka_html/helpers/live_crud.ex b/apps/mishka_html/lib/mishka_html/helpers/live_crud.ex index ddce8a61..c85bdb11 100644 --- a/apps/mishka_html/lib/mishka_html/helpers/live_crud.ex +++ b/apps/mishka_html/lib/mishka_html/helpers/live_crud.ex @@ -91,7 +91,7 @@ defmodule MishkaHtml.Helpers.LiveCRUD do def handle_event("delete", %{"id" => id} = _params, socket) do module_selected = Keyword.get(@interface_module, :module) skip_list = Keyword.get(@interface_module, :skip_list) - socket = MishkaHtml.Helpers.LiveCRUD.delete_item_of_list(socket, module_selected, unquote(function), id, unquote(user_id), unquote(component), skip_list, fn x -> x end) + socket = MishkaHtml.Helpers.LiveCRUD.delete_item_of_list(socket, module_selected, unquote(function), id, unquote(user_id), unquote(component), skip_list, nil) {:noreply, socket} end end @@ -562,7 +562,7 @@ defmodule MishkaHtml.Helpers.LiveCRUD do }, %{user_action: "delete_item_of_list", title: Map.get(repo_data, :title), full_name: Map.get(repo_data, :full_name), user_id: Map.get(socket.assigns, :user_id)}) # It should pass conn or socket output - after_condition.(id, socket) + if !is_nil(after_condition), do: after_condition.(id, socket), else: socket |> put_flash(:info, MishkaTranslator.Gettext.dgettext("macro_live", "رکورد مورد نظر با موفقیت حذف گردید")) {:error, :delete, :forced_to_delete, _error_atom} -> socket diff --git a/apps/mishka_html/lib/mishka_html_web/live/admin_notifs.ex b/apps/mishka_html/lib/mishka_html_web/live/admin_notifs.ex index e1174035..73d0e1e2 100644 --- a/apps/mishka_html/lib/mishka_html_web/live/admin_notifs.ex +++ b/apps/mishka_html/lib/mishka_html_web/live/admin_notifs.ex @@ -44,7 +44,7 @@ defmodule MishkaHtmlWeb.AdminBlogNotifsLive do page_title: MishkaTranslator.Gettext.dgettext("html_live", "مدیریت اعلانات"), notifs: Notif.notifs(conditions: {1, 10}, filters: %{}) ) - {:ok, socket, temporary_assigns: [categories: []]} + {:ok, socket, temporary_assigns: [notifs: []]} end # Live CRUD and Paginate @@ -52,7 +52,7 @@ defmodule MishkaHtmlWeb.AdminBlogNotifsLive do list_search_and_action() - delete_list_item(:notifs, DeleteErrorComponent, false) + delete_list_item(:notifs, MishkaHtmlWeb.AdminBlogNotifsLive, false) selected_menue("MishkaHtmlWeb.AdminBlogNotifsLive") diff --git a/apps/mishka_html/lib/mishka_html_web/live/components/public/flash_component.ex b/apps/mishka_html/lib/mishka_html_web/live/components/public/flash_component.ex index 3bec0ea2..1d03e826 100644 --- a/apps/mishka_html/lib/mishka_html_web/live/components/public/flash_component.ex +++ b/apps/mishka_html/lib/mishka_html_web/live/components/public/flash_component.ex @@ -3,35 +3,43 @@ defmodule MishkaHtml.Helpers.FlashComponent do def render(assigns) do ~H""" -
+
<%= if live_flash(@flash, :info) do %>
- <% end %> <%= if live_flash(@flash, :success) do %>
- <% end %> <%= if live_flash(@flash, :warning) do %>
- <% end %> <%= if live_flash(@flash, :error) do %>
- <% end %>
""" end + + def handle_event("delete_flash_message", _params, socket) do + socket = + socket + |> push_event("delete_flash_message", %{id: "flash_message_admin_alert"}) + + {:noreply, socket} + end end diff --git a/apps/mishka_html/lib/mishka_html_web/live/components/public/list_item_component.ex b/apps/mishka_html/lib/mishka_html_web/live/components/public/list_item_component.ex index b54f05c3..b190fccc 100644 --- a/apps/mishka_html/lib/mishka_html_web/live/components/public/list_item_component.ex +++ b/apps/mishka_html/lib/mishka_html_web/live/components/public/list_item_component.ex @@ -20,7 +20,7 @@ defmodule MishkaHtml.Helpers.ListItemComponent do <%= for {list_item, color} <- Enum.zip(@list, Stream.cycle(["wlist", "glist"])) do %> - + <%= for field_item <- get_list_headers(@fields) do %> <%= if field_item.form == "custom" do %>