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) ->
|
||||
projectIndex = @._getProjectIndex(projectId)
|
||||
|
||||
return if projectIndex == -1
|
||||
|
||||
projects = @currentUserService.projects
|
||||
.get('all')
|
||||
.update projectIndex, (project) ->
|
||||
|
||||
totalFans = project.get("total_fans")
|
||||
|
||||
if isFan then totalFans++ else totalFans--
|
||||
|
|
|
@ -118,6 +118,36 @@ describe "tgLikeProjectButtonService", ->
|
|||
|
||||
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) ->
|
||||
projectId = 5
|
||||
|
||||
|
|
|
@ -34,6 +34,8 @@ class WatchProjectButtonController
|
|||
@.showWatchOptions = false
|
||||
|
||||
watch: (notifyLevel) ->
|
||||
return if notifyLevel == @.project.get('notify_level')
|
||||
|
||||
@.loading = true
|
||||
@.closeWatcherOptions()
|
||||
|
||||
|
|
|
@ -95,6 +95,19 @@ describe "WatchProjectButton", ->
|
|||
|
||||
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) ->
|
||||
notifyLevel = 5
|
||||
project = Immutable.fromJS({
|
||||
|
|
|
@ -40,7 +40,7 @@ ul.watch-options(
|
|||
a(
|
||||
href="",
|
||||
title="{{ 'PROJECT.WATCH_BUTTON.OPTIONS.NOTIFY_INVOLVED_TITLE' | translate }}",
|
||||
ng-click="vm.watch(1)",
|
||||
ng-click="vm.watch(1)",
|
||||
ng-class="{'active': vm.project.get('is_watcher') && vm.project.get('notify_level') == 1}"
|
||||
)
|
||||
span(translate="PROJECT.WATCH_BUTTON.OPTIONS.NOTIFY_INVOLVED")
|
||||
|
|
|
@ -37,12 +37,18 @@ class WatchProjectButtonService extends taiga.Service
|
|||
_updateProjects: (projectId, notifyLevel, isWatcher) ->
|
||||
projectIndex = @._getProjectIndex(projectId)
|
||||
|
||||
return if projectIndex == -1
|
||||
|
||||
projects = @currentUserService.projects
|
||||
.get('all')
|
||||
.update projectIndex, (project) =>
|
||||
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({
|
||||
is_watcher: isWatcher,
|
||||
|
@ -55,12 +61,15 @@ class WatchProjectButtonService extends taiga.Service
|
|||
_updateCurrentProject: (notifyLevel, isWatcher) ->
|
||||
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({
|
||||
is_watcher: isWatcher,
|
||||
notify_level: notifyLevel,
|
||||
total_watchers: totalWatchers
|
||||
notify_level: notifyLevel
|
||||
})
|
||||
|
||||
@projectService.setProject(project)
|
||||
|
|
|
@ -121,6 +121,71 @@ describe "tgWatchProjectButtonService", ->
|
|||
), '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 () ->
|
||||
expect(mocks.tgCurrentUserService.setProjects).to.have.been.calledWith(userServiceCheckImmutable)
|
||||
expect(mocks.tgProjectService.setProject).to.have.been.calledWith(projectServiceCheckImmutable)
|
||||
|
|
Loading…
Reference in New Issue