frontend/src/modules/listHistory.ts
konrad fa8492f97c Add typescript support for helper functions (#598)
Co-authored-by: kolaente <k@knt.li>
Reviewed-on: vikunja/frontend#598
Co-authored-by: konrad <konrad@kola-entertainments.de>
Co-committed-by: konrad <konrad@kola-entertainments.de>
2021-07-19 18:20:49 +00:00

34 lines
738 B
TypeScript

interface ListHistory {
id: number;
}
export function getHistory(): ListHistory[] {
const savedHistory = localStorage.getItem('listHistory')
if (savedHistory === null) {
return []
}
return JSON.parse(savedHistory)
}
export function saveListToHistory(list: ListHistory) {
const history = getHistory()
// list.id = parseInt(list.id)
// Remove the element if it already exists in history, preventing duplicates and essentially moving it to the beginning
history.forEach((l, i) => {
if (l.id === list.id) {
history.splice(i, 1)
}
})
// Add the new list to the beginning of the list
history.unshift(list)
if (history.length > 5) {
history.pop()
}
localStorage.setItem('listHistory', JSON.stringify(history))
}