InitPasswordPopup.tsx 2.5 KB

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