From 6d8541f8403adafacf086a1ca344487192d0c956 Mon Sep 17 00:00:00 2001 From: Jannik Streek Date: Sun, 8 Sep 2024 14:04:38 +0200 Subject: [PATCH] Revert "save - user changes to server with all positions" This reverts commit 09ae8a8753132dbd9362e9198d621b796d0fef77. --- assets/js/app.js | 8 +++++--- assets/scss/_bootstrap_custom.scss | 7 ------- lib/mindwendel/ideas.ex | 18 ++++++++++-------- .../live/idea_live/index_component.ex | 5 ++--- .../live/idea_live/index_component.html.heex | 7 ++++--- test/mindwendel/ideas_test.exs | 4 ++-- 6 files changed, 23 insertions(+), 26 deletions(-) diff --git a/assets/js/app.js b/assets/js/app.js index cdf35bb6..87cf2286 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -54,10 +54,12 @@ Hooks.NativeSharingButton = { // see https://github.com/drag-drop-touch-js/dragdroptouch for mobile support Hooks.Sortable = { mounted(){ - sortable('.sortable', { forcePlaceholderSize: true, placeholderClass: 'card-body-mindwendel-idea-ph-class' })[0].addEventListener('sortupdate', (e) => { + sortable('.sortable')[0].addEventListener('sortupdate', (e) => { this.pushEventTo(this.el, "change_position", { - brainstorming_id: e.detail.item.dataset.brainstormingId, - new_idea_positions: e.detail.destination.items.map((item, i) => { return { id: item.dataset.id, position: i + 1 }}), + id: e.detail.item.children[0].dataset.id, + brainstorming_id: e.detail.item.children[0].dataset.brainstormingId, + // on the server, positions start with 1 not 0 + new_position: e.detail.destination.elementIndex + 1 }) }); }, diff --git a/assets/scss/_bootstrap_custom.scss b/assets/scss/_bootstrap_custom.scss index 33e5c3f0..af5f4fb2 100644 --- a/assets/scss/_bootstrap_custom.scss +++ b/assets/scss/_bootstrap_custom.scss @@ -6,13 +6,6 @@ padding: 0.5rem 0.5rem; } -.card-body-mindwendel-idea-ph-class { - @extend .card; - @extend .m-3; - @extend .shadow-sm; - border-style: dashed; -} - .card-footer-mindwendel-idea { @extend .card-footer; padding: 0.5rem 0.5rem; diff --git a/lib/mindwendel/ideas.ex b/lib/mindwendel/ideas.ex index 9a174a92..27a734e4 100644 --- a/lib/mindwendel/ideas.ex +++ b/lib/mindwendel/ideas.ex @@ -112,7 +112,7 @@ defmodule Mindwendel.Ideas do where: idea.brainstorming_id == ^id, select: %{ idea_id: idea.id, - like_count: fragment("(CASE WHEN ? IS NOT NULL THEN ? ELSE 0 END)", idea_counts.like_count, idea_counts.like_count), + like_count: idea_counts.like_count, idea_rank: over(row_number(), order_by: [desc_nulls_last: idea_counts.like_count]) } ) @@ -151,14 +151,16 @@ defmodule Mindwendel.Ideas do |> Repo.update_all([]) end - def update_ideas_for_brainstorming_by_user_move(brainstorming_id, new_idea_positions) do - changesets = - new_idea_positions - |> Enum.map(fn {id, position} -> %Idea{id: id} |> Ecto.Changeset.cast(%{position: position}) end) + def update_ideas_for_brainstorming_by_user_move(brainstorming_id, idea_id, new_position) do + from(idea in Idea, + where: idea.brainstorming_id == ^brainstorming_id and idea.order_position >= ^new_position and idea.id != ^idea_id, + update: [set: [order_position: idea.order_position + 1]] + ) + |> Repo.update_all([]) + |> IO.inspect - Repo.transaction(fn -> - Enum.each(changesets, &Repo.update!(&1, [])) - end) + from(idea in Idea, where: idea.id == ^idea_id and idea.brainstorming_id == ^brainstorming_id, update: [set: [order_position: ^new_position]]) + |> Repo.update_all([]) end @doc """ diff --git a/lib/mindwendel_web/live/idea_live/index_component.ex b/lib/mindwendel_web/live/idea_live/index_component.ex index 2090494c..b608d0d0 100644 --- a/lib/mindwendel_web/live/idea_live/index_component.ex +++ b/lib/mindwendel_web/live/idea_live/index_component.ex @@ -45,9 +45,8 @@ defmodule MindwendelWeb.IdeaLive.IndexComponent do {:noreply, socket} end - def handle_event("change_position", %{"brainstorming_id" => brainstorming_id, "new_idea_positions" => new_idea_positions}, socket) do - IO.inspect new_idea_positions - Ideas.update_ideas_for_brainstorming_by_user_move(brainstorming_id, new_idea_positions) + def handle_event("change_position", %{"id" => id, "brainstorming_id" => brainstorming_id, "new_position" => new_position}, socket) do + Ideas.update_ideas_for_brainstorming_by_user_move(brainstorming_id, id, new_position) Brainstormings.broadcast({:ok, Brainstormings.get_brainstorming!(brainstorming_id)}, :brainstorming_updated) {:noreply, socket} diff --git a/lib/mindwendel_web/live/idea_live/index_component.html.heex b/lib/mindwendel_web/live/idea_live/index_component.html.heex index 306a6a12..1bee1f11 100644 --- a/lib/mindwendel_web/live/idea_live/index_component.html.heex +++ b/lib/mindwendel_web/live/idea_live/index_component.html.heex @@ -1,9 +1,9 @@
-
+
<%= for idea <- @ideas do %> +
+
<% end %> diff --git a/test/mindwendel/ideas_test.exs b/test/mindwendel/ideas_test.exs index e3d23340..00b6c787 100644 --- a/test/mindwendel/ideas_test.exs +++ b/test/mindwendel/ideas_test.exs @@ -230,8 +230,7 @@ defmodule Mindwendel.IdeasTest do second_idea = Factory.insert!(:idea, brainstorming: brainstorming, - order_position: 1, - updated_at: ~N[2021-01-01 15:06:30] + order_position: 1 ) third_idea = @@ -249,6 +248,7 @@ defmodule Mindwendel.IdeasTest do ) ideas_sorted_by_position = Repo.all(query) + IO.inspect(ideas_sorted_by_position) assert ideas_sorted_by_position |> Enum.map(& &1.id) == [ idea.id,