Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions frontend/src/views/settings/UsersTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
>
Tilbakestill passord
</button>
<span v-if="saveError" class="text-sm text-red-500 mr-auto">{{ saveError }}</span>
<button class="btn btn-secondary" @click="showModal = false">Avbryt</button>
<button class="btn btn-primary" @click="save">Lagre</button>
</template>
Expand All @@ -142,6 +143,7 @@ const loading = ref(false)
const showModal = ref(false)
const isEditing = ref(false)
const editingId = ref<number | null>(null)
const saveError = ref<string | null>(null)

type FormData = Omit<SettingsUser, 'id' | 'colorBg' | 'colorText'>

Expand Down Expand Up @@ -203,13 +205,15 @@ function getAvatarColors(role: UserRole): { bg: string; text: string } {
function openAddModal() {
isEditing.value = false
editingId.value = null
saveError.value = null
Object.assign(form, emptyForm())
showModal.value = true
}

function openEditModal(user: SettingsUser) {
isEditing.value = true
editingId.value = user.id
saveError.value = null
Object.assign(form, {
firstName: user.firstName,
lastName: user.lastName,
Expand All @@ -222,18 +226,23 @@ function openEditModal(user: SettingsUser) {
}

async function save() {
saveError.value = null
const colors = getAvatarColors(form.role)
const userData = { ...form, colorBg: colors.bg, colorText: colors.text }

if (isEditing.value && editingId.value !== null) {
const updated = await organizationService.updateUser(editingId.value, userData)
const idx = users.value.findIndex((u) => u.id === editingId.value)
if (idx !== -1) users.value[idx] = { ...users.value[idx], ...updated }
} else {
const created = await organizationService.createUser(userData)
users.value.push(created)
try {
if (isEditing.value && editingId.value !== null) {
const updated = await organizationService.updateUser(editingId.value, userData)
const idx = users.value.findIndex((u) => u.id === editingId.value)
if (idx !== -1) users.value[idx] = { ...users.value[idx], ...updated }
} else {
const created = await organizationService.createUser(userData)
users.value.push(created)
}
showModal.value = false
} catch (e: unknown) {
saveError.value = e instanceof Error ? e.message : 'Noe gikk galt. Prøv igjen.'
}
showModal.value = false
}

function resetPassword() {
Expand Down
Loading