2023-11-27 14:20:09 +00:00
|
|
|
<script lang="ts" setup>
|
2024-05-27 08:56:57 +00:00
|
|
|
import { useUserStore } from "@/stores/user";
|
2024-04-26 10:06:32 +00:00
|
|
|
import type { VerifyCodeRequest } from "@halo-dev/api-client";
|
2024-06-26 10:42:50 +00:00
|
|
|
import { consoleApiClient } from "@halo-dev/api-client";
|
2024-05-27 08:56:57 +00:00
|
|
|
import { Toast, VButton, VModal, VSpace } from "@halo-dev/components";
|
|
|
|
import { useMutation, useQueryClient } from "@tanstack/vue-query";
|
2023-11-27 14:20:09 +00:00
|
|
|
import { useIntervalFn } from "@vueuse/shared";
|
2024-05-27 08:56:57 +00:00
|
|
|
import { computed, ref } from "vue";
|
2023-11-30 10:56:10 +00:00
|
|
|
import { useI18n } from "vue-i18n";
|
2023-11-27 14:20:09 +00:00
|
|
|
|
|
|
|
const queryClient = useQueryClient();
|
2023-11-30 10:56:10 +00:00
|
|
|
const { t } = useI18n();
|
2023-11-27 14:20:09 +00:00
|
|
|
|
|
|
|
const { currentUser, fetchCurrentUser } = useUserStore();
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
(event: "close"): void;
|
|
|
|
}>();
|
|
|
|
|
2024-05-27 08:56:57 +00:00
|
|
|
const modal = ref<InstanceType<typeof VModal> | null>(null);
|
2023-11-27 14:20:09 +00:00
|
|
|
|
|
|
|
// count down
|
|
|
|
const timer = ref(0);
|
|
|
|
const { pause, resume, isActive } = useIntervalFn(
|
|
|
|
() => {
|
|
|
|
if (timer.value <= 0) {
|
|
|
|
pause();
|
|
|
|
} else {
|
|
|
|
timer.value--;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
1000,
|
|
|
|
{
|
|
|
|
immediate: false,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const email = ref(currentUser?.spec.email);
|
|
|
|
|
|
|
|
const { mutate: sendVerifyCode, isLoading: isSending } = useMutation({
|
|
|
|
mutationKey: ["send-verify-code"],
|
|
|
|
mutationFn: async () => {
|
|
|
|
if (!email.value) {
|
2023-11-30 10:56:10 +00:00
|
|
|
Toast.error(
|
|
|
|
t(
|
|
|
|
"core.uc_profile.email_verify_modal.operations.send_code.toast_email_empty"
|
|
|
|
)
|
|
|
|
);
|
2023-11-27 14:20:09 +00:00
|
|
|
throw new Error("email is empty");
|
|
|
|
}
|
2024-06-25 04:31:44 +00:00
|
|
|
return await consoleApiClient.user.sendEmailVerificationCode({
|
2023-11-27 14:20:09 +00:00
|
|
|
emailVerifyRequest: {
|
|
|
|
email: email.value,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onSuccess() {
|
2023-11-30 10:56:10 +00:00
|
|
|
Toast.success(
|
|
|
|
t("core.uc_profile.email_verify_modal.operations.send_code.toast_success")
|
|
|
|
);
|
2023-11-27 14:20:09 +00:00
|
|
|
timer.value = 60;
|
|
|
|
resume();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const sendVerifyCodeButtonText = computed(() => {
|
|
|
|
if (isSending.value) {
|
2023-11-30 10:56:10 +00:00
|
|
|
return t(
|
|
|
|
"core.uc_profile.email_verify_modal.operations.send_code.buttons.sending"
|
|
|
|
);
|
2023-11-27 14:20:09 +00:00
|
|
|
}
|
2023-11-30 10:56:10 +00:00
|
|
|
return isActive.value
|
|
|
|
? t(
|
|
|
|
"core.uc_profile.email_verify_modal.operations.send_code.buttons.countdown",
|
|
|
|
{ timer: timer.value }
|
|
|
|
)
|
|
|
|
: t("core.uc_profile.email_verify_modal.operations.send_code.buttons.send");
|
2023-11-27 14:20:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const { mutate: verifyEmail, isLoading: isVerifying } = useMutation({
|
|
|
|
mutationKey: ["verify-email"],
|
2024-04-26 10:06:32 +00:00
|
|
|
mutationFn: async ({ password, code }: VerifyCodeRequest) => {
|
2024-06-25 04:31:44 +00:00
|
|
|
return await consoleApiClient.user.verifyEmail({
|
2023-11-27 14:20:09 +00:00
|
|
|
verifyCodeRequest: {
|
2024-04-26 10:06:32 +00:00
|
|
|
password,
|
|
|
|
code,
|
2023-11-27 14:20:09 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onSuccess() {
|
2023-11-30 10:56:10 +00:00
|
|
|
Toast.success(
|
|
|
|
t("core.uc_profile.email_verify_modal.operations.verify.toast_success")
|
|
|
|
);
|
2023-11-27 14:20:09 +00:00
|
|
|
queryClient.invalidateQueries({ queryKey: ["user-detail"] });
|
|
|
|
fetchCurrentUser();
|
2024-05-27 08:56:57 +00:00
|
|
|
modal.value?.close();
|
2023-11-27 14:20:09 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-04-26 10:06:32 +00:00
|
|
|
function handleVerify({ password, code }: VerifyCodeRequest) {
|
|
|
|
verifyEmail({ password, code });
|
2023-11-27 14:20:09 +00:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<VModal
|
2024-01-10 05:51:05 +00:00
|
|
|
ref="modal"
|
2023-11-30 10:56:10 +00:00
|
|
|
:title="
|
|
|
|
currentUser?.spec.emailVerified
|
|
|
|
? $t('core.uc_profile.email_verify_modal.titles.modify')
|
|
|
|
: $t('core.uc_profile.email_verify_modal.titles.verify')
|
|
|
|
"
|
2024-01-10 05:51:05 +00:00
|
|
|
@close="emit('close')"
|
2023-11-27 14:20:09 +00:00
|
|
|
>
|
|
|
|
<FormKit
|
|
|
|
id="email-verify-form"
|
|
|
|
type="form"
|
|
|
|
name="email-verify-form"
|
|
|
|
@submit="handleVerify"
|
|
|
|
>
|
|
|
|
<FormKit
|
|
|
|
v-model="email"
|
|
|
|
type="email"
|
2023-11-30 10:56:10 +00:00
|
|
|
:label="
|
|
|
|
currentUser?.spec.emailVerified
|
|
|
|
? $t('core.uc_profile.email_verify_modal.fields.new_email.label')
|
|
|
|
: $t('core.uc_profile.email_verify_modal.fields.email.label')
|
|
|
|
"
|
2023-11-27 14:20:09 +00:00
|
|
|
name="email"
|
|
|
|
validation="required|email"
|
|
|
|
></FormKit>
|
2023-11-30 10:56:10 +00:00
|
|
|
<FormKit
|
|
|
|
type="number"
|
|
|
|
name="code"
|
|
|
|
:label="$t('core.uc_profile.email_verify_modal.fields.code.label')"
|
|
|
|
validation="required"
|
|
|
|
>
|
2023-11-27 14:20:09 +00:00
|
|
|
<template #suffix>
|
|
|
|
<VButton
|
|
|
|
:loading="isSending"
|
|
|
|
:disabled="isActive"
|
|
|
|
class="rounded-none border-y-0 border-l border-r-0 tabular-nums"
|
|
|
|
@click="sendVerifyCode"
|
|
|
|
>
|
|
|
|
{{ sendVerifyCodeButtonText }}
|
|
|
|
</VButton>
|
|
|
|
</template>
|
|
|
|
</FormKit>
|
2024-04-26 10:06:32 +00:00
|
|
|
<FormKit
|
|
|
|
:label="$t('core.uc_profile.email_verify_modal.fields.password.label')"
|
|
|
|
:help="$t('core.uc_profile.email_verify_modal.fields.password.help')"
|
|
|
|
name="password"
|
|
|
|
type="password"
|
|
|
|
validation="required:trim"
|
|
|
|
></FormKit>
|
2023-11-27 14:20:09 +00:00
|
|
|
</FormKit>
|
|
|
|
<template #footer>
|
|
|
|
<VSpace>
|
|
|
|
<VButton
|
|
|
|
:loading="isVerifying"
|
|
|
|
type="secondary"
|
|
|
|
@click="$formkit.submit('email-verify-form')"
|
|
|
|
>
|
2023-11-30 10:56:10 +00:00
|
|
|
{{ $t("core.common.buttons.verify") }}
|
|
|
|
</VButton>
|
2024-05-27 08:56:57 +00:00
|
|
|
<VButton @click="modal?.close()">
|
2023-11-30 10:56:10 +00:00
|
|
|
{{ $t("core.common.buttons.close_and_shortcut") }}
|
2023-11-27 14:20:09 +00:00
|
|
|
</VButton>
|
|
|
|
</VSpace>
|
|
|
|
</template>
|
|
|
|
</VModal>
|
|
|
|
</template>
|