From 9fdba4c7bde18333e2fd4c6310ba506ed8aaaf0e Mon Sep 17 00:00:00 2001 From: Gardient Date: Sat, 13 Jan 2018 10:31:39 +0200 Subject: [PATCH] This is a start --- .angular-cli.json | 2 +- e2e/app.e2e-spec.ts | 2 +- package.json | 6 +- src/app/app-routing.module.ts | 2 - src/app/app.component.html | 8 ++- src/app/app.module.ts | 3 +- src/app/auth.guard.spec.ts | 15 ----- src/app/auth.guard.ts | 19 ------- src/app/gapi.service.spec.ts | 15 ----- src/app/gapi.service.ts | 84 ---------------------------- src/app/header/header.component.html | 20 +++---- src/app/header/header.component.ts | 16 +----- yarn.lock | 20 ------- 13 files changed, 23 insertions(+), 189 deletions(-) delete mode 100644 src/app/auth.guard.spec.ts delete mode 100644 src/app/auth.guard.ts delete mode 100644 src/app/gapi.service.spec.ts delete mode 100644 src/app/gapi.service.ts diff --git a/.angular-cli.json b/.angular-cli.json index a6ca6ed..b9a874b 100644 --- a/.angular-cli.json +++ b/.angular-cli.json @@ -1,7 +1,7 @@ { "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "project": { - "name": "q-contacts" + "name": "gsheets-2-vcf" }, "apps": [ { diff --git a/e2e/app.e2e-spec.ts b/e2e/app.e2e-spec.ts index 0951b9d..ec5405b 100644 --- a/e2e/app.e2e-spec.ts +++ b/e2e/app.e2e-spec.ts @@ -1,6 +1,6 @@ import { AppPage } from './app.po'; -describe('q-contacts App', () => { +describe('gsheets-2-vcf App', () => { let page: AppPage; beforeEach(() => { diff --git a/package.json b/package.json index 472fe66..07545ac 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "q-contacts", + "name": "gsheets-2-vcf", "version": "0.0.0", "license": "MIT", "scripts": { @@ -29,10 +29,6 @@ "@angular/cli": "1.6.3", "@angular/compiler-cli": "^5.0.0", "@angular/language-service": "^5.0.0", - "@types/gapi": "^0.0.35", - "@types/gapi.auth2": "^0.0.46", - "@types/gapi.client": "^1.0.0", - "@types/gapi.client.sheets": "^4.0.0", "@types/jasmine": "~2.5.53", "@types/jasminewd2": "~2.0.2", "@types/node": "~6.0.60", diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index dc8c2d1..7080e28 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -3,8 +3,6 @@ import { Routes, RouterModule } from '@angular/router'; import { HomeComponent } from './home/home.component'; -import { AuthGuard } from './auth.guard'; - const routes: Routes = [ { path: '', diff --git a/src/app/app.component.html b/src/app/app.component.html index 0d4a7b9..6f1aa50 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,4 +1,10 @@ - +
diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 4a7ba58..a9b94d2 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -6,7 +6,6 @@ import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { HeaderComponent } from './header/header.component'; import { HomeComponent } from './home/home.component'; -import { GApiService } from './gapi.service'; @NgModule({ @@ -19,7 +18,7 @@ import { GApiService } from './gapi.service'; BrowserModule, AppRoutingModule ], - providers: [GApiService], + providers: [], bootstrap: [AppComponent] }) export class AppModule { } diff --git a/src/app/auth.guard.spec.ts b/src/app/auth.guard.spec.ts deleted file mode 100644 index 7ed05ee..0000000 --- a/src/app/auth.guard.spec.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { TestBed, async, inject } from '@angular/core/testing'; - -import { AuthGuard } from './auth.guard'; - -describe('AuthGuard', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - providers: [AuthGuard] - }); - }); - - it('should ...', inject([AuthGuard], (guard: AuthGuard) => { - expect(guard).toBeTruthy(); - })); -}); diff --git a/src/app/auth.guard.ts b/src/app/auth.guard.ts deleted file mode 100644 index d05535a..0000000 --- a/src/app/auth.guard.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { Injectable } from '@angular/core'; -import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router'; -import { Observable } from 'rxjs/Observable'; -import { GApiService } from './gapi.service'; - -@Injectable() -export class AuthGuard implements CanActivate { - constructor (private gapiService: GApiService, private router: Router) { } - - canActivate ( - next: ActivatedRouteSnapshot, - state: RouterStateSnapshot): Observable | Promise | boolean { - const authenticated = this.gapiService.isAuthenticated(); - if (!authenticated) { - this.router.navigate(['/']); - } - return authenticated; - } -} diff --git a/src/app/gapi.service.spec.ts b/src/app/gapi.service.spec.ts deleted file mode 100644 index ab3cca8..0000000 --- a/src/app/gapi.service.spec.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { TestBed, inject } from '@angular/core/testing'; - -import { GApiService } from './gapi.service'; - -describe('GapiService', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - providers: [GApiService] - }); - }); - - it('should be created', inject([GApiService], (service: GApiService) => { - expect(service).toBeTruthy(); - })); -}); diff --git a/src/app/gapi.service.ts b/src/app/gapi.service.ts deleted file mode 100644 index 3e7a814..0000000 --- a/src/app/gapi.service.ts +++ /dev/null @@ -1,84 +0,0 @@ -import { bindCallback } from 'rxjs/Observable/bindCallback'; -import { fromPromise } from 'rxjs/Observable/fromPromise'; -import { Injectable } from '@angular/core'; -import { Observable } from 'rxjs/Observable'; -import 'rxjs/add/operator/map'; - -const API_KEY = 'AIzaSyA9BX0C-ku3NBkTSeHY59DiJi02U-DbCe0'; -const CLIENT_ID = '505888163689-q01h4puni0e74t36m04mcksr0g7pti5v.apps.googleusercontent.com'; - -const DISCOVERY_DOCS: string[] = ['https://sheets.googleapis.com/$discovery/rest?version=v4']; -const SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'; - -function gapiRequestToObservable(request: gapi.client.Request): Observable { - return fromPromise>(request).map(res => res.result); -} - -/** - * Takes a positive integer and returns the corresponding column name. - * @param {number} num The positive integer to convert to a column name. - * @return {string} The column name. - */ -function numberToColumnName(num: number): string { - let ret = ''; - for (; num > 0; num = Math.floor(num % 26)) { - num--; // because nobody starts counting columns at 0 and A needs to be 1 - ret = String.fromCharCode(Math.floor(num % 26) + 65) + ret; - } - return ret; -} - -@Injectable() -export class GApiService { - private observableGetAuthInstance: () => Observable; - private authInstance: gapi.auth2.GoogleAuth; - - constructor () { - gapi.load('client:auth2', () => { - gapi.client.init({ - apiKey: API_KEY, - clientId: CLIENT_ID, - discoveryDocs: DISCOVERY_DOCS, - scope: SCOPES - }).then(() => { - this.authInstance = gapi.auth2.getAuthInstance(); - // Listen for sign-in state changes. - this.observableGetAuthInstance = bindCallback(this.authInstance.isSignedIn.listen); - }); - }); - } - - get authStatusChange(): Observable { return this.observableGetAuthInstance(); } - - isAuthenticated (): boolean { - return this.authInstance.isSignedIn.get(); - } - - signIn (): void { - gapi.auth2.getAuthInstance().signIn(); - } - - signOut (): void { - gapi.auth2.getAuthInstance().signOut(); - } - - getSpreadsheet (id: string): Observable { - return gapiRequestToObservable(gapi.client.spreadsheets.get({ - spreadsheetId: id - })); - } - - getDataOnSheet (spreadsheetId: string, sheet: gapi.client.sheets.Sheet) { - gapi.client.spreadsheets.values.get({ - spreadsheetId, - range: sheet.properties.title + '!A1:1', - majorDimension: 'ROWS' - }).then(firstRowResponse => { - const firstRow = firstRowResponse.result.values[0]; - gapi.client.spreadsheets.values.get({ - spreadsheetId, - range: sheet.properties.title + '!A1:' + numberToColumnName(firstRow.length) - }); - }); - } -} diff --git a/src/app/header/header.component.html b/src/app/header/header.component.html index d7db7ba..2ce0b39 100644 --- a/src/app/header/header.component.html +++ b/src/app/header/header.component.html @@ -1,11 +1,9 @@ - + + GSheets-2-VCF + + + diff --git a/src/app/header/header.component.ts b/src/app/header/header.component.ts index 137e18b..4712951 100644 --- a/src/app/header/header.component.ts +++ b/src/app/header/header.component.ts @@ -1,26 +1,16 @@ -import { Component, OnInit, OnDestroy } from '@angular/core'; -import { Observable } from 'rxjs/Observable'; -import { GApiService } from '../gapi.service'; -import { Subscription } from 'rxjs/Subscription'; +import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-header', templateUrl: './header.component.html', styleUrls: ['./header.component.css'] }) -export class HeaderComponent implements OnInit, OnDestroy { - private sub: Subscription; - constructor(private gapiService: GApiService) { +export class HeaderComponent implements OnInit { + constructor() { } loggedIn = false; ngOnInit(): void { - this.loggedIn = this.gapiService.isAuthenticated(); - this.sub = this.gapiService.authStatusChange.subscribe(newValue => this.loggedIn = newValue); - } - - ngOnDestroy(): void { - this.sub.unsubscribe(); } } diff --git a/yarn.lock b/yarn.lock index 615a61c..07b8aa5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -185,26 +185,6 @@ version "0.0.11" resolved "https://registry.yarnpkg.com/@schematics/schematics/-/schematics-0.0.11.tgz#c8f70f270ed38f29b2873248126fd59abd635862" -"@types/gapi.auth2@^0.0.46": - version "0.0.46" - resolved "https://registry.yarnpkg.com/@types/gapi.auth2/-/gapi.auth2-0.0.46.tgz#5c929d3e66d74b84293c65aaa0ce12e0f8d4fb36" - dependencies: - "@types/gapi" "*" - -"@types/gapi.client.sheets@^4.0.0": - version "4.0.0" - resolved "https://registry.yarnpkg.com/@types/gapi.client.sheets/-/gapi.client.sheets-4.0.0.tgz#bfc4f4f24a0292da3b4d7b561d6d67ded7b11ebb" - dependencies: - "@types/gapi.client" "*" - -"@types/gapi.client@*", "@types/gapi.client@^1.0.0": - version "1.0.0" - resolved "https://registry.yarnpkg.com/@types/gapi.client/-/gapi.client-1.0.0.tgz#aa42501465fcf6440421050da70ec724d4c4c4df" - -"@types/gapi@*", "@types/gapi@^0.0.35": - version "0.0.35" - resolved "https://registry.yarnpkg.com/@types/gapi/-/gapi-0.0.35.tgz#8e0d672d66d8c6ce8493f5e8e550344f7ff500dc" - "@types/jasmine@*": version "2.8.3" resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-2.8.3.tgz#f910edc67d69393d562d10f8f3d205ea3f3306bf"