diff --git a/src/App.tsx b/src/App.tsx
index 88eafe1..f1e74a8 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -7,8 +7,7 @@ import ExistingTeam from './ExistingTeam';
import DarkModeToggle from './DarkModeToggle';
import hokLogo from './assets/hokLogo.svg';
import { ProjectDetailsComponent } from './api/types';
-import { getProjectContainer } from './api';
-import { type DB, insertAllFromContainer, open } from './db';
+import { type DB, open } from './db';
function App() {
const [db, setDb] = useState(undefined as DB | undefined);
@@ -27,7 +26,7 @@ function App() {
setParsedUsers([]);
}, []);
- const [containerIsAddedToDb, setContainerIsAddedToDb] = useState(false);
+ const [dbIsReady, setDbIsReady] = useState(false);
useEffect(() => {
let ignore = false;
@@ -41,14 +40,10 @@ function App() {
.then((openedDb) => {
setDb(openedDb);
closeDb = () => openedDb.close();
-
- const container = getProjectContainer(projectEntityId);
-
- return insertAllFromContainer(openedDb, container);
})
.then(() => {
if (!ignore) {
- setContainerIsAddedToDb(true);
+ setDbIsReady(true);
}
});
@@ -62,7 +57,7 @@ function App() {
return undefined;
});
- setContainerIsAddedToDb(false);
+ setDbIsReady(false);
}
}, [selectedProject]);
@@ -96,7 +91,7 @@ function App() {
/>
diff --git a/src/db/index.ts b/src/db/index.ts
index d4ecc63..4ee5f4b 100644
--- a/src/db/index.ts
+++ b/src/db/index.ts
@@ -11,6 +11,7 @@ import {
ServiceDetailsComponent,
ServiceGroupComponent,
} from '../api/types';
+import { getProjectContainer } from '../api';
export type DB = IDBPDatabase;
@@ -81,8 +82,12 @@ export interface ComponentDB extends DBSchema {
}
export const open = async (projectEntityId: string) => {
- return openDB(projectEntityId, 1, {
+ let dbAlreadyExisted = true;
+
+ const db = await openDB(projectEntityId, 1, {
upgrade(db) {
+ dbAlreadyExisted = false;
+
const companyDetailsStore = db.createObjectStore(
ComponentType.CompanyDetails,
{ keyPath: 'id' }
@@ -147,6 +152,16 @@ export const open = async (projectEntityId: string) => {
]);
},
});
+
+ if (!dbAlreadyExisted) {
+ const container = getProjectContainer(projectEntityId);
+
+ await insertAllFromContainer(db, container);
+ } else {
+ // TODO: Check if DB container data is stale
+ }
+
+ return db;
};
export const getPeople = async (db: DB) => {