added firebase

This commit is contained in:
Zakary Timson 2019-07-11 20:12:18 -04:00
parent b098b722c4
commit 4199a3cdcc
4 changed files with 34 additions and 3 deletions

View File

@ -18,6 +18,7 @@
"@angular/common": "~8.0.1", "@angular/common": "~8.0.1",
"@angular/compiler": "~8.0.1", "@angular/compiler": "~8.0.1",
"@angular/core": "~8.0.1", "@angular/core": "~8.0.1",
"@angular/fire": "^5.2.1",
"@angular/forms": "~8.0.1", "@angular/forms": "~8.0.1",
"@angular/material": "^8.0.1", "@angular/material": "^8.0.1",
"@angular/platform-browser": "~8.0.1", "@angular/platform-browser": "~8.0.1",
@ -26,6 +27,7 @@
"@angular/router": "~8.0.1", "@angular/router": "~8.0.1",
"@angular/service-worker": "~8.0.1", "@angular/service-worker": "~8.0.1",
"bootstrap-scss": "^4.3.1", "bootstrap-scss": "^4.3.1",
"firebase": "^6.3.0",
"hammerjs": "^2.0.8", "hammerjs": "^2.0.8",
"rxjs": "~6.4.0", "rxjs": "~6.4.0",
"tslib": "^1.9.0", "tslib": "^1.9.0",

View File

@ -13,6 +13,8 @@ import {MaterialModule} from "./material.module";
import {CalibrateComponent} from "./map/calibrate/calibrate.component"; import {CalibrateComponent} from "./map/calibrate/calibrate.component";
import {MatInputModule} from "@angular/material"; import {MatInputModule} from "@angular/material";
import {PermissionsComponent} from "./permissions/permissions.component"; import {PermissionsComponent} from "./permissions/permissions.component";
import {AngularFireModule} from "@angular/fire";
import {AngularFirestoreModule} from "@angular/fire/firestore";
@NgModule({ @NgModule({
declarations: [ declarations: [
@ -24,6 +26,8 @@ import {PermissionsComponent} from "./permissions/permissions.component";
], ],
imports: [ imports: [
AgmCoreModule.forRoot({apiKey: 'AIzaSyDFtvCY6nH_HUoTBNf_5b-E8nRweSLYtxE'}), AgmCoreModule.forRoot({apiKey: 'AIzaSyDFtvCY6nH_HUoTBNf_5b-E8nRweSLYtxE'}),
AngularFireModule.initializeApp(environment.firebaseConfig),
AngularFirestoreModule.enablePersistence(),
AppRouting, AppRouting,
BrowserAnimationsModule, BrowserAnimationsModule,
BrowserModule, BrowserModule,

View File

@ -2,6 +2,7 @@ import {Component} from "@angular/core";
import {Observable, timer} from "rxjs"; import {Observable, timer} from "rxjs";
import {map, take} from "rxjs/operators"; import {map, take} from "rxjs/operators";
import {Router} from "@angular/router"; import {Router} from "@angular/router";
import {SyncService} from "../map/sync.service";
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
@ -13,7 +14,7 @@ export class HomeComponent {
phrase = 'If you\'re into that'; phrase = 'If you\'re into that';
typedText: Observable<string>; typedText: Observable<string>;
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))); 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; let mapCode: string;
do { do {
mapCode = Array(16).fill(0).map(() => chars[Math.round(Math.random() * chars.length)]).join(''); mapCode = Array(16).fill(0).map(() => chars[Math.round(Math.random() * chars.length)]).join('');
} while (false); } while (await this.syncService.exists(mapCode));
this.router.navigate(['/', mapCode]); return this.router.navigate(['/', mapCode]);
} }
} }

View File

@ -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});
}))
}
}