!title && !expandOnHover && setOpen(!open)}
>
diff --git a/project-warstone/src/services/game-systems.ts b/project-warstone/src/services/game-systems.ts
index ec95e1d..131d08c 100644
--- a/project-warstone/src/services/game-systems.ts
+++ b/project-warstone/src/services/game-systems.ts
@@ -1,51 +1,45 @@
-import { GameSystem } from '../types/gameSystem';
-import { Schema } from '../types/schema';
+import { GameSystem } from "../types/gameSystem";
+import { Schema } from "../types/schema";
+import { idb } from "./indexeddb";
-const emptySchema = { name: '', types: {}, templates: {}, id: crypto.randomUUID() };
+const emptySchema = {
+ name: "",
+ types: {},
+ templates: {},
+ id: crypto.randomUUID(),
+};
export const GameSystemsService = {
// todo - connect to service to save schema for game
saveSchema: async (schema: Schema) => {
- localStorage.setItem('schema ' + schema.id, JSON.stringify(schema));
-
- return { status: 200 }
+ // localStorage.setItem('schema ' + schema.id, JSON.stringify(schema));
+ try {
+ return await idb.createOrUpdate({
+ storeName: "schema",
+ ...schema,
+ });
+ } catch (e) {
+ console.log(e);
+ }
},
// todo - connect to service to fetch schema for game
- getSchema: async (id: string): Promise<{ status: 200 | 404, json: () => Promise
}> => {
- const schema = localStorage.getItem('schema ' + id);
-
- if (schema)
- return {
- status: 200,
- json: async () => JSON.parse(schema)
- }
-
- return {
- status: 404,
- json: async () => (emptySchema)
- }
- },
- getSchemaList: async () => {
- return {
- status: 200, json: async () => [{
- name: 'Test Schema',
- id: '286f4c18-d280-444b-8d7e-9a3dd09f64ef'
- }]
+ getSchema: async (
+ id: string,
+ ): Promise => {
+ try {
+ const schema = await idb.read("schema", id);
+ return schema;
+ } catch (e) {
+ throw e;
}
},
+ getSchemaList: async () => await idb.listAll("schema", "name"),
+
saveGameSystem: async (gs: GameSystem) => {
- localStorage.setItem('game-system ' + gs.id, JSON.stringify(gs));
- return { status: 200 }
+ localStorage.setItem("game-system " + gs.id, JSON.stringify(gs));
+ return { status: 200 };
},
- getGameSystem: async (id: string): Promise<{ status: 200 | 404, json: () => Promise }> => {
- const gs = localStorage.getItem('game-system ' + id);
- if (!gs) return {status: 404, json:async () => ({
- accolades: [],
- name: '',
- id: crypto.randomUUID(),
- schema: emptySchema,
- schemaId: ''
- })}
- return { status: 200, json:async () => (JSON.parse(gs))}
- }
-}
\ No newline at end of file
+ getGameSystem: async (
+ id: string,
+ ): Promise => await idb.read("game-system", id),
+};
diff --git a/project-warstone/src/services/indexeddb.ts b/project-warstone/src/services/indexeddb.ts
new file mode 100644
index 0000000..b8eb002
--- /dev/null
+++ b/project-warstone/src/services/indexeddb.ts
@@ -0,0 +1,7 @@
+import { IndexedDBService } from "../utils/indexeddb";
+
+export const idb = new IndexedDBService("commander", 1, [
+ "game-system",
+ "publication",
+ "schema",
+]);
diff --git a/project-warstone/src/utils/indexeddb.ts b/project-warstone/src/utils/indexeddb.ts
index 419023c..3406e81 100644
--- a/project-warstone/src/utils/indexeddb.ts
+++ b/project-warstone/src/utils/indexeddb.ts
@@ -1,4 +1,6 @@
-class IndexedDBService {
+type Data = Record & { storeName: string };
+
+export class IndexedDBService {
private dbName: string;
private dbVersion: number;
private storeNames: string[];
@@ -17,7 +19,7 @@ class IndexedDBService {
const request = indexedDB.open(this.dbName, this.dbVersion);
request.onerror = () => {
- reject(new Error('Failed to open the database.'));
+ reject(new Error("Failed to open the database."));
};
request.onsuccess = () => {
@@ -31,13 +33,13 @@ class IndexedDBService {
// Create all the required object stores during the upgrade
for (const storeName of this.storeNames) {
if (!db.objectStoreNames.contains(storeName)) {
- db.createObjectStore(storeName, { keyPath: 'uuid' });
+ db.createObjectStore(storeName, { keyPath: "uuid" });
}
}
};
request.onblocked = () => {
- reject(new Error('Database is blocked and cannot be accessed.'));
+ reject(new Error("Database is blocked and cannot be accessed."));
};
});
}
@@ -46,11 +48,11 @@ class IndexedDBService {
return crypto.randomUUID();
}
- public async create(data: any): Promise {
+ public async create(data: Data): Promise {
const db = await this.openDB();
const uuid = this.generateUUID();
return new Promise((resolve, reject) => {
- const transaction = db.transaction(data.storeName, 'readwrite');
+ const transaction = db.transaction(data.storeName, "readwrite");
const objectStore = transaction.objectStore(data.storeName);
const request = objectStore.add({ ...data, uuid });
@@ -60,7 +62,7 @@ class IndexedDBService {
};
request.onerror = () => {
- reject(new Error('Failed to add data to IndexedDB.'));
+ reject(new Error("Failed to add data to IndexedDB."));
};
});
}
@@ -68,7 +70,7 @@ class IndexedDBService {
public async read(storeName: string, uuid: string): Promise {
const db = await this.openDB();
return new Promise((resolve, reject) => {
- const transaction = db.transaction(storeName, 'readonly');
+ const transaction = db.transaction(storeName, "readonly");
const objectStore = transaction.objectStore(storeName);
const request = objectStore.get(uuid);
@@ -78,25 +80,29 @@ class IndexedDBService {
};
request.onerror = () => {
- reject(new Error('Failed to read data from IndexedDB.'));
+ reject(new Error("Failed to read data from IndexedDB."));
};
});
}
- public async update(storeName: string, uuid: string, newData: any): Promise {
+ public async update(
+ storeName: string,
+ uuid: string,
+ newData: any,
+ ): Promise {
const db = await this.openDB();
return new Promise((resolve, reject) => {
- const transaction = db.transaction(storeName, 'readwrite');
+ const transaction = db.transaction(storeName, "readwrite");
const objectStore = transaction.objectStore(storeName);
- const request = objectStore.put({ ...newData, uuid }, uuid);
+ const request = objectStore.put({ ...newData, uuid });
request.onsuccess = () => {
resolve();
};
request.onerror = () => {
- reject(new Error('Failed to update data in IndexedDB.'));
+ reject(new Error("Failed to update data in IndexedDB."));
};
});
}
@@ -104,7 +110,7 @@ class IndexedDBService {
public async delete(storeName: string, uuid: string): Promise {
const db = await this.openDB();
return new Promise((resolve, reject) => {
- const transaction = db.transaction(storeName, 'readwrite');
+ const transaction = db.transaction(storeName, "readwrite");
const objectStore = transaction.objectStore(storeName);
const request = objectStore.delete(uuid);
@@ -114,16 +120,19 @@ class IndexedDBService {
};
request.onerror = () => {
- reject(new Error('Failed to delete data from IndexedDB.'));
+ reject(new Error("Failed to delete data from IndexedDB."));
};
});
}
- public async listAll(storeName: string, fieldName: string): Promise<[string, any][]> {
+ public async listAll(
+ storeName: string,
+ fieldName: string,
+ ): Promise<[string, T][]> {
const db = await this.openDB();
return new Promise((resolve, reject) => {
- const transaction = db.transaction(storeName, 'readonly');
+ const transaction = db.transaction(storeName, "readonly");
const objectStore = transaction.objectStore(storeName);
const request = objectStore.openCursor();
@@ -143,14 +152,12 @@ class IndexedDBService {
};
request.onerror = () => {
- reject(new Error('Failed to list data from IndexedDB.'));
+ reject(new Error("Failed to list data from IndexedDB."));
};
});
}
- public async createOrUpdate(data: any): Promise {
- const db = await this.openDB();
-
+ public async createOrUpdate(data: Data): Promise {
// If the provided data already has a UUID, check if it exists in the database
if (data.uuid) {
const existingData = await this.read(data.storeName, data.uuid);