From f2fc403c44a574dd5b84700f0e933d70c2ea7d43 Mon Sep 17 00:00:00 2001 From: "girish.jangam" <59350195+Girish-Jangam@users.noreply.github.com> Date: Sat, 21 Oct 2023 09:52:26 -0700 Subject: [PATCH] Add Pagination to Package Search Results (#1657) --- .../components/pagination.eml | 27 ++++++++++-------- src/ocamlorg_frontend/pages/blog.eml | 2 +- .../pages/blog.eml:OECustomProperty | Bin 0 -> 143 bytes .../pages/community.eml:OECustomProperty | Bin 0 -> 143 bytes .../industrial_users.eml:OECustomProperty | Bin 0 -> 143 bytes .../pages/install.eml:OECustomProperty | Bin 0 -> 143 bytes .../pages/jobs.eml:OECustomProperty | Bin 0 -> 143 bytes .../pages/not_found.eml:OECustomProperty | Bin 0 -> 143 bytes .../pages/packages_search.eml | 6 +++- .../packages_search.eml:OECustomProperty | Bin 0 -> 143 bytes .../pages/platform.eml:OECustomProperty | Bin 0 -> 143 bytes .../pages/playground.eml:OECustomProperty | Bin 0 -> 143 bytes .../pages/releases.eml:OECustomProperty | Bin 0 -> 143 bytes .../pages/success_story.eml:OECustomProperty | Bin 0 -> 143 bytes src/ocamlorg_web/lib/handler.ml | 24 ++++++++++------ 15 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 src/ocamlorg_frontend/pages/blog.eml:OECustomProperty create mode 100644 src/ocamlorg_frontend/pages/community.eml:OECustomProperty create mode 100644 src/ocamlorg_frontend/pages/industrial_users.eml:OECustomProperty create mode 100644 src/ocamlorg_frontend/pages/install.eml:OECustomProperty create mode 100644 src/ocamlorg_frontend/pages/jobs.eml:OECustomProperty create mode 100644 src/ocamlorg_frontend/pages/not_found.eml:OECustomProperty create mode 100644 src/ocamlorg_frontend/pages/packages_search.eml:OECustomProperty create mode 100644 src/ocamlorg_frontend/pages/platform.eml:OECustomProperty create mode 100644 src/ocamlorg_frontend/pages/playground.eml:OECustomProperty create mode 100644 src/ocamlorg_frontend/pages/releases.eml:OECustomProperty create mode 100644 src/ocamlorg_frontend/pages/success_story.eml:OECustomProperty diff --git a/src/ocamlorg_frontend/components/pagination.eml b/src/ocamlorg_frontend/components/pagination.eml index 0daf6a27b8..5a9c611812 100644 --- a/src/ocamlorg_frontend/components/pagination.eml +++ b/src/ocamlorg_frontend/components/pagination.eml @@ -1,36 +1,39 @@ -let link ~base_url ~page ~current = +let paginated_href ~base_url ~page ~query = + base_url ^ "?p=" ^ string_of_int page ^ (if query = "" then "" else "&q=" ^ query) + +let link ~base_url ~page ~current ~query = let active_class = "border-2 border-current text-primary-600" in let non_active_class = "border border-body-100 text-lighter hover:bg-gray-50 hover:no-underline" in - <%i page %> -let left_arrow ~base_url page = - <%s! Icons.chevron_left "h-5 w-5" %> -let right_arrow ~base_url page = - <%s! Icons.chevron_right "h-5 w-5" %> -let render ~total_page_count ~page_number ~base_url = +let render ~total_page_count ~page_number ~base_url ~query=
- <%s! if page_number != 1 then (left_arrow ~base_url page_number) else "" %> - <%s! link ~base_url ~page:1 ~current:page_number %> + <%s! if page_number != 1 then (left_arrow ~base_url page_number ~query) else "" %> + <%s! link ~base_url ~page:1 ~current:page_number ~query %> <% if page_number >= 4 then ( %> ... <% ); %> <% for page = 2 to (total_page_count - 1) do %> - <%s! if (abs (page_number - page) <= 1) || (page_number = 1 && page = 3) || (page_number = total_page_count && total_page_count - page = 2) then (link ~base_url ~page ~current:page_number) else "" %> + <%s! if (abs (page_number - page) <= 1) || (page_number = 1 && page = 3) || (page_number = total_page_count && total_page_count - page = 2) then (link ~base_url ~page ~current:page_number ~query) else "" %> <% done; %> <% if total_page_count - page_number >= 3 then ( %> ... <% ); %> - <%s! link ~base_url ~page:total_page_count ~current:page_number %> - <%s! if page_number != total_page_count then (right_arrow ~base_url page_number) else "" %> + <%s! link ~base_url ~page:total_page_count ~current:page_number ~query %> + <%s! if page_number != total_page_count then (right_arrow ~base_url page_number ~query) else "" %>
diff --git a/src/ocamlorg_frontend/pages/blog.eml b/src/ocamlorg_frontend/pages/blog.eml index 14e33e0541..2903fbe2d9 100644 --- a/src/ocamlorg_frontend/pages/blog.eml +++ b/src/ocamlorg_frontend/pages/blog.eml @@ -64,7 +64,7 @@ Layout.render <% ); %> - <%s! Pagination.render ~total_page_count:planet_pages_number ~page_number:planet_page ~base_url:Url.blog %> + <%s! Pagination.render ~total_page_count:planet_pages_number ~page_number:planet_page ~base_url:Url.blog ~query:""%>
diff --git a/src/ocamlorg_frontend/pages/blog.eml:OECustomProperty b/src/ocamlorg_frontend/pages/blog.eml:OECustomProperty new file mode 100644 index 0000000000000000000000000000000000000000..a996244e3afd2f25bac3a290bfd8ffa0049a42b6 GIT binary patch literal 143 zcmeBXU|`S%V#DBo;AL6K8n#m}32wc{aKQhj#05d1xFirW2m`Szqk{v84O3ilUVhGW z-Cd4b)}7WfOI-E@q?iMwfEz?W0Zj1)X762rynSvHwT}K?u_50Yq&N+v24pe|hyeuv DS??XF literal 0 HcmV?d00001 diff --git a/src/ocamlorg_frontend/pages/community.eml:OECustomProperty b/src/ocamlorg_frontend/pages/community.eml:OECustomProperty new file mode 100644 index 0000000000000000000000000000000000000000..a996244e3afd2f25bac3a290bfd8ffa0049a42b6 GIT binary patch literal 143 zcmeBXU|`S%V#DBo;AL6K8n#m}32wc{aKQhj#05d1xFirW2m`Szqk{v84O3ilUVhGW z-Cd4b)}7WfOI-E@q?iMwfEz?W0Zj1)X762rynSvHwT}K?u_50Yq&N+v24pe|hyeuv DS??XF literal 0 HcmV?d00001 diff --git a/src/ocamlorg_frontend/pages/industrial_users.eml:OECustomProperty b/src/ocamlorg_frontend/pages/industrial_users.eml:OECustomProperty new file mode 100644 index 0000000000000000000000000000000000000000..a996244e3afd2f25bac3a290bfd8ffa0049a42b6 GIT binary patch literal 143 zcmeBXU|`S%V#DBo;AL6K8n#m}32wc{aKQhj#05d1xFirW2m`Szqk{v84O3ilUVhGW z-Cd4b)}7WfOI-E@q?iMwfEz?W0Zj1)X762rynSvHwT}K?u_50Yq&N+v24pe|hyeuv DS??XF literal 0 HcmV?d00001 diff --git a/src/ocamlorg_frontend/pages/install.eml:OECustomProperty b/src/ocamlorg_frontend/pages/install.eml:OECustomProperty new file mode 100644 index 0000000000000000000000000000000000000000..a996244e3afd2f25bac3a290bfd8ffa0049a42b6 GIT binary patch literal 143 zcmeBXU|`S%V#DBo;AL6K8n#m}32wc{aKQhj#05d1xFirW2m`Szqk{v84O3ilUVhGW z-Cd4b)}7WfOI-E@q?iMwfEz?W0Zj1)X762rynSvHwT}K?u_50Yq&N+v24pe|hyeuv DS??XF literal 0 HcmV?d00001 diff --git a/src/ocamlorg_frontend/pages/jobs.eml:OECustomProperty b/src/ocamlorg_frontend/pages/jobs.eml:OECustomProperty new file mode 100644 index 0000000000000000000000000000000000000000..a996244e3afd2f25bac3a290bfd8ffa0049a42b6 GIT binary patch literal 143 zcmeBXU|`S%V#DBo;AL6K8n#m}32wc{aKQhj#05d1xFirW2m`Szqk{v84O3ilUVhGW z-Cd4b)}7WfOI-E@q?iMwfEz?W0Zj1)X762rynSvHwT}K?u_50Yq&N+v24pe|hyeuv DS??XF literal 0 HcmV?d00001 diff --git a/src/ocamlorg_frontend/pages/not_found.eml:OECustomProperty b/src/ocamlorg_frontend/pages/not_found.eml:OECustomProperty new file mode 100644 index 0000000000000000000000000000000000000000..a996244e3afd2f25bac3a290bfd8ffa0049a42b6 GIT binary patch literal 143 zcmeBXU|`S%V#DBo;AL6K8n#m}32wc{aKQhj#05d1xFirW2m`Szqk{v84O3ilUVhGW z-Cd4b)}7WfOI-E@q?iMwfEz?W0Zj1)X762rynSvHwT}K?u_50Yq&N+v24pe|hyeuv DS??XF literal 0 HcmV?d00001 diff --git a/src/ocamlorg_frontend/pages/packages_search.eml b/src/ocamlorg_frontend/pages/packages_search.eml index 713c679f98..dc0cfb678e 100644 --- a/src/ocamlorg_frontend/pages/packages_search.eml +++ b/src/ocamlorg_frontend/pages/packages_search.eml @@ -1,4 +1,4 @@ -let render ~total ~search (packages : Package.package list) = Layout.render ~title:"OCaml Packages · Search Result" +let render ~total ~search ~page ~number_of_pages (packages : Package.package list) = Layout.render ~title:"OCaml Packages · Search Result" ~description:"Find the package you need to build your application in the thousands of available OCaml packages." ~canonical:(Url.packages_search ^ "?q=" ^ search) ~active_top_nav_item:Header.Packages @@ @@ -42,6 +42,9 @@ let render ~total ~search (packages : Package.package list) = Layout.render ~tit
<% if List.length packages > 0 then ( %> +
+ Showing <%i ((page - 1) * 50 + 1) %> - <%i (min (page * 50) total) %> +
    <% packages |> List.iter (fun (package : Package.package) -> %>
  1. @@ -111,6 +114,7 @@ let render ~total ~search (packages : Package.package list) = Layout.render ~tit <% ); %>
<% ); %> + <%s! Pagination.render ~total_page_count:number_of_pages ~page_number:page ~base_url:Url.packages_search ~query:search %> diff --git a/src/ocamlorg_frontend/pages/packages_search.eml:OECustomProperty b/src/ocamlorg_frontend/pages/packages_search.eml:OECustomProperty new file mode 100644 index 0000000000000000000000000000000000000000..a996244e3afd2f25bac3a290bfd8ffa0049a42b6 GIT binary patch literal 143 zcmeBXU|`S%V#DBo;AL6K8n#m}32wc{aKQhj#05d1xFirW2m`Szqk{v84O3ilUVhGW z-Cd4b)}7WfOI-E@q?iMwfEz?W0Zj1)X762rynSvHwT}K?u_50Yq&N+v24pe|hyeuv DS??XF literal 0 HcmV?d00001 diff --git a/src/ocamlorg_frontend/pages/platform.eml:OECustomProperty b/src/ocamlorg_frontend/pages/platform.eml:OECustomProperty new file mode 100644 index 0000000000000000000000000000000000000000..a996244e3afd2f25bac3a290bfd8ffa0049a42b6 GIT binary patch literal 143 zcmeBXU|`S%V#DBo;AL6K8n#m}32wc{aKQhj#05d1xFirW2m`Szqk{v84O3ilUVhGW z-Cd4b)}7WfOI-E@q?iMwfEz?W0Zj1)X762rynSvHwT}K?u_50Yq&N+v24pe|hyeuv DS??XF literal 0 HcmV?d00001 diff --git a/src/ocamlorg_frontend/pages/playground.eml:OECustomProperty b/src/ocamlorg_frontend/pages/playground.eml:OECustomProperty new file mode 100644 index 0000000000000000000000000000000000000000..a996244e3afd2f25bac3a290bfd8ffa0049a42b6 GIT binary patch literal 143 zcmeBXU|`S%V#DBo;AL6K8n#m}32wc{aKQhj#05d1xFirW2m`Szqk{v84O3ilUVhGW z-Cd4b)}7WfOI-E@q?iMwfEz?W0Zj1)X762rynSvHwT}K?u_50Yq&N+v24pe|hyeuv DS??XF literal 0 HcmV?d00001 diff --git a/src/ocamlorg_frontend/pages/releases.eml:OECustomProperty b/src/ocamlorg_frontend/pages/releases.eml:OECustomProperty new file mode 100644 index 0000000000000000000000000000000000000000..a996244e3afd2f25bac3a290bfd8ffa0049a42b6 GIT binary patch literal 143 zcmeBXU|`S%V#DBo;AL6K8n#m}32wc{aKQhj#05d1xFirW2m`Szqk{v84O3ilUVhGW z-Cd4b)}7WfOI-E@q?iMwfEz?W0Zj1)X762rynSvHwT}K?u_50Yq&N+v24pe|hyeuv DS??XF literal 0 HcmV?d00001 diff --git a/src/ocamlorg_frontend/pages/success_story.eml:OECustomProperty b/src/ocamlorg_frontend/pages/success_story.eml:OECustomProperty new file mode 100644 index 0000000000000000000000000000000000000000..a996244e3afd2f25bac3a290bfd8ffa0049a42b6 GIT binary patch literal 143 zcmeBXU|`S%V#DBo;AL6K8n#m}32wc{aKQhj#05d1xFirW2m`Szqk{v84O3ilUVhGW z-Cd4b)}7WfOI-E@q?iMwfEz?W0Zj1)X762rynSvHwT}K?u_50Yq&N+v24pe|hyeuv DS??XF literal 0 HcmV?d00001 diff --git a/src/ocamlorg_web/lib/handler.ml b/src/ocamlorg_web/lib/handler.ml index 9d114193c8..0cb31a3057 100644 --- a/src/ocamlorg_web/lib/handler.ml +++ b/src/ocamlorg_web/lib/handler.ml @@ -589,17 +589,23 @@ let is_author_match name pattern = match_opt (Some name) || match_opt email || match_opt github_username let packages_search t req = - match Dream.query req "q" with - | Some search -> - let packages = + let packages = + match Dream.query req "q" with + | Some search -> Ocamlorg_package.search ~is_author_match ~sort_by_popularity:true t search - in - let total = List.length packages in - let results = List.map (Package_helper.frontend_package t) packages in - let search = Dream.from_percent_encoded search in - Dream.html (Ocamlorg_frontend.packages_search ~total ~search results) - | None -> Dream.redirect req Url.packages + | None -> Ocamlorg_package.all_latest t + in + let total = List.length packages in + let page, number_of_pages, current_items = paginate ~req ~n:50 packages in + let search = + Dream.from_percent_encoded + (match Dream.query req "q" with Some search -> search | None -> "") + in + let results = List.map (Package_helper.frontend_package t) current_items in + Dream.html + (Ocamlorg_frontend.packages_search ~total ~search ~page ~number_of_pages + results) let packages_autocomplete_fragment t req = match Dream.query req "q" with