1
0

chore: don't resolve when returning from promise & improve list store module

This commit is contained in:
Dominik Pschenitschni
2021-10-09 16:34:57 +02:00
parent 3b940cb56c
commit a776e1d2f3
16 changed files with 130 additions and 150 deletions

View File

@ -101,17 +101,17 @@ export default {
// Tell others the user is autheticated
ctx.dispatch('checkAuth')
return Promise.resolve()
})
.catch(e => {
if (e.response) {
if (e.response.data.code === 1017 && !credentials.totpPasscode) {
ctx.commit('needsTotpPasscode', true)
return Promise.reject(e)
}
if (
e.response &&
e.response.data.code === 1017 &&
!credentials.totpPasscode
) {
ctx.commit('needsTotpPasscode', true)
}
return Promise.reject(e)
throw e
})
.finally(() => {
ctx.commit(LOADING, false, {root: true})
@ -134,7 +134,7 @@ export default {
ctx.commit(ERROR_MESSAGE, e.response.data.message, {root: true})
}
return Promise.reject(e)
throw e
})
.finally(() => {
ctx.commit(LOADING, false, {root: true})
@ -157,7 +157,6 @@ export default {
// Tell others the user is autheticated
ctx.dispatch('checkAuth')
return Promise.resolve()
})
.finally(() => {
ctx.commit(LOADING, false, {root: true})
@ -180,7 +179,7 @@ export default {
// This function can be called from multiple places at the same time and shortly after one another.
// To prevent hitting the api too frequently or race conditions, we check at most once per minute.
if (ctx.state.lastUserInfoRefresh !== null && ctx.state.lastUserInfoRefresh > (new Date()).setMinutes((new Date()).getMinutes() + 1)) {
return Promise.resolve()
return
}
const jwt = getToken()
@ -190,14 +189,13 @@ export default {
.split('.')[1]
.replace('-', '+')
.replace('_', '/')
const info = new UserModel(JSON.parse(window.atob(base64)))
const info = new UserModel(JSON.parse(atob(base64)))
const ts = Math.round((new Date()).getTime() / 1000)
authenticated = info.exp >= ts
ctx.commit('info', info)
if (authenticated) {
ctx.dispatch('refreshUserInfo')
ctx.commit('authenticated', authenticated)
}
}
@ -206,8 +204,6 @@ export default {
ctx.commit('info', null)
ctx.dispatch('config/redirectToProviderIfNothingElseIsEnabled', null, {root: true})
}
return Promise.resolve()
},
refreshUserInfo(ctx) {
const jwt = getToken()

View File

@ -66,7 +66,7 @@ export default {
return HTTP.get('info')
.then(r => {
ctx.commit(CONFIG, r.data)
return Promise.resolve(r)
return r
})
},
redirectToProviderIfNothingElseIsEnabled(ctx) {

View File

@ -227,19 +227,19 @@ export default {
.finally(() => cancel())
},
loadNextTasksForBucket(ctx, {listId, ps = {}, bucketId}) {
async loadNextTasksForBucket(ctx, {listId, ps = {}, bucketId}) {
const bucketIndex = findIndexById(ctx.state.buckets, bucketId)
const isLoading = ctx.state.bucketLoading[bucketIndex] ?? false
if (isLoading) {
return Promise.resolve()
return
}
const page = (ctx.state.taskPagesPerBucket[bucketIndex] ?? 1) + 1
const alreadyLoaded = ctx.state.allTasksLoadedForBucket[bucketIndex] ?? false
if (alreadyLoaded) {
return Promise.resolve()
return
}
const cancel = setLoading(ctx, 'kanban')
@ -334,7 +334,7 @@ export default {
// restore original state
ctx.commit('setBucketByIndex', {bucketIndex, bucket: oldBucket})
return Promise.reject(e)
throw e
})
.finally(() => cancel())
},

View File

@ -1,5 +1,7 @@
import LabelService from '@/services/label'
import {setLoading} from '@/store/helper'
import { success } from '@/message'
import {i18n} from '@/i18n'
/**
* Returns the labels by id if found
@ -28,6 +30,18 @@ function getLabelsByIds(state, ids) {
})
}
const labelService = new LabelService()
const getAllLabels = async (page = 1) => {
const labels = await labelService.getAll({}, {}, page)
if (page < labelService.totalPages) {
const nextLabels = await getAllLabels(page + 1)
return labels.concat(nextLabels)
} else {
return labels
}
}
export default {
namespaced: true,
state: () => ({
@ -60,71 +74,59 @@ export default {
},
},
actions: {
loadAllLabels(ctx, {forceLoad} = {}) {
async loadAllLabels(ctx, {forceLoad} = {}) {
if (ctx.state.loaded && !forceLoad) {
return Promise.resolve()
return
}
const cancel = setLoading(ctx, 'labels')
const labelService = new LabelService()
const getAllLabels = (page = 1) => {
return labelService.getAll({}, {}, page)
.then(labels => {
if (page < labelService.totalPages) {
return getAllLabels(page + 1)
.then(nextLabels => {
return labels.concat(nextLabels)
})
} else {
return labels
}
})
.catch(e => {
return Promise.reject(e)
})
try {
const labels = await getAllLabels()
ctx.commit('setLabels', labels)
ctx.commit('setLoaded', true)
return labels
} finally {
cancel()
}
return getAllLabels()
.then(r => {
ctx.commit('setLabels', r)
ctx.commit('setLoaded', true)
return Promise.resolve(r)
})
.finally(() => cancel())
},
deleteLabel(ctx, label) {
async deleteLabel(ctx, label) {
const cancel = setLoading(ctx, 'labels')
const labelService = new LabelService()
return labelService.delete(label)
.then(r => {
ctx.commit('removeLabelById', label)
return Promise.resolve(r)
})
.finally(() => cancel())
try {
const result = await labelService.delete(label)
ctx.commit('removeLabelById', label)
success({message: i18n.global.t('label.deleteSuccess')})
return result
} finally {
cancel()
}
},
updateLabel(ctx, label) {
async updateLabel(ctx, label) {
const cancel = setLoading(ctx, 'labels')
const labelService = new LabelService()
return labelService.update(label)
.then(r => {
ctx.commit('setLabel', r)
return Promise.resolve(r)
})
.finally(() => cancel())
try {
const newLabel = await labelService.update(label)
ctx.commit('setLabel', newLabel)
success({message: i18n.global.t('label.edit.success')})
return newLabel
} finally {
cancel()
}
},
createLabel(ctx, label) {
async createLabel(ctx, label) {
const cancel = setLoading(ctx, 'labels')
const labelService = new LabelService()
return labelService.create(label)
.then(r => {
ctx.commit('setLabel', r)
return Promise.resolve(r)
})
.finally(() => cancel())
try {
const newLabel = await labelService.create(label)
ctx.commit('setLabel', newLabel)
return newLabel
} finally {
cancel()
}
},
},
}

View File

@ -51,7 +51,7 @@ export default {
r.namespaceId = list.namespaceId
ctx.commit('namespaces/addListToNamespace', r, {root: true})
ctx.commit('setList', r)
return Promise.resolve(r)
return r
})
.finally(() => cancel())
},
@ -77,7 +77,7 @@ export default {
}
ctx.dispatch('namespaces/loadNamespacesIfFavoritesDontExist', null, {root: true})
ctx.dispatch('namespaces/removeFavoritesNamespaceIfEmpty', null, {root: true})
return Promise.resolve(newList)
return newList
})
.catch(e => {
// Reset the list state to the initial one to avoid confusion for the user
@ -85,7 +85,7 @@ export default {
...list,
isFavorite: !list.isFavorite,
})
return Promise.reject(e)
throw e
})
.finally(() => cancel())
},

View File

@ -113,7 +113,7 @@ export default {
ctx.commit('lists/setLists', lists, {root: true})
return Promise.resolve(r)
return r
})
.finally(() => {
cancel()
@ -128,7 +128,6 @@ export default {
removeFavoritesNamespaceIfEmpty(ctx) {
if (ctx.state.namespaces[0].id === -2 && ctx.state.namespaces[0].lists.length === 0) {
ctx.state.namespaces.splice(0, 1)
return Promise.resolve()
}
},
deleteNamespace(ctx, namespace) {
@ -138,7 +137,7 @@ export default {
return namespaceService.delete(namespace)
.then(r => {
ctx.commit('removeNamespaceById', namespace.id)
return Promise.resolve(r)
return r
})
.finally(() => cancel())
},
@ -149,7 +148,7 @@ export default {
return namespaceService.create(namespace)
.then(r => {
ctx.commit('addNamespace', r)
return Promise.resolve(r)
return r
})
.finally(() => cancel())
},

View File

@ -43,23 +43,23 @@ function addLabelToTask(task, label) {
return labelTaskService.create(labelTask)
.then(result => {
task.labels.push(label)
return Promise.resolve(result)
return result
})
}
function findAssignees(parsedTaskAssignees) {
async function findAssignees(parsedTaskAssignees) {
if (parsedTaskAssignees.length <= 0) {
return Promise.resolve([])
return []
}
const userService = new UserService()
const assignees = parsedTaskAssignees.map(a =>
userService.getAll({}, {s: a})
.then(users => validateUsername(users, a)),
)
const assignees = parsedTaskAssignees.map(async a => {
const users = await userService.getAll({}, {s: a})
return validateUsername(users, a)
})
return Promise.all(assignees).filter((item) => Boolean(item))
const validatedUsers = await Promise.all(assignees)
return validatedUsers.filter((item) => Boolean(item))
}
@ -136,7 +136,7 @@ export default {
// Usually this means the kanban board hasn't been accessed until now.
// Vuex seems to have its difficulties with that, so we just log the error and fail silently.
console.debug('Could not add assignee to task in kanban, task not found', t)
return Promise.resolve(r)
return r
}
// FIXME: direct store manipulation (task)
t.task.assignees.push(user)
@ -157,7 +157,7 @@ export default {
// Usually this means the kanban board hasn't been accessed until now.
// Vuex seems to have its difficulties with that, so we just log the error and fail silently.
console.debug('Could not remove assignee from task in kanban, task not found', t)
return Promise.resolve(r)
return r
}
for (const a in t.task.assignees) {
@ -186,7 +186,7 @@ export default {
// Usually this means the kanban board hasn't been accessed until now.
// Vuex seems to have its difficulties with that, so we just log the error and fail silently.
console.debug('Could not add label to task in kanban, task not found', t)
return Promise.resolve(r)
return r
}
// FIXME: direct store manipulation (task)
t.task.labels.push(label)
@ -208,7 +208,7 @@ export default {
// Usually this means the kanban board hasn't been accessed until now.
// Vuex seems to have its difficulties with that, so we just log the error and fail silently.
console.debug('Could not remove label from task in kanban, task not found', t)
return Promise.resolve(r)
return r
}
// Remove the label from the list
@ -234,22 +234,21 @@ export default {
const {labels} = rootState.labels
const labelAddsToWaitFor = parsedLabels.map(labelTitle => new Promise((resolve) => {
const labelAddsToWaitFor = parsedLabels.map(async labelTitle => {
let label = validateLabel(labels, labelTitle)
if (typeof label !== 'undefined') {
return resolve(label)
return label
}
// label not found, create it
const labelModel = new LabelModel({title: labelTitle})
return dispatch('labels/createLabel', labelModel).then(() => resolve(label))
await dispatch('labels/createLabel', labelModel)
addLabelToTask(task, label)
})
.then((label) => addLabelToTask(task, label))
.catch(e => Promise.reject(e)),
)
// This waits until all labels are created and added to the task
return Promise.all(labelAddsToWaitFor).then(() => task)
await Promise.all(labelAddsToWaitFor)
return task
},
findListId({ rootGetters }, { list, listId }) {