Skip to content
Snippets Groups Projects
challengeStore.ts 3.12 KiB
Newer Older
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
import { defineStore } from 'pinia'
import { ref } from 'vue'
import authInterceptor from '@/services/authInterceptor'
import type { Challenge } from '@/types/challenge'
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed

export const useChallengeStore = defineStore('challenge', () => {
    // Reactive state to hold the list of challenges
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
    const challenges = ref<Challenge[]>([])
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed

    // Function to fetch challenges for the current user
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
    const getUserChallenges = async () => {
        try {
            const response = await authInterceptor('/challenges')
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
            if (response.data && response.data.content) {
                challenges.value = response.data.content
            } else {
                challenges.value = []
                console.error('No challenge content found:', response.data)
            }
        } catch (error) {
            console.error('Error fetching challenges:', error)
            challenges.value = [] // Ensure challenges is always an array
        }
    // Function to edit a user challenge
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
    const editUserChallenge = async (challenge: Challenge) => {
        try {
            const response = await authInterceptor.put(`/challenges/${challenge.id}`, challenge)
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
            if (response.data) {
                // Update local challenge state to reflect changes
                const index = challenges.value.findIndex((c) => c.id === challenge.id)
                if (index !== -1) {
                    challenges.value[index] = { ...challenges.value[index], ...response.data }
                    console.log('Updated Challenge:', response.data)
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
                    return challenges.value[index]
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
                }
            } else {
                console.error('No challenge content found in response data')
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
                return null
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
            }
        } catch (error) {
            console.error('Error updating challenge:', error)
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
            return null
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
        }
    }

    // Function to mark a user challenge as completed
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
    const completeUserChallenge = async (challenge: Challenge) => {
        try {
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
            const response = await authInterceptor.put(
                `/challenges/${challenge.id}/complete`,
                challenge
            )
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
            if (response.data) {
                // Update local challenge state to reflect changes
                const index = challenges.value.findIndex((c) => c.id === challenge.id)
                if (index !== -1) {
                    challenges.value[index] = { ...challenges.value[index], ...response.data }
                    console.log('Updated Challenge:', response.data)
                    console.log('Challenge Completed store:', challenges.value[index])
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
                    return challenges.value[index]
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
                }
            } else {
                console.error('No challenge content found in response data')
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
                return null
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
            }
        } catch (error) {
            console.error('Error updating challenge:', error)
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
            return null
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed

    // Return reactive state and functions to be used by components
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
    return {
        challenges,
        getUserChallenges,
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
        editUserChallenge,
        completeUserChallenge
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
    }
})