From d799f6023d72b76f63a682f87db24c1beed2f9ac Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sat, 10 Jun 2017 19:12:54 -0700 Subject: [PATCH] new post and reply to functions --- src/app/app.module.ts | 8 ++- src/pages/stream/new-post.html | 20 ++++++ src/pages/stream/stream.html | 9 ++- src/pages/stream/stream.ts | 120 +++++++++++++++++++++++++++++---- 4 files changed, 139 insertions(+), 18 deletions(-) create mode 100644 src/pages/stream/new-post.html diff --git a/src/app/app.module.ts b/src/app/app.module.ts index bbcc53d..a98a887 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -6,7 +6,7 @@ import { MyApp } from './app.component'; import { HomePage } from '../pages/home/home'; import { ListPage } from '../pages/list/list'; import { LoginPage } from '../pages/login/login'; -import { StreamPage } from '../pages/stream/stream'; +import { StreamPage, NewPostModal } from '../pages/stream/stream'; import { PostDetailsPage } from '../pages/post-details/post-details'; import { StatusBar } from '@ionic-native/status-bar'; @@ -22,7 +22,8 @@ import { TimeagoPipe } from '../pipes/timeago/timeago'; LoginPage, StreamPage, PostDetailsPage, - TimeagoPipe + TimeagoPipe, + NewPostModal ], imports: [ BrowserModule, @@ -36,7 +37,8 @@ import { TimeagoPipe } from '../pipes/timeago/timeago'; ListPage, LoginPage, StreamPage, - PostDetailsPage + PostDetailsPage, + NewPostModal ], providers: [ StatusBar, diff --git a/src/pages/stream/new-post.html b/src/pages/stream/new-post.html new file mode 100644 index 0000000..083daef --- /dev/null +++ b/src/pages/stream/new-post.html @@ -0,0 +1,20 @@ + + + New Post + + + + + @@ -77,7 +80,7 @@ - diff --git a/src/pages/stream/stream.ts b/src/pages/stream/stream.ts index d192274..f3459bd 100644 --- a/src/pages/stream/stream.ts +++ b/src/pages/stream/stream.ts @@ -1,5 +1,5 @@ import { Component } from '@angular/core'; -import { IonicPage, NavController, NavParams } from 'ionic-angular'; +import { ViewController, IonicPage, NavController, NavParams, ModalController } from 'ionic-angular'; import { PostDetailsPage } from '../post-details/post-details'; import * as pnut from 'pnut-butter'; @@ -20,7 +20,7 @@ export class StreamPage { title: string; posts: Array = [] - constructor(public navCtrl: NavController, public navParams: NavParams) { + constructor(public navCtrl: NavController, public navParams: NavParams, public modalCtrl: ModalController) { // console.log(JSON.stringify(navParams)); switch (navParams.data.stream) { case 'global': @@ -62,14 +62,14 @@ export class StreamPage { if (bookmarked) { pnut.deleteBookmark(postid).then(res => { console.log(res); - this.updatePost(res.data); + this.updatePost(res.data.id); }).catch(err => { console.log(err); }); } else { pnut.bookmark(postid).then(res => { console.log(res); - this.updatePost(res.data); + this.updatePost(res.data.id); }).catch(err => { console.log(err); }); @@ -80,27 +80,123 @@ export class StreamPage { if (reposted) { pnut.deleteRepost(postid).then(res => { console.log(res); - this.updatePost(res.data); + this.updatePost(res.data.id); }).catch(err => { console.log(err); }); } else { pnut.repost(postid).then(res => { console.log(res); - this.updatePost(res.data); + this.updatePost(res.data.id); }).catch(err => { console.log(err); }); } } - updatePost(postData) { - for (var i = 0; i < this.posts.length; i++) { - if (this.posts[i]['id'] === postData.id) { - this.posts[i] = postData; - break; + updatePost(postid) { + pnut.post(postid, {include_raw: 1}).then(res => { + for (var i = 0; i < this.posts.length; i++) { + if (this.posts[i]['id'] === postid) { + this.posts[i] = res.data; + break; + } } - } + }).catch(err => { + console.log(err); + }); + } + + showNewPost() { + let newPostModal = this.modalCtrl.create(NewPostModal); + newPostModal.present(); + } + + showReplyPost(postData) { + console.log(postData); + let newPostModal = this.modalCtrl.create(NewPostModal, {type: 'reply', post: postData}); + newPostModal.present(); } } + +@Component({ + // templateUrl: 'new-post.html' + template: ` + + + + + + + + New Post + + + + + + + + + + + + + + + + +` +}) +export class NewPostModal { + replyid: string; + ptext: string; + options: Object; + + constructor(public navParams: NavParams, public viewCtrl: ViewController) { + console.log(this.navParams); + if (navParams.data.type === 'reply') { + this.replyid = navParams.data.post.id; + this.options = {replyTo: this.replyid}; + this.ptext = "@" + navParams.data.post.user.username + " "; + if (navParams.data.post.content.entities) { + if (navParams.data.post.content.entities.mentions) { + this.ptext = this.ptext + this.parseMentions(navParams.data.post.content.entities.mentions); + } + } + } + console.log('replying to ' + this.replyid); + } + + dismiss() { + this.viewCtrl.dismiss(); + } + + send() { + console.log(this.ptext); + pnut.createPost(this.ptext, this.options).then(res => { + console.log(res); + }).catch(err => { + console.log(err); + }); + this.viewCtrl.dismiss(); + } + + parseMentions(mentions) { + let mtext = "" + for(var i = 0; i < mentions.length; i++) { + let mu = mentions[i].text; + mtext += "@" + mu + " "; + } + if (mtext.length > 0) { + return mtext; + } else { + return ""; + } + } +}