2021-07-06 22:22:57 +02:00
|
|
|
export const getHistory = () => {
|
|
|
|
const savedHistory = localStorage.getItem('listHistory')
|
|
|
|
if (savedHistory === null) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.parse(savedHistory)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function saveListToHistory(list) {
|
|
|
|
const history = getHistory()
|
|
|
|
|
2021-07-17 19:10:05 +02:00
|
|
|
list.id = parseInt(list.id)
|
|
|
|
|
2021-07-06 22:22:57 +02:00
|
|
|
// Remove the element if it already exists in history, preventing duplicates and essentially moving it to the beginning
|
|
|
|
for (const i in history) {
|
|
|
|
if (history[i].id === list.id) {
|
|
|
|
history.splice(i, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 19:10:05 +02:00
|
|
|
// Add the new list to the beginning of the list
|
2021-07-06 22:22:57 +02:00
|
|
|
history.unshift(list)
|
|
|
|
|
|
|
|
if (history.length > 5) {
|
|
|
|
history.pop()
|
|
|
|
}
|
|
|
|
localStorage.setItem('listHistory', JSON.stringify(history))
|
|
|
|
}
|