Browse Source

Fixes: Mobile - Don't refresh the ticket list, when going back from the ticket view

Vladimir Sheremet 1 year ago
parent
commit
faa53acb84

+ 10 - 1
app/frontend/apps/mobile/composables/usePagination.ts

@@ -31,8 +31,17 @@ export default function usePagination<
     return pageInfo.value?.hasPreviousPage ?? false
   })
 
+  const getInitialCurrentPage = (): number => {
+    const result: OperationQueryResult = query.result().value || {}
+    const data = result[resultKey] as BaseConnection
+    if (!data) return 1
+    const currentLength = data.edges?.length || 0
+    if (!currentLength) return 1
+    return Math.ceil(currentLength / pageSize)
+  }
+
   const loadingNewPage = ref(false)
-  const currentPage = ref(1)
+  const currentPage = ref(getInitialCurrentPage())
 
   return reactive({
     pageInfo: readonly(pageInfo),

+ 4 - 1
app/frontend/apps/mobile/pages/ticket/components/TicketList/TicketList.vue

@@ -39,7 +39,10 @@ const ticketsQueryVariables = computed(() => {
 })
 
 const ticketsQuery = new QueryHandler(
-  useTicketsByOverviewQuery(ticketsQueryVariables),
+  useTicketsByOverviewQuery(ticketsQueryVariables, {
+    fetchPolicy: 'cache-first',
+    nextFetchPolicy: 'cache-first',
+  }),
 )
 
 const ticketsResult = ticketsQuery.result()

+ 2 - 0
app/graphql/gql/queries/tickets_by_overview.rb

@@ -3,6 +3,8 @@
 module Gql::Queries
   class TicketsByOverview < BaseQuery
 
+    max_page_size 2000
+
     description 'Fetch tickets of a given ticket overview'
 
     argument :overview_id, GraphQL::Types::ID, description: 'Overview ID'

+ 23 - 13
spec/system/apps/mobile/tickets/ticket_list_spec.rb

@@ -7,7 +7,7 @@ RSpec.describe 'Mobile > Tickets', app: :mobile, authenticated_as: :agent, type:
   let(:user)             { create(:user, organization: organization) }
   let(:group)            { create(:group) }
   let(:agent)            { create(:agent) }
-  let(:open_tickets)     { create_list(:ticket, 20, customer: user, organization: organization, group: group, created_by_id: user.id, state: Ticket::State.lookup(name: 'open')) }
+  let(:open_tickets)     { create_list(:ticket, 20, title: 'Test Ticket', customer: user, organization: organization, group: group, created_by_id: user.id, state: Ticket::State.lookup(name: 'open')) }
   let(:overview_tickets) { Ticket::Overviews.tickets_for_overview(Overview.find_by(link: 'all_open'), agent).limit(nil) }
 
   before do
@@ -24,26 +24,36 @@ RSpec.describe 'Mobile > Tickets', app: :mobile, authenticated_as: :agent, type:
     agent.group_names_access_map = Group.all.to_h { |g| [g.name, ['full']] }
   end
 
+  def wait_for_tickets(count:)
+    wait.until do
+      expect(page).to have_css('a[href^="/mobile/tickets/"]:not([href$="/create"])', count: count)
+    end
+  end
+
   context 'when on "Open Tickets" view' do
     before do
       visit '/tickets/view/all_open'
     end
 
-    context 'when checking displayed tickets' do
-      it 'displays 10 tickets by default' do
-        expect(page).to have_link(href: %r{/mobile/tickets/\d+}, count: 10)
-      end
-
-      it 'loads more tickets when scrolling down' do
-        wait.until do
-          expect(page).to have_link(href: %r{/mobile/tickets/\d+}, count: 10)
-        end
+    context 'when going between list and ticket' do
+      it 'going back keeps the scroll position' do
+        wait_for_tickets(count: 10)
 
         page.scroll_to :bottom
 
-        wait.until do
-          expect(page).to have_link(href: %r{/mobile/tickets/\d+}, count: 20)
-        end
+        wait_for_tickets(count: 20)
+
+        link = find_link(href: "/mobile/tickets/#{open_tickets[6].id}")
+        position_old = link.evaluate_script('this.getBoundingClientRect().top')
+
+        link.find('span', text: 'Test Ticket').click
+
+        find_button('Go back').click
+
+        expect_current_route '/tickets/view/all_open'
+
+        position_new = find_link(href: "/mobile/tickets/#{open_tickets[6].id}").evaluate_script('this.getBoundingClientRect().top')
+        expect(position_old).to eq(position_new)
       end
     end