aboutsummaryrefslogtreecommitdiff
path: root/src/components/friends_list/friends_list.js
blob: d5c1837a194ea7e07f3f61f4c21df136aba45008 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import UserCard from '../user_card/user_card.vue'

const FriendsList = {
  data () {
    return {
      loading: false,
      bottomedOut: false,
      error: false
    }
  },
  props: ['userId'],
  created () {
    window.addEventListener('scroll', this.scrollLoad)
    if (this.user.followers.length === 0) {
      this.fetchFriends()
    }
  },
  destroyed () {
    window.removeEventListener('scroll', this.scrollLoad)
    this.$store.dispatch('clearFriendsAndFollowers', this.userId)
  },
  computed: {
    user () {
      return this.$store.getters.userById(this.userId)
    },
    friends () {
      return this.user.friends
    }
  },
  methods: {
    fetchFriends () {
      if (!this.loading) {
        this.loading = true
        this.$store.dispatch('addFriends', this.userId).then(friends => {
          this.error = false
          this.loading = false
          this.bottomedOut = friends.length === 0
        }).catch(() => {
          this.error = true
          this.loading = false
        })
      }
    },
    scrollLoad (e) {
      const bodyBRect = document.body.getBoundingClientRect()
      const height = Math.max(bodyBRect.height, -(bodyBRect.y))
      if (this.loading === false &&
        this.bottomedOut === false &&
        this.$el.offsetHeight > 0 &&
        (window.innerHeight + window.pageYOffset) >= (height - 750)
      ) {
        this.fetchFriends()
      }
    }
  },
  components: {
    UserCard
  }
}

export default FriendsList