2022-07-18 03:52:13 +00:00
|
|
|
<script lang="ts" setup>
|
2022-09-09 13:48:07 +00:00
|
|
|
import { VButton, VModal, VSpace } from "@halo-dev/components";
|
2022-09-26 07:48:55 +00:00
|
|
|
import SubmitButton from "@/components/button/SubmitButton.vue";
|
2024-05-23 06:32:49 +00:00
|
|
|
import { onMounted, ref } from "vue";
|
2022-07-18 03:52:13 +00:00
|
|
|
import type { User } from "@halo-dev/api-client";
|
2022-09-22 08:46:32 +00:00
|
|
|
import { apiClient } from "@/utils/api-client";
|
2023-12-28 09:13:38 +00:00
|
|
|
import { cloneDeep } from "lodash-es";
|
2022-09-09 13:48:07 +00:00
|
|
|
import { setFocus } from "@/formkit/utils/focus";
|
2022-07-18 03:52:13 +00:00
|
|
|
|
2022-08-15 09:56:13 +00:00
|
|
|
const props = withDefaults(
|
|
|
|
defineProps<{
|
2022-09-27 09:13:20 +00:00
|
|
|
user?: User;
|
2022-08-15 09:56:13 +00:00
|
|
|
}>(),
|
|
|
|
{
|
2022-09-27 09:13:20 +00:00
|
|
|
user: undefined,
|
2022-08-15 09:56:13 +00:00
|
|
|
}
|
|
|
|
);
|
2022-07-18 03:52:13 +00:00
|
|
|
|
2022-08-15 12:31:51 +00:00
|
|
|
const emit = defineEmits<{
|
|
|
|
(event: "close"): void;
|
|
|
|
}>();
|
|
|
|
|
2022-07-18 03:52:13 +00:00
|
|
|
interface PasswordChangeFormState {
|
2022-09-09 13:48:07 +00:00
|
|
|
password: string;
|
|
|
|
password_confirm?: string;
|
2022-07-18 03:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 13:48:07 +00:00
|
|
|
const initialFormState: PasswordChangeFormState = {
|
|
|
|
password: "",
|
|
|
|
password_confirm: "",
|
|
|
|
};
|
|
|
|
|
2024-05-23 06:32:49 +00:00
|
|
|
const modal = ref<InstanceType<typeof VModal>>();
|
2022-09-09 13:48:07 +00:00
|
|
|
const formState = ref<PasswordChangeFormState>(cloneDeep(initialFormState));
|
2024-05-23 06:32:49 +00:00
|
|
|
const isSubmitting = ref(false);
|
2022-09-09 13:48:07 +00:00
|
|
|
|
2024-05-23 06:32:49 +00:00
|
|
|
onMounted(() => {
|
|
|
|
setFocus("passwordInput");
|
|
|
|
});
|
2022-09-09 13:48:07 +00:00
|
|
|
|
2022-07-18 03:52:13 +00:00
|
|
|
const handleChangePassword = async () => {
|
|
|
|
try {
|
2024-05-23 06:32:49 +00:00
|
|
|
isSubmitting.value = true;
|
2022-07-18 03:52:13 +00:00
|
|
|
|
2022-09-09 13:48:07 +00:00
|
|
|
const changePasswordRequest = cloneDeep(formState.value);
|
2022-07-18 03:52:13 +00:00
|
|
|
delete changePasswordRequest.password_confirm;
|
|
|
|
|
2024-04-21 07:07:46 +00:00
|
|
|
await apiClient.user.changeAnyonePassword({
|
2023-11-13 08:56:08 +00:00
|
|
|
name: props.user?.metadata.name || "",
|
|
|
|
changePasswordRequest,
|
|
|
|
});
|
2022-07-18 03:52:13 +00:00
|
|
|
|
2024-05-23 06:32:49 +00:00
|
|
|
modal.value?.close();
|
2022-07-18 03:52:13 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
} finally {
|
2024-05-23 06:32:49 +00:00
|
|
|
isSubmitting.value = false;
|
2022-07-18 03:52:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<VModal
|
2024-05-23 06:32:49 +00:00
|
|
|
ref="modal"
|
2022-07-18 03:52:13 +00:00
|
|
|
:width="500"
|
2023-03-23 08:54:33 +00:00
|
|
|
:title="$t('core.user.change_password_modal.title')"
|
2024-05-23 06:32:49 +00:00
|
|
|
@close="emit('close')"
|
2022-07-18 03:52:13 +00:00
|
|
|
>
|
|
|
|
<FormKit
|
|
|
|
id="password-form"
|
2022-09-09 13:48:07 +00:00
|
|
|
v-model="formState"
|
2022-09-23 10:03:55 +00:00
|
|
|
name="password-form"
|
2022-07-18 03:52:13 +00:00
|
|
|
:actions="false"
|
|
|
|
type="form"
|
2022-09-09 13:48:07 +00:00
|
|
|
:config="{ validationVisibility: 'submit' }"
|
2022-07-18 03:52:13 +00:00
|
|
|
@submit="handleChangePassword"
|
|
|
|
>
|
|
|
|
<FormKit
|
2022-09-09 13:48:07 +00:00
|
|
|
id="passwordInput"
|
2023-03-23 08:54:33 +00:00
|
|
|
:label="$t('core.user.change_password_modal.fields.new_password.label')"
|
2022-07-18 03:52:13 +00:00
|
|
|
name="password"
|
|
|
|
type="password"
|
2023-07-19 11:48:12 +00:00
|
|
|
validation="required:trim|length:5,100|matches:/^\S.*\S$/"
|
|
|
|
:validation-messages="{
|
|
|
|
matches: $t('core.formkit.validation.trim'),
|
|
|
|
}"
|
2022-07-18 03:52:13 +00:00
|
|
|
></FormKit>
|
|
|
|
<FormKit
|
2023-03-23 08:54:33 +00:00
|
|
|
:label="
|
|
|
|
$t('core.user.change_password_modal.fields.confirm_password.label')
|
|
|
|
"
|
2022-07-18 03:52:13 +00:00
|
|
|
name="password_confirm"
|
|
|
|
type="password"
|
2023-07-19 11:48:12 +00:00
|
|
|
validation="confirm|required:trim|length:5,100|matches:/^\S.*\S$/"
|
|
|
|
:validation-messages="{
|
|
|
|
matches: $t('core.formkit.validation.trim'),
|
|
|
|
}"
|
2022-07-18 03:52:13 +00:00
|
|
|
></FormKit>
|
|
|
|
</FormKit>
|
|
|
|
<template #footer>
|
2022-09-09 13:48:07 +00:00
|
|
|
<VSpace>
|
2022-09-26 07:48:55 +00:00
|
|
|
<SubmitButton
|
2024-05-23 06:32:49 +00:00
|
|
|
:loading="isSubmitting"
|
2022-09-09 13:48:07 +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('password-form')"
|
2022-09-09 13:48:07 +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-09-09 13:48:07 +00:00
|
|
|
</VSpace>
|
2022-07-18 03:52:13 +00:00
|
|
|
</template>
|
|
|
|
</VModal>
|
|
|
|
</template>
|