Skip to content
Draft
Show file tree
Hide file tree
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
510 changes: 226 additions & 284 deletions apps/settings/src/components/UserList.vue

Large diffs are not rendered by default.

161 changes: 71 additions & 90 deletions apps/settings/src/components/Users/EditUserDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,113 +36,94 @@
</NcDialog>
</template>

<script>
<script setup lang="ts">
import type { IUser } from '../../views/user-types.d.ts'
import type { QuotaOption } from './userFormUtils.ts'

import { showError, showSuccess } from '@nextcloud/dialogs'
import { translate as t } from '@nextcloud/l10n'
import { confirmPassword } from '@nextcloud/password-confirmation'
import { computed, provide, reactive, ref } from 'vue'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcDialog from '@nextcloud/vue/components/NcDialog'
import UserFormFields from './UserFormFields.vue'
import logger from '../../logger.ts'
import { useStore } from '../../store/index.js'
import { formDataKey } from './injectionKeys.ts'
import { diffPayload, userToFormData } from './userFormUtils.ts'

export default {
name: 'EditUserDialog',
const props = defineProps<{
user: IUser
quotaOptions: QuotaOption[]
}>()

components: {
NcButton,
NcDialog,
UserFormFields,
},
const emit = defineEmits<{
closing: []
}>()

// Children inject this reactive object and mutate its properties via v-model.
// Do not reassign editedUser entirely, the injected reference would go stale.
provide() {
return {
formData: this.editedUser,
}
},
const store = useStore()

props: {
user: {
type: Object,
required: true,
},
const allGroups = store.getters.getGroups
const serverLanguages = store.getters.getServerData.languages
const formData = userToFormData(props.user, allGroups, props.quotaOptions, serverLanguages)

quotaOptions: {
type: Array,
required: true,
},
},
const initialData = structuredClone(formData)
// Children inject and mutate this object's properties; never reassign it or the
// injected reference goes stale.
const editedUser = reactive(formData)
const saving = ref(false)
const fieldErrors = ref<Record<string, string>>({})

emits: ['closing'],

data() {
const allGroups = this.$store.getters.getGroups
const serverLanguages = this.$store.getters.getServerData.languages
const formData = userToFormData(this.user, allGroups, this.quotaOptions, serverLanguages)
return {
/** Snapshot of initial state for diffing */
initialData: structuredClone(formData),
/** Mutable form state */
editedUser: formData,
saving: false,
fieldErrors: {},
}
},
provide(formDataKey, editedUser)

const settings = computed(() => store.getters.getServerData)

computed: {
settings() {
return this.$store.getters.getServerData
},

fieldConfig() {
return {
username: {
show: true,
disabled: true,
label: t('settings', 'Account name'),
},

password: {
show: this.settings.canChangePassword && this.user.backendCapabilities.setPassword,
label: t('settings', 'New password'),
},
}
},
const fieldConfig = computed(() => ({
username: {
show: true,
disabled: true,
label: t('settings', 'Account name'),
},

methods: {
async save() {
this.fieldErrors = {}

const payload = diffPayload(this.initialData, this.editedUser)
if (Object.keys(payload).length === 0) {
this.$emit('closing')
return
}

this.saving = true
try {
await confirmPassword()
await this.$store.dispatch('editUserMultiField', {
userid: this.user.id,
payload,
})
showSuccess(t('settings', 'Account updated'))
this.$emit('closing')
} catch (error) {
const errors = error.response?.data?.ocs?.data?.errors
if (errors && typeof errors === 'object') {
this.fieldErrors = errors
} else {
logger.error('Failed to update account', { error })
showError(t('settings', 'Failed to update account'))
}
} finally {
this.saving = false
}
},
password: {
show: settings.value.canChangePassword && props.user.backendCapabilities.setPassword,
label: t('settings', 'New password'),
},
}))

/**
* Submit the changed fields; map a 422 to per-field errors, else close.
*/
async function save() {
fieldErrors.value = {}

const payload = diffPayload(initialData, editedUser)
if (Object.keys(payload).length === 0) {
emit('closing')
return
}

saving.value = true
try {
await confirmPassword()
await store.dispatch('editUserMultiField', {
userid: props.user.id,
payload,
})
showSuccess(t('settings', 'Account updated'))
emit('closing')
} catch (error) {
const errors = (error as { response?: { data?: { ocs?: { data?: { errors?: Record<string, string> } } } } })
.response?.data?.ocs?.data?.errors
if (errors && typeof errors === 'object') {
fieldErrors.value = errors
} else {
logger.error('Failed to update account', { error })
showError(t('settings', 'Failed to update account'))
}
} finally {
saving.value = false
}
}
</script>

Expand Down
Loading
Loading