Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

InitPasswordPopup.tsx 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. const InitPasswordPopup = () => {
  12. const [password, setPassword] = useState('')
  13. const [loading, setLoading] = useState(true)
  14. const [validated, setValidated] = useState(false)
  15. const router = useRouter()
  16. const { t } = useTranslation()
  17. const handleValidation = async () => {
  18. setLoading(true)
  19. try {
  20. const response = await initValidate({ body: { password } })
  21. if (response.result === 'success') {
  22. setValidated(true)
  23. router.push('/install') // or render setup form
  24. }
  25. else {
  26. throw new Error('Validation failed')
  27. }
  28. }
  29. catch (e: any) {
  30. Toast.notify({
  31. type: 'error',
  32. message: e.message,
  33. duration: 5000,
  34. })
  35. setLoading(false)
  36. }
  37. }
  38. useEffect(() => {
  39. fetchInitValidateStatus().then((res: InitValidateStatusResponse) => {
  40. if (res.status === 'finished')
  41. window.location.href = `${basePath}/install`
  42. else
  43. setLoading(false)
  44. })
  45. }, [])
  46. return (
  47. loading
  48. ? <Loading />
  49. : <div>
  50. {!validated && (
  51. <div className="mx-12 block min-w-28">
  52. <div className="mb-4">
  53. <label htmlFor="password" className="block text-sm font-medium text-text-secondary">
  54. {t('login.adminInitPassword')}
  55. </label>
  56. <div className="relative mt-1 rounded-md shadow-sm">
  57. <input
  58. id="password"
  59. type="password"
  60. value={password}
  61. onChange={e => setPassword(e.target.value)}
  62. 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"
  63. />
  64. </div>
  65. </div>
  66. <div className="flex flex-row flex-wrap justify-stretch p-0">
  67. <Button variant="primary" onClick={handleValidation} className="min-w-28 basis-full">
  68. {t('login.validate')}
  69. </Button>
  70. </div>
  71. </div>
  72. )}
  73. </div>
  74. )
  75. }
  76. export default InitPasswordPopup