|
| 1 | +defmodule Librecov.FileLive.List do |
| 2 | + use Surface.Component |
| 3 | + import Librecov.CommonView |
| 4 | + alias Surface.Components.Link |
| 5 | + alias Surface.Components.LivePatch |
| 6 | + alias Librecov.Common.Icon |
| 7 | + alias Librecov.RepositoryLive.CoverageDiff |
| 8 | + alias Librecov.Router.Helpers, as: Routes |
| 9 | + import Scrivener.HTML |
| 10 | + prop paginator, :struct, required: true |
| 11 | + prop files, :list, required: true |
| 12 | + prop filters, :list, required: false |
| 13 | + prop order, :tuple, required: true |
| 14 | + prop path_fn, :fun, required: true |
| 15 | + prop path_args, :list, required: true |
| 16 | + |
| 17 | + def raw_filters do |
| 18 | + %{ |
| 19 | + "changed" => "Changed", |
| 20 | + "cov_changed" => "Coverage changed", |
| 21 | + "covered" => "Covered", |
| 22 | + "unperfect" => "Unperfect" |
| 23 | + } |
| 24 | + end |
| 25 | + |
| 26 | + defp row_order(order, k) do |
| 27 | + if elem(order, 0) == k && elem(order, 1) == "desc", do: "asc", else: "desc" |
| 28 | + end |
| 29 | + |
| 30 | + defp sort_icon("desc"), do: "down" |
| 31 | + defp sort_icon(:desc), do: "down" |
| 32 | + defp sort_icon("asc"), do: "up" |
| 33 | + defp sort_icon(:asc), do: "up" |
| 34 | + |
| 35 | + def render(assigns) do |
| 36 | + order_args = [order_field: elem(assigns.order, 0), order_direction: elem(assigns.order, 1)] |
| 37 | + |
| 38 | + ~F""" |
| 39 | + <div class="block block-rounded"> |
| 40 | + <div class="block-header block-header-default"> |
| 41 | + <h3 class="block-title">Files <span class="small">({@paginator.total_entries})</span></h3> |
| 42 | + <div class="block-options"> |
| 43 | + <div class="dropdown"> |
| 44 | + <button |
| 45 | + type="button" |
| 46 | + class="btn-block-option" |
| 47 | + id="dropdown-ecom-filters" |
| 48 | + data-bs-toggle="dropdown" |
| 49 | + aria-haspopup="true" |
| 50 | + aria-expanded="false" |
| 51 | + > |
| 52 | + Filters <i class="fa fa-angle-down ms-1" /> |
| 53 | + </button> |
| 54 | + <div class="dropdown-menu dropdown-menu-end" aria-labelledby="dropdown-ecom-filters" style=""> |
| 55 | + {#for {k, v} <- raw_filters()} |
| 56 | + {#if Enum.any?(@filters, &(&1 == k))} |
| 57 | + <LivePatch |
| 58 | + class="dropdown-item d-flex align-items-center justify-content-between" |
| 59 | + label={v} |
| 60 | + to={apply(@path_fn, @path_args ++ [[{:filters, @filters -- [k]} | order_args]])} |
| 61 | + /> |
| 62 | + {#else} |
| 63 | + <LivePatch |
| 64 | + class="dropdown-item d-flex align-items-center justify-content-between" |
| 65 | + label={v} |
| 66 | + to={apply(@path_fn, @path_args ++ [[{:filters, [k | @filters]} | order_args]])} |
| 67 | + /> |
| 68 | + {/if} |
| 69 | + {/for} |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + <div class="block-content"> |
| 75 | + <!-- All Orders Table --> |
| 76 | + <div class="table-responsive"> |
| 77 | + <table class="table table-borderless table-striped table-vcenter"> |
| 78 | + <thead> |
| 79 | + <tr> |
| 80 | + {#for {k, v} <- %{"coverage" => "Coverage", "diff" => "Diff", "name" => "Name"}} |
| 81 | + <th class="d-none d-sm-table-cell text-center"> |
| 82 | + <LivePatch to={apply( |
| 83 | + @path_fn, |
| 84 | + @path_args ++ [[filters: @filters, order_field: k, order_direction: row_order(@order, k)]] |
| 85 | + )}> |
| 86 | + <span>{v}</span> |
| 87 | + <Icon family="fas" icon={"sort-#{elem(@order, 1) |> sort_icon}"} :if={elem(@order, 0) == k} /> |
| 88 | + </LivePatch> |
| 89 | + </th> |
| 90 | + {/for} |
| 91 | + </tr> |
| 92 | + </thead> |
| 93 | + <tbody> |
| 94 | + {#for file <- @files} |
| 95 | + <tr> |
| 96 | + <td class="d-none d-sm-table-cell text-center fs-sm">{format_coverage(file.coverage)}</td> |
| 97 | + <td class="text-center fs-sm"> |
| 98 | + <span class="fw-semibold"> |
| 99 | + <CoverageDiff diff={file.coverage - file.previous_coverage} :if={file.previous_coverage} /> |
| 100 | + </span> |
| 101 | + </td> |
| 102 | +
|
| 103 | + <td class="text-center d-none d-xl-table-cell fs-sm"> |
| 104 | + <Link class="fw-semibold" label={file.name} to={Routes.file_show_path(@socket, :show, file)} /> |
| 105 | + </td> |
| 106 | + </tr> |
| 107 | + {/for} |
| 108 | + </tbody> |
| 109 | + </table> |
| 110 | + </div> |
| 111 | + <!-- END All Orders Table --> |
| 112 | +
|
| 113 | + <!-- Pagination --> |
| 114 | + <nav aria-label="Photos Search Navigation" :if={@paginator.total_pages > 1}> |
| 115 | + {pagination_links( |
| 116 | + @socket, |
| 117 | + @paginator, |
| 118 | + Enum.drop(@path_args, 2), |
| 119 | + path: @path_fn, |
| 120 | + action: Enum.at(@path_args, 1), |
| 121 | + filters: @filters, |
| 122 | + order_field: elem(@order, 0), |
| 123 | + order_direction: elem(@order, 1) |
| 124 | + )} |
| 125 | + </nav> |
| 126 | + <!-- END Pagination --> |
| 127 | + </div> |
| 128 | + </div> |
| 129 | + """ |
| 130 | + end |
| 131 | +end |
0 commit comments