diff --git a/CHANGELOG.md b/CHANGELOG.md index 205cb1ff5..dc807de6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ All notable changes to this project will be documented in this file. ### Changed - Cache the tracking script for 24 hours - Move `entry_page` and `exit_page` to be part of the `Page` filter group +- Paginate /api/sites results and add a `View all` link to the site-switcher dropdown in the dashboard. +- Remove the `+ Add Site` link to the site-switcher dropdown in the dashboard. ## v1.4.1 diff --git a/assets/js/dashboard/site-switcher.js b/assets/js/dashboard/site-switcher.js index 6f2fbd841..8298618da 100644 --- a/assets/js/dashboard/site-switcher.js +++ b/assets/js/dashboard/site-switcher.js @@ -23,7 +23,7 @@ export default class SiteSwitcher extends React.Component { document.addEventListener("keydown", this.handleKeydown); document.addEventListener('click', this.handleClick, false); } - + componentWillUnmount() { this.siteSwitcherButton.current.removeEventListener("click", this.toggle); document.removeEventListener("keydown", this.handleKeydown); @@ -38,7 +38,11 @@ export default class SiteSwitcher extends React.Component { if (!response.ok) { throw response } return response.json() }) - .then((sites) => this.setState({loading: false, sites: sites})) + .then((sites) => this.setState( + { + loading: false, + sites: sites.data.map(s => s.domain) + })) .catch((e) => this.setState({loading: false, error: e})) } @@ -55,7 +59,7 @@ export default class SiteSwitcher extends React.Component { handleKeydown(e) { if (!this.props.loggedIn) return; - + const { site } = this.props; const { sites } = this.state; @@ -73,7 +77,7 @@ export default class SiteSwitcher extends React.Component { toggle(e) { /** * React doesn't seem to prioritise its own events when events are bubbling, and is unable to stop its events from propagating to the document's (root) event listeners which are attached on the DOM. - * + * * A simple trick is to hook up our own click event listener via a ref node, which allows React to manage events in this situation better between the two. */ e.stopPropagation(); @@ -143,12 +147,9 @@ export default class SiteSwitcher extends React.Component { { this.state.sites.map(this.renderSiteLink.bind(this)) }
-
- - - Add Site - -
+ + View all + ) } diff --git a/lib/plausible_web/controllers/api/internal_controller.ex b/lib/plausible_web/controllers/api/internal_controller.ex index 6bd858ba8..31b16ecf6 100644 --- a/lib/plausible_web/controllers/api/internal_controller.ex +++ b/lib/plausible_web/controllers/api/internal_controller.ex @@ -11,14 +11,40 @@ defmodule PlausibleWeb.Api.InternalController do end end - def sites(conn, _) do - if conn.assigns[:current_user] do - user = Repo.preload(conn.assigns[:current_user], :sites) - json(conn, Enum.map(user.sites, & &1.domain)) + def sites(conn, params) do + current_user = conn.assigns[:current_user] + + if current_user do + sites = + sites_for(current_user, params) + |> buildResponse(conn) + + json(conn, sites) else - conn - |> put_status(401) - |> json(%{error: "You need to be logged in to request a list of sites"}) + PlausibleWeb.Api.Helpers.unauthorized( + conn, + "You need to be logged in to request a list of sites" + ) end end + + defp sites_for(user, params) do + Repo.paginate( + from( + s in Plausible.Site, + join: sm in Plausible.Site.Membership, + on: sm.site_id == s.id, + where: sm.user_id == ^user.id, + order_by: s.domain + ), + params + ) + end + + defp buildResponse({sites, pagination}, conn) do + %{ + data: Enum.map(sites, &%{domain: &1.domain}), + pagination: Phoenix.Pagination.JSON.paginate(conn, pagination) + } + end end diff --git a/test/plausible_web/controllers/api/internal_controller_test.exs b/test/plausible_web/controllers/api/internal_controller_test.exs index c5bbbd094..d18089976 100644 --- a/test/plausible_web/controllers/api/internal_controller_test.exs +++ b/test/plausible_web/controllers/api/internal_controller_test.exs @@ -31,7 +31,9 @@ defmodule PlausibleWeb.Api.InternalControllerTest do site2 = insert(:site, members: [user]) conn = get(conn, "/api/sites") - assert json_response(conn, 200) == [site.domain, site2.domain] + %{"data" => sites} = json_response(conn, 200) + + assert Enum.map(sites, & &1["domain"]) == [site.domain, site2.domain] end end