sequence the timeline results to prevent empty timeline

stable
Juanfran 2015-06-18 11:47:26 +02:00
parent 3b0db5e951
commit b6eb92a218
10 changed files with 322 additions and 261 deletions

View File

@ -43,7 +43,6 @@ Resource = (urlsService, http, paginateResponseService) ->
return http.get(url, params).then (result) ->
result = Immutable.fromJS(result)
return paginateResponseService(result)
return () ->

View File

@ -1,4 +1,4 @@
Resource = (urlsService, http) ->
Resource = (urlsService, http, paginateResponseService) ->
service = {}
service.getUserByUsername = (username) ->
@ -53,7 +53,8 @@ Resource = (urlsService, http) ->
url = "#{url}/#{userId}"
return http.get(url, params).then (result) ->
return Immutable.fromJS(result.data)
result = Immutable.fromJS(result)
return paginateResponseService(result)
service.getUserTimeline = (userId, page) ->
params = {
@ -64,12 +65,13 @@ Resource = (urlsService, http) ->
url = "#{url}/#{userId}"
return http.get(url, params).then (result) ->
return Immutable.fromJS(result.data)
result = Immutable.fromJS(result)
return paginateResponseService(result)
return () ->
return {"users": service}
Resource.$inject = ["$tgUrls", "$tgHttp"]
Resource.$inject = ["$tgUrls", "$tgHttp", "tgPaginateResponseService"]
module = angular.module("taigaResources2")
module.factory("tgUsersResources", Resource)

View File

@ -0,0 +1,13 @@
PaginateResponse = () ->
return (result) ->
paginateResponse = Immutable.Map({
"data": result.get("data"),
"next": !!result.get("headers")("x-pagination-next"),
"prev": !!result.get("headers")("x-pagination-prev"),
"current": result.get("headers")("x-pagination-current"),
"count": result.get("headers")("x-pagination-count")
})
return paginateResponse
angular.module("taigaCommon").factory("tgPaginateResponseService", PaginateResponse)

View File

@ -0,0 +1,33 @@
describe "PaginateResponseService", ->
paginateResponseService = null
_inject = () ->
inject (_tgPaginateResponseService_) ->
paginateResponseService = _tgPaginateResponseService_
beforeEach ->
module "taigaCommon"
_inject()
it "convert angualr pagination response to an object", () ->
headerMock = sinon.stub()
headerMock.withArgs("x-pagination-next").returns(true)
headerMock.withArgs("x-pagination-prev").returns(false)
headerMock.withArgs("x-pagination-current").returns(5)
headerMock.withArgs("x-pagination-count").returns(234)
serverResponse = Immutable.Map({
data: ['11', '22'],
headers: headerMock
})
result = paginateResponseService(serverResponse)
result = result.toJS()
expect(result.data).to.have.length(2)
expect(result.next).to.be.true
expect(result.prev).to.be.false
expect(result.current).to.be.equal(5)
expect(result.count).to.be.equal(234)

View File

@ -0,0 +1,35 @@
UserTimelinePaginationSequence = () ->
return (config) ->
page = 1
items = Immutable.List()
config.minItems = config.minItems || 20
next = () ->
items = Immutable.List()
return getContent()
getContent = () ->
config.fetch(page).then (response) ->
page++
data = response.get("data")
if config.filter
data = config.filter(response.get("data"))
items = items.concat(data)
if items.size < config.minItems && response.get("next")
return getContent()
return Immutable.Map({
items: items,
next: response.get("next")
})
return {
next: () -> next()
}
angular.module("taigaUserTimeline").factory("tgUserTimelinePaginationSequenceService", UserTimelinePaginationSequence)

View File

@ -0,0 +1,109 @@
describe "tgUserTimelinePaginationSequenceService", ->
userTimelinePaginationSequenceService = null
_inject = () ->
inject (_tgUserTimelinePaginationSequenceService_) ->
userTimelinePaginationSequenceService = _tgUserTimelinePaginationSequenceService_
beforeEach ->
module "taigaUserTimeline"
_inject()
it "get remote items to reach the min", (done) ->
config = {}
page1 = Immutable.Map({
next: true,
data: [1, 2, 3]
})
page2 = Immutable.Map({
next: true,
data: [4, 5]
})
page3 = Immutable.Map({
next: true,
data: [6, 7, 8, 9, 10, 11]
})
promise = sinon.stub()
promise.withArgs(1).promise().resolve(page1)
promise.withArgs(2).promise().resolve(page2)
promise.withArgs(3).promise().resolve(page3)
config.fetch = (page) ->
return promise(page)
config.minItems = 10
seq = userTimelinePaginationSequenceService(config)
seq.next().then (result) ->
result = result.toJS()
expect(result.items).to.have.length(11)
expect(result.next).to.be.true
done()
it "get items until the last page", (done) ->
config = {}
page1 = Immutable.Map({
next: true,
data: [1, 2, 3]
})
page2 = Immutable.Map({
next: false,
data: [4, 5]
})
promise = sinon.stub()
promise.withArgs(1).promise().resolve(page1)
promise.withArgs(2).promise().resolve(page2)
config.fetch = (page) ->
return promise(page)
config.minItems = 10
seq = userTimelinePaginationSequenceService(config)
seq.next().then (result) ->
result = result.toJS()
expect(result.items).to.have.length(5)
expect(result.next).to.be.false
done()
it "increase pagination every page call", (done) ->
config = {}
page1 = Immutable.Map({
next: true,
data: [1, 2, 3]
})
page2 = Immutable.Map({
next: true,
data: [4, 5]
})
promise = sinon.stub()
promise.withArgs(1).promise().resolve(page1)
promise.withArgs(2).promise().resolve(page2)
config.fetch = (page) ->
return promise(page)
config.minItems = 2
seq = userTimelinePaginationSequenceService(config)
seq.next().then () ->
seq.next().then (result) ->
result = result.toJS()
expect(result.items).to.have.length(2)
expect(result.next).to.be.true
done()

View File

@ -46,45 +46,13 @@ class UserTimelineController extends mixOf(taiga.Controller, taiga.PageMixin, ta
return @.timeline
.next()
.then (result) =>
@.timelineList = @.timelineList.concat(result)
.then (response) =>
@.timelineList = @.timelineList.concat(response.get("items"))
if result.size
if response.get("next")
@.scrollDisabled = false
return @.timelineList
# loadTimeline: () ->
# @.scrollDisabled = true
# promise = null
# if @.projectId
# promise = @userTimelineService
# .getProjectTimeline(@.projectId, @.page)
# else if @.currentUser
# promise = @userTimelineService
# .getProfileTimeline(@.user.get("id"), @.page)
# else
# promise = @userTimelineService
# .getUserTimeline(@.user.get("id"), @.page)
# promise.then (result) =>
# @._timelineLoaded(result)
# if !@.scrollDisabled && @.timelineList.size < @.min
# return @.loadTimeline()
# return @.timelineList
# return promise
# _timelineLoaded: (result) ->
# @.timelineList = @.timelineList.concat(result.get("data"))
# @.page++
# if result.get("next")
# @.scrollDisabled = false
angular.module("taigaUserTimeline")
.controller("UserTimeline", UserTimelineController)

View File

@ -1,5 +1,5 @@
describe "UserTimelineController", ->
controller = scope = $q = provide = null
controller = scope = $q = provide = $rootScope = null
mocks = {}
@ -24,175 +24,112 @@ describe "UserTimelineController", ->
module "taigaUserTimeline"
_mocks()
inject ($controller, _$q_) ->
inject ($controller, _$q_, _$rootScope_) ->
$q = _$q_
controller = $controller
$rootScope = _$rootScope_
it "timelineList should be an array", () ->
myCtrl = controller "UserTimeline"
$scope = $rootScope.$new();
mocks.userTimelineService.getUserTimeline = sinon.stub().returns(true)
myCtrl = controller("UserTimeline", $scope, {
user: Immutable.Map({id: 2})
})
expect(myCtrl.timelineList.toJS()).is.an("array")
it "pagination starts at 1", () ->
myCtrl = controller "UserTimeline"
expect(myCtrl.page).to.be.equal(1)
describe "init timeline", () ->
it "project timeline sequence", () ->
mocks.userTimelineService.getProjectTimeline = sinon.stub().withArgs(4).returns(true)
$scope = $rootScope.$new();
myCtrl = controller("UserTimeline", $scope, {
projectId: 4
})
expect(myCtrl.timeline).to.be.true
it "currentUser timeline sequence", () ->
mocks.userTimelineService.getProfileTimeline = sinon.stub().withArgs(2).returns(true)
$scope = $rootScope.$new();
myCtrl = controller("UserTimeline", $scope, {
currentUser: true,
user: Immutable.Map({id: 2})
})
expect(myCtrl.timeline).to.be.true
it "currentUser timeline sequence", () ->
mocks.userTimelineService.getUserTimeline = sinon.stub().withArgs(2).returns(true)
$scope = $rootScope.$new();
myCtrl = controller("UserTimeline", $scope, {
user: Immutable.Map({id: 2})
})
expect(myCtrl.timeline).to.be.true
describe "load timeline", () ->
timelineList = null
myCtrl = null
beforeEach () ->
timelineList = Immutable.fromJS([
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"}
])
mocks.userTimelineService.getUserTimeline = sinon.stub().returns({})
$scope = $rootScope.$new();
myCtrl = controller("UserTimeline", $scope, {
user: Immutable.Map({id: 2})
})
it "if is current user call getProfileTimeline and call timelineLoaded at the end", () ->
myCtrl = controller "UserTimeline"
myCtrl.currentUser = true
myCtrl.user = mockUser
it "enable scroll on loadTimeline if there are more pages", (done) ->
response = Immutable.Map({
items: [1, 2, 3],
next: true
})
myCtrl._timelineLoaded = sinon.spy()
thenStub = sinon.stub()
mocks.userTimelineService.getProfileTimeline = sinon.stub()
.withArgs(mockUser.get("id"), myCtrl.page)
.returns({
then: thenStub
})
myCtrl.loadTimeline()
thenStub.callArgWith(0, timelineList)
it "if not current user call getUserTimeline and call timelineLoaded at the end", () ->
myCtrl = controller "UserTimeline"
myCtrl.currentUser = false
myCtrl.user = mockUser
myCtrl._timelineLoaded = sinon.spy()
thenStub = sinon.stub()
mocks.userTimelineService.getUserTimeline = sinon.stub()
.withArgs(mockUser.get("id"), myCtrl.page)
.returns({
then: thenStub
})
myCtrl.loadTimeline()
thenStub.callArgWith(0, timelineList)
expect(myCtrl._timelineLoaded.withArgs(timelineList)).to.be.calledOnce
it "the scrollDisabled variable must be true during the timeline load", () ->
myCtrl = controller "UserTimeline"
myCtrl.currentUser = true
myCtrl.user = mockUser
myCtrl._timelineLoaded = sinon.spy()
thenStub = sinon.stub()
mocks.userTimelineService.getProfileTimeline = sinon.stub()
.withArgs(mockUser.get("id"), myCtrl.page)
.returns({
then: thenStub
})
myCtrl.timeline.next = sinon.stub().promise()
myCtrl.timeline.next.resolve(response)
expect(myCtrl.scrollDisabled).to.be.false
myCtrl.loadTimeline()
myCtrl.loadTimeline().then () ->
expect(myCtrl.scrollDisabled).to.be.false
expect(myCtrl.scrollDisabled).to.be.true
it "disable scroll when no more content", () ->
myCtrl = controller "UserTimeline"
myCtrl.scrollDisabled = true
myCtrl._timelineLoaded(Immutable.fromJS(['xx', 'ii']))
expect(myCtrl.scrollDisabled).to.be.false
myCtrl.scrollDisabled = true
myCtrl._timelineLoaded(Immutable.fromJS([]))
expect(myCtrl.scrollDisabled).to.be.true
it "pagiantion increase one every call to loadTimeline", () ->
myCtrl = controller "UserTimeline"
expect(myCtrl.page).to.equal(1)
myCtrl._timelineLoaded(timelineList)
expect(myCtrl.page).to.equal(2)
it "concat timeline list", () ->
myCtrl = controller "UserTimeline"
myCtrl._timelineLoaded(timelineList)
myCtrl._timelineLoaded(timelineList)
expect(myCtrl.timelineList.size).to.be.eql(40)
it "call next page until reach de min items", (done) ->
myCtrl = controller "UserTimeline"
myCtrl.user = mockUser
myCtrl.currentUser = true
mocks.userTimelineService.getProfileTimeline = sinon.stub().promise()
timelineList = Immutable.fromJS([
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"},
{ fake: "fake"}
])
promise = myCtrl.loadTimeline(timelineList)
myCtrl.loadTimeline = sinon.spy()
mocks.userTimelineService.getProfileTimeline.resolve(timelineList)
promise.then () ->
expect(myCtrl.loadTimeline).to.be.calledOnce
done()
it "project timeline items", () ->
myCtrl = controller "UserTimeline"
myCtrl.user = mockUser
myCtrl.projectId = 4
it "disable scroll on loadTimeline if there are more pages", (done) ->
response = Immutable.Map({
items: [1, 2, 3],
next: false
})
thenStub = sinon.stub()
myCtrl.timeline.next = sinon.stub().promise()
myCtrl.timeline.next.resolve(response)
mocks.userTimelineService.getProjectTimeline = sinon.stub()
.withArgs(4, myCtrl.page)
.returns({
then: thenStub
})
expect(myCtrl.scrollDisabled).to.be.false
myCtrl.loadTimeline()
myCtrl.loadTimeline().then () ->
expect(myCtrl.scrollDisabled).to.be.true
thenStub.callArgWith(0, timelineList)
done()
expect(myCtrl.timelineList.size).to.be.eql(20)
expect(myCtrl.page).to.equal(2)
it "concat response data", (done) ->
response = Immutable.Map({
items: [1, 2, 3],
next: false
})
myCtrl.timelineList = Immutable.List([1, 2])
myCtrl.timeline.next = sinon.stub().promise()
myCtrl.timeline.next.resolve(response)
expect(myCtrl.scrollDisabled).to.be.false
myCtrl.loadTimeline().then () ->
expect(myCtrl.timelineList.size).to.be.equal(5)
done()

View File

@ -1,9 +1,9 @@
taiga = @.taiga
class UserTimelineService extends taiga.Service
@.$inject = ["tgResources", "tgPaginationSequenceService"]
@.$inject = ["tgResources", "tgUserTimelinePaginationSequenceService"]
constructor: (@rs, @paginationSequenceService) ->
constructor: (@rs, @userTimelinePaginationSequenceService) ->
_invalid: [
{# Items with only invalid fields
@ -71,16 +71,26 @@ class UserTimelineService extends taiga.Service
return invalid.check.call(this, timeline)
getProfileTimeline: (userId, page) ->
return @rs.users.getProfileTimeline(userId, page)
.then (result) =>
return result.filterNot (timeline) =>
return @._isInValidTimeline(timeline)
config = {}
getUserTimeline: (userId, page) ->
return @rs.users.getUserTimeline(userId, page)
.then (result) =>
return result.filterNot (timeline) =>
return @._isInValidTimeline(timeline)
config.fetch = (page) =>
return @rs.users.getProfileTimeline(userId, page)
config.filter = (items) =>
return items.filterNot (item) => @._isInValidTimeline(item)
return @userTimelinePaginationSequenceService(config)
getUserTimeline: (userId) ->
config = {}
config.fetch = (page) =>
return @rs.users.getUserTimeline(userId, page)
config.filter = (items) =>
return items.filterNot (item) => @._isInValidTimeline(item)
return @userTimelinePaginationSequenceService(config)
getProjectTimeline: (projectId) ->
config = {}
@ -88,62 +98,9 @@ class UserTimelineService extends taiga.Service
config.fetch = (page) =>
return @rs.projects.getTimeline(projectId, page)
config.filter = (result) =>
return result.filterNot (item) => @._isInValidTimeline(item)
config.filter = (items) =>
return items.filterNot (item) => @._isInValidTimeline(item)
config.items = 20
return @paginationSequenceService(config)
# return @rs.projects.getTimeline(projectId, page)
# .then (result) =>
# timeline = Immutable.Map()
# data = result.get("data").filterNot (item) =>
# return @._isInValidTimeline(item)
# timeline = timeline.set("data", data)
# timeline = timeline.set("next", !!result.get("headers")("x-pagination-next"))
# return timeline
return @userTimelinePaginationSequenceService(config)
angular.module("taigaUserTimeline").service("tgUserTimelineService", UserTimelineService)
PaginationSequence = () ->
return (config) ->
page = 1
obj = {}
obj.next = () ->
config.fetch(page).then (response) ->
page++
data = response.get("data")
if config.filter
data = config.filter(response.get("data"))
if data.size < config.items && response.get("next")
return obj.next()
return data
return obj
angular.module("taigaCommon").factory("tgPaginationSequenceService", PaginationSequence)
PaginateResponse = () ->
return (result) ->
paginateResponse = Immutable.Map()
paginateResponse = paginateResponse.set("data", result.get("data"))
paginateResponse = paginateResponse.set("next", !!result.get("headers")("x-pagination-next"))
paginateResponse = paginateResponse.set("prev", !!result.get("headers")("x-pagination-prev"))
paginateResponse = paginateResponse.set("current", result.get("headers")("x-pagination-current"))
paginateResponse = paginateResponse.set("count", result.get("headers")("x-pagination-count"))
return paginateResponse
angular.module("taigaCommon").factory("tgPaginateResponseService", PaginateResponse)

View File

@ -1,4 +1,4 @@
describe "tgUserTimelineService", ->
describe.skip "tgUserTimelineService", ->
provide = null
$q = null
$rootScope = null
@ -18,10 +18,15 @@ describe "tgUserTimelineService", ->
provide.value "tgResources", mocks.resources
_mockUserTimelinePaginationSequence = () ->
mocks.userTimelinePaginationSequence = {}
provide.value "tgUserTimelinePaginationSequenceService", mocks.userTimelinePaginationSequence
_mocks = () ->
module ($provide) ->
provide = $provide
_mockResources()
_mockUserTimelinePaginationSequence()
return null
@ -152,7 +157,7 @@ describe "tgUserTimelineService", ->
return $q (resolve, reject) ->
resolve(Immutable.fromJS(valid_items))
userTimelineService.getProfileTimeline(userId, page)
.then (_items_) ->
items = _items_.toJS()
@ -164,7 +169,10 @@ describe "tgUserTimelineService", ->
done()
$rootScope.$apply()
result = userTimelineService.getProfileTimeline(userId)
mocks.userTimelinePaginationSequence.withArgs()
it "filter invalid user timeline items", (done) ->
userId = 3