-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add logic for posts and comments #3
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,16 @@ | |
color="primary" | ||
> | ||
<v-toolbar-side-icon @click.stop="changeSidebarState"></v-toolbar-side-icon> | ||
<v-toolbar-title>Vue Reddit PWA {{subredditTitle}}</v-toolbar-title> | ||
<v-toolbar-title> | ||
<span >Vue Reddit PWA {{ subredditTitle }}</span> | ||
</v-toolbar-title> | ||
<div v-if="!isHome" class="back-button"> | ||
<router-link :to="{ path: '/' }"> | ||
<v-btn icon> | ||
<v-icon>reply</v-icon> | ||
</v-btn> | ||
</router-link> | ||
</div> | ||
</v-toolbar> | ||
</template> | ||
|
||
|
@@ -26,6 +35,13 @@ | |
]), | ||
subredditTitle () { | ||
return this.selectedSubreddit ? `- ${this.selectedSubreddit}` : '' | ||
}, | ||
isHome () { | ||
if (this.$route.name === 'Subreddit') { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
}, | ||
methods: { | ||
|
@@ -37,5 +53,8 @@ | |
</script> | ||
|
||
<style> | ||
|
||
.back-button { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe, vuetify has some prebuild button? explore it's docs |
||
position: absolute; | ||
right: 10px; | ||
} | ||
</style> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<template> | ||
<div class="comments-item"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. play with styles - no border bottom, no strange icon. Some user info (eg name, time, upvotes - inspire from reddit and ionic2-reddit-reader) Collapse/expand would be good |
||
<v-icon color="black">chat</v-icon> | ||
<span>{{ comment.body }}</span> | ||
<div class="comments-reply" v-for="reply in comment.replies"> | ||
<comments-item :comment="reply"></comments-item> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name: 'CommentsItem', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename to SelectedPostComment |
||
props: { | ||
comment: { | ||
type: Object, | ||
default: function () { return {} } | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style> | ||
.comments-item { | ||
margin-top: 10px; | ||
} | ||
.comments-reply { | ||
border-left: 1px solid black; | ||
border-bottom: 1px solid black; | ||
padding-left: 10px; | ||
padding-bottom: 10px; | ||
} | ||
</style> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<template> | ||
<div class="posts-item"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Play with vuetify components - everything might look much better (cards, panels etc) |
||
<h1>{{ openedPost.title }}</h1> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't it good to split Post component to wrapper, body (3 types) and comments list? |
||
<div class="post-item__content" | ||
v-if="!openedPost.post_hint"> | ||
<span v-if="openedPost.selftext"> | ||
{{ openedPost.selftext }} | ||
</span> | ||
<a v-else :href="openedPost.url"> | ||
{{ openedPost.url }} | ||
</a> | ||
</div> | ||
<div class="post-item__image" | ||
v-if="openedPost.post_hint && openedPost.post_hint === 'image'"> | ||
<img :src="openedPost.url"/> | ||
</div> | ||
<div class="post-item__image" | ||
v-if="openedPost.post_hint && openedPost.post_hint === 'link'"> | ||
<a :href="openedPost.url"> | ||
{{ openedPost.url }} | ||
</a> | ||
</div> | ||
<div class="post-item__comments" | ||
v-if="openedPost.num_comments !== 0"> | ||
<h3>Comments</h3> | ||
<div class="post-item__comment" | ||
v-for="comment in comments"> | ||
<comments-item :comment="comment"/> | ||
</div> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import {mapGetters, mapActions} from 'vuex' | ||
import CommentsItem from './CommentsItem' | ||
|
||
export default { | ||
name: 'PostsItem', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to SelectedPost |
||
components: {CommentsItem}, | ||
mounted () { | ||
this.getComments(this.openedPost) | ||
}, | ||
methods: { | ||
...mapActions('posts', [ | ||
'getComments' | ||
]) | ||
}, | ||
computed: { | ||
...mapGetters('posts', { | ||
openedPost: 'openedPost', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. selectedPost |
||
comments: 'comments' | ||
}) | ||
} | ||
} | ||
</script> | ||
|
||
<style> | ||
.post-item__comments { | ||
margin-top: 10px; | ||
} | ||
</style> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import Vue from 'vue' | ||
import Router from 'vue-router' | ||
import Subreddit from '@/components/Subreddit' | ||
import PostsItem from '@/components/PostsItem' | ||
|
||
Vue.use(Router) | ||
|
||
|
@@ -11,6 +12,11 @@ export default new Router({ | |
path: '/', | ||
name: 'Subreddit', | ||
component: Subreddit | ||
}, | ||
{ | ||
path: '/:id', | ||
name: 'Post', | ||
component: PostsItem | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename everywhere |
||
} | ||
] | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,22 @@ export default { | |
state: { | ||
error: null, | ||
isLoading: false, | ||
posts: [] | ||
openedPost: localStorage.getItem('openedPost') ? JSON.parse(localStorage.getItem('openedPost')) : '', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't think we need local storage here |
||
posts: [], | ||
comments: [] | ||
}, | ||
getters: { | ||
isLoading: state => state.isLoading, | ||
error: state => state.error, | ||
posts: state => state.posts | ||
posts: state => state.posts, | ||
openedPost: state => state.openedPost, | ||
comments: state => state.comments | ||
}, | ||
mutations: { | ||
updateOpenedPost (state, payload) { | ||
state.openedPost = payload | ||
localStorage.setItem('openedPost', JSON.stringify(state.openedPost)) | ||
}, | ||
updateError (state, payload) { | ||
state.error = payload | ||
}, | ||
|
@@ -22,6 +30,9 @@ export default { | |
updatePosts (state, payload) { | ||
state.posts = payload | ||
}, | ||
updateComments (state, payload) { | ||
state.comments = payload | ||
}, | ||
concatenatePosts (state, payload) { | ||
state.posts = [...state.posts, ...payload] | ||
} | ||
|
@@ -31,6 +42,14 @@ export default { | |
commit('updatePosts', []) | ||
}, | ||
|
||
resetComments ({commit}) { | ||
commit('updateComments', []) | ||
}, | ||
|
||
setOpenedPost ({commit}, post) { | ||
commit('updateOpenedPost', post) | ||
}, | ||
|
||
async getPostsFromSubreddit ({commit}, subreddit) { | ||
commit('updateError', null) | ||
commit('updateIsLoading', true) | ||
|
@@ -55,6 +74,19 @@ export default { | |
} finally { | ||
commit('updateIsLoading', false) | ||
} | ||
}, | ||
|
||
async getComments ({commit}, post) { | ||
commit('updateError', null) | ||
commit('updateIsLoading', true) | ||
try { | ||
let comments = await PostsApi.fetchComments(post) | ||
commit('updateComments', comments) | ||
} catch (err) { | ||
commit('updateError', err.message) | ||
} finally { | ||
commit('updateIsLoading', false) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redundant construction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, can we do it some other, more beautiful way (no === 'Subreddit)?