|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import Link from 'next/link'; |
| 4 | +import { useSearchParams } from 'next/navigation'; |
| 5 | +import { Suspense, useState } from 'react'; |
| 6 | +import { Button } from '@/components/ui/button'; |
| 7 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; |
| 8 | +import { Input } from '@/components/ui/input'; |
| 9 | +import { useResetPasswordForm } from '@/features/public/auth/reset-password/hooks'; |
| 10 | + |
| 11 | +export default function ResetPasswordPage() { |
| 12 | + return ( |
| 13 | + <Suspense fallback={null}> |
| 14 | + <ResetPasswordPageContent /> |
| 15 | + </Suspense> |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +function ResetPasswordPageContent() { |
| 20 | + const searchParams = useSearchParams(); |
| 21 | + const token = searchParams.get('token'); |
| 22 | + const linkError = searchParams.get('error'); |
| 23 | + const [completed, setCompleted] = useState(false); |
| 24 | + const { form, error, validators } = useResetPasswordForm(token, () => { |
| 25 | + setCompleted(true); |
| 26 | + }); |
| 27 | + |
| 28 | + if (completed) { |
| 29 | + return ( |
| 30 | + <div className='container mx-auto px-4 py-16 flex justify-center'> |
| 31 | + <Card className='w-full max-w-md'> |
| 32 | + <CardHeader> |
| 33 | + <CardTitle>パスワードを再設定しました</CardTitle> |
| 34 | + <CardDescription>新しいパスワードでログインできます</CardDescription> |
| 35 | + </CardHeader> |
| 36 | + <CardContent> |
| 37 | + <Button className='w-full' render={<Link href='/auth/login' />}> |
| 38 | + ログインへ |
| 39 | + </Button> |
| 40 | + </CardContent> |
| 41 | + </Card> |
| 42 | + </div> |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + return ( |
| 47 | + <div className='container mx-auto px-4 py-16 flex justify-center'> |
| 48 | + <Card className='w-full max-w-md'> |
| 49 | + <CardHeader> |
| 50 | + <CardTitle>新しいパスワード</CardTitle> |
| 51 | + <CardDescription>アカウントに設定する新しいパスワードを入力してください</CardDescription> |
| 52 | + </CardHeader> |
| 53 | + <CardContent> |
| 54 | + <form |
| 55 | + onSubmit={(e) => { |
| 56 | + e.preventDefault(); |
| 57 | + form.handleSubmit(); |
| 58 | + }} |
| 59 | + className='space-y-4' |
| 60 | + > |
| 61 | + <form.Field name='password' validators={{ onChange: validators.password }}> |
| 62 | + {(field) => ( |
| 63 | + <div className='space-y-1'> |
| 64 | + <label htmlFor={field.name} className='text-sm font-medium'> |
| 65 | + パスワード |
| 66 | + </label> |
| 67 | + <Input |
| 68 | + id={field.name} |
| 69 | + type='password' |
| 70 | + value={field.state.value} |
| 71 | + onBlur={field.handleBlur} |
| 72 | + onChange={(e) => field.handleChange(e.target.value)} |
| 73 | + /> |
| 74 | + {field.state.meta.errors[0] && ( |
| 75 | + <p className='text-sm text-destructive'>{field.state.meta.errors[0].message}</p> |
| 76 | + )} |
| 77 | + </div> |
| 78 | + )} |
| 79 | + </form.Field> |
| 80 | + <form.Field |
| 81 | + name='confirmPassword' |
| 82 | + validators={{ |
| 83 | + onChangeListenTo: ['password'], |
| 84 | + onChange: ({ value, fieldApi }) => { |
| 85 | + return validators.confirmPassword({ |
| 86 | + value, |
| 87 | + password: fieldApi.form.getFieldValue('password'), |
| 88 | + }); |
| 89 | + }, |
| 90 | + }} |
| 91 | + > |
| 92 | + {(field) => ( |
| 93 | + <div className='space-y-1'> |
| 94 | + <label htmlFor={field.name} className='text-sm font-medium'> |
| 95 | + パスワード(確認) |
| 96 | + </label> |
| 97 | + <Input |
| 98 | + id={field.name} |
| 99 | + type='password' |
| 100 | + value={field.state.value} |
| 101 | + onBlur={field.handleBlur} |
| 102 | + onChange={(e) => field.handleChange(e.target.value)} |
| 103 | + /> |
| 104 | + {field.state.meta.errors[0] && ( |
| 105 | + <p className='text-sm text-destructive'>{field.state.meta.errors[0].message}</p> |
| 106 | + )} |
| 107 | + </div> |
| 108 | + )} |
| 109 | + </form.Field> |
| 110 | + {linkError && <p className='text-sm text-destructive'>再設定リンクが無効です</p>} |
| 111 | + {error && <p className='text-sm text-destructive'>{error}</p>} |
| 112 | + <Button type='submit' className='w-full' disabled={form.state.isSubmitting || !token}> |
| 113 | + {form.state.isSubmitting ? '再設定中...' : 'パスワードを再設定'} |
| 114 | + </Button> |
| 115 | + </form> |
| 116 | + </CardContent> |
| 117 | + </Card> |
| 118 | + </div> |
| 119 | + ); |
| 120 | +} |
0 commit comments