Parcourir la source

loads all posts now

Allison Ziegler il y a 6 ans
Parent
révision
415f1ea369

+ 7
- 2
zlg/src/app/app.module.ts Voir le fichier

16
 import { HomePage } from '../pages/home/home';
16
 import { HomePage } from '../pages/home/home';
17
 import { WebsocketProvider } from '../providers/websocket/websocket';
17
 import { WebsocketProvider } from '../providers/websocket/websocket';
18
 import { UserProvider } from '../providers/user/user';
18
 import { UserProvider } from '../providers/user/user';
19
+import { PostServiceProvider } from '../providers/post-service/post-service';
20
+import { HttpClient } from '@angular/common/http';
21
+import { HttpClientModule } from '@angular/common/http';
19
 
22
 
20
 @NgModule({
23
 @NgModule({
21
   declarations: [
24
   declarations: [
28
   imports: [
31
   imports: [
29
     BrowserModule,
32
     BrowserModule,
30
     IonicModule.forRoot(MyApp),
33
     IonicModule.forRoot(MyApp),
31
-    SocketIoModule.forRoot(config)
34
+    SocketIoModule.forRoot(config),
35
+    HttpClientModule
32
   ],
36
   ],
33
   bootstrap: [IonicApp],
37
   bootstrap: [IonicApp],
34
   entryComponents: [
38
   entryComponents: [
43
     SplashScreen,
47
     SplashScreen,
44
     {provide: ErrorHandler, useClass: IonicErrorHandler},
48
     {provide: ErrorHandler, useClass: IonicErrorHandler},
45
     WebsocketProvider,
49
     WebsocketProvider,
46
-    UserProvider
50
+    UserProvider,
51
+    PostServiceProvider
47
   ]
52
   ]
48
 })
53
 })
49
 export class AppModule {}
54
 export class AppModule {}

+ 4
- 5
zlg/src/pages/home/home.html Voir le fichier

21
     <button (click)="sendPost()" id="myVault-button18" style="font-weight:600;font-size:20px;" class="codystar button button-royal button-block button-small">Post</button>
21
     <button (click)="sendPost()" id="myVault-button18" style="font-weight:600;font-size:20px;" class="codystar button button-royal button-block button-small">Post</button>
22
   </div>
22
   </div>
23
   
23
   
24
-  
25
-  <div id="myVault-markdown4" class="show-list-numbers-and-dots" >
26
-      <ul style="color:#FFFFFF;list-style-type: none" *ngFor="let p of posts">
27
-        <li><strong>{{ p.userName }}: </strong> {{ p.message }}</li>
28
-      </ul>
24
+  <div id="myVault-markdown4" class="show-list-numbers-and-dots overflow-box">
25
+      <div style="color:#FFFFFF;list-style-type: none;overflow: scroll;" *ngFor="let p of posts" >
26
+        <button (click)="goToUsersFeed(p)" class="button button-block" style="background-color:lightgray;text-align: left"><strong>{{ p.userName }}: </strong>{{ p.message }}</button>
27
+      </div>
29
   </div>
28
   </div>
30
 </ion-content>
29
 </ion-content>

+ 29
- 3
zlg/src/pages/home/home.ts Voir le fichier

6
 
6
 
7
 
7
 
8
 import { NavController } from 'ionic-angular';
8
 import { NavController } from 'ionic-angular';
9
+import { PostServiceProvider } from '../../providers/post-service/post-service';
9
 
10
 
10
 @Component({
11
 @Component({
11
   selector: 'page-home',
12
   selector: 'page-home',
12
-  templateUrl: 'home.html'
13
+  templateUrl: 'home.html',
14
+  providers: [PostServiceProvider]
13
 })
15
 })
14
 export class HomePage implements OnInit{
16
 export class HomePage implements OnInit{
15
 
17
 
16
-  constructor(public navCtrl: NavController) {
18
+  constructor(public navCtrl: NavController, private postProvider: PostServiceProvider) {
17
 
19
 
18
   }
20
   }
19
 
21
 
24
   postInput = "";
26
   postInput = "";
25
 
27
 
26
   ngOnInit() {
28
   ngOnInit() {
29
+    this.getAllPosts();
27
     this.connect();  
30
     this.connect();  
28
   }
31
   }
29
   connect() {
32
   connect() {
34
     this.stompClient.connect({}, function (frame) {
37
     this.stompClient.connect({}, function (frame) {
35
       console.log('Connected: ' + frame);
38
       console.log('Connected: ' + frame);
36
       _this.stompClient.subscribe('/topic/posts', (post) => {
39
       _this.stompClient.subscribe('/topic/posts', (post) => {
37
-        _this.posts.push(JSON.parse(post.body));
40
+        _this.posts.unshift(JSON.parse(post.body));
38
      });
41
      });
39
     });
42
     });
40
   }
43
   }
41
 
44
 
45
+  getAllPosts() {
46
+    console.log("getting posts...");
47
+    //let previousPosts: any[] = this.postProvider.getAllPosts();
48
+    this.postProvider.getAllPosts().subscribe (
49
+      (posts) => { 
50
+        console.log("hello there?");
51
+        console.log(posts);
52
+        let postarray = posts as any[];
53
+        for (let i = 0; i < postarray.length; i++) {
54
+           this.posts.unshift(posts[i]);
55
+        }
56
+      }
57
+    );
58
+        
59
+  }
60
+
61
+  goToUsersFeed(post) {
62
+    let params = {id: post.userId};
63
+    //this.navCtrl.push('UsersFeed', params);
64
+  }
65
+
66
+
42
   sendPost() {
67
   sendPost() {
43
     this.stompClient.send(this.userPostUrl, {}, JSON.stringify({'id': '','message': this.postInput, 'userId': this.userId}))
68
     this.stompClient.send(this.userPostUrl, {}, JSON.stringify({'id': '','message': this.postInput, 'userId': this.userId}))
69
+    this.postInput = "";
44
   }
70
   }
45
 
71
 
46
   //  showPost(message, userName) {
72
   //  showPost(message, userName) {

+ 21
- 0
zlg/src/providers/post-service/post-service.ts Voir le fichier

1
+import { HttpClient, HttpResponse } from '@angular/common/http';
2
+import { Injectable } from '@angular/core';
3
+import { Observable } from 'rx';
4
+
5
+/*
6
+  Generated class for the PostServiceProvider provider.
7
+
8
+  See https://angular.io/guide/dependency-injection for more info on providers
9
+  and Angular DI.
10
+*/
11
+@Injectable()
12
+export class PostServiceProvider {
13
+
14
+  constructor(public http: HttpClient) {
15
+    console.log('Hello PostServiceProvider Provider');
16
+  }
17
+
18
+  getAllPosts() {
19
+    return this.http.get("https://fast-headland-39537.herokuapp.com/posts/all");
20
+  }
21
+}