Skip to content

Commit

Permalink
Revert "save - user changes to server with all positions"
Browse files Browse the repository at this point in the history
This reverts commit 09ae8a8.
  • Loading branch information
JannikStreek committed Sep 8, 2024
1 parent 09ae8a8 commit 6d8541f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 26 deletions.
8 changes: 5 additions & 3 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
});
},
Expand Down
7 changes: 0 additions & 7 deletions assets/scss/_bootstrap_custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 10 additions & 8 deletions lib/mindwendel/ideas.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
)
Expand Down Expand Up @@ -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 """
Expand Down
5 changes: 2 additions & 3 deletions lib/mindwendel_web/live/idea_live/index_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
7 changes: 4 additions & 3 deletions lib/mindwendel_web/live/idea_live/index_component.html.heex
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div>
<div id="ideas" phx-hook="Sortable" class="mb-5 sortable d-flex flex-wrap justify-content-center">
<div id="ideas" phx-hook="Sortable" class="row mb-5 sortable">
<%= for idea <- @ideas do %>
<div class="col-md-6">
<div
class="card m-3 shadow-sm p-2 rounded IndexComponent__IdeaCard"
style="width: 394px; cursor: grab;"
class="card mt-3 shadow-sm p-2 rounded IndexComponent__IdeaCard"
data-testid={idea.id}
data-id={idea.id}
data-brainstorming-id={idea.brainstorming_id}
Expand Down Expand Up @@ -101,6 +101,7 @@
<% end %>
</div>
</div>
</div>
</div>
<% end %>

Expand Down
4 changes: 2 additions & 2 deletions test/mindwendel/ideas_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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,
Expand Down

0 comments on commit 6d8541f

Please sign in to comment.