浏览代码

ratings and login cooperating

rjsmall90 6 年前
父节点
当前提交
242af2a95a

+ 1
- 1
src/app/app.component.ts 查看文件

@@ -19,7 +19,7 @@ import { LoginPage } from '../pages/login/login';
19 19
 export class MyApp {
20 20
   @ViewChild(Nav) nav: Nav;
21 21
 
22
-  rootPage: any = HomePage;
22
+  rootPage: any = LoginPage;
23 23
 
24 24
   pages: Array<{title: string, component: any}>;
25 25
 

+ 9
- 3
src/app/app.module.ts 查看文件

@@ -2,6 +2,7 @@ import { BrowserModule } from '@angular/platform-browser';
2 2
 import { ErrorHandler, NgModule } from '@angular/core';
3 3
 import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
4 4
 import { HttpClientModule } from '@angular/common/http';
5
+import { HttpModule } from '@angular/http';
5 6
 import { Ionic2RatingModule } from 'ionic2-rating';
6 7
 
7 8
 
@@ -19,42 +20,47 @@ import { StatusBar } from '@ionic-native/status-bar';
19 20
 import { SplashScreen } from '@ionic-native/splash-screen';
20 21
 import { ConnectionProvider } from '../providers/connection/connection';
21 22
 import { DistrictProvider } from '../providers/connection/district';
23
+import { UserLoginProvider } from '../providers/connection/userLogin';
24
+
22 25
 
23 26
 @NgModule({
24 27
   declarations: [
25 28
     MyApp,
29
+    LoginPage,
26 30
     HomePage,
27 31
     ListPage,
28 32
     NewProfilePage,
29 33
     TeacherRegistrationPage,
30 34
     SearchPage,
31 35
     SchoolPage,
32
-    LoginPage,
36
+    
33 37
   ],
34 38
   imports: [
35 39
     BrowserModule,
36 40
     IonicModule.forRoot(MyApp),
37 41
     HttpClientModule, 
38
-    Ionic2RatingModule
42
+    Ionic2RatingModule,
43
+    HttpModule
39 44
 
40 45
 
41 46
   ],
42 47
   bootstrap: [IonicApp],
43 48
   entryComponents: [
44 49
     MyApp,
50
+    LoginPage, 
45 51
     HomePage,
46 52
     ListPage, 
47 53
     NewProfilePage,
48 54
     TeacherRegistrationPage,
49 55
     SearchPage,
50 56
     SchoolPage,
51
-    LoginPage, 
52 57
   ],
53 58
   providers: [
54 59
     StatusBar,
55 60
     SplashScreen,
56 61
     ConnectionProvider,
57 62
     DistrictProvider, 
63
+    UserLoginProvider,
58 64
     {provide: ErrorHandler, useClass: IonicErrorHandler}
59 65
     
60 66
   ]

+ 18
- 12
src/pages/login/login.html 查看文件

@@ -9,26 +9,32 @@
9 9
   </ion-navbar>
10 10
 </ion-header>
11 11
 <ion-content padding style="background:url(assets/imgs/final_spitball.jpg) no-repeat center;background-size:cover;" id="page7">
12
-  <form id="login-form1">
12
+  
13
+  <form [formGroup]="formgroup" (ngSubmit)="goToUserProfile(formgroup.value)">
14
+    
13 15
     <ion-list id="login-list1">
14 16
       <ion-item id="login-input1">
15
-        <ion-label>
16
-          Email or Display Name
17
-        </ion-label>
18
-        <ion-input type="email" placeholder=""></ion-input>
17
+        <ion-input type="email" placeholder="Email or Display Name" formControlName="username"></ion-input>
19 18
       </ion-item>
20
-      <ion-item id="login-input2">
21
-        <ion-label>
22
-          Password
23
-        </ion-label>
24
-        <ion-input type="password" placeholder=""></ion-input>
19
+      <ion-item text-center *ngIf="username.hasError('required') && username.touched">
20
+        <p> *Please enter your e-mail or display name</p>
21
+      </ion-item>
22
+
23
+      <ion-item text-center>
24
+        <ion-input type="password" placeholder="Password" formControlName="password"></ion-input>
25
+      </ion-item>
26
+      <ion-item text-center *ngIf="password.hasError('required') && password.touched">
27
+        <p> *Please enter your password</p>
25 28
       </ion-item>
26 29
     </ion-list>
30
+
31
+
27 32
     <div class="spacer" style="height:20px;" id="login-spacer1"></div>
28
-    <button id="login-button1" ion-button color="calm" block on-click="goToUserProfile()">
33
+    <button id="login-button1" ion-button color="calm" block on-click="goToUserProfile(formgroup.value)">
29 34
       Log in
30 35
     </button>
31 36
   </form>
37
+
32 38
   <button id="login-button11" ion-button color="positive" small icon-left>
33 39
     <ion-icon name="linkedin"></ion-icon>
34 40
     LinkedIn Login
@@ -40,7 +46,7 @@
40 46
     <ion-icon name="googleplus"></ion-icon>
41 47
     Google + Login
42 48
   </button>
43
-  <button id="login-button2" ion-button color="assertive" full small on-click="goToNewProfile()">
49
+  <button id="login-button2" ion-button color="assertive" full small on-click="goToUserRegistration()">
44 50
     Create A New Profile!
45 51
   </button>
46 52
   <button id="login-button5" ion-button color="balanced" full small on-click="goToTeacherRegistration()">

+ 48
- 5
src/pages/login/login.ts 查看文件

@@ -1,6 +1,14 @@
1 1
 import { Component } from '@angular/core';
2 2
 import { NavController } from 'ionic-angular';
3 3
 
4
+import { FormGroup, FormBuilder, Validators, AbstractControl } from '@angular/forms';
5
+import { UserLoginProvider } from './../../providers/connection/userLogin';
6
+
7
+import { HomePage } from './../../pages/home/home';
8
+import { NewProfilePage } from './../../pages/registerUser/registerUser';
9
+import { TeacherRegistrationPage } from './../../pages/registerTeacher/registerTeacher';
10
+
11
+
4 12
 
5 13
 @Component({
6 14
   selector: 'page-login',
@@ -8,11 +16,46 @@ import { NavController } from 'ionic-angular';
8 16
 })
9 17
 export class LoginPage {
10 18
 
11
-  constructor(public navCtrl: NavController) {
19
+  formgroup: FormGroup;
20
+
21
+  username: AbstractControl;
22
+  password: AbstractControl;
23
+
24
+  constructor(public navCtrl: NavController,  private formbuilder: FormBuilder, private userLogin: UserLoginProvider) {
25
+  
26
+    this.formgroup=this.formbuilder.group(
27
+      
28
+      {
29
+      username : ['', Validators.required],
30
+      password : ['', Validators.required]
31
+      });
32
+
33
+    this.username=this.formgroup.controls['username'];
34
+    this.password=this.formgroup.controls['password'];
35
+  
36
+  }
37
+
38
+  goToUserProfile(loginForm){
39
+        this.userLogin.login(loginForm.username, loginForm.password, result =>{
40
+        if(result.status == 200){
41
+        console.log("Success!");
42
+        this.userLogin.setToken(result.headers.get("authorization"));
43
+        console.log(this.userLogin.getToken());
44
+        this.navCtrl.setRoot(HomePage);
45
+
46
+
47
+      } else {
48
+        console.log("invalid login attempt");
49
+      }
50
+    });
51
+  }
52
+
53
+  goToTeacherRegistration(){
54
+    this.navCtrl.push(TeacherRegistrationPage); 
55
+  }
56
+
57
+  goToUserRegistration(){
58
+    this.navCtrl.push(NewProfilePage); 
12 59
   }
13
-//   goToNewProfile(params){
14
-//     if (!params) params = {};
15
-//     this.navCtrl.push(NewProfilePage);
16
-//   }
17 60
   
18 61
 }

+ 1
- 0
src/pages/schools/schools.html 查看文件

@@ -20,6 +20,7 @@
20 20
         School Aggregate Score
21 21
       </h2>
22 22
   </ion-item>
23
+  
23 24
      <rating id="stars"
24 25
         readOnly="false"
25 26
         max="5"

+ 7
- 10
src/pages/schools/schools.ts 查看文件

@@ -2,7 +2,7 @@ import { Component } from '@angular/core';
2 2
 import { NavController, NavParams } from 'ionic-angular';
3 3
 import { ConnectionProvider } from './../../providers/connection/connection';
4 4
 
5
-import { Rating } from './../../app/component/rating';
5
+//import { Rating } from './../../app/component/rating';
6 6
 import { Review } from './../../app/component/review';
7 7
 
8 8
 
@@ -19,7 +19,6 @@ export class SchoolPage {
19 19
     public gMap;
20 20
 
21 21
     review: Review = new Review();
22
-    // review: string;
23 22
     // rate: Rating = new Rating();
24 23
 
25 24
     // @ViewChild('mapsId') mapElement: ElementRef;
@@ -28,6 +27,12 @@ export class SchoolPage {
28 27
   constructor(public navCtrl: NavController, public navParams: NavParams, public conn: ConnectionProvider) {
29 28
       this.school = this.navParams.get("school");
30 29
   }
30
+  postReview(){
31
+    console.log(this.review);
32
+    this.conn.post('reviews/post', this.review).subscribe(
33
+      data => console.log(data)
34
+    );
35
+  }
31 36
 
32 37
   // postRating(rate: number){
33 38
   //   this.conn.post('ratings/rate', this.rate).subscribe(
@@ -35,14 +40,6 @@ export class SchoolPage {
35 40
   //   );
36 41
   // }
37 42
 
38
-   postReview(){
39
-     console.log("post reviw");
40
-     console.log(this.review);
41
-    this.conn.post('reviews/post', this.review).subscribe(
42
-      data => console.log(data)
43
-    );
44
-  }
45
-
46 43
   // ionViewDidLoad () {
47 44
   //   this.loadMap();
48 45
   // }

+ 10
- 2
src/providers/connection/connection.ts 查看文件

@@ -16,12 +16,20 @@ public apiUrl = 'http://localhost:8080/';
16 16
     console.log('Hello ConnectionProvider Provider');
17 17
   }
18 18
 
19
+  setAPI(apiUrl: string){
20
+    this.apiUrl = apiUrl;
21
+  }
22
+
23
+  getAPI(){
24
+    return this.apiUrl;
25
+  }
26
+
19 27
 post(partialPath: string, data: any) {
20
-  return this.http.post(this.apiUrl + partialPath, data);
28
+  return this.http.post(this.getAPI() + partialPath, data);
21 29
 }
22 30
 
23 31
 get(partialPath: string, data: any) {
24
-  return this.http.get(this.apiUrl + partialPath, data);
32
+  return this.http.get(this.getAPI() + partialPath, data);
25 33
 }
26 34
 
27 35
 

+ 47
- 0
src/providers/connection/userLogin.ts 查看文件

@@ -0,0 +1,47 @@
1
+import { HttpClient } from '@angular/common/http';
2
+import { Injectable } from '@angular/core';
3
+import { Http, Headers, RequestOptions } from '@angular/http';
4
+import { ConnectionProvider } from '../connection/connection';
5
+
6
+/*
7
+  Generated class for the UserProvider provider.
8
+
9
+  See https://angular.io/guide/dependency-injection for more info on providers
10
+  and Angular DI.
11
+*/
12
+@Injectable()
13
+export class UserLoginProvider {
14
+
15
+  constructor( public http: Http, public conn: ConnectionProvider) {
16
+    console.log('Hello UserProvider Provider');
17
+  }
18
+
19
+
20
+  private token: string;
21
+
22
+  getToken(){return this.token}
23
+
24
+  setToken(token: string){this.token = token}
25
+  
26
+  login(username, password, callback){
27
+   
28
+    var url: string = this.conn.getAPI() + "/login";
29
+ 
30
+    var header = new Headers({
31
+      'content-type': 'application/json',
32
+    })
33
+ 
34
+    let options = new RequestOptions({headers: header})
35
+ 
36
+    var body = {
37
+      "username": username,
38
+      "password": password
39
+    };
40
+ 
41
+    this.http.post(url, body, options).subscribe( result => {
42
+      callback(result);
43
+      //console.log(result.headers.get("authorization"));
44
+    })
45
+  }
46
+
47
+}