124124 >
125125 Tilbakestill passord
126126 </button >
127+ <span v-if =" saveError" class =" text-sm text-red-500 mr-auto" >{{ saveError }}</span >
127128 <button class =" btn btn-secondary" @click =" showModal = false" >Avbryt</button >
128129 <button class =" btn btn-primary" @click =" save" >Lagre</button >
129130 </template >
@@ -142,6 +143,7 @@ const loading = ref(false)
142143const showModal = ref (false )
143144const isEditing = ref (false )
144145const editingId = ref <number | null >(null )
146+ const saveError = ref <string | null >(null )
145147
146148type FormData = Omit <SettingsUser , ' id' | ' colorBg' | ' colorText' >
147149
@@ -203,13 +205,15 @@ function getAvatarColors(role: UserRole): { bg: string; text: string } {
203205function openAddModal() {
204206 isEditing .value = false
205207 editingId .value = null
208+ saveError .value = null
206209 Object .assign (form , emptyForm ())
207210 showModal .value = true
208211}
209212
210213function openEditModal(user : SettingsUser ) {
211214 isEditing .value = true
212215 editingId .value = user .id
216+ saveError .value = null
213217 Object .assign (form , {
214218 firstName: user .firstName ,
215219 lastName: user .lastName ,
@@ -222,18 +226,23 @@ function openEditModal(user: SettingsUser) {
222226}
223227
224228async function save() {
229+ saveError .value = null
225230 const colors = getAvatarColors (form .role )
226231 const userData = { ... form , colorBg: colors .bg , colorText: colors .text }
227232
228- if (isEditing .value && editingId .value !== null ) {
229- const updated = await organizationService .updateUser (editingId .value , userData )
230- const idx = users .value .findIndex ((u ) => u .id === editingId .value )
231- if (idx !== - 1 ) users .value [idx ] = { ... users .value [idx ], ... updated }
232- } else {
233- const created = await organizationService .createUser (userData )
234- users .value .push (created )
233+ try {
234+ if (isEditing .value && editingId .value !== null ) {
235+ const updated = await organizationService .updateUser (editingId .value , userData )
236+ const idx = users .value .findIndex ((u ) => u .id === editingId .value )
237+ if (idx !== - 1 ) users .value [idx ] = { ... users .value [idx ], ... updated }
238+ } else {
239+ const created = await organizationService .createUser (userData )
240+ users .value .push (created )
241+ }
242+ showModal .value = false
243+ } catch (e : unknown ) {
244+ saveError .value = e instanceof Error ? e .message : ' Noe gikk galt. Prøv igjen.'
235245 }
236- showModal .value = false
237246}
238247
239248function resetPassword() {
0 commit comments