<commit_before>import Vue from 'vue'
import pickups from '@/services/api/pickups'

export const types = {

  SET_STORE_ID_FILTER: 'Set storeIdFilter',
  CLEAR_STORE_ID_FILTER: 'Clear storeIdFilter',

  REQUEST_LIST: 'Request List',
  RECEIVE_LIST: 'Receive List',
  RECEIVE_LIST_ERROR: 'Receive List Error',

  REQUEST_ITEM: 'Request Item',
  RECEIVE_ITEM: 'Receive Item',
  RECEIVE_ITEM_ERROR: 'Receive Item Error',

  REQUEST_JOIN: 'Request Join',
  RECEIVE_JOIN: 'Receive Join',
  RECEIVE_JOIN_ERROR: 'Receive Join Error',

  REQUEST_LEAVE: 'Request Leave',
  RECEIVE_LEAVE: 'Receive Leave',
  RECEIVE_LEAVE_ERROR: 'Receive Leave Error',

  CLEAR: 'Clear',

}

export const state = {
  entries: {},
  waiting: {},
  idList: [],
  idListGroupId: null,
  storeIdFilter: null,
}

export const getters = {
  all: (state, getters, rootState, rootGetters) => {
    let currentUserId = rootGetters['auth/userId']
    return state.idList.map(id => state.entries[id]).map(e => {
      return {
        ...e,
        isWaiting: state.waiting[e.id],
        isUserMember: e.collectorIds.includes(currentUserId),
        store: rootGetters['stores/get'](e.store),
        collectors: e.collectorIds.map(id => rootGetters['users/get'](id)),
      }
    })
  },
  filtered: (state, getters) => {
    return getters.all.filter(e => !state.storeIdFilter || e.store.id === state.storeIdFilter)
  },
  empty: (state, getters) => {
    return getters.all.filter((e) => {
      let nextWeek = new Date(+new Date() + 6096e5)
      return e.collectorIds.length < 1 && new Date(e.date) < nextWeek
    })
  },
  isCollector: (state, getters) => (pickupId, userId) => {
    let pickup = getters.all.find(pickup => pickup.id === pickupId)
    if (pickup && pickup.collectorIds.includes(userId)) {
      return true
    }
    return false
  },
  mine: (state, getters, rootState, rootGetters) => {
    if (!rootGetters['auth/isLoggedIn']) return []
    return getters.all.filter(e => e.collectorIds.includes(rootGetters['auth/userId']))
  },
}

export const actions = {

  async fetch ({ commit }, { pickupId }) {
    commit(types.REQUEST_ITEM)
    try {
      commit(types.RECEIVE_ITEM, { pickup: await pickups.get(pickupId) })
    }
    catch (error) {
      commit(types.RECEIVE_ITEM_ERROR, { error })
    }
  },

  setStoreFilter ({ commit }, storeId) {
    commit(types.SET_STORE_ID_FILTER, { storeId })
  },

  clear ({ commit }) {
    commit(types.CLEAR)
  },

  clearStoreFilter ({ commit }) {
    commit(types.CLEAR_STORE_ID_FILTER)
  },

  async fetchList ({ commit }) {
    commit(types.REQUEST_LIST)
    try {
      commit(types.RECEIVE_LIST, { pickups: await pickups.list() })
    }
    catch (error) {
      commit(types.RECEIVE_LIST_ERROR, { error })
    }
  },

  async fetchListByGroupId ({ commit }, groupId) {
    commit(types.REQUEST_LIST)
    try {
      commit(types.RECEIVE_LIST, { pickups: await pickups.listByGroupId(groupId), groupId })
    }
    catch (error) {
      commit(types.RECEIVE_LIST_ERROR, { error })
    }
  },

  async join ({ commit, dispatch, rootGetters }, pickupId) {
    commit(types.REQUEST_JOIN, { pickupId })
    try {
      await pickups.join(pickupId)
      commit(types.RECEIVE_JOIN, { pickupId, userId: rootGetters['auth/userId'] })
    }
    catch (error) {
      commit(types.RECEIVE_JOIN_ERROR, { error, pickupId })
      dispatch('fetch', { pickupId })
    }
  },

  async leave ({ commit, dispatch, rootGetters }, pickupId) {
    commit(types.REQUEST_LEAVE, { pickupId })
    try {
      await pickups.leave(pickupId)
      commit(types.RECEIVE_LEAVE, { pickupId, userId: rootGetters['auth/userId'] })
    }
    catch (error) {
      commit(types.RECEIVE_LEAVE_ERROR, { error, pickupId })
      dispatch('fetch', { pickupId })
    }
  },

}

export const mutations = {
  [types.SET_STORE_ID_FILTER] (state, { storeId }) {
    state.storeIdFilter = parseInt(storeId)
  },

  [types.CLEAR_STORE_ID_FILTER] (state) {
    state.storeIdFilter = null
  },

  [types.CLEAR] (state) {
    state.entries = {}
    state.waiting = {}
    state.idList = []
    state.idListGroupId = null
  },
  [types.REQUEST_ITEM] (state) {},
  [types.RECEIVE_ITEM] (state, { pickup }) {
    Vue.set(state.entries, pickup.id, pickup)
  },
  [types.RECEIVE_PICKUP_ERROR] (state, { error }) {},

  [types.REQUEST_LIST] (state) {},
  [types.RECEIVE_LIST] (state, { pickups, groupId }) {
    let entries = {}
    let ids = []
    for (let pickup of pickups) {
      entries[pickup.id] = pickup
      ids.push(pickup.id)
    }
    state.entries = entries
    state.idList = ids
    state.idListGroupId = groupId
    state.error = null
  },
  [types.RECEIVE_LIST_ERROR] (state, { error }) {
    state.error = error.message
  },

  [types.REQUEST_JOIN] (state, { pickupId }) {
    Vue.set(state.waiting, pickupId, true)
  },
  [types.RECEIVE_JOIN] (state, { pickupId, userId }) {
    Vue.delete(state.waiting, pickupId)
    state.entries[pickupId].collectorIds.push(userId)
  },
  [types.RECEIVE_JOIN_ERROR] (state, { error, pickupId }) {
    Vue.delete(state.waiting, pickupId)
  },

  [types.REQUEST_LEAVE] (state, { pickupId }) {
    Vue.set(state.waiting, pickupId, true)
  },
  [types.RECEIVE_LEAVE] (state, { pickupId, userId }) {
    Vue.delete(state.waiting, pickupId)
    let { collectorIds } = state.entries[pickupId]
    let idx = collectorIds.indexOf(userId)
    if (idx !== -1) collectorIds.splice(idx, 1)
  },
  [types.RECEIVE_LEAVE_ERROR] (state, { error, pickupId }) {
    Vue.delete(state.waiting, pickupId)
  },
}
<commit_msg>Make pickups vuex store more consistent<commit_after>import Vue from 'vue'
import pickups from '@/services/api/pickups'

export const types = {

  SET_STORE_ID_FILTER: 'Set storeIdFilter',
  CLEAR_STORE_ID_FILTER: 'Clear storeIdFilter',

  REQUEST_LIST: 'Request List',
  RECEIVE_LIST: 'Receive List',
  RECEIVE_LIST_ERROR: 'Receive List Error',

  REQUEST_ITEM: 'Request Item',
  RECEIVE_ITEM: 'Receive Item',
  RECEIVE_ITEM_ERROR: 'Receive Item Error',

  REQUEST_JOIN: 'Request Join',
  RECEIVE_JOIN: 'Receive Join',
  RECEIVE_JOIN_ERROR: 'Receive Join Error',

  REQUEST_LEAVE: 'Request Leave',
  RECEIVE_LEAVE: 'Receive Leave',
  RECEIVE_LEAVE_ERROR: 'Receive Leave Error',

  CLEAR: 'Clear',

}

export const state = {
  entries: {},
  waiting: {},
  idList: [],
  idListGroupId: null,
  storeIdFilter: null,
}

export const getters = {
  get: (state, getters, rootState, rootGetters) => pickupId => {
    return getters.enrich(state.entries[pickupId])
  },
  enrich: (state, getters, rootState, rootGetters) => pickup => {
    const userId = rootGetters['auth/userId']
    return pickup && {
      ...pickup,
      isWaiting: state.waiting[pickup.id],
      isUserMember: pickup.collectorIds.includes(userId),
      store: rootGetters['stores/get'](pickup.store),
      collectors: pickup.collectorIds.map(rootGetters['users/get']),
    }
  },
  all: (state, getters, rootState, rootGetters) => {
    return state.idList.map(getters.get)
  },
  filtered: (state, getters) => {
    return getters.all.filter(e => !state.storeIdFilter || e.store.id === state.storeIdFilter)
  },
  empty: (state, getters) => {
    return getters.all.filter((e) => {
      let nextWeek = new Date(+new Date() + 6096e5)
      return e.collectorIds.length < 1 && new Date(e.date) < nextWeek
    })
  },
  mine: (state, getters, rootState, rootGetters) => {
    if (!rootGetters['auth/isLoggedIn']) return []
    return getters.all.filter(e => e.collectorIds.includes(rootGetters['auth/userId']))
  },
}

export const actions = {

  async fetch ({ commit }, { pickupId }) {
    commit(types.REQUEST_ITEM)
    try {
      commit(types.RECEIVE_ITEM, { pickup: await pickups.get(pickupId) })
    }
    catch (error) {
      commit(types.RECEIVE_ITEM_ERROR, { error })
    }
  },

  setStoreFilter ({ commit }, storeId) {
    commit(types.SET_STORE_ID_FILTER, { storeId })
  },

  clear ({ commit }) {
    commit(types.CLEAR)
  },

  clearStoreFilter ({ commit }) {
    commit(types.CLEAR_STORE_ID_FILTER)
  },

  async fetchList ({ commit }) {
    commit(types.REQUEST_LIST)
    try {
      commit(types.RECEIVE_LIST, { pickups: await pickups.list() })
    }
    catch (error) {
      commit(types.RECEIVE_LIST_ERROR, { error })
    }
  },

  async fetchListByGroupId ({ commit }, groupId) {
    commit(types.REQUEST_LIST)
    try {
      commit(types.RECEIVE_LIST, { pickups: await pickups.listByGroupId(groupId), groupId })
    }
    catch (error) {
      commit(types.RECEIVE_LIST_ERROR, { error })
    }
  },

  async join ({ commit, dispatch, rootGetters }, pickupId) {
    commit(types.REQUEST_JOIN, { pickupId })
    try {
      await pickups.join(pickupId)
      commit(types.RECEIVE_JOIN, { pickupId, userId: rootGetters['auth/userId'] })
    }
    catch (error) {
      commit(types.RECEIVE_JOIN_ERROR, { error, pickupId })
      dispatch('fetch', { pickupId })
    }
  },

  async leave ({ commit, dispatch, rootGetters }, pickupId) {
    commit(types.REQUEST_LEAVE, { pickupId })
    try {
      await pickups.leave(pickupId)
      commit(types.RECEIVE_LEAVE, { pickupId, userId: rootGetters['auth/userId'] })
    }
    catch (error) {
      commit(types.RECEIVE_LEAVE_ERROR, { error, pickupId })
      dispatch('fetch', { pickupId })
    }
  },

}

export const mutations = {
  [types.SET_STORE_ID_FILTER] (state, { storeId }) {
    state.storeIdFilter = parseInt(storeId)
  },

  [types.CLEAR_STORE_ID_FILTER] (state) {
    state.storeIdFilter = null
  },

  [types.CLEAR] (state) {
    state.entries = {}
    state.waiting = {}
    state.idList = []
    state.idListGroupId = null
  },
  [types.REQUEST_ITEM] (state) {},
  [types.RECEIVE_ITEM] (state, { pickup }) {
    Vue.set(state.entries, pickup.id, pickup)
  },
  [types.RECEIVE_PICKUP_ERROR] (state, { error }) {},

  [types.REQUEST_LIST] (state) {},
  [types.RECEIVE_LIST] (state, { pickups, groupId }) {
    let entries = {}
    let ids = []
    for (let pickup of pickups) {
      entries[pickup.id] = pickup
      ids.push(pickup.id)
    }
    state.entries = entries
    state.idList = ids
    state.idListGroupId = groupId
    state.error = null
  },
  [types.RECEIVE_LIST_ERROR] (state, { error }) {
    state.error = error.message
  },

  [types.REQUEST_JOIN] (state, { pickupId }) {
    Vue.set(state.waiting, pickupId, true)
  },
  [types.RECEIVE_JOIN] (state, { pickupId, userId }) {
    Vue.delete(state.waiting, pickupId)
    state.entries[pickupId].collectorIds.push(userId)
  },
  [types.RECEIVE_JOIN_ERROR] (state, { error, pickupId }) {
    Vue.delete(state.waiting, pickupId)
  },

  [types.REQUEST_LEAVE] (state, { pickupId }) {
    Vue.set(state.waiting, pickupId, true)
  },
  [types.RECEIVE_LEAVE] (state, { pickupId, userId }) {
    Vue.delete(state.waiting, pickupId)
    let { collectorIds } = state.entries[pickupId]
    let idx = collectorIds.indexOf(userId)
    if (idx !== -1) collectorIds.splice(idx, 1)
  },
  [types.RECEIVE_LEAVE_ERROR] (state, { error, pickupId }) {
    Vue.delete(state.waiting, pickupId)
  },
}

{'fraction_non_alphanumeric': 0.14725593189639558, 'fraction_numerical': 0.0014490128599891324, 'mean_word_length': 3.3238057948316366, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4368228', 'n_tokens_mistral': 4067, 'n_tokens_neox': 3919, 'n_words': 713}