aboutsummaryrefslogtreecommitdiff
path: root/src/components/lists_edit/lists_edit.js
blob: b1516af04fc681a5352d273073a1b869c7817489 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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 PanelLoading from 'src/components/panel_loading/panel_loading.vue'
import UserAvatar from '../user_avatar/user_avatar.vue'
import TabSwitcher from 'src/components/tab_switcher/tab_switcher.jsx'
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,
    TabSwitcher,
    PanelLoading
  },
  data () {
    return {
      title: '',
      titleDraft: '',
      membersUserIds: [],
      removedUserIds: new Set([]), // users we added for members, to undo
      searchUserIds: [],
      addedUserIds: new Set([]), // users we added from search, to undo
      searchLoading: false,
      reallyDelete: false
    }
  },
  created () {
    this.$store.dispatch('fetchList', { listId: this.id })
      .then(() => {
        this.title = this.findListTitle(this.id)
        this.titleDraft = this.title
      })
    this.$store.dispatch('fetchListAccounts', { listId: this.id })
      .then(() => {
        this.membersUserIds = this.findListAccounts(this.id)
        this.membersUserIds.forEach(userId => {
          this.$store.dispatch('fetchUserIfMissing', userId)
        })
      })
  },
  computed: {
    id () {
      return this.$route.params.id
    },
    membersUsers () {
      return [...this.membersUserIds, ...this.addedUserIds]
        .map(userId => this.findUser(userId)).filter(user => user)
    },
    searchUsers () {
      return this.searchUserIds.map(userId => this.findUser(userId)).filter(user => user)
    },
    ...mapState({
      currentUser: state => state.users.currentUser
    }),
    ...mapGetters(['findUser', 'findListTitle', 'findListAccounts'])
  },
  methods: {
    onInput () {
      this.search(this.query)
    },
    toggleRemoveMember (user) {
      if (this.removedUserIds.has(user.id)) {
        this.addUser(user)
        this.removedUserIds.delete(user.id)
      } else {
        this.removeUser(user.id)
        this.removedUserIds.add(user.id)
      }
    },
    toggleAddFromSearch (user) {
      if (this.addedUserIds.has(user.id)) {
        this.removeUser(user.id)
        this.addedUserIds.delete(user.id)
      } else {
        this.addUser(user)
        this.addedUserIds.add(user.id)
      }
    },
    isRemoved (user) {
      return this.removedUserIds.has(user.id)
    },
    isAdded (user) {
      return this.addedUserIds.has(user.id)
    },
    addUser (user) {
      // this.selectedUserIds.push(user.id)
    },
    removeUser (userId) {
      // this.selectedUserIds = this.selectedUserIds.filter(id => id !== userId)
    },
    onSearchLoading (results) {
      this.searchLoading = true
    },
    onSearchLoadingDone (results) {
      this.searchLoading = false
    },
    onSearchResults (results) {
      this.searchLoading = false
      this.searchUserIds = results
    },
    updateListTitle () {
      this.$store.dispatch('setList', { listId: this.id, title: this.titleDraft })
        .then(() => {
          this.title = this.findListTitle(this.id)
        })
    },
    deleteList () {
      this.$store.dispatch('deleteList', { listId: this.id })
      this.$router.push({ name: 'lists' })
    }
  }
}

export default ListsNew