2022-08-23 07:27:42 +00:00
|
|
|
<script lang="ts" setup>
|
|
|
|
// core libs
|
2024-05-23 06:32:49 +00:00
|
|
|
import { nextTick, ref } from "vue";
|
2024-06-25 04:31:44 +00:00
|
|
|
import { coreApiClient } from "@halo-dev/api-client";
|
2022-08-23 07:27:42 +00:00
|
|
|
import type { User } from "@halo-dev/api-client";
|
|
|
|
|
|
|
|
// components
|
2022-12-26 10:36:32 +00:00
|
|
|
import { Toast, VButton, VModal, VSpace } from "@halo-dev/components";
|
2022-09-26 07:48:55 +00:00
|
|
|
import SubmitButton from "@/components/button/SubmitButton.vue";
|
2022-08-23 07:27:42 +00:00
|
|
|
|
|
|
|
// libs
|
2023-12-28 09:13:38 +00:00
|
|
|
import { cloneDeep } from "lodash-es";
|
2022-06-27 08:16:49 +00:00
|
|
|
|
2022-08-23 07:27:42 +00:00
|
|
|
// hooks
|
2022-12-26 15:32:31 +00:00
|
|
|
import AnnotationsForm from "@/components/form/AnnotationsForm.vue";
|
2023-03-23 08:54:33 +00:00
|
|
|
import { useI18n } from "vue-i18n";
|
2023-04-24 08:19:42 +00:00
|
|
|
import { useQueryClient } from "@tanstack/vue-query";
|
2022-12-29 13:04:32 +00:00
|
|
|
|
2023-03-23 08:54:33 +00:00
|
|
|
const { t } = useI18n();
|
2023-04-24 08:19:42 +00:00
|
|
|
const queryClient = useQueryClient();
|
2022-08-23 07:27:42 +00:00
|
|
|
|
2022-08-15 09:56:13 +00:00
|
|
|
const props = withDefaults(
|
|
|
|
defineProps<{
|
2024-05-23 06:32:49 +00:00
|
|
|
user: User;
|
2022-08-15 09:56:13 +00:00
|
|
|
}>(),
|
2024-05-23 06:32:49 +00:00
|
|
|
{}
|
2022-08-15 09:56:13 +00:00
|
|
|
);
|
2022-06-27 08:16:49 +00:00
|
|
|
|
2022-08-15 12:31:51 +00:00
|
|
|
const emit = defineEmits<{
|
|
|
|
(event: "close"): void;
|
|
|
|
}>();
|
2022-06-27 08:16:49 +00:00
|
|
|
|
2024-05-27 08:56:57 +00:00
|
|
|
const modal = ref<InstanceType<typeof VModal> | null>(null);
|
2024-05-23 06:32:49 +00:00
|
|
|
const formState = ref<User>(cloneDeep(props.user));
|
|
|
|
const isSubmitting = ref(false);
|
2022-06-27 08:16:49 +00:00
|
|
|
|
2022-12-26 15:32:31 +00:00
|
|
|
const annotationsFormRef = ref<InstanceType<typeof AnnotationsForm>>();
|
|
|
|
|
2023-04-24 08:19:42 +00:00
|
|
|
const handleUpdateUser = async () => {
|
2022-12-26 15:32:31 +00:00
|
|
|
annotationsFormRef.value?.handleSubmit();
|
|
|
|
await nextTick();
|
|
|
|
|
|
|
|
const { customAnnotations, annotations, customFormInvalid, specFormInvalid } =
|
|
|
|
annotationsFormRef.value || {};
|
|
|
|
if (customFormInvalid || specFormInvalid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
formState.value.metadata.annotations = {
|
|
|
|
...annotations,
|
|
|
|
...customAnnotations,
|
|
|
|
};
|
|
|
|
|
2022-06-27 08:16:49 +00:00
|
|
|
try {
|
2024-05-23 06:32:49 +00:00
|
|
|
isSubmitting.value = true;
|
2023-04-24 08:19:42 +00:00
|
|
|
|
2024-06-25 04:31:44 +00:00
|
|
|
await coreApiClient.user.updateUser({
|
2023-11-13 08:56:08 +00:00
|
|
|
name: formState.value.metadata.name,
|
|
|
|
user: formState.value,
|
|
|
|
});
|
2022-06-27 08:16:49 +00:00
|
|
|
|
2024-05-23 06:32:49 +00:00
|
|
|
modal.value?.close();
|
2022-12-20 11:04:29 +00:00
|
|
|
|
2023-04-24 08:19:42 +00:00
|
|
|
queryClient.invalidateQueries({ queryKey: ["users"] });
|
|
|
|
queryClient.invalidateQueries({ queryKey: ["user-detail"] });
|
|
|
|
|
2023-03-23 08:54:33 +00:00
|
|
|
Toast.success(t("core.common.toast.save_success"));
|
2022-06-27 08:16:49 +00:00
|
|
|
} catch (e) {
|
2022-09-27 09:13:20 +00:00
|
|
|
console.error("Failed to create or update user", e);
|
2022-06-27 08:16:49 +00:00
|
|
|
} finally {
|
2024-05-23 06:32:49 +00:00
|
|
|
isSubmitting.value = false;
|
2022-06-27 08:16:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<VModal
|
2024-05-23 06:32:49 +00:00
|
|
|
ref="modal"
|
2023-04-24 08:19:42 +00:00
|
|
|
:title="$t('core.user.editing_modal.titles.update')"
|
2022-12-26 10:36:32 +00:00
|
|
|
:width="700"
|
2024-05-23 06:32:49 +00:00
|
|
|
@close="emit('close')"
|
2022-06-27 08:16:49 +00:00
|
|
|
>
|
2022-12-26 10:36:32 +00:00
|
|
|
<FormKit
|
|
|
|
id="user-form"
|
|
|
|
name="user-form"
|
|
|
|
:config="{ validationVisibility: 'submit' }"
|
|
|
|
type="form"
|
2023-04-24 08:19:42 +00:00
|
|
|
@submit="handleUpdateUser"
|
2022-12-26 10:36:32 +00:00
|
|
|
>
|
2022-12-26 15:32:31 +00:00
|
|
|
<div>
|
|
|
|
<div class="md:grid md:grid-cols-4 md:gap-6">
|
|
|
|
<div class="md:col-span-1">
|
|
|
|
<div class="sticky top-0">
|
2023-03-23 08:54:33 +00:00
|
|
|
<span class="text-base font-medium text-gray-900">
|
|
|
|
{{ $t("core.user.editing_modal.groups.general") }}
|
|
|
|
</span>
|
2022-12-26 15:32:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="mt-5 divide-y divide-gray-100 md:col-span-3 md:mt-0">
|
|
|
|
<FormKit
|
|
|
|
id="userNameInput"
|
|
|
|
v-model="formState.metadata.name"
|
2023-04-24 08:19:42 +00:00
|
|
|
:disabled="true"
|
2023-03-23 08:54:33 +00:00
|
|
|
:label="$t('core.user.editing_modal.fields.username.label')"
|
2022-12-26 15:32:31 +00:00
|
|
|
type="text"
|
|
|
|
name="name"
|
|
|
|
></FormKit>
|
|
|
|
<FormKit
|
|
|
|
id="displayNameInput"
|
|
|
|
v-model="formState.spec.displayName"
|
2023-03-23 08:54:33 +00:00
|
|
|
:label="$t('core.user.editing_modal.fields.display_name.label')"
|
2022-12-26 15:32:31 +00:00
|
|
|
type="text"
|
|
|
|
name="displayName"
|
|
|
|
validation="required|length:0,50"
|
|
|
|
></FormKit>
|
|
|
|
<FormKit
|
|
|
|
v-model="formState.spec.email"
|
2023-03-23 08:54:33 +00:00
|
|
|
:label="$t('core.user.editing_modal.fields.email.label')"
|
2022-12-26 15:32:31 +00:00
|
|
|
type="email"
|
|
|
|
name="email"
|
|
|
|
validation="required|email|length:0,100"
|
|
|
|
></FormKit>
|
|
|
|
<FormKit
|
|
|
|
v-model="formState.spec.bio"
|
2023-03-23 08:54:33 +00:00
|
|
|
:label="$t('core.user.editing_modal.fields.bio.label')"
|
2022-12-26 15:32:31 +00:00
|
|
|
type="textarea"
|
|
|
|
name="bio"
|
|
|
|
validation="length:0,2048"
|
|
|
|
></FormKit>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-12-26 10:36:32 +00:00
|
|
|
</FormKit>
|
2022-12-26 15:32:31 +00:00
|
|
|
|
|
|
|
<div class="py-5">
|
|
|
|
<div class="border-t border-gray-200"></div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="md:grid md:grid-cols-4 md:gap-6">
|
|
|
|
<div class="md:col-span-1">
|
|
|
|
<div class="sticky top-0">
|
2023-03-23 08:54:33 +00:00
|
|
|
<span class="text-base font-medium text-gray-900">
|
|
|
|
{{ $t("core.user.editing_modal.groups.annotations") }}
|
|
|
|
</span>
|
2022-12-26 15:32:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="mt-5 divide-y divide-gray-100 md:col-span-3 md:mt-0">
|
|
|
|
<AnnotationsForm
|
|
|
|
:key="formState.metadata.name"
|
|
|
|
ref="annotationsFormRef"
|
|
|
|
:value="formState.metadata.annotations"
|
|
|
|
kind="User"
|
|
|
|
group=""
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-06-27 08:16:49 +00:00
|
|
|
<template #footer>
|
2022-07-27 02:44:50 +00:00
|
|
|
<VSpace>
|
2022-09-26 07:48:55 +00:00
|
|
|
<SubmitButton
|
2024-05-23 06:32:49 +00:00
|
|
|
:loading="isSubmitting"
|
2022-07-27 02:44:50 +00:00
|
|
|
type="secondary"
|
2023-03-23 08:54:33 +00:00
|
|
|
:text="$t('core.common.buttons.submit')"
|
2022-09-26 07:48:55 +00:00
|
|
|
@submit="$formkit.submit('user-form')"
|
2022-07-27 02:44:50 +00:00
|
|
|
>
|
2022-09-26 07:48:55 +00:00
|
|
|
</SubmitButton>
|
2024-05-23 06:32:49 +00:00
|
|
|
<VButton @click="modal?.close()">
|
2023-03-23 08:54:33 +00:00
|
|
|
{{ $t("core.common.buttons.cancel_and_shortcut") }}
|
|
|
|
</VButton>
|
2022-07-27 02:44:50 +00:00
|
|
|
</VSpace>
|
2022-06-27 08:16:49 +00:00
|
|
|
</template>
|
|
|
|
</VModal>
|
|
|
|
</template>
|