Skip to content

Commit

Permalink
Merge pull request rapidpro#153 from praekelt/feature/auto-refesh-inbox
Browse files Browse the repository at this point in the history
add 10sec refresh interval
  • Loading branch information
projectmushroom authored Nov 14, 2016
2 parents d05ddd8 + 8b5229f commit 1cbcc2c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
16 changes: 14 additions & 2 deletions karma/test-controllers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,14 @@ describe('controllers:', () ->
# Tests for MessagesController
#=======================================================================
describe('MessagesController', () ->
$intervalSpy = null

beforeEach(() ->
$controller('MessagesController', {$scope: $scope})
inject((_$interval_) ->
$intervalSpy = jasmine.createSpy('$interval', _$interval_).and.callThrough()
)

$controller('MessagesController', {$scope: $scope, $interval: $intervalSpy})

$inboxScope.init('inbox', serverTime)
$scope.init()
Expand All @@ -355,7 +360,7 @@ describe('controllers:', () ->
expect($scope.inactiveLabels).toEqual([test.tea, test.coffee])
)

it('loadOldItems', () ->
it('should loadOldItems and auto-refresh on interval', () ->
fetchOld = spyOnPromise($q, $scope, MessageService, 'fetchOld')

$scope.loadOldItems()
Expand All @@ -366,12 +371,19 @@ describe('controllers:', () ->
}, $scope.startTime, 1)

fetchOld.resolve({results: [test.msg3, test.msg2], hasMore: true})

expect($intervalSpy).toHaveBeenCalledWith($scope.autoRefresh, 10000)

expect($scope.items).toEqual([test.msg3, test.msg2])
expect($scope.oldItemsMore).toEqual(true)
expect($scope.isInfiniteScrollEnabled()).toEqual(true)
)

it 'should cancel the intervals on destroy', ->
spyOn($intervalSpy, 'cancel')
$scope.$destroy()
expect($intervalSpy.cancel).toHaveBeenCalledWith($scope.autoRefresh)

it('getItemFilter', () ->
filter = $scope.getItemFilter()
expect(filter({archived: false})).toEqual(true)
Expand Down
15 changes: 13 additions & 2 deletions static/coffee/controllers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ controllers.controller('BaseItemsController', ['$scope', 'UtilsService', ($scope
#============================================================================
# Incoming messages controller
#============================================================================
controllers.controller('MessagesController', ['$scope', '$timeout', '$uibModal', '$controller', 'CaseService', 'MessageService', 'PartnerService', 'UserService', 'UtilsService', ($scope, $timeout, $uibModal, $controller, CaseService, MessageService, PartnerService, UserService, UtilsService) ->
controllers.controller('MessagesController', ['$scope', '$timeout', '$interval', '$uibModal', '$controller', 'CaseService', 'MessageService', 'PartnerService', 'UserService', 'UtilsService', ($scope, $timeout, $interval, $uibModal, $controller, CaseService, MessageService, PartnerService, UserService, UtilsService) ->
$controller('BaseItemsController', {$scope: $scope})

$scope.advancedSearch = false
Expand Down Expand Up @@ -270,7 +270,18 @@ controllers.controller('MessagesController', ['$scope', '$timeout', '$uibModal',
)

$scope.fetchOldItems = (search, startTime, page) ->
return MessageService.fetchOld(search, startTime, page)
$scope.autoRefresh = ->
MessageService.fetchOld(search, startTime, page).then((data) ->
$scope.items = data.results
$scope.oldItemsMore = data.hasMore
$scope.oldItemsLoading = false
)
$scope.autoRefresh()
$interval.cancel($scope.autoRefresh)
$interval($scope.autoRefresh, 10000)

$scope.$on '$destroy', ->
$interval.cancel($scope.autoRefresh)

$scope.onExpandMessage = (message) ->
$scope.expandedMessageId = message.id
Expand Down

0 comments on commit 1cbcc2c

Please sign in to comment.