Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/master' into schedu…
Browse files Browse the repository at this point in the history
…ling

# Conflicts:
#	src/app/app.module.ts
#	src/app/router.config.ts
  • Loading branch information
AsianPsychoBoy committed Dec 17, 2016
2 parents 07e864c + a4a5171 commit 4545ace
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { UserService } from './shared/model/user.service';
import { CreateSessionComponent } from './scheduling/create-session/create-session.component';
import { SessionComponent } from './session/session.component';
import { DisplaySessionComponent } from './scheduling/display-session/display-session.component';
import { SettingsComponent } from './settings/settings.component';

@NgModule({
declarations: [
Expand All @@ -42,7 +43,9 @@ import { DisplaySessionComponent } from './scheduling/display-session/display-se
RegisterComponent,
CreateSessionComponent,
SessionComponent,
DisplaySessionComponent
DisplaySessionComponent,
RegisterComponent,
SettingsComponent
],
imports: [
BrowserModule,
Expand Down
6 changes: 3 additions & 3 deletions src/app/login/login.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Validators, FormGroup, FormBuilder } from '@angular/forms';
import { AuthService } from '../shared/security/auth.service';
import { UserService } from '../shared/model/user.service';

@Component({
selector: 'app-login',
Expand All @@ -12,7 +12,7 @@ export class LoginComponent implements OnInit {

form: FormGroup;

constructor(private fb: FormBuilder, private router: Router, private authService: AuthService) {
constructor(private fb: FormBuilder, private router: Router, private userService: UserService) {
this.form = this.fb.group({
email: ['', Validators.required],
password: ['', Validators.required]
Expand All @@ -24,7 +24,7 @@ export class LoginComponent implements OnInit {

login() {
const formValue = this.form.value;
this.authService.login(formValue.email, formValue.password).subscribe(
this.userService.login(formValue.email, formValue.password).subscribe(
data => {
this.router.navigate(['/home']);
},
Expand Down
3 changes: 3 additions & 0 deletions src/app/navbar/navbar.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<li class="nav-item">
<a class="nav-link" routerLink="whiteboard" routerLinkActive="active">Whiteboard</a>
</li>
<li class="nav-item">
<a class="nav-link" routerLink="settings" routerLinkActive="active">Settings</a>
</li>
</ul>

<!-- If user is logged out -->
Expand Down
6 changes: 3 additions & 3 deletions src/app/register/register.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Validators, FormGroup, FormBuilder } from '@angular/forms';
import { AuthService } from '../shared/security/auth.service';
import { UserService } from '../shared/model/user.service';

@Component({
selector: 'app-register',
Expand All @@ -12,7 +12,7 @@ export class RegisterComponent implements OnInit {

form: FormGroup;

constructor(private fb: FormBuilder, private router: Router, private authService: AuthService) {
constructor(private fb: FormBuilder, private router: Router, private userService: UserService) {
this.form = this.fb.group({
email: ['', Validators.required],
password: ['', Validators.required]
Expand All @@ -24,7 +24,7 @@ export class RegisterComponent implements OnInit {

register() {
const formValue = this.form.value;
this.authService.register(formValue.email, formValue.password).subscribe(
this.userService.register(formValue.email, formValue.password).subscribe(
data => {
this.router.navigate(['/home']);
},
Expand Down
5 changes: 5 additions & 0 deletions src/app/router.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SchedulingComponent } from './scheduling/scheduling.component';
import { CreateSessionComponent } from './scheduling/create-session/create-session.component';
import { RegisterComponent } from './register/register.component';
import { SessionComponent } from './session/session.component';
import { SettingsComponent } from './settings/settings.component';

export const routerConfig: Route[] = [
{
Expand Down Expand Up @@ -61,5 +62,9 @@ export const routerConfig: Route[] = [
{
path: 'session/:id',
component: SessionComponent
},
{
path: 'settings',
component: SettingsComponent
}
];
7 changes: 7 additions & 0 deletions src/app/settings/settings.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>Settings</h1>
<form [formGroup]="form" (ngSubmit)="saveSettings()">
<h2>Profile picture</h2>
<input type="file" (change)="onPfpChange($event)">
<button>submit</button>
</form>
<h2 *ngIf="finished">changes saved</h2>
Empty file.
28 changes: 28 additions & 0 deletions src/app/settings/settings.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* tslint:disable:no-unused-variable */
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';

import { SettingsComponent } from './settings.component';

describe('SettingsComponent', () => {
let component: SettingsComponent;
let fixture: ComponentFixture<SettingsComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SettingsComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(SettingsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
34 changes: 34 additions & 0 deletions src/app/settings/settings.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Component, OnInit } from '@angular/core';
import { Validators, FormGroup, FormBuilder, FormControl } from '@angular/forms';
import { UserService} from '../shared/model/user.service';

@Component({
selector: 'app-settings',
templateUrl: './settings.component.html',
styleUrls: ['./settings.component.scss']
})
export class SettingsComponent implements OnInit {

form: FormGroup;
userPfp: File;
finished: boolean;

constructor(private fb: FormBuilder, private userService: UserService) {
this.form = fb.group({
});
}

ngOnInit() {
}

saveSettings() {
this.userService.uploadPfp(this.userPfp).subscribe(val => {
this.finished = true;
},
console.log);
}

onPfpChange($event) {
this.userPfp = $event.target.files[0];
}
}
32 changes: 30 additions & 2 deletions src/app/shared/model/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { Injectable, Inject } from '@angular/core';
import { AngularFireDatabase, FirebaseRef } from 'angularfire2';
import { AngularFireDatabase, FirebaseRef, FirebaseAuthState } from 'angularfire2';
import { AuthService } from '../security/auth.service';
import { Observable, Subject } from 'rxjs/Rx';
import { User } from './user';

@Injectable()
export class UserService {

sdkDb: any;
sdkStorage: any;
uid: string;

constructor(@Inject(FirebaseRef) fb, private db: AngularFireDatabase) {
constructor(@Inject(FirebaseRef) fb, private db: AngularFireDatabase, private auth: AuthService) {
this.sdkDb = fb.database().ref();
this.sdkStorage = fb.storage().ref();
auth.auth$.subscribe(val => this.uid = val ? val.uid : undefined);
}

login(email: string, password: string): Observable<FirebaseAuthState> {
return this.auth.login(email, password);
}

register(email: string, password: string): Observable<any> {
return this.auth.register(email, password)
.flatMap(val => {
let newUid = val.uid;
return this.saveUser({email}, newUid);
})
}

saveUser(user: any, uid: string): Observable<any> {
Expand All @@ -30,6 +47,17 @@ export class UserService {
.map(User.fromJson);
}

uploadPfp(pfp: File): Observable<any> {
if (!this.uid) return Observable.throw('Rip no login info');
return Observable.from(this.sdkStorage.child(`userPfps/${this.uid}/`).put(pfp))
.flatMap((snap: any) => {
let userToSave = Object.assign({}, {pfp: snap.metadata.downloadURLs[0]});
let dataToSave = {};
dataToSave[`users/${this.uid}`] = userToSave;
return this.firebaseUpdate(dataToSave);
});
}

private firebaseUpdate(dataToSave) {
const subject = new Subject();

Expand Down
9 changes: 2 additions & 7 deletions src/app/shared/security/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, Subject, BehaviorSubject } from 'rxjs/Rx';
import { FirebaseAuth, FirebaseAuthState } from 'angularfire2';
import { UserService } from '../model/user.service';

@Injectable()
export class AuthService {

auth$: BehaviorSubject<FirebaseAuthState> = new BehaviorSubject(null);

constructor (private auth: FirebaseAuth, private router: Router, private userService: UserService) {
constructor (private auth: FirebaseAuth, private router: Router) {
this.auth.subscribe(
data => {
this.auth$.next(data);
Expand All @@ -25,11 +24,7 @@ export class AuthService {
}

register(email: string, password: string): Observable<FirebaseAuthState> {
return this.fromFirebaseAuthPromise(this.auth.createUser({ email, password }))
.flatMap(val => {
let userUid = this.auth.getAuth().uid;
return this.userService.saveUser({email}, userUid);
});
return this.fromFirebaseAuthPromise(this.auth.createUser({ email, password }));
}

fromFirebaseAuthPromise(promise): Observable<any> {
Expand Down

0 comments on commit 4545ace

Please sign in to comment.