Skip to content

Commit

Permalink
feat: improve logic and use getters instead of store
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroAkbal committed Sep 4, 2020
1 parent 93eefe4 commit 2a480b3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
18 changes: 10 additions & 8 deletions store/booru/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@ export const actions = {
}
},

pidManager({ state, commit, getters }, { operation, value }) {
pidManager({ commit, getters }, { operation, value }) {
switch (operation) {
case 'add':
commit('setPIDQuery', state.queries.pid + 1)
commit('setPIDQuery', getters.getPageID + 1)
break

case 'subtract':
commit('setPIDQuery', state.queries.pid - 1)
commit('setPIDQuery', getters.getPageID - 1)
break

case 'set':
Expand All @@ -168,25 +168,27 @@ export const actions = {
}
},

addedTagsManager({ state, commit }, { operation, value }) {
addedTagsManager({ commit, getters }, { operation, value }) {
switch (operation) {
case 'add': {
// value: string
const isTheTagAlreadyAdded = state.search.addedTags.includes(value)
const isTheTagAlreadyAdded = getters.getSearchAddedTags.includes(value)

if (isTheTagAlreadyAdded) {
console.debug('This tag is already added!')
return
}

commit('pushAddedTags', value)
const addedTagsWithNewTag = [...getters.getSearchAddedTags, value]

commit('setAddedTags', addedTagsWithNewTag)
break
}

case 'concat': {
// value: string[]
const uniqueMergedAddedTags = [
...new Set([...state.search.addedTags, ...value]),
...new Set([...getters.getSearchAddedTags, ...value]),
]

commit('setAddedTags', uniqueMergedAddedTags)
Expand All @@ -195,7 +197,7 @@ export const actions = {

case 'remove': {
// value: string
const addedTagsWithoutValue = state.search.addedTags.filter(
const addedTagsWithoutValue = getters.getSearchAddedTags.filter(
(tag) => tag !== value
)

Expand Down
41 changes: 17 additions & 24 deletions store/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ export const mutations = {
}

export const actions = {
customBoorusManager({ state, commit }, { operation, value }) {
customBoorusManager({ getters, commit }, { operation, value }) {
switch (operation) {
case 'add': {
const doesTheBooruAlreadyExist = state.custom.boorus.some(
const doesTheBooruAlreadyExist = getters.getCustomBoorus.some(
(booruObj) => booruObj.domain === value.domain
)

Expand All @@ -180,14 +180,18 @@ export const actions = {
return
}

commit('pushCustomBooruValue', value)
const arrayWithAddedBooru = [...getters.getCustomBoorus, value]

commit('setCustomBoorus', arrayWithAddedBooru)
break
}

case 'remove': {
const arrayWithoutBooru = state.custom.boorus.filter((customBooru) => {
return customBooru.domain !== value.domain
})
const arrayWithoutBooru = getters.getCustomBoorus.filter(
(customBooru) => {
return customBooru.domain !== value.domain
}
)

commit('setCustomBoorus', arrayWithoutBooru)
break
Expand All @@ -202,10 +206,10 @@ export const actions = {
}
},

customTagCollectionsManager({ state, commit }, { operation, value }) {
customTagCollectionsManager({ getters, commit }, { operation, value }) {
switch (operation) {
case 'add': {
const doesTheTagCollectionAlreadyExist = state.custom.tagCollections.some(
const doesTheTagCollectionAlreadyExist = getters.getTagCollections.some(
(tagCollection) => tagCollection.name === value.name
)

Expand All @@ -214,31 +218,20 @@ export const actions = {
return
}

commit('pushCustomTagCollection', value)
const arrayWithTagCollection = [...getters.getTagCollections, value]

commit('setCustomTagCollections', arrayWithTagCollection)
break
}

// case 'modify': {
// const targetIndex = state.custom.tagCollections.findIndex(
// (tagCollection) => tagCollection.name === value.name
// )

// const modifiedArray = state.custom.tagCollections

// modifiedArray[targetIndex] = value

// commit('setCustomTagCollections', modifiedArray)
// break
// }

case 'remove': {
const arrayWithoutCollection = state.custom.tagCollections.filter(
const arrayWithoutTagCollection = getters.getTagCollections.filter(
(tagCollection) => {
return tagCollection.name !== value.name
}
)

commit('setCustomTagCollections', arrayWithoutCollection)
commit('setCustomTagCollections', arrayWithoutTagCollection)
break
}

Expand Down

0 comments on commit 2a480b3

Please sign in to comment.