Files
classroomio/apps/dashboard/src/lib/components/Org/VerifyEmail/VerifyEmailModal.svelte
T

88 lines
2.6 KiB
Svelte
Raw Normal View History

2024-03-22 06:30:24 +01:00
<script>
import Modal from '$lib/components/Modal/index.svelte';
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
import { snackbar } from '$lib/components/Snackbar/store';
import { t } from '$lib/utils/functions/translations';
2025-11-01 15:49:29 +01:00
import { NOTIFICATION_NAME, triggerSendEmail } from '$lib/utils/services/notification/notification';
2024-03-22 06:30:24 +01:00
import { currentOrg } from '$lib/utils/store/org';
import { profile } from '$lib/utils/store/user';
2024-03-22 06:30:24 +01:00
const WAIT_SEC = 120;
const WAIT_TIME = WAIT_SEC * 1000;
2025-11-01 15:49:29 +01:00
let loading = $state(false);
let isSent = $state(false);
let autoTriggered = $state(false);
2024-03-22 06:30:24 +01:00
let interval;
2025-11-01 15:49:29 +01:00
let countDown = $state(WAIT_SEC);
const open = $derived(Boolean(!$profile.is_email_verified && !!$profile.id && !!$currentOrg.id));
2024-03-22 06:30:24 +01:00
2024-11-06 21:28:55 +01:00
const sendVerificationCode = async () => {
2024-03-22 06:30:24 +01:00
loading = true;
try {
triggerSendEmail(NOTIFICATION_NAME.VERIFY_EMAIL, {
to: $profile.email,
profileId: $profile.id,
2024-05-09 09:15:08 +01:00
fullname: $profile.fullname,
2024-03-22 06:30:24 +01:00
orgSiteName: $currentOrg.siteName
});
isSent = true;
} catch (error) {
2024-04-19 05:45:48 +01:00
snackbar.error('verify_email_modal.snackbar.error');
2024-03-22 06:30:24 +01:00
} finally {
loading = false;
}
interval = setInterval(() => {
countDown = countDown - 1;
}, 1000);
setTimeout(() => {
isSent = false;
clearInterval(interval);
}, WAIT_TIME);
};
2025-11-01 15:49:29 +01:00
$effect(() => {
if (!open || isSent || loading || autoTriggered) return;
// Only send verification code on load once
autoTriggered = true;
sendVerificationCode();
});
2024-03-22 06:30:24 +01:00
</script>
<Modal {open} isCloseable={false} width="w-4/5" maxWidth="w-[500px]" containerClass="p-4">
2024-03-22 06:30:24 +01:00
<div class="flex flex-col items-center space-y-6 text-center">
<img src="/verify-email.svg" alt="email verification" />
<p class="text-xl">{$t('verify_email_modal.heading')}</p>
<p class="w-[70%] text-sm text-gray-700 dark:text-gray-200">
2024-04-19 05:45:48 +01:00
{$t('verify_email_modal.sent_verification')}
{$profile.email}
{$t('verify_email_modal.to_confirm')}
2024-03-22 06:30:24 +01:00
</p>
<div class="flex flex-col items-center">
2025-11-01 15:49:29 +01:00
<PrimaryButton isDisabled={loading || isSent} className="font-normal" onClick={sendVerificationCode}>
2024-03-22 06:30:24 +01:00
{#if loading}
2024-04-19 05:45:48 +01:00
{$t('verify_email_modal.loading')}
2024-03-22 06:30:24 +01:00
{:else}
2024-04-19 05:45:48 +01:00
{$t('verify_email_modal.resend')}
2024-03-22 06:30:24 +01:00
{/if}
</PrimaryButton>
{#if isSent}
2024-04-19 05:45:48 +01:00
<p class="text-xs text-gray-700">
{$t('verify_email_modal.resend_in')}
{countDown}
{$t('verify_email_modal.seconds')}
</p>
2024-03-22 06:30:24 +01:00
{/if}
</div>
</div>
</Modal>