feat: use async / await where it makes sense
This commit is contained in:
@ -11,24 +11,22 @@ export function uploadFile(taskId: number, file: FileModel, onSuccess: () => Fun
|
||||
return uploadFiles(attachmentService, taskId, files, onSuccess)
|
||||
}
|
||||
|
||||
export function uploadFiles(attachmentService: AttachmentService, taskId: number, files: FileModel[], onSuccess : Function = () => {}) {
|
||||
export async function uploadFiles(attachmentService: AttachmentService, taskId: number, files: FileModel[], onSuccess : Function = () => {}) {
|
||||
const attachmentModel = new AttachmentModel({taskId})
|
||||
attachmentService.create(attachmentModel, files)
|
||||
.then(r => {
|
||||
console.debug(`Uploaded attachments for task ${taskId}, response was`, r)
|
||||
if (r.success !== null) {
|
||||
r.success.forEach((attachment: AttachmentModel) => {
|
||||
store.dispatch('tasks/addTaskAttachment', {
|
||||
taskId,
|
||||
attachment,
|
||||
})
|
||||
onSuccess(generateAttachmentUrl(taskId, attachment.id))
|
||||
})
|
||||
}
|
||||
if (r.errors !== null) {
|
||||
throw Error(r.errors)
|
||||
}
|
||||
const response = await attachmentService.create(attachmentModel, files)
|
||||
console.debug(`Uploaded attachments for task ${taskId}, response was`, response)
|
||||
|
||||
response.success?.map((attachment: AttachmentModel) => {
|
||||
store.dispatch('tasks/addTaskAttachment', {
|
||||
taskId,
|
||||
attachment,
|
||||
})
|
||||
onSuccess(generateAttachmentUrl(taskId, attachment.id))
|
||||
})
|
||||
|
||||
if (response.errors !== null) {
|
||||
throw Error(response.errors)
|
||||
}
|
||||
}
|
||||
|
||||
export function generateAttachmentUrl(taskId: number, attachmentId: number) : any {
|
||||
|
@ -41,19 +41,19 @@ export const removeToken = () => {
|
||||
* Refreshes an auth token while ensuring it is updated everywhere.
|
||||
* @returns {Promise<AxiosResponse<any>>}
|
||||
*/
|
||||
export const refreshToken = (persist: boolean): Promise<AxiosResponse> => {
|
||||
export async function refreshToken(persist: boolean): Promise<AxiosResponse> {
|
||||
const HTTP = HTTPFactory()
|
||||
return HTTP.post('user/token', null, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${getToken()}`,
|
||||
},
|
||||
})
|
||||
.then(r => {
|
||||
saveToken(r.data.token, persist)
|
||||
return r
|
||||
})
|
||||
.catch(e => {
|
||||
throw new Error('Error renewing token: ', { cause: e })
|
||||
try {
|
||||
const response = await HTTP.post('user/token', null, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${getToken()}`,
|
||||
},
|
||||
})
|
||||
saveToken(response.data.token, persist)
|
||||
return response
|
||||
|
||||
} catch(e) {
|
||||
throw new Error('Error renewing token: ', { cause: e })
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user