filebrowser/frontend/src/views/settings/User.vue

127 lines
3.2 KiB
Vue
Raw Normal View History

2018-02-01 12:17:04 +00:00
<template>
<errors v-if="error" :errorCode="error.status" />
2024-09-16 21:01:16 +00:00
<form @submit="save" class="card active">
2024-08-24 22:02:33 +00:00
<div class="card-title">
<h2 v-if="user.id === 0">{{ $t("settings.newUser") }}</h2>
<h2 v-else>{{ $t("settings.user") }} {{ user.username }}</h2>
</div>
2018-02-01 12:17:04 +00:00
2024-08-24 22:02:33 +00:00
<div class="card-content">
<user-form
2024-11-21 00:15:30 +00:00
v-model:user="user"
2024-08-24 22:02:33 +00:00
:createUserDir="createUserDir"
:isDefault="false"
:isNew="isNew"
@update:createUserDir="(updatedDir) => (createUserDir = updatedDir)"
/>
</div>
2018-02-01 12:17:04 +00:00
2024-08-24 22:02:33 +00:00
<div class="card-action">
<button
v-if="!isNew"
@click.prevent="deletePrompt"
type="button"
class="button button--flat button--red"
:aria-label="$t('buttons.delete')"
:title="$t('buttons.delete')"
>
{{ $t("buttons.delete") }}
</button>
<input class="button button--flat" type="submit" :value="$t('buttons.save')" />
2021-02-19 13:15:46 +00:00
</div>
2024-08-24 22:02:33 +00:00
</form>
2018-02-01 12:17:04 +00:00
</template>
2024-11-21 00:15:30 +00:00
2018-02-01 12:17:04 +00:00
<script>
2024-11-21 00:15:30 +00:00
import { getters, mutations, state } from "@/store";
import { usersApi, settingsApi } from "@/api";
2024-07-30 17:45:27 +00:00
import UserForm from "@/components/settings/UserForm.vue";
import Errors from "@/views/Errors.vue";
2024-09-16 21:01:16 +00:00
import { notify } from "@/notify";
2018-02-01 12:17:04 +00:00
export default {
2021-03-21 11:51:58 +00:00
name: "user",
components: {
2021-03-21 11:51:58 +00:00
UserForm,
Errors,
},
data() {
2018-02-01 12:17:04 +00:00
return {
error: null,
2018-02-01 12:17:04 +00:00
originalUser: null,
2024-09-16 21:01:16 +00:00
user: {
scope: ".",
username: "",
perm: { admin: false },
},
showDelete: false,
createUserDir: false,
2021-03-21 11:51:58 +00:00
};
2018-02-01 12:17:04 +00:00
},
2021-03-21 11:51:58 +00:00
created() {
this.fetchData();
2018-02-01 12:17:04 +00:00
},
computed: {
2024-08-24 22:02:33 +00:00
settings() {
return state.settings;
},
2021-03-21 11:51:58 +00:00
isNew() {
2024-11-21 00:15:30 +00:00
return getters.routePath().endsWith("settings/users/new");
},
userPayload() {
return JSON.parse(JSON.stringify(this.user)); // Deep copy for safety
2021-03-21 11:51:58 +00:00
},
},
2018-02-01 12:17:04 +00:00
methods: {
2021-03-21 11:51:58 +00:00
async fetchData() {
2024-08-24 22:02:33 +00:00
mutations.setLoading("users", true);
try {
if (this.isNew) {
2024-11-21 00:15:30 +00:00
let { defaults, createUserDir } = await settingsApi.get();
this.createUserDir = createUserDir;
this.user = {
...defaults,
2021-03-21 11:51:58 +00:00
username: "",
2024-07-30 17:45:27 +00:00
password: "",
rules: [],
lockPassword: false,
2021-03-21 11:51:58 +00:00
id: 0,
};
} else {
2024-09-16 21:01:16 +00:00
const id = Array.isArray(state.route.params.id)
? state.route.params.id.join("")
: state.route.params.id;
2024-11-21 00:15:30 +00:00
if (id === undefined) {
return;
}
this.user = { ...(await usersApi.get(id)) };
2018-02-01 12:17:04 +00:00
}
} catch (e) {
2024-09-16 21:01:16 +00:00
notify.showError(e);
this.error = e;
} finally {
2024-08-24 22:02:33 +00:00
mutations.setLoading("users", false);
2018-02-01 12:17:04 +00:00
}
},
2021-03-21 11:51:58 +00:00
deletePrompt() {
2024-07-30 17:45:27 +00:00
mutations.showHover({ name: "deleteUser", props: { user: this.user } });
2018-02-01 12:17:04 +00:00
},
2021-03-21 11:51:58 +00:00
async save(event) {
event.preventDefault();
try {
if (this.isNew) {
2024-11-21 00:15:30 +00:00
const loc = await usersApi.create(this.userPayload); // Use the computed property
2021-03-21 11:51:58 +00:00
this.$router.push({ path: loc });
2024-09-16 21:01:16 +00:00
notify.showSuccess(this.$t("settings.userCreated"));
} else {
2024-11-21 00:15:30 +00:00
await usersApi.update(this.userPayload);
2024-09-16 21:01:16 +00:00
notify.showSuccess(this.$t("settings.userUpdated"));
2018-02-01 12:17:04 +00:00
}
} catch (e) {
2024-09-16 21:01:16 +00:00
notify.showError(e);
2018-02-01 12:17:04 +00:00
}
2021-03-21 11:51:58 +00:00
},
},
};
2018-02-01 12:17:04 +00:00
</script>