12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <template>
- <div class='route_container'>
- <div class='user_posts' :class='{ "user_posts--no_border_bottom": !posts.length }'>
- <div class='user_posts__title'>Posts by username</div>
- <scroll-load
- :loading='loadingPosts'
- :show='nextURL !== null'
- @load='loadNewPosts'
- v-if='sortedPosts.length'
- >
- <thread-post
- v-for='(post, index) in sortedPosts'
- :post='post'
- :show-thread='true'
- :class='{"post--last": index === posts.length-1}'
- ></thread-post>
- </scroll-load>
- <template v-else>This user hasn't posted anything yet</template>
- </div>
- </div>
- </template>
- <script>
- import ScrollLoad from '../ScrollLoad'
- import ThreadPost from '../ThreadPost'
- import AjaxErrorHandler from '../../assets/js/errorHandler'
- export default {
- name: 'user',
- components: {
- ThreadPost,
- ScrollLoad
- },
- data () {
- return {
- posts: [],
- loadingPosts: false,
- nextURL: ''
- }
- },
- computed: {
- sortedPosts () {
- return this.posts.sort((a, b) => {
- return new Date(a.createdAt) - new Date(b.createdAt)
- })
- }
- },
- methods: {
- loadNewPosts () {
- if(this.nextURL === null) return
- this.loadingPosts = true
- this.axios
- .get(this.nextURL)
- .then(res => {
- this.loadingPosts = false
- let currentPostsIds = this.posts.map(p => p.id)
- let filteredPosts =
- res.data.Posts.filter(p => !currentPostsIds.includes(p.id))
- this.posts.push(...filteredPosts)
- this.nextURL = res.data.meta.nextURL
- })
- .catch((e) => {
- this.loadingPosts = false
- AjaxErrorHandler(this.$store)(e)
- })
- }
- },
- created () {
- this.axios
- .get(`/api/v1/user/${this.$route.params.username}?posts=true`)
- .then(res => {
- this.posts = res.data.Posts
- this.nextURL = res.data.meta.nextURL
- })
- .catch(AjaxErrorHandler(this.$store))
- }
- }
- </script>
- <style lang='scss' scoped>
- @import '../../assets/scss/variables.scss';
- .user_posts {
- width: calc(75% + 5rem);
- @at-root #{&}__title {
- font-size: 1.5rem;
- margin-bottom: 1rem;
- }
- }
- </style>
|