adding and displaying of entries works"
;
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import moment, { Moment } from "moment";
|
||||
import { Ref, ref } from "vue";
|
||||
|
||||
const localStorageKey = 'entries'
|
||||
|
||||
export const entries: Ref<Entry[]> = ref(parseFromPossibleString(localStorage.getItem(localStorageKey)))
|
||||
|
||||
export interface Entry {
|
||||
name: string
|
||||
text: string | undefined
|
||||
last_reset: Moment
|
||||
}
|
||||
|
||||
export function parseFromPossibleString(input: string | null): Entry[] {
|
||||
if (input === null) {
|
||||
return []
|
||||
}
|
||||
|
||||
const entries: Entry[] = []
|
||||
const rawObjects: any[] = JSON.parse(input)
|
||||
|
||||
for (const rawObject of rawObjects) {
|
||||
const { name, text, last_reset } = rawObject
|
||||
|
||||
if (name && text && last_reset) {
|
||||
entries.push({ name, text, last_reset: moment(last_reset) })
|
||||
}
|
||||
}
|
||||
|
||||
return entries
|
||||
}
|
||||
|
||||
export function save() {
|
||||
localStorage.setItem(localStorageKey, JSON.stringify(entries.value))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user