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

114 lines
3.1 KiB

  1. import axios from 'axios'
  2. import { useEffect, useState } from 'react'
  3. import { Helmet } from 'react-helmet-async'
  4. import Form from '@/components/common/Form'
  5. import Label from '@/components/common/Label'
  6. import PageTitle from '@/components/common/PageTitle'
  7. import MainArea from '@/components/layout/MainArea'
  8. import InheritDialogue from '@/components/users/InheritDialogue'
  9. import UserCodeDialogue from '@/components/users/UserCodeDialogue'
  10. import { Button } from '@/components/ui/button'
  11. import { toast } from '@/components/ui/use-toast'
  12. import { API_BASE_URL, SITE_TITLE } from '@/config'
  13. import type { User } from '@/types'
  14. type Props = { user: User | null
  15. setUser: React.Dispatch<React.SetStateAction<User | null>> }
  16. export default ({ user, setUser }: Props) => {
  17. const [name, setName] = useState ('')
  18. const [userCodeVsbl, setUserCodeVsbl] = useState (false)
  19. const [inheritVsbl, setInheritVsbl] = useState (false)
  20. const handleSubmit = async () => {
  21. if (!(user))
  22. return
  23. const formData = new FormData
  24. formData.append ('name', name)
  25. try
  26. {
  27. const res = await axios.put (`${ API_BASE_URL }/users/${ user.id }`, formData, {
  28. headers: { 'Content-Type': 'multipart/form-data',
  29. 'X-Transfer-Code': localStorage.getItem ('user_code') || '' } })
  30. const data = res.data as User
  31. setUser (user => ({ ...user, ...data }))
  32. toast ({ title: '設定を更新しました.' })
  33. }
  34. catch
  35. {
  36. toast ({ title: 'しっぱい……' })
  37. }
  38. }
  39. useEffect (() => {
  40. if (!user)
  41. return
  42. setName (user.name ?? '')
  43. }, [user])
  44. return (
  45. <MainArea>
  46. <Helmet>
  47. <meta name="robots" content="noindex"/>
  48. <title>設定 | {SITE_TITLE}</title>
  49. </Helmet>
  50. <Form>
  51. <PageTitle>設定</PageTitle>
  52. {user ? (
  53. <>
  54. {/* 名前 */}
  55. <div>
  56. <Label>表示名</Label>
  57. <input type="text"
  58. className="w-full border rounded p-2"
  59. value={name}
  60. placeholder="名もなきニジラー"
  61. onChange={ev => setName (ev.target.value)}/>
  62. {(user && !(user.name)) && (
  63. <p className="mt-1 text-sm text-red-500">
  64. 名前が未設定のアカウントは 30 日間アクセスしないと削除されます!!!!
  65. </p>)}
  66. </div>
  67. {/* 送信 */}
  68. <Button onClick={handleSubmit}
  69. className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400">
  70. 更新
  71. </Button>
  72. {/* 引継ぎ */}
  73. <div>
  74. <Label>引継ぎ</Label>
  75. <Button onClick={() => setUserCodeVsbl (true)}
  76. className="px-4 py-2 bg-gray-600 text-white rounded disabled:bg-gray-400"
  77. disabled={!(user)}>
  78. 引継ぎコードを表示
  79. </Button>
  80. <Button onClick={() => setInheritVsbl (true)}
  81. className="ml-2 px-4 py-2 bg-red-600 text-white rounded disabled:bg-gray-400"
  82. disabled={!(user)}>
  83. ほかのブラウザから引継ぐ
  84. </Button>
  85. </div>
  86. </>) : 'Loading...'}
  87. </Form>
  88. <UserCodeDialogue visible={userCodeVsbl}
  89. onVisibleChange={setUserCodeVsbl}
  90. user={user}
  91. setUser={setUser}/>
  92. <InheritDialogue visible={inheritVsbl}
  93. onVisibleChange={setInheritVsbl}
  94. setUser={setUser}/>
  95. </MainArea>)
  96. }