Fix quick actions not working when nonexisting lists where left over in history
This commit is contained in:
parent
176c6462bb
commit
d81b4117f5
6 changed files with 60 additions and 17 deletions
|
@ -127,7 +127,7 @@ export default {
|
||||||
...Object.values(this.$store.state.lists)])]
|
...Object.values(this.$store.state.lists)])]
|
||||||
|
|
||||||
lists = (allLists.filter(l => {
|
lists = (allLists.filter(l => {
|
||||||
if (typeof l === 'undefined') {
|
if (typeof l === 'undefined' || l === null) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {getHistory, saveListToHistory} from './listHistory'
|
import {getHistory, removeListFromHistory, saveListToHistory} from './listHistory'
|
||||||
|
|
||||||
test('return an empty history when none was saved', () => {
|
test('return an empty history when none was saved', () => {
|
||||||
Storage.prototype.getItem = jest.fn(() => null)
|
Storage.prototype.getItem = jest.fn(() => null)
|
||||||
|
@ -65,3 +65,17 @@ test('move a list to the beginning when storing it multiple times', () => {
|
||||||
saveListToHistory({id: 1})
|
saveListToHistory({id: 1})
|
||||||
expect(saved).toBe('[{"id":1},{"id":2}]')
|
expect(saved).toBe('[{"id":1},{"id":2}]')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('remove list from history', () => {
|
||||||
|
let saved: string | null = '[{"id": 1}]'
|
||||||
|
Storage.prototype.getItem = jest.fn(() => null)
|
||||||
|
Storage.prototype.setItem = jest.fn((key: string, lists: string) => {
|
||||||
|
saved = lists
|
||||||
|
})
|
||||||
|
Storage.prototype.removeItem = jest.fn((key: string) => {
|
||||||
|
saved = null
|
||||||
|
})
|
||||||
|
|
||||||
|
removeListFromHistory({id: 1})
|
||||||
|
expect(saved).toBeNull()
|
||||||
|
})
|
||||||
|
|
|
@ -11,10 +11,17 @@ export function getHistory(): ListHistory[] {
|
||||||
return JSON.parse(savedHistory)
|
return JSON.parse(savedHistory)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function saveListToHistory(list: ListHistory) {
|
function saveHistory(history: ListHistory[]) {
|
||||||
const history = getHistory()
|
if (history.length === 0) {
|
||||||
|
localStorage.removeItem('listHistory')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// list.id = parseInt(list.id)
|
localStorage.setItem('listHistory', JSON.stringify(history))
|
||||||
|
}
|
||||||
|
|
||||||
|
export function saveListToHistory(list: ListHistory) {
|
||||||
|
const history: ListHistory[] = getHistory()
|
||||||
|
|
||||||
// Remove the element if it already exists in history, preventing duplicates and essentially moving it to the beginning
|
// Remove the element if it already exists in history, preventing duplicates and essentially moving it to the beginning
|
||||||
history.forEach((l, i) => {
|
history.forEach((l, i) => {
|
||||||
|
@ -29,5 +36,16 @@ export function saveListToHistory(list: ListHistory) {
|
||||||
if (history.length > 5) {
|
if (history.length > 5) {
|
||||||
history.pop()
|
history.pop()
|
||||||
}
|
}
|
||||||
localStorage.setItem('listHistory', JSON.stringify(history))
|
saveHistory(history)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removeListFromHistory(list: ListHistory) {
|
||||||
|
const history: ListHistory[] = getHistory()
|
||||||
|
|
||||||
|
history.forEach((l, i) => {
|
||||||
|
if (l.id === list.id) {
|
||||||
|
history.splice(i, 1)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
saveHistory(history)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
import ListService from '@/services/list'
|
import ListService from '@/services/list'
|
||||||
import {setLoading} from '@/store/helper'
|
import {setLoading} from '@/store/helper'
|
||||||
|
import {removeListFromHistory} from '@/modules/listHistory.ts'
|
||||||
|
|
||||||
const FavoriteListsNamespace = -2
|
const FavoriteListsNamespace = -2
|
||||||
|
|
||||||
|
@ -17,6 +18,9 @@ export default {
|
||||||
Vue.set(state, l.id, l)
|
Vue.set(state, l.id, l)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
removeListById(state, list) {
|
||||||
|
delete state[list.id]
|
||||||
|
},
|
||||||
},
|
},
|
||||||
getters: {
|
getters: {
|
||||||
getListById: state => id => {
|
getListById: state => id => {
|
||||||
|
@ -79,5 +83,21 @@ export default {
|
||||||
})
|
})
|
||||||
.finally(() => cancel())
|
.finally(() => cancel())
|
||||||
},
|
},
|
||||||
|
deleteList(ctx, list) {
|
||||||
|
const cancel = setLoading(ctx, 'lists')
|
||||||
|
const listService = new ListService()
|
||||||
|
|
||||||
|
return listService.delete(list)
|
||||||
|
.then(r => {
|
||||||
|
ctx.commit('removeListById', list)
|
||||||
|
ctx.commit('namespaces/removeListFromNamespaceById', list, {root: true})
|
||||||
|
removeListFromHistory({id: list.id})
|
||||||
|
return Promise.resolve(r)
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
return Promise.reject(e)
|
||||||
|
})
|
||||||
|
.finally(() => cancel())
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
|
@ -91,7 +91,7 @@ export default {
|
||||||
const history = getHistory()
|
const history = getHistory()
|
||||||
return history.map(l => {
|
return history.map(l => {
|
||||||
return this.$store.getters['lists/getListById'](l.id)
|
return this.$store.getters['lists/getListById'](l.id)
|
||||||
})
|
}).filter(l => l !== null)
|
||||||
},
|
},
|
||||||
...mapState({
|
...mapState({
|
||||||
migratorsEnabled: state =>
|
migratorsEnabled: state =>
|
||||||
|
|
|
@ -12,17 +12,9 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import ListService from '@/services/list'
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'list-setting-delete',
|
name: 'list-setting-delete',
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
listService: ListService,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
created() {
|
created() {
|
||||||
this.listService = new ListService()
|
|
||||||
const list = this.$store.getters['lists/getListById'](this.$route.params.listId)
|
const list = this.$store.getters['lists/getListById'](this.$route.params.listId)
|
||||||
this.setTitle(this.$t('list.delete.title', {list: list.title}))
|
this.setTitle(this.$t('list.delete.title', {list: list.title}))
|
||||||
},
|
},
|
||||||
|
@ -30,9 +22,8 @@ export default {
|
||||||
deleteList() {
|
deleteList() {
|
||||||
const list = this.$store.getters['lists/getListById'](this.$route.params.listId)
|
const list = this.$store.getters['lists/getListById'](this.$route.params.listId)
|
||||||
|
|
||||||
this.listService.delete(list)
|
this.$store.dispatch('lists/deleteList', list)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
this.$store.commit('namespaces/removeListFromNamespaceById', list)
|
|
||||||
this.success({message: this.$t('list.delete.success')})
|
this.success({message: this.$t('list.delete.success')})
|
||||||
this.$router.push({name: 'home'})
|
this.$router.push({name: 'home'})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue