diff --git a/package.json b/package.json index 54ee770..874ce64 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@angular/common": "~8.0.1", "@angular/compiler": "~8.0.1", "@angular/core": "~8.0.1", + "@angular/fire": "^5.2.1", "@angular/forms": "~8.0.1", "@angular/material": "^8.0.1", "@angular/platform-browser": "~8.0.1", @@ -26,6 +27,7 @@ "@angular/router": "~8.0.1", "@angular/service-worker": "~8.0.1", "bootstrap-scss": "^4.3.1", + "firebase": "^6.3.0", "hammerjs": "^2.0.8", "rxjs": "~6.4.0", "tslib": "^1.9.0", diff --git a/src/app/app.module.ts b/src/app/app.module.ts index e24f342..3804fad 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -13,6 +13,8 @@ import {MaterialModule} from "./material.module"; import {CalibrateComponent} from "./map/calibrate/calibrate.component"; import {MatInputModule} from "@angular/material"; import {PermissionsComponent} from "./permissions/permissions.component"; +import {AngularFireModule} from "@angular/fire"; +import {AngularFirestoreModule} from "@angular/fire/firestore"; @NgModule({ declarations: [ @@ -24,6 +26,8 @@ import {PermissionsComponent} from "./permissions/permissions.component"; ], imports: [ AgmCoreModule.forRoot({apiKey: 'AIzaSyDFtvCY6nH_HUoTBNf_5b-E8nRweSLYtxE'}), + AngularFireModule.initializeApp(environment.firebaseConfig), + AngularFirestoreModule.enablePersistence(), AppRouting, BrowserAnimationsModule, BrowserModule, diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts index 165200d..8cec525 100644 --- a/src/app/home/home.component.ts +++ b/src/app/home/home.component.ts @@ -2,6 +2,7 @@ import {Component} from "@angular/core"; import {Observable, timer} from "rxjs"; import {map, take} from "rxjs/operators"; import {Router} from "@angular/router"; +import {SyncService} from "../map/sync.service"; const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; @@ -13,7 +14,7 @@ export class HomeComponent { phrase = 'If you\'re into that'; typedText: Observable; - constructor(private router: Router) { + constructor(private syncService: SyncService, private router: Router) { this.typedText = timer(750, 50).pipe(take(this.phrase.length), map((i: number) => this.phrase.substring(0, i + 1))); } @@ -21,7 +22,7 @@ export class HomeComponent { let mapCode: string; do { mapCode = Array(16).fill(0).map(() => chars[Math.round(Math.random() * chars.length)]).join(''); - } while (false); - this.router.navigate(['/', mapCode]); + } while (await this.syncService.exists(mapCode)); + return this.router.navigate(['/', mapCode]); } } diff --git a/src/app/map/sync.service.ts b/src/app/map/sync.service.ts new file mode 100644 index 0000000..eadf527 --- /dev/null +++ b/src/app/map/sync.service.ts @@ -0,0 +1,24 @@ +import {Injectable} from "@angular/core"; +import {AngularFirestore, AngularFirestoreCollection, DocumentSnapshot} from "@angular/fire/firestore"; +import {map} from "rxjs/operators"; + +@Injectable({ + providedIn: 'root' +}) +export class SyncService { + private collection: AngularFirestoreCollection; + + constructor(private db: AngularFirestore) { + this.collection = this.db.collection('Maps'); + } + + async exists(mapCode: string) { + return (await this.collection.doc(mapCode).ref.get()).exists; + } + + load(mapCode: string) { + return this.collection.doc(mapCode).snapshotChanges().pipe(map((snap: any) => { + return Object.assign({}, snap.data, {delete: snap.ref.delete, set: snap.ref.set, update: snap.ref.update}); + })) + } +}