first commit

This commit is contained in:
Sean Morley
2024-03-29 21:41:22 +00:00
commit 9addb5462e
18 changed files with 2293 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
import type { Adventure } from '$lib/utils/types';
let adventures: Adventure[] = [];
// Check if localStorage is available (browser environment)
const isBrowser = typeof window !== 'undefined';
// Load adventures from localStorage on startup (only in the browser)
if (isBrowser) {
const storedAdventures = localStorage.getItem('adventures');
if (storedAdventures) {
adventures = JSON.parse(storedAdventures);
}
}
export function getNextId() {
let nextId = Math.max(0, ...adventures.map(adventure => adventure.id)) + 1;
return nextId;
}
export function addAdventure(adventure: Adventure) {
adventures = [...adventures, adventure];
if (isBrowser) {
localStorage.setItem('adventures', JSON.stringify(adventures));
}
console.log(adventures);
}
export function getAdventures(): Adventure[] {
return adventures;
}
export function removeAdventure(event: { detail: string; }) {
adventures = adventures.filter(adventure => adventure.name !== event.detail);
if (isBrowser) {
localStorage.setItem('adventures', JSON.stringify(adventures));
}
}
export function saveEdit(editId:number, editName:string, editLocation:string, editCreated:string) {
const adventure = adventures.find(adventure => adventure.id === editId);
if (adventure) {
adventure.name = editName;
adventure.location = editLocation;
adventure.created = editCreated;
}
editId = NaN;
console.log("done")
if (isBrowser) {
localStorage.setItem('adventures', JSON.stringify(adventures));
}
}