Skip to content

Commit

Permalink
ordering now works properly
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikStreek committed Sep 8, 2024
1 parent 6d8541f commit 04496bf
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
9 changes: 5 additions & 4 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ Hooks.NativeSharingButton = {
// see https://github.com/drag-drop-touch-js/dragdroptouch for mobile support
Hooks.Sortable = {
mounted(){
sortable('.sortable')[0].addEventListener('sortupdate', (e) => {
sortable('.sortable', {forcePlaceholderSize: true, placeholderClass: 'card-body-mindwendel-idea-ph-class'})[0].addEventListener('sortupdate', (e) => {
this.pushEventTo(this.el, "change_position", {
id: e.detail.item.children[0].dataset.id,
brainstorming_id: e.detail.item.children[0].dataset.brainstormingId,
id: e.detail.item.dataset.id,
brainstorming_id: e.detail.item.dataset.brainstormingId,
// on the server, positions start with 1 not 0
new_position: e.detail.destination.elementIndex + 1
new_position: e.detail.destination.elementIndex + 1,
old_position: e.detail.origin.elementIndex + 1
})
});
},
Expand Down
7 changes: 7 additions & 0 deletions assets/scss/_bootstrap_custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@
padding: 0.5rem 0.5rem;
background-color: inherit;
}

.card-body-mindwendel-idea-ph-class {
@extend .card;
@extend .m-3;
@extend .shadow-sm;
border-style: dashed;
}
28 changes: 21 additions & 7 deletions lib/mindwendel/ideas.ex
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,30 @@ defmodule Mindwendel.Ideas do
|> Repo.update_all([])
end

def update_ideas_for_brainstorming_by_user_move(brainstorming_id, idea_id, new_position) do
def update_ideas_for_brainstorming_by_user_move(brainstorming_id, idea_id, new_position, old_position) do
get_idea!(idea_id) |> update_idea(%{order_position: new_position})

# depending on moving a card bottom up or up to bottom, we need to correct the ordering
order = if new_position < old_position, do: [asc: :order_position, desc: :updated_at], else: [asc: :order_position, asc: :updated_at]

idea_rank_query =
from(idea in Idea,
where: idea.brainstorming_id == ^brainstorming_id,
windows: [o: [order_by: ^order]],
select: %{
idea_id: idea.id,
idea_rank:
over(row_number(), :o)
}
)

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]]
join: idea_ranks in subquery(idea_rank_query),
on: idea_ranks.idea_id == idea.id,
where: idea.brainstorming_id == ^brainstorming_id,
update: [set: [order_position: idea_ranks.idea_rank]]
)
|> Repo.update_all([])
|> IO.inspect

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
4 changes: 2 additions & 2 deletions lib/mindwendel_web/live/idea_live/index_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ defmodule MindwendelWeb.IdeaLive.IndexComponent do
{:noreply, socket}
end

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)
def handle_event("change_position", %{"id" => id, "brainstorming_id" => brainstorming_id, "new_position" => new_position, "old_position" => old_position}, socket) do
Ideas.update_ideas_for_brainstorming_by_user_move(brainstorming_id, id, new_position, old_position)
Brainstormings.broadcast({:ok, Brainstormings.get_brainstorming!(brainstorming_id)}, :brainstorming_updated)

{:noreply, socket}
Expand Down
7 changes: 3 additions & 4 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="row mb-5 sortable">
<div id="ideas" phx-hook="Sortable" class="mb-5 sortable d-flex flex-wrap justify-content-center">
<%= for idea <- @ideas do %>
<div class="col-md-6">
<div
class="card mt-3 shadow-sm p-2 rounded IndexComponent__IdeaCard"
class="card m-3 shadow-sm p-2 rounded IndexComponent__IdeaCard"
- style="width: 394px; cursor: grab;"
data-testid={idea.id}
data-id={idea.id}
data-brainstorming-id={idea.brainstorming_id}
Expand Down Expand Up @@ -100,7 +100,6 @@
<% end %>
<% end %>
</div>
</div>
</div>
</div>
<% end %>
Expand Down

0 comments on commit 04496bf

Please sign in to comment.