ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 lines
1.8 KiB

  1. import axios from 'axios'
  2. import toCamel from 'camelcase-keys'
  3. import { useState } from 'react'
  4. import { Button } from '@/components/ui/button'
  5. import { Dialog,
  6. DialogContent,
  7. DialogTitle } from '@/components/ui/dialog'
  8. import { Input } from '@/components/ui/input'
  9. import { toast } from '@/components/ui/use-toast'
  10. import { API_BASE_URL } from '@/config'
  11. import type { User } from '@/types'
  12. type Props = { visible: boolean
  13. onVisibleChange: (visible: boolean) => void
  14. setUser: (user: User) => void }
  15. export default ({ visible, onVisibleChange, setUser }: Props) => {
  16. const [inputCode, setInputCode] = useState ('')
  17. const handleTransfer = async () => {
  18. if (!(confirm ('引継ぎを行ってもよろしいですか?\n現在のアカウントからはログアウトされます.')))
  19. return
  20. try
  21. {
  22. const res = await axios.post (`${ API_BASE_URL }/users/verify`, { code: inputCode })
  23. const data = res.data as { valid: boolean; user: any }
  24. if (data.valid)
  25. {
  26. localStorage.setItem ('user_code', inputCode)
  27. setUser (toCamel (data.user, { deep: true }))
  28. toast ({ title: '引継ぎ成功!' })
  29. onVisibleChange (false)
  30. }
  31. else
  32. toast ({ title: '認証失敗', description: 'そのコードは使へません.' })
  33. }
  34. catch
  35. {
  36. toast ({ title: '通信エラー', description: 'またあとで試してね.' })
  37. }
  38. }
  39. return (
  40. <Dialog open={visible} onOpenChange={onVisibleChange}>
  41. <DialogContent>
  42. <DialogTitle>ほかのブラウザから引継ぐ</DialogTitle>
  43. <div className="flex gap-2">
  44. <Input placeholder="引継ぎコードを入力"
  45. value={inputCode}
  46. onChange={ev => setInputCode (ev.target.value)}/>
  47. <Button onClick={handleTransfer}>引継ぐ</Button>
  48. </div>
  49. </DialogContent>
  50. </Dialog>)
  51. }