@@ -5,6 +5,7 @@ defmodule Backpex.HTML.Resource do
5
5
use BackpexWeb , :html
6
6
7
7
import Phoenix.LiveView.TagEngine
8
+ import Backpex.HTML.CoreComponents
8
9
import Backpex.HTML.Form
9
10
import Backpex.HTML.Layout
10
11
@@ -47,51 +48,38 @@ defmodule Backpex.HTML.Resource do
47
48
attr :name , :atom , required: true , doc: "name of the column the link should change order for"
48
49
49
50
def order_link ( assigns ) do
50
- order_direction =
51
- if assigns . name == assigns . query_options . order_by do
52
- toggle_order_direction ( assigns . query_options . order_direction )
53
- else
54
- :asc
51
+ next_order_direction =
52
+ cond do
53
+ assigns . name != assigns . query_options . order_by -> :asc
54
+ assigns . query_options . order_direction == :asc -> :desc
55
+ true -> :asc
55
56
end
56
57
58
+ patch_link =
59
+ Router . get_path (
60
+ assigns . socket ,
61
+ assigns . live_resource ,
62
+ assigns . params ,
63
+ :index ,
64
+ Map . merge ( assigns . query_options , % { order_direction: next_order_direction , order_by: assigns . name } )
65
+ )
66
+
57
67
assigns =
58
68
assigns
59
- |> assign ( :next_order_direction , order_direction )
69
+ |> assign ( :next_order_direction , next_order_direction )
70
+ |> assign ( :patch_link , patch_link )
60
71
61
72
~H"""
62
- < . link
63
- class = "flex items-center space-x-1 "
64
- patch = {
65
- Router . get_path (
66
- @ socket ,
67
- @ live_resource ,
68
- @ params ,
69
- :index ,
70
- Map . merge ( @ query_options , % { order_direction: @ next_order_direction , order_by: @ name } )
71
- )
72
- }
73
- replace
74
- >
73
+ < . link class = "flex items-center space-x-1 " patch = { @ patch_link } replace >
75
74
< p > { @ label } </ p >
76
75
<%= if @ name == @ query_options . order_by do %>
77
- { order_icon ( assigns , @ query_options . order_direction ) }
76
+ < . icon :if = { @ next_order_direction == :asc } name = "hero-arrow-down-solid " class = "size-4 " />
77
+ < . icon :if = { @ next_order_direction == :desc } name = "hero-arrow-up-solid " class = "size-4 " />
78
78
<% end %>
79
79
</ . link >
80
80
"""
81
81
end
82
82
83
- defp order_icon ( assigns , :asc ) do
84
- ~H"""
85
- < Backpex.HTML.CoreComponents . icon name = "hero-arrow-up-solid " class = "h-4 w-4 " />
86
- """
87
- end
88
-
89
- defp order_icon ( assigns , :desc ) do
90
- ~H"""
91
- < Backpex.HTML.CoreComponents . icon name = "hero-arrow-down-solid " class = "h-4 w-4 " />
92
- """
93
- end
94
-
95
83
@ doc """
96
84
Renders the field of the given resource.
97
85
"""
@@ -1046,7 +1034,4 @@ defmodule Backpex.HTML.Resource do
1046
1034
defp align_class ( :right ) , do: "justify-end text-right"
1047
1035
defp align_class ( :center ) , do: "justify-center text-center"
1048
1036
defp align_class ( _alignment ) , do: "justify-start text-left"
1049
-
1050
- defp toggle_order_direction ( :asc ) , do: :desc
1051
- defp toggle_order_direction ( :desc ) , do: :asc
1052
1037
end
0 commit comments