Fix quick actions not working when nonexisting lists where left over in history

This commit is contained in:
kolaente 2021-07-20 18:03:38 +02:00
parent 176c6462bb
commit d81b4117f5
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
6 changed files with 60 additions and 17 deletions

View file

@ -127,7 +127,7 @@ export default {
...Object.values(this.$store.state.lists)])]
lists = (allLists.filter(l => {
if (typeof l === 'undefined') {
if (typeof l === 'undefined' || l === null) {
return false
}

View file

@ -1,4 +1,4 @@
import {getHistory, saveListToHistory} from './listHistory'
import {getHistory, removeListFromHistory, saveListToHistory} from './listHistory'
test('return an empty history when none was saved', () => {
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})
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()
})

View file

@ -11,10 +11,17 @@ export function getHistory(): ListHistory[] {
return JSON.parse(savedHistory)
}
export function saveListToHistory(list: ListHistory) {
const history = getHistory()
function saveHistory(history: ListHistory[]) {
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
history.forEach((l, i) => {
@ -29,5 +36,16 @@ export function saveListToHistory(list: ListHistory) {
if (history.length > 5) {
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)
}

View file

@ -1,6 +1,7 @@
import Vue from 'vue'
import ListService from '@/services/list'
import {setLoading} from '@/store/helper'
import {removeListFromHistory} from '@/modules/listHistory.ts'
const FavoriteListsNamespace = -2
@ -17,6 +18,9 @@ export default {
Vue.set(state, l.id, l)
})
},
removeListById(state, list) {
delete state[list.id]
},
},
getters: {
getListById: state => id => {
@ -79,5 +83,21 @@ export default {
})
.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())
},
},
}

View file

@ -91,7 +91,7 @@ export default {
const history = getHistory()
return history.map(l => {
return this.$store.getters['lists/getListById'](l.id)
})
}).filter(l => l !== null)
},
...mapState({
migratorsEnabled: state =>

View file

@ -12,17 +12,9 @@
</template>
<script>
import ListService from '@/services/list'
export default {
name: 'list-setting-delete',
data() {
return {
listService: ListService,
}
},
created() {
this.listService = new ListService()
const list = this.$store.getters['lists/getListById'](this.$route.params.listId)
this.setTitle(this.$t('list.delete.title', {list: list.title}))
},
@ -30,9 +22,8 @@ export default {
deleteList() {
const list = this.$store.getters['lists/getListById'](this.$route.params.listId)
this.listService.delete(list)
this.$store.dispatch('lists/deleteList', list)
.then(() => {
this.$store.commit('namespaces/removeListFromNamespaceById', list)
this.success({message: this.$t('list.delete.success')})
this.$router.push({name: 'home'})
})