104 lines
3.7 KiB
TypeScript
104 lines
3.7 KiB
TypeScript
import { Check, Copy, KeyRound, Loader2 } from 'lucide-react'
|
|
import { type FormEvent, useState } from 'react'
|
|
import { Button, Input, Label } from '../../components/ui'
|
|
import { formatPairingCode } from './copy'
|
|
import { useRequestWhatsappPairingCode } from './useRequestWhatsappPairingCode'
|
|
import { Trans, useTranslation } from 'react-i18next'
|
|
|
|
/**
|
|
* Vinculación con el número de teléfono: el profesor escribe su número,
|
|
* WhatsApp le avisa en ese teléfono y ahí ingresa el código que mostramos. Es
|
|
* la forma de vincular desde el mismo teléfono en el que usa Gruperly.
|
|
*/
|
|
export function WhatsappPairingCodePanel() {
|
|
const { requestCode, pairingCode, isRequesting, error, reset } = useRequestWhatsappPairingCode()
|
|
const [phoneNumber, setPhoneNumber] = useState('')
|
|
const { t } = useTranslation('whatsapp')
|
|
const [copied, setCopied] = useState(false)
|
|
|
|
const handleSubmit = (event: FormEvent) => {
|
|
event.preventDefault()
|
|
if (phoneNumber.trim()) requestCode(phoneNumber)
|
|
}
|
|
|
|
const handleCopy = async (code: string) => {
|
|
try {
|
|
await navigator.clipboard.writeText(code)
|
|
setCopied(true)
|
|
setTimeout(() => setCopied(false), 2000)
|
|
} catch {
|
|
// Sin permiso de portapapeles: el código igual está a la vista.
|
|
}
|
|
}
|
|
|
|
if (pairingCode) {
|
|
return (
|
|
<div className="space-y-4">
|
|
<div className="flex items-center justify-center gap-2 rounded-xl border border-border bg-primary-soft/50 px-4 py-5">
|
|
<p className="select-all font-mono text-3xl font-bold tracking-widest text-primary">
|
|
{formatPairingCode(pairingCode)}
|
|
</p>
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
aria-label={t('pairing.copy')}
|
|
onClick={() => void handleCopy(pairingCode)}
|
|
>
|
|
{copied ? <Check className="size-4 text-success" /> : <Copy className="size-4" />}
|
|
</Button>
|
|
</div>
|
|
<ol className="space-y-1.5 text-sm text-foreground/70">
|
|
<li>1. {t('pairing.step1')}</li>
|
|
<li>
|
|
2. <Trans t={t} i18nKey="pairing.step2" components={{ strong: <span className="font-medium text-primary" /> }} />
|
|
</li>
|
|
<li>3. {t('pairing.step3')}</li>
|
|
</ol>
|
|
<div className="flex items-center justify-between gap-3">
|
|
<p className="flex items-center gap-2 text-xs text-foreground/50">
|
|
<Loader2 className="size-3.5 animate-spin" />
|
|
{t('pairing.waiting')}
|
|
</p>
|
|
<Button variant="ghost" size="sm" onClick={reset}>
|
|
{t('pairing.otherNumber')}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<form className="space-y-3" onSubmit={handleSubmit}>
|
|
<div>
|
|
<Label htmlFor="whatsapp-pairing-phone">{t('pairing.phone')}</Label>
|
|
<Input
|
|
id="whatsapp-pairing-phone"
|
|
type="tel"
|
|
inputMode="tel"
|
|
autoComplete="tel"
|
|
autoFocus
|
|
placeholder={t('pairing.phonePlaceholder')}
|
|
value={phoneNumber}
|
|
invalid={Boolean(error)}
|
|
onChange={(event) => setPhoneNumber(event.target.value)}
|
|
/>
|
|
<p className="mt-1 text-xs text-foreground/50">
|
|
{t('pairing.phoneHint')}
|
|
</p>
|
|
</div>
|
|
{error ? (
|
|
<p className="rounded-xl bg-danger-soft px-3 py-2 text-sm text-danger">{error}</p>
|
|
) : null}
|
|
<Button
|
|
type="submit"
|
|
variant="primary"
|
|
className="w-full"
|
|
disabled={isRequesting || !phoneNumber.trim()}
|
|
>
|
|
{isRequesting ? <Loader2 className="size-4 animate-spin" /> : <KeyRound className="size-4" />}
|
|
{isRequesting ? t('pairing.requesting') : t('pairing.request')}
|
|
</Button>
|
|
</form>
|
|
)
|
|
}
|