fix like/watch for new users & prevent watch twice
parent
ade44bfa86
commit
6269fea418
|
@ -31,10 +31,12 @@ class LikeProjectButtonService extends taiga.Service
|
||||||
|
|
||||||
_updateProjects: (projectId, isFan) ->
|
_updateProjects: (projectId, isFan) ->
|
||||||
projectIndex = @._getProjectIndex(projectId)
|
projectIndex = @._getProjectIndex(projectId)
|
||||||
|
|
||||||
|
return if projectIndex == -1
|
||||||
|
|
||||||
projects = @currentUserService.projects
|
projects = @currentUserService.projects
|
||||||
.get('all')
|
.get('all')
|
||||||
.update projectIndex, (project) ->
|
.update projectIndex, (project) ->
|
||||||
|
|
||||||
totalFans = project.get("total_fans")
|
totalFans = project.get("total_fans")
|
||||||
|
|
||||||
if isFan then totalFans++ else totalFans--
|
if isFan then totalFans++ else totalFans--
|
||||||
|
|
|
@ -118,6 +118,36 @@ describe "tgLikeProjectButtonService", ->
|
||||||
|
|
||||||
done()
|
done()
|
||||||
|
|
||||||
|
it "like, if the user doesn't have the project", (done) ->
|
||||||
|
projectId = 4
|
||||||
|
|
||||||
|
mocks.tgResources.projects.likeProject.withArgs(projectId).promise().resolve()
|
||||||
|
|
||||||
|
newProject = {
|
||||||
|
id: 4,
|
||||||
|
total_fans: 3,
|
||||||
|
is_fan: true
|
||||||
|
}
|
||||||
|
|
||||||
|
mocks.tgProjectService.project = mocks.tgCurrentUserService.projects.getIn(['all', 0])
|
||||||
|
|
||||||
|
mocks.tgCurrentUserService.projects = Immutable.fromJS({
|
||||||
|
all: []
|
||||||
|
})
|
||||||
|
|
||||||
|
projectServiceCheckImmutable = sinon.match ((immutable) ->
|
||||||
|
immutable = immutable.toJS()
|
||||||
|
|
||||||
|
return _.isEqual(immutable, newProject)
|
||||||
|
), 'projectServiceCheckImmutable'
|
||||||
|
|
||||||
|
|
||||||
|
likeButtonService.like(projectId).finally () ->
|
||||||
|
expect(mocks.tgCurrentUserService.setProjects).to.not.have.been.called
|
||||||
|
expect(mocks.tgProjectService.setProject).to.have.been.calledWith(projectServiceCheckImmutable)
|
||||||
|
|
||||||
|
done()
|
||||||
|
|
||||||
it "unlike", (done) ->
|
it "unlike", (done) ->
|
||||||
projectId = 5
|
projectId = 5
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,8 @@ class WatchProjectButtonController
|
||||||
@.showWatchOptions = false
|
@.showWatchOptions = false
|
||||||
|
|
||||||
watch: (notifyLevel) ->
|
watch: (notifyLevel) ->
|
||||||
|
return if notifyLevel == @.project.get('notify_level')
|
||||||
|
|
||||||
@.loading = true
|
@.loading = true
|
||||||
@.closeWatcherOptions()
|
@.closeWatcherOptions()
|
||||||
|
|
||||||
|
|
|
@ -95,6 +95,19 @@ describe "WatchProjectButton", ->
|
||||||
|
|
||||||
done()
|
done()
|
||||||
|
|
||||||
|
it "watch the same option", () ->
|
||||||
|
notifyLevel = 5
|
||||||
|
project = Immutable.fromJS({
|
||||||
|
id: 3,
|
||||||
|
notify_level: 5
|
||||||
|
})
|
||||||
|
|
||||||
|
ctrl = $controller("WatchProjectButton")
|
||||||
|
ctrl.project = project
|
||||||
|
|
||||||
|
result = ctrl.watch(notifyLevel)
|
||||||
|
expect(result).to.be.falsy
|
||||||
|
|
||||||
it "watch, notify error", (done) ->
|
it "watch, notify error", (done) ->
|
||||||
notifyLevel = 5
|
notifyLevel = 5
|
||||||
project = Immutable.fromJS({
|
project = Immutable.fromJS({
|
||||||
|
|
|
@ -37,12 +37,18 @@ class WatchProjectButtonService extends taiga.Service
|
||||||
_updateProjects: (projectId, notifyLevel, isWatcher) ->
|
_updateProjects: (projectId, notifyLevel, isWatcher) ->
|
||||||
projectIndex = @._getProjectIndex(projectId)
|
projectIndex = @._getProjectIndex(projectId)
|
||||||
|
|
||||||
|
return if projectIndex == -1
|
||||||
|
|
||||||
projects = @currentUserService.projects
|
projects = @currentUserService.projects
|
||||||
.get('all')
|
.get('all')
|
||||||
.update projectIndex, (project) =>
|
.update projectIndex, (project) =>
|
||||||
totalWatchers = project.get('total_watchers')
|
totalWatchers = project.get('total_watchers')
|
||||||
|
|
||||||
if isWatcher then totalWatchers++ else totalWatchers--
|
|
||||||
|
if !@projectService.project.get('is_watcher') && isWatcher
|
||||||
|
totalWatchers++
|
||||||
|
else if @projectService.project.get('is_watcher') && !isWatcher
|
||||||
|
totalWatchers--
|
||||||
|
|
||||||
return project.merge({
|
return project.merge({
|
||||||
is_watcher: isWatcher,
|
is_watcher: isWatcher,
|
||||||
|
@ -55,12 +61,15 @@ class WatchProjectButtonService extends taiga.Service
|
||||||
_updateCurrentProject: (notifyLevel, isWatcher) ->
|
_updateCurrentProject: (notifyLevel, isWatcher) ->
|
||||||
totalWatchers = @projectService.project.get("total_watchers")
|
totalWatchers = @projectService.project.get("total_watchers")
|
||||||
|
|
||||||
if isWatcher then totalWatchers++ else totalWatchers--
|
if !@projectService.project.get('is_watcher') && isWatcher
|
||||||
|
totalWatchers++
|
||||||
|
else if @projectService.project.get('is_watcher') && !isWatcher
|
||||||
|
totalWatchers--
|
||||||
|
|
||||||
project = @projectService.project.merge({
|
project = @projectService.project.merge({
|
||||||
is_watcher: isWatcher,
|
is_watcher: isWatcher,
|
||||||
|
notify_level: notifyLevel,
|
||||||
total_watchers: totalWatchers
|
total_watchers: totalWatchers
|
||||||
notify_level: notifyLevel
|
|
||||||
})
|
})
|
||||||
|
|
||||||
@projectService.setProject(project)
|
@projectService.setProject(project)
|
||||||
|
|
|
@ -121,6 +121,71 @@ describe "tgWatchProjectButtonService", ->
|
||||||
), 'projectServiceCheckImmutable'
|
), 'projectServiceCheckImmutable'
|
||||||
|
|
||||||
|
|
||||||
|
watchButtonService.watch(projectId, notifyLevel).finally () ->
|
||||||
|
expect(mocks.tgCurrentUserService.setProjects).to.have.been.calledWith(userServiceCheckImmutable)
|
||||||
|
expect(mocks.tgProjectService.setProject).to.have.been.calledWith(projectServiceCheckImmutable)
|
||||||
|
|
||||||
|
done()
|
||||||
|
|
||||||
|
it "watch, if the user doesn't have the projects", (done) ->
|
||||||
|
projectId = 4
|
||||||
|
notifyLevel = 3
|
||||||
|
|
||||||
|
mocks.tgResources.projects.watchProject.withArgs(projectId, notifyLevel).promise().resolve()
|
||||||
|
|
||||||
|
newProject = {
|
||||||
|
id: 4,
|
||||||
|
total_watchers: 1,
|
||||||
|
is_watcher: true,
|
||||||
|
notify_level: notifyLevel
|
||||||
|
}
|
||||||
|
|
||||||
|
mocks.tgProjectService.project = mocks.tgCurrentUserService.projects.getIn(['all', 0])
|
||||||
|
mocks.tgCurrentUserService.projects = Immutable.fromJS({
|
||||||
|
all: []
|
||||||
|
})
|
||||||
|
|
||||||
|
projectServiceCheckImmutable = sinon.match ((immutable) ->
|
||||||
|
immutable = immutable.toJS()
|
||||||
|
|
||||||
|
return _.isEqual(immutable, newProject)
|
||||||
|
), 'projectServiceCheckImmutable'
|
||||||
|
|
||||||
|
|
||||||
|
watchButtonService.watch(projectId, notifyLevel).finally () ->
|
||||||
|
expect(mocks.tgCurrentUserService.setProjects).to.not.have.been.called
|
||||||
|
expect(mocks.tgProjectService.setProject).to.have.been.calledWith(projectServiceCheckImmutable)
|
||||||
|
|
||||||
|
done()
|
||||||
|
|
||||||
|
it "watch another option", (done) ->
|
||||||
|
projectId = 5
|
||||||
|
notifyLevel = 3
|
||||||
|
|
||||||
|
mocks.tgResources.projects.watchProject.withArgs(projectId, notifyLevel).promise().resolve()
|
||||||
|
|
||||||
|
newProject = {
|
||||||
|
id: 5,
|
||||||
|
total_watchers: 1,
|
||||||
|
is_watcher: true,
|
||||||
|
notify_level: notifyLevel
|
||||||
|
}
|
||||||
|
|
||||||
|
mocks.tgProjectService.project = mocks.tgCurrentUserService.projects.getIn(['all', 1])
|
||||||
|
|
||||||
|
userServiceCheckImmutable = sinon.match ((immutable) ->
|
||||||
|
immutable = immutable.toJS()
|
||||||
|
|
||||||
|
return _.isEqual(immutable[1], newProject)
|
||||||
|
), 'userServiceCheckImmutable'
|
||||||
|
|
||||||
|
projectServiceCheckImmutable = sinon.match ((immutable) ->
|
||||||
|
immutable = immutable.toJS()
|
||||||
|
|
||||||
|
return _.isEqual(immutable, newProject)
|
||||||
|
), 'projectServiceCheckImmutable'
|
||||||
|
|
||||||
|
|
||||||
watchButtonService.watch(projectId, notifyLevel).finally () ->
|
watchButtonService.watch(projectId, notifyLevel).finally () ->
|
||||||
expect(mocks.tgCurrentUserService.setProjects).to.have.been.calledWith(userServiceCheckImmutable)
|
expect(mocks.tgCurrentUserService.setProjects).to.have.been.calledWith(userServiceCheckImmutable)
|
||||||
expect(mocks.tgProjectService.setProject).to.have.been.calledWith(projectServiceCheckImmutable)
|
expect(mocks.tgProjectService.setProject).to.have.been.calledWith(projectServiceCheckImmutable)
|
||||||
|
|
Loading…
Reference in New Issue