Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

InitPasswordPopup.tsx 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use client'
  2. import { useEffect, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useRouter } from 'next/navigation'
  5. import Toast from '../components/base/toast'
  6. import Loading from '../components/base/loading'
  7. import Button from '@/app/components/base/button'
  8. import { basePath } from '@/utils/var'
  9. import { fetchInitValidateStatus, initValidate } from '@/service/common'
  10. import type { InitValidateStatusResponse } from '@/models/common'
  11. import useDocumentTitle from '@/hooks/use-document-title'
  12. const InitPasswordPopup = () => {
  13. useDocumentTitle('')
  14. const [password, setPassword] = useState('')
  15. const [loading, setLoading] = useState(true)
  16. const [validated, setValidated] = useState(false)
  17. const router = useRouter()
  18. const { t } = useTranslation()
  19. const handleValidation = async () => {
  20. setLoading(true)
  21. try {
  22. const response = await initValidate({ body: { password } })
  23. if (response.result === 'success') {
  24. setValidated(true)
  25. router.push('/install') // or render setup form
  26. }
  27. else {
  28. throw new Error('Validation failed')
  29. }
  30. }
  31. catch (e: any) {
  32. Toast.notify({
  33. type: 'error',
  34. message: e.message,
  35. duration: 5000,
  36. })
  37. setLoading(false)
  38. }
  39. }
  40. useEffect(() => {
  41. fetchInitValidateStatus().then((res: InitValidateStatusResponse) => {
  42. if (res.status === 'finished')
  43. window.location.href = `${basePath}/install`
  44. else
  45. setLoading(false)
  46. })
  47. }, [])
  48. return (
  49. loading
  50. ? <Loading />
  51. : <div>
  52. {!validated && (
  53. <div className="mx-12 block min-w-28">
  54. <div className="mb-4">
  55. <label htmlFor="password" className="block text-sm font-medium text-text-secondary">
  56. {t('login.adminInitPassword')}
  57. </label>
  58. <div className="relative mt-1 rounded-md shadow-sm">
  59. <input
  60. id="password"
  61. type="password"
  62. value={password}
  63. onChange={e => setPassword(e.target.value)}
  64. className="block w-full appearance-none rounded-md border border-divider-regular px-3 py-2 shadow-sm placeholder:text-text-quaternary focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm"
  65. />
  66. </div>
  67. </div>
  68. <div className="flex flex-row flex-wrap justify-stretch p-0">
  69. <Button variant="primary" onClick={handleValidation} className="min-w-28 basis-full">
  70. {t('login.validate')}
  71. </Button>
  72. </div>
  73. </div>
  74. )}
  75. </div>
  76. )
  77. }
  78. export default InitPasswordPopup