Consistent code formatting
This commit is contained in:
@@ -1,80 +1,76 @@
|
||||
import type { Adventure } from '$lib/utils/types';
|
||||
import type { Adventure } from "$lib/utils/types";
|
||||
|
||||
let adventures: Adventure[] = [];
|
||||
|
||||
import { visitCount } from '$lib/utils/stores/visitCountStore';
|
||||
import { visitCount } from "$lib/utils/stores/visitCountStore";
|
||||
|
||||
// Check if localStorage is available (browser environment)
|
||||
const isBrowser = typeof window !== 'undefined';
|
||||
|
||||
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);
|
||||
}
|
||||
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;
|
||||
let nextId = Math.max(0, ...adventures.map((adventure) => adventure.id)) + 1;
|
||||
return nextId;
|
||||
}
|
||||
|
||||
export function setAdventures(importArray: Adventure[]) {
|
||||
adventures = importArray
|
||||
adventures = importArray;
|
||||
}
|
||||
|
||||
export function addAdventure(adventure: Adventure) {
|
||||
adventures = [...adventures, adventure];
|
||||
if (isBrowser) {
|
||||
localStorage.setItem('adventures', JSON.stringify(adventures));
|
||||
visitCount.update((n) => n + 1);
|
||||
}
|
||||
console.log(adventures);
|
||||
|
||||
adventures = [...adventures, adventure];
|
||||
if (isBrowser) {
|
||||
localStorage.setItem("adventures", JSON.stringify(adventures));
|
||||
visitCount.update((n) => n + 1);
|
||||
}
|
||||
console.log(adventures);
|
||||
}
|
||||
|
||||
export function getAdventures(): Adventure[] {
|
||||
return adventures;
|
||||
return adventures;
|
||||
}
|
||||
|
||||
export function removeAdventure(event: { detail: number; }) {
|
||||
adventures = adventures.filter(adventure => adventure.id !== event.detail);
|
||||
if (isBrowser) {
|
||||
localStorage.setItem('adventures', JSON.stringify(adventures));
|
||||
visitCount.update((n) => n - 1);
|
||||
}
|
||||
|
||||
export function removeAdventure(event: { detail: number }) {
|
||||
adventures = adventures.filter((adventure) => adventure.id !== event.detail);
|
||||
if (isBrowser) {
|
||||
localStorage.setItem("adventures", JSON.stringify(adventures));
|
||||
visitCount.update((n) => n - 1);
|
||||
}
|
||||
}
|
||||
|
||||
export function saveEdit(adventure:Adventure) {
|
||||
let editId = adventure.id;
|
||||
console.log("saving edit")
|
||||
let editName = adventure.name;
|
||||
let editLocation = adventure.location;
|
||||
let editCreated = adventure.created;
|
||||
let oldAdventure: Adventure | undefined = adventures.find(adventure => adventure.id === editId);
|
||||
console.log("old" + oldAdventure)
|
||||
if (oldAdventure) {
|
||||
oldAdventure.name = editName;
|
||||
oldAdventure.location = editLocation;
|
||||
oldAdventure.created = editCreated;
|
||||
}
|
||||
editId = NaN;
|
||||
console.log("done")
|
||||
if (isBrowser) {
|
||||
localStorage.setItem('adventures', JSON.stringify(adventures));
|
||||
}
|
||||
export function saveEdit(adventure: Adventure) {
|
||||
let editId = adventure.id;
|
||||
console.log("saving edit");
|
||||
let editName = adventure.name;
|
||||
let editLocation = adventure.location;
|
||||
let editCreated = adventure.created;
|
||||
let oldAdventure: Adventure | undefined = adventures.find(
|
||||
(adventure) => adventure.id === editId
|
||||
);
|
||||
console.log("old" + oldAdventure);
|
||||
if (oldAdventure) {
|
||||
oldAdventure.name = editName;
|
||||
oldAdventure.location = editLocation;
|
||||
oldAdventure.created = editCreated;
|
||||
}
|
||||
editId = NaN;
|
||||
console.log("done");
|
||||
if (isBrowser) {
|
||||
localStorage.setItem("adventures", JSON.stringify(adventures));
|
||||
}
|
||||
}
|
||||
|
||||
export function clearAdventures() {
|
||||
adventures = [];
|
||||
if (isBrowser) {
|
||||
localStorage.setItem('adventures', JSON.stringify(adventures));
|
||||
visitCount.set(0);
|
||||
}
|
||||
|
||||
adventures = [];
|
||||
if (isBrowser) {
|
||||
localStorage.setItem("adventures", JSON.stringify(adventures));
|
||||
visitCount.set(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import type { Adventure } from '$lib/utils/types';
|
||||
import { getAdventures } from './adventureService';
|
||||
import type { Adventure } from "$lib/utils/types";
|
||||
import { getAdventures } from "./adventureService";
|
||||
|
||||
export function exportData() {
|
||||
let adventures: Adventure[] = getAdventures()
|
||||
let jsonArray = JSON.stringify(adventures)
|
||||
console.log(jsonArray)
|
||||
let blob = new Blob([jsonArray], {type: "application/json"});
|
||||
let url = URL.createObjectURL(blob);
|
||||
return url
|
||||
}
|
||||
let adventures: Adventure[] = getAdventures();
|
||||
let jsonArray = JSON.stringify(adventures);
|
||||
console.log(jsonArray);
|
||||
let blob = new Blob([jsonArray], { type: "application/json" });
|
||||
let url = URL.createObjectURL(blob);
|
||||
return url;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import type { Adventure } from '$lib/utils/types';
|
||||
import { setAdventures } from './adventureService';
|
||||
import type { Adventure } from "$lib/utils/types";
|
||||
import { setAdventures } from "./adventureService";
|
||||
|
||||
export function importData(file:File) {
|
||||
let reader = new FileReader();
|
||||
reader.onload = function() {
|
||||
let importArray: Adventure[] = JSON.parse(reader.result as string);
|
||||
setAdventures(importArray);
|
||||
}
|
||||
reader.readAsText(file);
|
||||
|
||||
}
|
||||
export function importData(file: File) {
|
||||
let reader = new FileReader();
|
||||
reader.onload = function () {
|
||||
let importArray: Adventure[] = JSON.parse(reader.result as string);
|
||||
setAdventures(importArray);
|
||||
};
|
||||
reader.readAsText(file);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user