2026-04-30 12:55:39 +03:30
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
import { Suspense } from 'react';
|
2026-06-20 14:51:43 +03:30
|
|
|
import { useTranslations } from 'next-intl';
|
2026-06-20 12:37:38 +03:30
|
|
|
import { Link, useRouter } from '@/i18n/navigation';
|
|
|
|
|
import { useSearchParams } from 'next/navigation';
|
2026-05-18 12:31:21 +03:30
|
|
|
import { Button } from '@/components/ui/shared/Button';
|
|
|
|
|
import { Input } from '@/components/ui/shared/Input';
|
2026-04-30 12:55:39 +03:30
|
|
|
import { staffApi } from '@/lib/api/staff';
|
|
|
|
|
|
|
|
|
|
function AcceptInviteContent() {
|
2026-06-20 14:51:43 +03:30
|
|
|
const t = useTranslations('auth');
|
2026-04-30 12:55:39 +03:30
|
|
|
const params = useSearchParams();
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
const token = useMemo(() => params.get('token') || '', [params]);
|
|
|
|
|
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
const [submitting, setSubmitting] = useState(false);
|
|
|
|
|
const [error, setError] = useState('');
|
|
|
|
|
const [success, setSuccess] = useState('');
|
|
|
|
|
const [inviteInfo, setInviteInfo] = useState<{
|
|
|
|
|
email: string;
|
|
|
|
|
name: string;
|
|
|
|
|
organizationName: string;
|
|
|
|
|
expiresAt: string;
|
|
|
|
|
status: 'PENDING' | 'ACCEPTED';
|
|
|
|
|
} | null>(null);
|
|
|
|
|
|
|
|
|
|
const [name, setName] = useState('');
|
|
|
|
|
const [password, setPassword] = useState('');
|
|
|
|
|
const [confirmPassword, setConfirmPassword] = useState('');
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!token) {
|
|
|
|
|
setLoading(false);
|
2026-06-20 14:51:43 +03:30
|
|
|
setError(t('invalidInvitationLink'));
|
2026-04-30 12:55:39 +03:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void (async () => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setError('');
|
|
|
|
|
try {
|
|
|
|
|
const res = await staffApi.previewInvite(token);
|
|
|
|
|
setInviteInfo(res.data);
|
|
|
|
|
setName(res.data.name || '');
|
|
|
|
|
if (res.data.status === 'ACCEPTED') {
|
2026-06-20 14:51:43 +03:30
|
|
|
setSuccess(t('invitationAlreadyAccepted'));
|
2026-04-30 12:55:39 +03:30
|
|
|
}
|
2026-06-20 14:51:43 +03:30
|
|
|
} catch (e: unknown) {
|
|
|
|
|
const message = e && typeof e === 'object' && 'message' in e ? String(e.message) : '';
|
|
|
|
|
setError(message || t('errorLoadInvitation'));
|
2026-04-30 12:55:39 +03:30
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
})();
|
2026-06-20 14:51:43 +03:30
|
|
|
}, [token, t]);
|
2026-04-30 12:55:39 +03:30
|
|
|
|
|
|
|
|
async function onAccept() {
|
|
|
|
|
if (!token) return;
|
|
|
|
|
setError('');
|
|
|
|
|
setSuccess('');
|
|
|
|
|
if (!name.trim()) {
|
2026-06-20 14:51:43 +03:30
|
|
|
setError(t('nameRequired'));
|
2026-04-30 12:55:39 +03:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (password.length < 8) {
|
2026-06-20 14:51:43 +03:30
|
|
|
setError(t('passwordMinLength8'));
|
2026-04-30 12:55:39 +03:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (password !== confirmPassword) {
|
2026-06-20 14:51:43 +03:30
|
|
|
setError(t('passwordsDoNotMatch'));
|
2026-04-30 12:55:39 +03:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setSubmitting(true);
|
|
|
|
|
try {
|
|
|
|
|
await staffApi.acceptInvite({
|
|
|
|
|
token,
|
|
|
|
|
name: name.trim(),
|
|
|
|
|
password,
|
|
|
|
|
});
|
2026-06-20 14:51:43 +03:30
|
|
|
setSuccess(t('invitationAcceptedRedirect'));
|
2026-04-30 12:55:39 +03:30
|
|
|
setTimeout(() => {
|
|
|
|
|
router.replace('/login');
|
|
|
|
|
}, 1000);
|
2026-06-20 14:51:43 +03:30
|
|
|
} catch (e: unknown) {
|
|
|
|
|
const message = e && typeof e === 'object' && 'message' in e ? String(e.message) : '';
|
|
|
|
|
setError(message || t('errorAcceptInvitation'));
|
2026-04-30 12:55:39 +03:30
|
|
|
} finally {
|
|
|
|
|
setSubmitting(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="min-h-screen app-web-bg flex items-center justify-center p-4">
|
|
|
|
|
<div className="w-full max-w-md surface-card p-6 space-y-5">
|
2026-06-20 14:51:43 +03:30
|
|
|
<h1 className="text-xl font-semibold text-text-primary">{t('acceptInviteTitle')}</h1>
|
2026-04-30 12:55:39 +03:30
|
|
|
|
|
|
|
|
{loading ? (
|
2026-06-20 14:51:43 +03:30
|
|
|
<p className="text-sm text-text-secondary">{t('loadingInvitation')}</p>
|
2026-04-30 12:55:39 +03:30
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{inviteInfo && (
|
|
|
|
|
<div className="rounded-[var(--radius-md)] border border-border/70 bg-background-secondary/70 px-3 py-2 text-sm text-text-secondary space-y-1">
|
|
|
|
|
<p>
|
2026-06-20 14:51:43 +03:30
|
|
|
{t('organizationLabel')}{' '}
|
|
|
|
|
<span className="text-text-primary">{inviteInfo.organizationName}</span>
|
2026-04-30 12:55:39 +03:30
|
|
|
</p>
|
|
|
|
|
<p>
|
2026-06-20 14:51:43 +03:30
|
|
|
{t('emailLabel')}{' '}
|
|
|
|
|
<span className="text-text-primary">{inviteInfo.email}</span>
|
2026-04-30 12:55:39 +03:30
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{error && (
|
|
|
|
|
<div className="rounded-[var(--radius-md)] border border-red-500/40 bg-red-500/10 px-3 py-2 text-sm text-red-300">
|
|
|
|
|
{error}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{success && (
|
|
|
|
|
<div className="rounded-[var(--radius-md)] border border-primary/30 bg-primary-soft/40 px-3 py-2 text-sm text-text-primary">
|
|
|
|
|
{success}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{inviteInfo?.status !== 'ACCEPTED' && (
|
|
|
|
|
<div className="space-y-3">
|
2026-06-20 14:51:43 +03:30
|
|
|
<Input label={t('labelName')} value={name} onChange={(e) => setName(e.target.value)} />
|
2026-04-30 12:55:39 +03:30
|
|
|
<Input
|
2026-06-20 14:51:43 +03:30
|
|
|
label={t('labelCreatePassword')}
|
2026-04-30 12:55:39 +03:30
|
|
|
type="password"
|
|
|
|
|
value={password}
|
|
|
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
<Input
|
2026-06-20 14:51:43 +03:30
|
|
|
label={t('labelConfirmPassword')}
|
2026-04-30 12:55:39 +03:30
|
|
|
type="password"
|
|
|
|
|
value={confirmPassword}
|
|
|
|
|
onChange={(e) => setConfirmPassword(e.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
<Button type="button" fullWidth isLoading={submitting} onClick={() => void onAccept()}>
|
2026-06-20 14:51:43 +03:30
|
|
|
{t('activateAccount')}
|
2026-04-30 12:55:39 +03:30
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<p className="text-xs text-text-muted">
|
2026-06-20 14:51:43 +03:30
|
|
|
{t('alreadyHaveAccess')}{' '}
|
|
|
|
|
<Link href="/login" className="text-primary">{t('goToLogin')}</Link>
|
2026-04-30 12:55:39 +03:30
|
|
|
</p>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-20 14:51:43 +03:30
|
|
|
function AcceptInviteFallback() {
|
|
|
|
|
const t = useTranslations('auth');
|
|
|
|
|
return (
|
|
|
|
|
<div className="min-h-screen app-web-bg flex items-center justify-center">
|
|
|
|
|
<p className="text-sm text-text-secondary">{t('loadingInvitation')}</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 12:55:39 +03:30
|
|
|
export default function AcceptInvitePage() {
|
|
|
|
|
return (
|
2026-06-20 14:51:43 +03:30
|
|
|
<Suspense fallback={<AcceptInviteFallback />}>
|
2026-04-30 12:55:39 +03:30
|
|
|
<AcceptInviteContent />
|
|
|
|
|
</Suspense>
|
|
|
|
|
);
|
|
|
|
|
}
|