From 5e32897269fae875b860d7e54a8e0b1f528662b0 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 22 Oct 2019 11:04:37 +0200 Subject: [PATCH] registerService saves the user data and shows error messages too --- .../register/register.component.html | 3 +- .../components/register/register.component.ts | 37 ++++++++++++----- src/app/services/register/register.service.ts | 41 +++++++++++++++++-- 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/app/components/register/register.component.html b/src/app/components/register/register.component.html index 4683ee6..a737235 100644 --- a/src/app/components/register/register.component.html +++ b/src/app/components/register/register.component.html @@ -15,10 +15,11 @@ repeat password: -
+
+

{{errorMessage}}


You are already part of greenvironment? - login diff --git a/src/app/components/register/register.component.ts b/src/app/components/register/register.component.ts index 8182ede..b659e5a 100644 --- a/src/app/components/register/register.component.ts +++ b/src/app/components/register/register.component.ts @@ -3,6 +3,7 @@ import {RegisterService} from '../../services/register/register.service'; import {Registration} from '../../models/registration'; import {Router} from '@angular/router'; import {Md5} from 'ts-md5/dist/md5'; +import { parseWebDriverCommand } from 'blocking-proxy/built/lib/webdriver_commands'; @Component({ selector: 'registration', @@ -18,19 +19,33 @@ export class RegisterComponent implements OnInit { this.registration = {username: null, passwordHash: null, email: null}; } - onClickSubmit(pUsername: string, pEmail: string, pPasswordHash: string,pPasswordHashRepeat: string ) { - console.log('username: ' + pUsername); - console.log('email: ' + pEmail); + public registerError(error : any){ + console.log(error.errors[0].message); + this.errorOccurred = true; + this.errorMessage = error.errors[0].message; + } - if(pPasswordHash == pPasswordHashRepeat){ - console.log('password same'); - this.registration.username = pUsername - this.registration.email = pEmail - const md5 = new Md5(); - this.registration.passwordHash = md5.appendStr(pPasswordHash).end() as string + onClickSubmit(pUsername: string, pEmail: string, pPasswordHash: string,pPasswordHashRepeat: string ) { + this.errorOccurred = false; + this.errorMessage = " "; + if(this.passwordSame(pPasswordHash,pPasswordHashRepeat)){ + this.registration.username = pUsername + this.registration.email = pEmail + const md5 = new Md5(); + this.registration.passwordHash = md5.appendStr(pPasswordHash).end() as string + this.registerService.register(this.registration, error => this.registerError(error.json())); + } + } - this.registerService.register(this.registration) - } else{console.log('password NOT same'); } + passwordSame(pwd: string,pwd2: string){ + if(pwd == pwd2){ + console.log('password same'); + return true; + } else{ + this.errorOccurred = true; + this.errorMessage = "not the same password"; + return false; + } } ngOnInit() {} diff --git a/src/app/services/register/register.service.ts b/src/app/services/register/register.service.ts index a750046..dc8c2bd 100644 --- a/src/app/services/register/register.service.ts +++ b/src/app/services/register/register.service.ts @@ -1,15 +1,18 @@ import { Injectable } from '@angular/core'; import {Http, URLSearchParams, Headers} from '@angular/http'; import { Registration } from '../../models/registration'; +import {Router} from '@angular/router'; +import { DatasharingService } from '../datasharing.service'; +import { User } from 'src/app/models/user'; @Injectable({ providedIn: 'root' }) export class RegisterService { - constructor(private http: Http) { } + constructor(private http: Http, private data: DatasharingService,private router: Router) { } - public register(registration: Registration) { + public register(registration: Registration, errorCb: any) { //let url = './graphql' let url = 'https://greenvironment.net/graphql' @@ -17,12 +20,42 @@ export class RegisterService { let headers = new Headers(); headers.set('Content-Type', 'application/json'); - return this.http.post(url, this.buildJson(registration)).subscribe(response => console.log(response.text())); + return this.http.post(url, this.buildJson(registration)) + .subscribe(response => { + console.log(response.text()); + this.registerSuccess(); + this.updateUserInfo(response.json()) + }, errorCb + ); + } + + public registerSuccess(){ + console.log('alles supi dupi'); + //do routing + this.router.navigateByUrl(''); + } + + public updateUserInfo(response : any){ + const user: User = new User(); + user.loggedIn = true; + user.userID = response.data.register.id; + user.username = response.data.register.name; + user.handle = response.data.register.handle; + user.email = response.data.register.email; + user.points = response.data.register.points; + user.level = response.data.register.level; + user.friendIDs = response.data.register.friends; + user.groupIDs = response.data.register.groups; + user.chatIDs = response.data.register.chats; + user.requestIDs = response.data.register.requests; + + this.data.changeUserInfo(user) + } public buildJson(registration: Registration): any { const body = {query: `mutation($username: String, $email: String, $pwHash: String) { - register(username: $username, email: $email, passwordHash: $pwHash) {id} + register(username: $username, email: $email, passwordHash: $pwHash) {id, name, handle, points, level, friends{id}, groups{id},chats{id}} }`, variables: { email: registration.email, pwHash: registration.passwordHash,