diff --git a/src/stores/userConfigStore.ts b/src/stores/userConfigStore.ts index c1f6b0f1f548e3b21bb1a5d5c45ac80da9d8eab4..3b5661531c91b403d1fb631dc6959fa513065285 100644 --- a/src/stores/userConfigStore.ts +++ b/src/stores/userConfigStore.ts @@ -1,6 +1,7 @@ import { defineStore } from 'pinia' +import { ref } from 'vue' import authInterceptor from '@/services/authInterceptor' -import axios from 'axios' +import axios, { AxiosError } from 'axios' export const useUserConfigStore = defineStore('userConfig', { state: () => ({ @@ -11,7 +12,8 @@ export const useUserConfigStore = defineStore('userConfig', { type: string specificAmount: number generalAmount: number - }[] + }[], + errorMessage: ref<string>('') }), actions: { setExperience(value: string) { @@ -23,23 +25,28 @@ export const useUserConfigStore = defineStore('userConfig', { addChallengeTypeConfig(type: string, specificAmount: number, generalAmount: number) { this.challengeTypeConfigs.push({ type, specificAmount, generalAmount }) }, - async postUserConfig() { + postUserConfig() { const payload = { experience: this.experience, motivation: this.motivation, challengeTypeConfigs: Array.from(this.challengeTypeConfigs) } - try { - const response = await authInterceptor.post('/users/me/config/challenge', payload) - console.log('Success:', response.data) - } catch (error: unknown) { - if (axios.isAxiosError(error)) { - console.error('Axios error:', error.response?.data || error.message) - } else { - console.error('An unexpected error occurred:', error) - } - } + authInterceptor + .post('/config/challenge', payload) + .then((response) => { + console.log('Success:', response.data) + }) + .catch((error) => { + const axiosError = error as AxiosError + 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) + }) } } })