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

refactor: merged and refactored format of userConfigStore + accountStore

parent e3043c23
Branches feat/signup/config-new-user
No related tags found
3 merge requests!66Final merge,!44Configuration validation,!4Pipeline fix
import { defineStore } from 'pinia'
import { ref } from 'vue'
import authInterceptor from '@/services/authInterceptor'
import axios, { AxiosError } from 'axios'
export const useAccountStore = defineStore('account', {
state: () => ({
errorMessage: ref<string>('')
}),
actions: {
async postAccount(accountType: 'SAVING' | 'SPENDING', accNumber: string, balance: number) {
const payload = {
accountType,
accNumber,
balance
}
try {
const response = await authInterceptor.post('/accounts', payload)
console.log('Success:', response.data)
} catch (error) {
console.error('Error posting account:', error)
this.handleAxiosError(error)
}
},
handleAxiosError(error: any) {
const axiosError = error as AxiosError
if (axiosError.response && axiosError.response.data) {
const errorData = axiosError.response.data as { message: string }
} else {
this.errorMessage = 'An unexpected error occurred'
}
}
}
})
import { defineStore } from 'pinia'
import { ref } from 'vue'
import authInterceptor from '@/services/authInterceptor'
import axios, { AxiosError } from 'axios'
import { ref } from 'vue';
import { defineStore } from 'pinia';
import authInterceptor from '@/services/authInterceptor';
import { AxiosError } from 'axios';
export const useUserConfigStore = defineStore('userConfig', {
state: () => ({
role: 'USER',
experience: 'VERY_HIGH',
motivation: 'VERY_HIGH',
challengeTypeConfigs: [] as {
type: string
specificAmount: number
generalAmount: number
}[],
errorMessage: ref<string>('')
}),
actions: {
setExperience(value: string) {
this.experience = value
},
setMotivation(value: string) {
this.motivation = value
},
addChallengeTypeConfig(type: string, specificAmount: number, generalAmount: number) {
this.challengeTypeConfigs.push({ type, specificAmount, generalAmount })
},
postUserConfig() {
const payload = {
experience: this.experience,
motivation: this.motivation,
challengeTypeConfigs: Array.from(this.challengeTypeConfigs)
}
export const useUserConfigStore = defineStore('userConfig', () => {
const role = ref('USER');
const experience = ref('');
const motivation = ref('');
const challengeTypeConfigs = ref([] as {
type: string;
specificAmount: number;
generalAmount: number;
}[]);
const accounts = ref({
savings: '',
spending: ''
});
const errorMessage = ref<string>('');
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)
})
}
}
})
const setExperience = (value: string) => {
experience.value = value;
};
const setMotivation = (value: string) => {
motivation.value = value;
};
const addChallengeTypeConfig = (type: string, specificAmount: number, generalAmount: number) => {
challengeTypeConfigs.value.push({ type, specificAmount, generalAmount });
};
const postAccount = async (accountType: 'SAVING' | 'SPENDING', accNumber: string, balance: number) => {
const payload = {
accountType,
accNumber,
balance
};
await authInterceptor.post('/accounts', payload)
.then(response => {
console.log('Success:', response.data);
})
.catch(error => {
const axiosError = error as AxiosError;
errorMessage.value = (axiosError.response?.data as string) || 'An error occurred while posting account'
console.error('Error posting account:', errorMessage.value);
});
};
const postUserConfig = () => {
const payload = {
experience: experience.value,
motivation: motivation.value,
challengeTypeConfigs: Array.from(challengeTypeConfigs.value)
};
authInterceptor.post('/config/challenge', payload)
.then(response => {
console.log('Success:', response.data);
})
.catch(error => {
const axiosError = error as AxiosError;
errorMessage.value = (axiosError.response?.data as string) || 'An error occurred while updating configuration'
console.error('Error updating configuration:', errorMessage.value);
});
};
return {
role,
experience,
motivation,
challengeTypeConfigs,
accounts,
errorMessage,
setExperience,
setMotivation,
addChallengeTypeConfig,
postAccount,
postUserConfig
};
});
......@@ -47,12 +47,12 @@
<script setup lang="ts">
import { ref, computed } from 'vue'
import { useAccountStore } from '@/stores/accountStore'
import { useUserConfigStore } from '@/stores/userConfigStore'
import ContinueButtonComponent from '@/components/ContinueButtonComponent.vue'
import router from '@/router'
const MAX_DIGITS = 11
const accountStore = useAccountStore()
const userConfig = useUserConfigStore()
const spendingAccount = ref('')
const savingsAccount = ref('')
......@@ -68,9 +68,8 @@ async function onButtonClick() {
const savingAccountNumber = savingsAccount.value.replace(/\./g, '')
const spendingAccountNumber = spendingAccount.value.replace(/\./g, '')
await accountStore.postAccount('SAVING', savingAccountNumber, 0)
await accountStore.postAccount('SPENDING', spendingAccountNumber, 0)
await userConfig.postAccount('SAVING', savingAccountNumber, 0)
await userConfig.postAccount('SPENDING', spendingAccountNumber, 0)
await router.push({ name: 'home' })
}
......
......@@ -31,7 +31,7 @@ import { useChallengeStore } from '@/stores/challengeStore'
import SavingsPath from '@/components/SavingsPath.vue'
import GeneratedChallengesModal from '@/components/GeneratedChallengesModal.vue'
const showModal = ref(true);
const showModal = ref(true)
const goalStore = useGoalStore()
const challengeStore = useChallengeStore()
......
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