1
0
Fork 0
mirror of https://github.com/overleaf/overleaf.git synced 2025-04-11 18:45:25 +00:00

Merge pull request from overleaf/ns-cmg-history-labels-deletion

Ensure history label dissapears from UI when deleted

GitOrigin-RevId: fe48cbce9fbd70f7008043dc202a5d985bd6c5fb
This commit is contained in:
Jessica Lawshe 2020-06-11 10:36:32 -05:00 committed by Copybot
parent cc018357e1
commit d9e4bda90b
3 changed files with 124 additions and 31 deletions
services/web
frontend/js/ide/history
test/frontend/ide/history

View file

@ -656,35 +656,37 @@ export default (HistoryManager = (function() {
_loadLabels(labels, lastUpdateToV) {
let sortedLabels = this._sortLabelsByVersionAndDate(labels)
let nLabels = sortedLabels.length
let hasPseudoCurrentStateLabel = false
let needsPseudoCurrentStateLabel = false
if (lastUpdateToV) {
hasPseudoCurrentStateLabel =
nLabels > 0 ? sortedLabels[0].isPseudoCurrentStateLabel : false
if (hasPseudoCurrentStateLabel) {
needsPseudoCurrentStateLabel =
nLabels > 1 ? sortedLabels[1].version !== lastUpdateToV : false
} else {
needsPseudoCurrentStateLabel =
nLabels > 0 ? sortedLabels[0].version !== lastUpdateToV : true
}
if (needsPseudoCurrentStateLabel && !hasPseudoCurrentStateLabel) {
let pseudoCurrentStateLabel = {
id: '1',
isPseudoCurrentStateLabel: true,
version: lastUpdateToV,
created_at: new Date().toISOString()
}
sortedLabels.unshift(pseudoCurrentStateLabel)
} else if (
!needsPseudoCurrentStateLabel &&
hasPseudoCurrentStateLabel
) {
sortedLabels.shift()
}
let labelsWithoutPseudoLabel = this._deletePseudoCurrentStateLabelIfExistent(
sortedLabels
)
let labelsWithPseudoLabelIfNeeded = this._addPseudoCurrentStateLabelIfNeeded(
labelsWithoutPseudoLabel,
lastUpdateToV
)
return labelsWithPseudoLabelIfNeeded
}
_deletePseudoCurrentStateLabelIfExistent(labels) {
if (labels.length && labels[0].isPseudoCurrentStateLabel) {
labels.shift()
}
return sortedLabels
return labels
}
_addPseudoCurrentStateLabelIfNeeded(labels, mostRecentVersion) {
if (
(labels.length && labels[0].version !== mostRecentVersion) ||
labels.length === 0
) {
let pseudoCurrentStateLabel = {
id: '1',
isPseudoCurrentStateLabel: true,
version: mostRecentVersion,
created_at: new Date().toISOString()
}
labels.unshift(pseudoCurrentStateLabel)
}
return labels
}
_sortLabelsByVersionAndDate(labels) {

View file

@ -17,7 +17,7 @@ const historyLabelsListController = function($scope, $element, $attrs, _) {
ctrl.isDragging = false
ctrl.versionsWithLabels = []
$scope.$watchCollection('$ctrl.labels', function(labels) {
if (labels != null && labels.length > 0) {
if (labels) {
const groupedLabelsHash = _.groupBy(labels, 'version')
ctrl.versionsWithLabels = _.map(groupedLabelsHash, (labels, version) => {
return {

View file

@ -140,7 +140,7 @@ export default describe('HistoryV2Manager', function() {
}
]
inject(($q, $http, $rootScope) => {
inject(($q, $http, $filter, $rootScope) => {
this.$scope = $rootScope.$new()
this.$scope.project = {
features: {
@ -152,7 +152,8 @@ export default describe('HistoryV2Manager', function() {
}
this.ide = {
$q: $q,
$http: $http
$http: $http,
$filter: $filter
}
this.localStorage = sinon.stub().returns(null)
this.historyManager = new HistoryV2Manager(
@ -543,5 +544,95 @@ export default describe('HistoryV2Manager', function() {
})
})
})
describe('_loadLabels', function() {
it('should return labels list as is if there is a label for the last version', function() {
const labels = [
{
id: '1',
version: 1,
comment: 'foo',
created_at: new Date().toISOString()
},
{
id: '2',
version: 2,
comment: 'bar',
created_at: new Date().toISOString()
},
{
id: '3',
version: 3,
comment: 'baz',
created_at: new Date().toISOString()
}
]
const lastUpdate = 3
const labelsResult = this.historyManager._loadLabels(labels, lastUpdate)
expect(labelsResult).to.have.members(labels)
})
it('should return a labels list with a pseudo current state label if there is no label for the last version', function() {
const labels = [
{
id: '1',
version: 1,
comment: 'foo',
created_at: new Date().toISOString()
},
{
id: '2',
version: 2,
comment: 'bar',
created_at: new Date().toISOString()
},
{
id: '3',
version: 3,
comment: 'baz',
created_at: new Date().toISOString()
}
]
const lastUpdate = 5
const labelsResult = this.historyManager._loadLabels(labels, lastUpdate)
expect(labelsResult).to.include.members(labels)
expect(labelsResult[0].isPseudoCurrentStateLabel).to.equal(true)
expect(labelsResult[0].version).to.equal(5)
})
it('should keep pseudo label when deleting label', function() {
this.historyManager.$scope.history.labels = [
{
id: '1',
version: 1,
comment: 'foo',
created_at: new Date().toISOString()
}
]
const lastUpdate = 5
this.historyManager.$scope.history.labels = this.historyManager._loadLabels(
this.historyManager.$scope.history.labels,
lastUpdate
)
expect(
this.historyManager.$scope.history.labels[0].isPseudoCurrentStateLabel
).to.equal(true)
this.historyManager.$scope.history.labels = this.historyManager._loadLabels(
[],
lastUpdate
)
expect(
this.historyManager.$scope.history.labels[0].isPseudoCurrentStateLabel
).to.equal(true)
expect(this.historyManager.$scope.history.labels[0].version).to.equal(5)
})
})
})
})