From c9700906f5d4d066aa43d32197eecf3f789ae32b Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 20 May 2021 16:51:41 +0200 Subject: [PATCH] Paginate the list of conversations Signed-off-by: Thomas Citharel --- js/src/graphql/group.ts | 6 ++- js/src/views/Discussions/DiscussionsList.vue | 49 ++++++++++++++++++++ lib/graphql/resolvers/discussion.ex | 4 +- lib/graphql/schema/actors/group.ex | 6 +++ 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/js/src/graphql/group.ts b/js/src/graphql/group.ts index b6a74d94c..a60108b4c 100644 --- a/js/src/graphql/group.ts +++ b/js/src/graphql/group.ts @@ -119,7 +119,7 @@ export const GROUP_FIELDS_FRAGMENTS = gql` } total } - discussions { + discussions(page: $discussionsPage, limit: $discussionsLimit) { total elements { ...DiscussionBasicFields @@ -198,6 +198,8 @@ export const FETCH_GROUP = gql` $postsLimit: Int $membersPage: Int $membersLimit: Int + $discussionsPage: Int + $discussionsLimit: Int ) { group(preferredUsername: $name) { ...GroupFullFields @@ -220,6 +222,8 @@ export const GET_GROUP = gql` $postsLimit: Int $membersPage: Int $membersLimit: Int + $discussionsPage: Int + $discussionsLimit: Int ) { getGroup(id: $id) { mediaSize diff --git a/js/src/views/Discussions/DiscussionsList.vue b/js/src/views/Discussions/DiscussionsList.vue index 07dbfc00a..1d552c6bf 100644 --- a/js/src/views/Discussions/DiscussionsList.vue +++ b/js/src/views/Discussions/DiscussionsList.vue @@ -49,6 +49,17 @@ v-for="discussion in group.discussions.elements" :key="discussion.id" /> + + {{ $t("There's no discussions yet") }} @@ -82,6 +93,10 @@ import { } from "@/graphql/actor"; import { IMember } from "@/types/actor/member.model"; import EmptyContent from "@/components/Utils/EmptyContent.vue"; +import VueRouter from "vue-router"; +const { isNavigationFailure, NavigationFailureType } = VueRouter; + +const DISCUSSIONS_PER_PAGE = 10; @Component({ components: { DiscussionListItem, EmptyContent }, @@ -92,6 +107,8 @@ import EmptyContent from "@/components/Utils/EmptyContent.vue"; variables() { return { name: this.preferredUsername, + discussionsPage: this.page, + discussionsLimit: DISCUSSIONS_PER_PAGE, }; }, skip() { @@ -154,6 +171,18 @@ export default class DiscussionsList extends Vue { usernameWithDomain = usernameWithDomain; + DISCUSSIONS_PER_PAGE = DISCUSSIONS_PER_PAGE; + + get page(): number { + return parseInt((this.$route.query.page as string) || "1", 10); + } + + set page(page: number) { + this.pushRouter(RouteName.DISCUSSION_LIST, { + page: page.toString(), + }); + } + get groupMemberships(): (string | undefined)[] { if (!this.person || !this.person.id) return []; return this.person.memberships.elements @@ -174,10 +203,30 @@ export default class DiscussionsList extends Vue { this.groupMemberships.includes(this.group.id) ); } + + protected async pushRouter( + routeName: string, + args: Record + ): Promise { + try { + await this.$router.push({ + name: routeName, + query: { ...this.$route.query, ...args }, + }); + } catch (e) { + if (isNavigationFailure(e, NavigationFailureType.redirected)) { + throw Error(e.toString()); + } + } + } } diff --git a/lib/graphql/resolvers/discussion.ex b/lib/graphql/resolvers/discussion.ex index 73b9405ac..9603f3cf1 100644 --- a/lib/graphql/resolvers/discussion.ex +++ b/lib/graphql/resolvers/discussion.ex @@ -14,7 +14,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Discussion do def find_discussions_for_actor( %Actor{id: group_id}, - _args, + %{page: page, limit: limit}, %{ context: %{ current_user: %User{} = user @@ -24,7 +24,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Discussion do with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)}, {:ok, %Actor{type: :Group} = group} <- Actors.get_group_by_actor_id(group_id) do - {:ok, Discussions.find_discussions_for_actor(group)} + {:ok, Discussions.find_discussions_for_actor(group, page, limit)} else {:member, false} -> {:ok, %Page{total: 0, elements: []}} diff --git a/lib/graphql/schema/actors/group.ex b/lib/graphql/schema/actors/group.ex index 58b1c5be9..b0917a49a 100644 --- a/lib/graphql/schema/actors/group.ex +++ b/lib/graphql/schema/actors/group.ex @@ -86,6 +86,12 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do end field :discussions, :paginated_discussion_list do + arg(:page, :integer, + default_value: 1, + description: "The page in the paginated discussion list" + ) + + arg(:limit, :integer, default_value: 10, description: "The limit of discussions per page") resolve(&Discussion.find_discussions_for_actor/3) description("A list of the discussions for this group") end