mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
38 lines
821 B
TypeScript
38 lines
821 B
TypeScript
import { IStoreItem } from '../interfaces/IStoreItem'
|
|
|
|
const LOCAL_STORAGE_KEY = 'pdfer-data'
|
|
|
|
function readData(): IStoreItem[] {
|
|
const dataString = localStorage.getItem(LOCAL_STORAGE_KEY)
|
|
|
|
let data = []
|
|
|
|
if (dataString) {
|
|
data = JSON.parse(dataString)
|
|
}
|
|
|
|
return data
|
|
}
|
|
|
|
function storeData(data: IStoreItem[]): void {
|
|
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(data))
|
|
}
|
|
|
|
function withoutId(data: IStoreItem[], id: string) {
|
|
return data.filter((item) => item.id !== id)
|
|
}
|
|
|
|
export function addDocument(templateData: IStoreItem) {
|
|
const data = [templateData, ...withoutId(readData(), templateData.id)]
|
|
storeData(data)
|
|
}
|
|
|
|
export function removeDocument(id: string) {
|
|
const newData = withoutId(readData(), id)
|
|
storeData(newData)
|
|
}
|
|
|
|
export function getDocuments() {
|
|
return readData()
|
|
}
|