Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<script lang="ts" setup>
import authInterceptor from '@/services/authInterceptor'
import CardTemplate from '@/components/CardTemplate.vue'
import type { ChallengeConfig } from '@/types/challengeConfig'
import { onMounted, ref } from 'vue'
import ModalComponent from '@/components/ModalComponent.vue'
import router from '@/router'
const configuration = ref<ChallengeConfig>({
motivation: '',
experience: '',
challengeTypeConfigs: [
{
type: 'Kaffe',
generalAmount: 100,
specificAmount: 10
}
]
})
const error = ref<string | null>(null)
const deleteChallengeType = (type: string) => {
if (configuration.value.challengeTypeConfigs) {
configuration.value.challengeTypeConfigs = configuration.value.challengeTypeConfigs.filter(
(item) => item.type !== type
)
}
}
const createChallengeType = () => {
configuration.value.challengeTypeConfigs?.push({
type: '',
specificAmount: null,
generalAmount: null
})
}
const validateAndSave = () => {
if (!configuration.value.motivation) {
return (error.value = 'Du må velge hvor store vaneendringer du er villig til å gjøre')
}
if (!configuration.value.experience) {
return (error.value = 'Du må velge hvor kjent du er med sparing fra før av')
}
if (configuration.value.challengeTypeConfigs.length == 0) {
return (error.value = 'Du må legge til minst én ting du bruker mye penger på')
}
if (
configuration.value.challengeTypeConfigs.some(
(item) => !item.type || !item.specificAmount || !item.generalAmount
)
) {
return (error.value = 'Du må fylle ut alle feltene for ting du bruker mye penger på')
}
if (
configuration.value.challengeTypeConfigs.some(
(item) =>
(item.specificAmount && item.specificAmount < 0) ||
(item.generalAmount && item.generalAmount < 0)
)
) {
return (error.value = 'Prisene kan ikke være negative')
}
saveConfiguration()
}
const saveConfiguration = () => {
authInterceptor
.put('/config/challenge', configuration.value)
.then(() => {
router.push({ name: 'profile' })
})
.catch((error) => {
error.value = error.response.data.message
})
}
onMounted(() => {
authInterceptor('/config/challenge')
.then((response) => {
configuration.value = response.data
})
.catch((error) => {
console.error(error)
})
})
</script>
<template>
<div class="w-full flex px-10 justify-center">
<div class="flex flex-col justify-center items-center max-w-screen-xl gap-3">
<h1>Rediger konfigurasjon✏️</h1>
<h3 class="font-bold">Hvor store vaneedringer er du villig til å gjøre?</h3>
<div v-if="configuration" class="flex flex-row gap-5">
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<button @click="configuration.motivation = 'VERY_LOW'">
<CardTemplate
:class="{
'border-2 border-lime-400': configuration.motivation === 'VERY_LOW'
}"
class="cursor-pointer p-4 border-2"
@click="configuration.motivation = 'VERY_LOW'"
>
<p class="font-bold">Litt</p>
</CardTemplate>
</button>
<button @click="configuration.motivation = 'MEDIUM'">
<CardTemplate
:class="{
'border-2 border-lime-400': configuration.motivation === 'MEDIUM'
}"
class="cursor-pointer p-4 border-2"
>
<p class="font-bold">Passe</p>
</CardTemplate>
</button>
<button @click="configuration.motivation = 'VERY_HIGH'">
<CardTemplate
:class="{
'border-2 border-lime-400': configuration.motivation === 'VERY_HIGH'
}"
class="cursor-pointer p-4 border-2"
>
<p class="font-bold">Store</p>
</CardTemplate>
</button>
</div>
<h3 class="font-bold">Hvor kjent er du med sparing fra før av?</h3>
<div v-if="configuration" class="flex flex-row gap-5">
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<button @click="configuration.experience = 'VERY_LOW'">
<CardTemplate
:class="{
'border-2 border-lime-400': configuration.experience === 'VERY_LOW'
}"
class="cursor-pointer p-4 border-2"
>
<p class="font-bold">Litt kjent</p>
</CardTemplate>
</button>
<button @click="configuration.experience = 'MEDIUM'">
<CardTemplate
:class="{
'border-2 border-lime-400': configuration.experience === 'MEDIUM'
}"
class="cursor-pointer p-4 border-2"
>
<p class="font-bold">Noe kjent</p>
</CardTemplate>
</button>
<button @click="configuration.experience = 'VERY_HIGH'">
<CardTemplate
:class="{
'border-2 border-lime-400': configuration.experience === 'VERY_HIGH'
}"
class="cursor-pointer p-4 border-2"
>
<p class="font-bold">Godt kjent</p>
</CardTemplate>
</button>
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
</div>
<h3 class="font-bold my-0">Hva bruker du mye penger på?</h3>
<div class="flex flex-col gap-4 p-4 items-center">
<CardTemplate
v-for="(item, index) in configuration.challengeTypeConfigs"
:key="index"
class="flex flex-row flex-wrap justify-center gap-5 border-2 p-4"
>
<input v-model="item.type" placeholder="Type" type="text" />
<input
v-model="item.specificAmount"
placeholder="Generell pris"
type="number"
/>
<input v-model="item.generalAmount" placeholder="Pris per uke" type="number" />
<button
class="primary danger w-min items-center"
@click="deleteChallengeType(item.type)"
v-text="'Slett'"
/>
</CardTemplate>
<button
class="font-bold text-2xl cursor-pointer transition-transform duration-300 ease-in-out hover:scale-110 hover:opacity-100 justify-start"
@click="createChallengeType"
v-text="'Legg til flere📝'"
/>
</div>
<div class="flex flex-row justify-center gap-5">
<button class="primary danger" @click="router.back()">Avbryt</button>
<button class="primary" @click="validateAndSave">Lagre</button>
</div>
</div>
<ModalComponent v-if="error">
<p class="my-4" v-text="error" />
<button @click="error = null">Lukk</button>
</ModalComponent>
</div>
</template>
<style scoped></style>