aboutsummaryrefslogtreecommitdiff
path: root/src/components/lists_edit/lists_edit.js
blob: a68bb589863a3818b624c3d42a410ae3bdef608d (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { mapState, mapGetters } from 'vuex'
import BasicUserCard from '../basic_user_card/basic_user_card.vue'
import ListsUserSearch from '../lists_user_search/lists_user_search.vue'
import UserAvatar from '../user_avatar/user_avatar.vue'
import { library } from '@fortawesome/fontawesome-svg-core'
import {
  faSearch,
  faChevronLeft
} from '@fortawesome/free-solid-svg-icons'

library.add(
  faSearch,
  faChevronLeft
)

const ListsNew = {
  components: {
    BasicUserCard,
    UserAvatar,
    ListsUserSearch
  },
  data () {
    return {
      title: '',
      userIds: [],
      selectedUserIds: []
    }
  },
  created () {
    this.$store.dispatch('fetchList', { id: this.id })
      .then(() => { this.title = this.findListTitle(this.id) })
    this.$store.dispatch('fetchListAccounts', { id: this.id })
      .then(() => {
        this.selectedUserIds = this.findListAccounts(this.id)
        this.selectedUserIds.forEach(userId => {
          this.$store.dispatch('fetchUserIfMissing', userId)
        })
      })
  },
  computed: {
    id () {
      return this.$route.params.id
    },
    users () {
      return this.userIds.map(userId => this.findUser(userId))
    },
    selectedUsers () {
      return this.selectedUserIds.map(userId => this.findUser(userId)).filter(user => user)
    },
    ...mapState({
      currentUser: state => state.users.currentUser
    }),
    ...mapGetters(['findUser', 'findListTitle', 'findListAccounts'])
  },
  methods: {
    onInput () {
      this.search(this.query)
    },
    selectUser (user) {
      if (this.selectedUserIds.includes(user.id)) {
        this.removeUser(user.id)
      } else {
        this.addUser(user)
      }
    },
    isSelected (user) {
      return this.selectedUserIds.includes(user.id)
    },
    addUser (user) {
      this.selectedUserIds.push(user.id)
    },
    removeUser (userId) {
      this.selectedUserIds = this.selectedUserIds.filter(id => id !== userId)
    },
    onResults (results) {
      this.userIds = results
    },
    updateList () {
      this.$store.dispatch('setList', { id: this.id, title: this.title })
      this.$store.dispatch('setListAccounts', { id: this.id, accountIds: this.selectedUserIds })

      this.$router.push({ name: 'lists-timeline', params: { id: this.id } })
    },
    deleteList () {
      this.$store.dispatch('deleteList', { id: this.id })
      this.$router.push({ name: 'lists' })
    }
  }
}

export default ListsNew