diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index ec9b5b9..38f03c5 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -1,6 +1,8 @@ import { inject } from '@angular/core'; import { Router, type Routes } from '@angular/router'; import { AuthService } from './common/services/auth.service'; +import { from } from 'rxjs'; +import { RETROSKI_DB } from './common/db/db'; export const AuthGuard = () => { const authService = inject(AuthService); @@ -29,6 +31,10 @@ export const AvailableGuard = () => { return authService.isAvailable$(); }; +export const DatabaseReady = () => { + return from(RETROSKI_DB.populate()); +}; + export const routes: Routes = [ { path: '', loadComponent: () => import('./pages/home/home.component').then(m => m.HomeComponent) }, { @@ -45,7 +51,8 @@ export const routes: Routes = [ }, { path: 'ride-local', - loadComponent: () => import('./pages/ride-local/ride-local.component').then(m => m.RideLocalComponent) + loadComponent: () => import('./pages/ride-local/ride-local.component').then(m => m.RideLocalComponent), + canActivate: [DatabaseReady] }, { path: 'login', diff --git a/src/app/common/db/db.ts b/src/app/common/db/db.ts index 23032c7..fbddd74 100644 --- a/src/app/common/db/db.ts +++ b/src/app/common/db/db.ts @@ -15,10 +15,22 @@ export class RetroskiDB extends Dexie { ghosts: 'trackId, eventId', records: '++id, trackId, rider' }); - this.on('populate', () => this.populate()); } - async populate() {} + public async populate() { + const count = await this.tracks.count(); + if (count === 0) { + try { + // Charger le fichier JSON depuis les assets + const response = await fetch('/assets/tracks/tracks.json'); + const defaultTracks: StockableTrack[] = await response.json(); + await this.tracks.bulkAdd(defaultTracks); + console.log('Default tracks added to the local DB'); + } catch (error) { + console.warn('Unable to load default tracks', error); + } + } + } } export const RETROSKI_DB = new RetroskiDB(); diff --git a/src/app/pages/ride-local/ride-local.component.html b/src/app/pages/ride-local/ride-local.component.html index bfdec0d..814e320 100644 --- a/src/app/pages/ride-local/ride-local.component.html +++ b/src/app/pages/ride-local/ride-local.component.html @@ -3,6 +3,7 @@ +@if(availableTracks()) {
Ride local
Track setup
@@ -11,7 +12,7 @@ class="retro-form-line" [class.invalid]="form.get('track')?.invalid" > - @if(availableTracks()?.length) { + @if(availableTracks()!.length) {
Track