Browse Source

loads all posts now

Allison Ziegler 6 years ago
parent
commit
415f1ea369

+ 7
- 2
zlg/src/app/app.module.ts View File

@@ -16,6 +16,9 @@ import { Signup } from '../pages/signup/signup'
16 16
 import { HomePage } from '../pages/home/home';
17 17
 import { WebsocketProvider } from '../providers/websocket/websocket';
18 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 23
 @NgModule({
21 24
   declarations: [
@@ -28,7 +31,8 @@ import { UserProvider } from '../providers/user/user';
28 31
   imports: [
29 32
     BrowserModule,
30 33
     IonicModule.forRoot(MyApp),
31
-    SocketIoModule.forRoot(config)
34
+    SocketIoModule.forRoot(config),
35
+    HttpClientModule
32 36
   ],
33 37
   bootstrap: [IonicApp],
34 38
   entryComponents: [
@@ -43,7 +47,8 @@ import { UserProvider } from '../providers/user/user';
43 47
     SplashScreen,
44 48
     {provide: ErrorHandler, useClass: IonicErrorHandler},
45 49
     WebsocketProvider,
46
-    UserProvider
50
+    UserProvider,
51
+    PostServiceProvider
47 52
   ]
48 53
 })
49 54
 export class AppModule {}

+ 4
- 5
zlg/src/pages/home/home.html View File

@@ -21,10 +21,9 @@
21 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 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 28
   </div>
30 29
 </ion-content>

+ 29
- 3
zlg/src/pages/home/home.ts View File

@@ -6,14 +6,16 @@ import SockJS from 'sockjs-client';
6 6
 
7 7
 
8 8
 import { NavController } from 'ionic-angular';
9
+import { PostServiceProvider } from '../../providers/post-service/post-service';
9 10
 
10 11
 @Component({
11 12
   selector: 'page-home',
12
-  templateUrl: 'home.html'
13
+  templateUrl: 'home.html',
14
+  providers: [PostServiceProvider]
13 15
 })
14 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,6 +26,7 @@ export class HomePage implements OnInit{
24 26
   postInput = "";
25 27
 
26 28
   ngOnInit() {
29
+    this.getAllPosts();
27 30
     this.connect();  
28 31
   }
29 32
   connect() {
@@ -34,13 +37,36 @@ export class HomePage implements OnInit{
34 37
     this.stompClient.connect({}, function (frame) {
35 38
       console.log('Connected: ' + frame);
36 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 67
   sendPost() {
43 68
     this.stompClient.send(this.userPostUrl, {}, JSON.stringify({'id': '','message': this.postInput, 'userId': this.userId}))
69
+    this.postInput = "";
44 70
   }
45 71
 
46 72
   //  showPost(message, userName) {

+ 21
- 0
zlg/src/providers/post-service/post-service.ts View File

@@ -0,0 +1,21 @@
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
+}