This commit is contained in:
2025-07-07 02:46:57 +09:00
parent 191e5d3a76
commit ebcc535cf1
5 changed files with 105 additions and 130 deletions
@@ -1,9 +1,18 @@
import axios from 'axios'
import toCamel from 'camelcase-keys'
import { useState } from 'react'
import { Button } from '@/components/ui/button'
import { Dialog,
DialogContent,
DialogDescription,
DialogTitle,
DialogTrigger } from '@/components/ui/dialog'
import { Input } from '@/components/ui/input'
import { toast } from '@/components/ui/use-toast'
import { API_BASE_URL } from '@/config'
import type { User } from '@/types'
type Props = { visible: boolean
onVisibleChange: (visible: boolean) => void
@@ -12,10 +21,41 @@ type Props = { visible: boolean
export default ({ visible, onVisibleChange, user, setUser }: Props) => {
const [inputCode, setInputCode] = useState ('')
const handleTransfer = async () => {
if (!(confirm ('引継ぎを行ってもよろしいですか?\n現在のアカウントからはログアウトされます.')))
return
try
{
const { data } = await axios.post (`${ API_BASE_URL }/users/verify`, { code: inputCode })
if (data.valid)
{
localStorage.setItem ('user_code', inputCode)
setUser (toCamel (data.user, { deep: true }))
toast ({ title: '引継ぎ成功!' })
onVisibleChange (false)
}
else
toast ({ title: '認証失敗', description: 'そのコードは使へません.' })
}
catch
{
toast ({ title: '通信エラー', description: 'またあとで試してね.' })
}
}
return (
<Dialog open={visible} onOpenChange={onVisibleChange}>
<DialogContent className="space-y-6">
<DialogTitle></DialogTitle>
<div className="flex gap-2">
<Input placeholder="引継ぎコードを入力"
value={inputCode}
onChange={ev => setInputCode (ev.target.value)} />
<Button onClick={handleTransfer}></Button>
</div>
</DialogContent>
</Dialog>)
}
@@ -1,16 +1,38 @@
import axios from 'axios'
import { Button } from '@/components/ui/button'
import { Dialog,
DialogContent,
DialogDescription,
DialogTitle,
DialogTrigger } from '@/components/ui/dialog'
import { toast } from '@/components/ui/use-toast'
import { API_BASE_URL } from '@/config'
import type { User } from '@/types'
type Props = { visible: boolean
onVisibleChange: (visible: boolean) => void
user: User | null }
user: User | null
setUser: (user: User) => void }
export default ({ visible, onVisibleChange, user }: Props) => {
export default ({ visible, onVisibleChange, user, setUser }: Props) => {
const handleChange = async () => {
if (!(confirm ('引継ぎコードを再発行しますか?')))
return
const { data } = await axios.post (`${ API_BASE_URL }/users/code/renew`, { }, { headers: {
'Content-Type': 'multipart/form-data',
'X-Transfer-Code': localStorage.getItem ('user_code') || '' } })
if (data.code)
{
localStorage.setItem ('user_code', data.code)
setUser (user => ({ ...user, inheritanceCode: data.code }))
toast ({ title: '再発行しました.' })
}
}
return (
<Dialog open={visible} onOpenChange={onVisibleChange}>
<DialogContent className="space-y-6">
@@ -22,8 +44,9 @@ export default ({ visible, onVisibleChange, user }: Props) => {
!
</p>
<div className="my-4">
<Button className="px-4 py-2 bg-red-600 text-white rounded disabled:bg-gray-400">
<Button onClick={handleChange}
className="px-4 py-2 bg-red-600 text-white rounded disabled:bg-gray-400">
</Button>
</div>
</div>