82 行
2.5 KiB
TypeScript
82 行
2.5 KiB
TypeScript
import { useState } from 'react'
|
|
|
|
import { useDialogue } from '@/lib/dialogues/useDialogue'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogHeader,
|
|
DialogTitle } from '@/components/ui/dialog'
|
|
import { Input } from '@/components/ui/input'
|
|
import { toast } from '@/components/ui/use-toast'
|
|
import { apiPost } from '@/lib/api'
|
|
import { useUITranslation } from '@/lib/useUITranslation'
|
|
import { rememberUserCode } from '@/lib/users'
|
|
|
|
import type { FC } from 'react'
|
|
|
|
import type { User } from '@/types'
|
|
|
|
type Props = { visible: boolean
|
|
onVisibleChange: (visible: boolean) => void
|
|
setUser: (user: User) => void }
|
|
|
|
|
|
const InheritDialogue: FC<Props> = ({ visible, onVisibleChange, setUser }) => {
|
|
const dialogue = useDialogue ()
|
|
const { message, label } = useUITranslation ('settings')
|
|
|
|
const [inputCode, setInputCode] = useState ('')
|
|
|
|
const handleTransfer = async () => {
|
|
if (!(await dialogue.confirm ({
|
|
title: label ('transfer.confirm'),
|
|
description: label ('transfer.logoutWarning'),
|
|
confirmText: label ('transfer.submit'),
|
|
cancelText: label ('actions.cancel'),
|
|
closeLabel: label ('actions.close'),
|
|
variant: 'danger' })))
|
|
return
|
|
|
|
try
|
|
{
|
|
const data = await apiPost<{ valid: boolean; user: User }> (
|
|
'/users/verify', { code: inputCode })
|
|
if (data.valid)
|
|
{
|
|
rememberUserCode (data.user.inheritanceCode)
|
|
setUser (data.user)
|
|
toast ({ title: label ('transfer.success') })
|
|
onVisibleChange (false)
|
|
}
|
|
else
|
|
toast ({ title: label ('transfer.authFailed'), description: label ('transfer.invalidCode') })
|
|
}
|
|
catch
|
|
{
|
|
toast ({ title: label ('transfer.networkError'), description: label ('transfer.retryLater') })
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog open={visible} onOpenChange={onVisibleChange}>
|
|
<DialogContent className="px-6 pp-6 pt-7" closeLabel={label ('actions.close')}>
|
|
<DialogHeader className="pl-8">
|
|
<DialogTitle>{label ('account.inherit')}</DialogTitle>
|
|
<DialogDescription asChild>
|
|
<div className="flex gap-2">
|
|
<Input placeholder={message ('transfer.placeholder').text}
|
|
aria-label={message ('transfer.placeholder').text}
|
|
lang={message ('transfer.placeholder').language}
|
|
value={inputCode}
|
|
onChange={ev => setInputCode (ev.target.value)}/>
|
|
<Button onClick={handleTransfer}>{label ('transfer.submit')}</Button>
|
|
</div>
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
</DialogContent>
|
|
</Dialog>)
|
|
}
|
|
|
|
export default InheritDialogue
|