1
0

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
6 changed files with 60 additions and 17 deletions

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)
}