ぼざクリ タグ広場 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.
 
 
 
 
 
 

87 lines
3.0 KiB

  1. import React, { useEffect, useState } from 'react'
  2. import { Dialog, DialogTrigger, DialogContent, DialogTitle, DialogDescription } from './ui/dialog'
  3. import { Switch } from './ui/switch'
  4. import { Button } from './ui/button'
  5. import { Input } from '@/components/ui/input'
  6. import { toast } from '@/components/ui/use-toast'
  7. import axios from 'axios'
  8. import { Link, useNavigate, useLocation } from 'react-router-dom'
  9. import { API_BASE_URL } from '../config'
  10. import { camelizeKeys } from 'humps'
  11. type Tag = { id: number
  12. name: string
  13. category: string
  14. count?: number }
  15. type User = { id: number
  16. name: string | null
  17. inheritanceCode: string
  18. role: string }
  19. type Props = { visible: boolean
  20. onVisibleChange: (visible: boolean) => void
  21. user: User
  22. setUser: (user: User) => void }
  23. const SettingsDialogue: React.FC = ({ visible, onVisibleChange, user, setUser }: Props) => {
  24. const [inputCode, setInputCode] = useState ('')
  25. const handleShowCode = () => {
  26. if (user?.inheritanceCode)
  27. toast ({ title: 'あなたの引継ぎコード', description: user.inheritanceCode })
  28. else
  29. toast ({ title: '引継ぎコードが見つかりません.' })
  30. }
  31. const handleTransfer = async () => {
  32. try
  33. {
  34. void (axios.post (`${ API_BASE_URL }/users/verify`, { code: inputCode })
  35. .then (res => {
  36. if (res.data.valid)
  37. {
  38. localStorage.setItem ('user_code', inputCode)
  39. setUser (camelizeKeys (res.data.user))
  40. toast ({ title: '引継ぎ成功!' })
  41. }
  42. else
  43. toast ({ title: '認証失敗', description: 'そのコードは使へません.' })
  44. }))
  45. }
  46. catch (e)
  47. {
  48. toast ({ title: '通信エラー', description: 'またあとで試してね.' })
  49. }
  50. }
  51. return (
  52. <Dialog open={visible} onOpenChange={onVisibleChange}>
  53. <DialogContent className="space-y-6">
  54. <DialogTitle>ユーザ設定</DialogTitle>
  55. <DialogDescription>
  56. ユーザの設定や引継ぎコードの確認、変更ができます。
  57. </DialogDescription>
  58. <div>
  59. <p className="mb-1 font-semibold">引継ぎコード</p>
  60. <Button variant="secondary" onClick={handleShowCode}>
  61. 引継ぎコードを表示
  62. </Button>
  63. </div>
  64. <div>
  65. <p className="mb-1 font-semibold">ほかのブラウザから引継ぐ</p>
  66. <div className="flex gap-2">
  67. <Input placeholder="引継ぎコードを入力"
  68. value={inputCode}
  69. onChange={e => setInputCode (e.target.value)} />
  70. <Button onClick={handleTransfer}>引継ぐ</Button>
  71. </div>
  72. </div>
  73. </DialogContent>
  74. </Dialog>)
  75. }
  76. export default SettingsDialogue