Skip to content
Snippets Groups Projects
Commit 8c23ea7b authored by Eline Evje's avatar Eline Evje
Browse files

refactor: refactored try/catch of userConfigStore

parent e1c33bdb
No related branches found
No related tags found
3 merge requests!66Final merge,!36Enhancement/implement account number to config,!4Pipeline fix
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { ref } from 'vue'
import authInterceptor from '@/services/authInterceptor' import authInterceptor from '@/services/authInterceptor'
import axios from 'axios' import axios, { AxiosError } from 'axios'
export const useUserConfigStore = defineStore('userConfig', { export const useUserConfigStore = defineStore('userConfig', {
state: () => ({ state: () => ({
...@@ -11,7 +12,8 @@ export const useUserConfigStore = defineStore('userConfig', { ...@@ -11,7 +12,8 @@ export const useUserConfigStore = defineStore('userConfig', {
type: string type: string
specificAmount: number specificAmount: number
generalAmount: number generalAmount: number
}[] }[],
errorMessage: ref<string>('')
}), }),
actions: { actions: {
setExperience(value: string) { setExperience(value: string) {
...@@ -23,23 +25,28 @@ export const useUserConfigStore = defineStore('userConfig', { ...@@ -23,23 +25,28 @@ export const useUserConfigStore = defineStore('userConfig', {
addChallengeTypeConfig(type: string, specificAmount: number, generalAmount: number) { addChallengeTypeConfig(type: string, specificAmount: number, generalAmount: number) {
this.challengeTypeConfigs.push({ type, specificAmount, generalAmount }) this.challengeTypeConfigs.push({ type, specificAmount, generalAmount })
}, },
async postUserConfig() { postUserConfig() {
const payload = { const payload = {
experience: this.experience, experience: this.experience,
motivation: this.motivation, motivation: this.motivation,
challengeTypeConfigs: Array.from(this.challengeTypeConfigs) challengeTypeConfigs: Array.from(this.challengeTypeConfigs)
} }
try { authInterceptor
const response = await authInterceptor.post('/users/me/config/challenge', payload) .post('/config/challenge', payload)
console.log('Success:', response.data) .then((response) => {
} catch (error: unknown) { console.log('Success:', response.data)
if (axios.isAxiosError(error)) { })
console.error('Axios error:', error.response?.data || error.message) .catch((error) => {
} else { const axiosError = error as AxiosError
console.error('An unexpected error occurred:', error) if (axiosError.response && axiosError.response.data) {
} const errorData = axiosError.response.data as { message: string }
} this.errorMessage = errorData.message || 'An error occurred'
} else {
this.errorMessage = 'An unexpected error occurred'
}
console.error('Axios error:', this.errorMessage)
})
} }
} }
}) })
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment