From d81b4117f5281968203c0dead5df6ecc2ac90aab Mon Sep 17 00:00:00 2001 From: kolaente Date: Tue, 20 Jul 2021 18:03:38 +0200 Subject: [PATCH] Fix quick actions not working when nonexisting lists where left over in history --- .../quick-actions/quick-actions.vue | 2 +- src/modules/listHistory.test.ts | 16 +++++++++++- src/modules/listHistory.ts | 26 ++++++++++++++++--- src/store/modules/lists.js | 20 ++++++++++++++ src/views/Home.vue | 2 +- src/views/list/settings/delete.vue | 11 +------- 6 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/components/quick-actions/quick-actions.vue b/src/components/quick-actions/quick-actions.vue index 2459329cc..f7f6f3fc8 100644 --- a/src/components/quick-actions/quick-actions.vue +++ b/src/components/quick-actions/quick-actions.vue @@ -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 } diff --git a/src/modules/listHistory.test.ts b/src/modules/listHistory.test.ts index 9b62da69c..0e3a58ae5 100644 --- a/src/modules/listHistory.test.ts +++ b/src/modules/listHistory.test.ts @@ -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() +}) diff --git a/src/modules/listHistory.ts b/src/modules/listHistory.ts index aaa61726a..379910da2 100644 --- a/src/modules/listHistory.ts +++ b/src/modules/listHistory.ts @@ -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) } diff --git a/src/store/modules/lists.js b/src/store/modules/lists.js index 5f5b57db4..fe66d76b7 100644 --- a/src/store/modules/lists.js +++ b/src/store/modules/lists.js @@ -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()) + }, }, } \ No newline at end of file diff --git a/src/views/Home.vue b/src/views/Home.vue index cd434dc09..3e6a2088b 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -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 => diff --git a/src/views/list/settings/delete.vue b/src/views/list/settings/delete.vue index e8ebfdf1e..6a57e3d2d 100644 --- a/src/views/list/settings/delete.vue +++ b/src/views/list/settings/delete.vue @@ -12,17 +12,9 @@