1
0

Add typescript support for helper functions (#598)

Co-authored-by: kolaente <k@knt.li>
Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/598
Co-authored-by: konrad <konrad@kola-entertainments.de>
Co-committed-by: konrad <konrad@kola-entertainments.de>
This commit is contained in:
konrad
2021-07-19 18:20:49 +00:00
parent b812c422f9
commit fa8492f97c
12 changed files with 584 additions and 59 deletions

View File

@ -26,9 +26,9 @@ test('store list in history', () => {
})
test('store only the last 5 lists in history', () => {
let saved = null
let saved: string | null = null
Storage.prototype.getItem = jest.fn(() => saved)
Storage.prototype.setItem = jest.fn((key, lists) => {
Storage.prototype.setItem = jest.fn((key: string, lists: string) => {
saved = lists
})
@ -42,9 +42,9 @@ test('store only the last 5 lists in history', () => {
})
test('don\'t store the same list twice', () => {
let saved = null
let saved: string | null = null
Storage.prototype.getItem = jest.fn(() => saved)
Storage.prototype.setItem = jest.fn((key, lists) => {
Storage.prototype.setItem = jest.fn((key: string, lists: string) => {
saved = lists
})
@ -53,22 +53,10 @@ test('don\'t store the same list twice', () => {
expect(saved).toBe('[{"id":1}]')
})
test('don\'t store the same list twice with different id types', () => {
let saved = null
Storage.prototype.getItem = jest.fn(() => saved)
Storage.prototype.setItem = jest.fn((key, lists) => {
saved = lists
})
saveListToHistory({id: 1})
saveListToHistory({id: '1'})
expect(saved).toBe('[{"id":1}]')
})
test('move a list to the beginning when storing it multiple times', () => {
let saved = null
let saved: string | null = null
Storage.prototype.getItem = jest.fn(() => saved)
Storage.prototype.setItem = jest.fn((key, lists) => {
Storage.prototype.setItem = jest.fn((key: string, lists: string) => {
saved = lists
})

View File

@ -1,4 +1,8 @@
export const getHistory = () => {
interface ListHistory {
id: number;
}
export function getHistory(): ListHistory[] {
const savedHistory = localStorage.getItem('listHistory')
if (savedHistory === null) {
return []
@ -7,17 +11,17 @@ export const getHistory = () => {
return JSON.parse(savedHistory)
}
export function saveListToHistory(list) {
export function saveListToHistory(list: ListHistory) {
const history = getHistory()
list.id = parseInt(list.id)
// list.id = parseInt(list.id)
// 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.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)