Skip to content
Snippets Groups Projects
ViewProfileView.vue 6.24 KiB
Newer Older
<script lang="ts" setup>
import authInterceptor from '@/services/authInterceptor'
Ina Martini's avatar
Ina Martini committed
import { onMounted, ref } from 'vue'
import type { Profile } from '@/types/profile'
import CardTemplate from '@/components/CardTemplate.vue'
import type { Challenge } from '@/types/challenge'
import type { Goal } from '@/types/goal'
import CardGoal from '@/components/CardGoal.vue'
import router from '@/router'
Ina Martini's avatar
Ina Martini committed
import SpareComponent from '@/components/SpareComponent.vue'
import { useUserStore } from '@/stores/userStore'
import ModalEditAvatar from '@/components/ModalEditAvatar.vue'

const profile = ref<Profile>()
const completedGoals = ref<Goal[]>([])
const completedChallenges = ref<Challenge[]>([])
Ina Martini's avatar
Ina Martini committed
const speech = ref<string[]>([])
const profilePicture = ref<string>()

Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
const userStore = useUserStore()
const updateUser = async () => {
    authInterceptor('/profile')
        .then((response) => {
            profile.value = response.data
            console.log(profile.value)
        })
        .catch((error) => {
            return console.log(error)
        })
}

onMounted(async () => {
    await updateUser()

    await authInterceptor(`/goals/completed?page=0&size=3`)
            completedGoals.value = response.data.content
            return console.log(error)

    await authInterceptor('/challenges/completed?page=0&size=3')
        .then((response) => {
            completedChallenges.value = response.data.content
        })
        .catch((error) => {
            return console.log(error)
        })

    await userStore.getProfilePicture()
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
    profilePicture.value = userStore.profilePicture
Ina Martini's avatar
Ina Martini committed
    openSpare()
Ina Martini's avatar
Ina Martini committed
})
const updateBiometrics = async () => {
    await useUserStore().bioRegister()
    await updateUser()
}

Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
const updateProfilePicture = async () => {
    await updateUser()
    await userStore.getProfilePicture()
    profilePicture.value = userStore.profilePicture
Ina Martini's avatar
Ina Martini committed
const openSpare = () => {
Ina Martini's avatar
Ina Martini committed
    speech.value = [
        `Velkommen, ${profile.value?.firstName} ${profile.value?.lastName} !`,
        'Her kan du finne en oversikt over dine profilinstillinger!',
        'Du kan også se dine fullførte sparemål og utfordringer!'
    ]
Ina Martini's avatar
Ina Martini committed
}
    <div class="w-full flex px-10 justify-center">
        <div class="flex flex-row flex-wrap justify-center w-full max-w-screen-xl gap-20">
            <div class="flex flex-col max-w-96 w-full gap-5">
                <div class="flex flex-row gap-5">
                    <div class="flex flex-col gap-1">
Valdemar Åstorp Beere's avatar
Valdemar Åstorp Beere committed
                        <img
                            :src="profilePicture"
                            alt="could not load"
                            class="block mx-auto h-32 rounded-full border-green-600 border-2 sm:mx-0 sm:shrink-0"
                        />
                        <ModalEditAvatar @update-profile-picture="updateProfilePicture" />
                    <div class="w-full flex flex-col justify-between">
                        <h3 class="font-thin my-0">{{ profile?.username }}</h3>
                        <h3 class="font-thin my-0">
                            {{ profile?.firstName + ' ' + profile?.lastName }}
                        </h3>
                        <h3 class="font-thin my-0">{{ profile?.email }}</h3>
                    </div>
                </div>

                <h3
                    class="font-bold"
                    v-text="'Du har totalt spart ' + profile?.savedAmount + 'kr'"
                />
                    <div class="bg-red-100">
                        <p class="font-bold mx-3" v-text="'Brukskonto'" />
                    </div>
                    <p
                        class="mx-3"
                        v-text="profile?.spendingAccount.accNumber || 'Ingen brukskonto oppkoblet'"
                    />
                </CardTemplate>

                <CardTemplate>
                    <div class="bg-red-100">
                        <p class="font-bold mx-3" v-text="'Sparekonto'" />
                    </div>
                    <p
                        class="mx-3"
                        v-text="profile?.savingAccount.accNumber || 'Ingen sparekonto oppkoblet'"
                    />
                </CardTemplate>

                    @click="router.push({ name: 'edit-profile' })"
                    v-text="'Rediger bruker'"
                />
                    @click="router.push({ name: 'edit-configuration' })"
                    v-text="'Rediger konfigurasjon'"
                />
                <button class="primary" @click="updateBiometrics">
                    {{ profile?.hasPasskey ? 'Endre biometri' : 'Legg til biometri' }}
                </button>
            </div>

            <div class="flex flex-col">
Ina Martini's avatar
Ina Martini committed
                <SpareComponent
                    :speech="speech"
                    :png-size="15"
                    :imageDirection="'left'"
                    :direction="'right'"
                    class="mb-5"
                ></SpareComponent>
                <div class="flex flex-row justify-between mx-4">
                    <p class="font-bold">Fullførte sparemål</p>
                    <a class="hover:p-0 cursor-pointer" v-text="'Se alle'" />
                </div>
                <CardTemplate class="p-4 flex flex-row flex-wrap justify-center gap-2 mb-4 mt-2">
                    <CardGoal v-for="goal in completedGoals" :key="goal.id" :goal-instance="goal" />
                </CardTemplate>

                <div class="flex flex-row justify-between mx-4">
                    <p class="font-bold">Fullførte utfordringer</p>
                    <a class="hover:p-0 cursor-pointer" v-text="'Se alle'" />
                </div>
                <CardTemplate class="p-4 flex flex-row flex-wrap justify-center gap-2 mb-4 mt-2">
                    <CardGoal
                        v-for="challenge in completedChallenges"
                        :key="challenge.id"
                        :goal-instance="challenge"
                    />
                </CardTemplate>
            </div>
        </div>
    </div>