installForm.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. 'use client'
  2. import React, { useEffect } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import Link from 'next/link'
  5. import { useRouter } from 'next/navigation'
  6. // import { useContext } from 'use-context-selector'
  7. import Toast from '../components/base/toast'
  8. import Loading from '../components/base/loading'
  9. import Button from '@/app/components/base/button'
  10. // import I18n from '@/context/i18n'
  11. import { fetchInitValidateStatus, fetchSetupStatus, setup } from '@/service/common'
  12. import type { InitValidateStatusResponse, SetupStatusResponse } from '@/models/common'
  13. const validEmailReg = /^[\w\.-]+@([\w-]+\.)+[\w-]{2,}$/
  14. const validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/
  15. const InstallForm = () => {
  16. const { t } = useTranslation()
  17. const router = useRouter()
  18. const [email, setEmail] = React.useState('')
  19. const [name, setName] = React.useState('')
  20. const [password, setPassword] = React.useState('')
  21. const [showPassword, setShowPassword] = React.useState(false)
  22. const [loading, setLoading] = React.useState(true)
  23. const showErrorMessage = (message: string) => {
  24. Toast.notify({
  25. type: 'error',
  26. message,
  27. })
  28. }
  29. const valid = () => {
  30. if (!email) {
  31. showErrorMessage(t('login.error.emailEmpty'))
  32. return false
  33. }
  34. if (!validEmailReg.test(email)) {
  35. showErrorMessage(t('login.error.emailInValid'))
  36. return false
  37. }
  38. if (!name.trim()) {
  39. showErrorMessage(t('login.error.nameEmpty'))
  40. return false
  41. }
  42. if (!password.trim()) {
  43. showErrorMessage(t('login.error.passwordEmpty'))
  44. return false
  45. }
  46. if (!validPassword.test(password))
  47. showErrorMessage(t('login.error.passwordInvalid'))
  48. return true
  49. }
  50. const handleSetting = async () => {
  51. if (!valid())
  52. return
  53. await setup({
  54. body: {
  55. email,
  56. name,
  57. password,
  58. },
  59. })
  60. router.push('/signin')
  61. }
  62. useEffect(() => {
  63. fetchSetupStatus().then((res: SetupStatusResponse) => {
  64. if (res.step === 'finished') {
  65. window.location.href = '/signin'
  66. }
  67. else {
  68. fetchInitValidateStatus().then((res: InitValidateStatusResponse) => {
  69. if (res.status === 'not_started')
  70. window.location.href = '/init'
  71. })
  72. }
  73. setLoading(false)
  74. })
  75. }, [])
  76. return (
  77. loading
  78. ? <Loading />
  79. : <>
  80. <div className="sm:mx-auto sm:w-full sm:max-w-md">
  81. <h2 className="text-[32px] font-bold text-gray-900">{t('login.setAdminAccount')}</h2>
  82. <p className='
  83. mt-1 text-sm text-gray-600
  84. '>{t('login.setAdminAccountDesc')}</p>
  85. </div>
  86. <div className="grow mt-8 sm:mx-auto sm:w-full sm:max-w-md">
  87. <div className="bg-white ">
  88. <form onSubmit={() => { }}>
  89. <div className='mb-5'>
  90. <label htmlFor="email" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  91. {t('login.email')}
  92. </label>
  93. <div className="mt-1">
  94. <input
  95. id="email"
  96. type="email"
  97. value={email}
  98. onChange={e => setEmail(e.target.value)}
  99. placeholder={t('login.emailPlaceholder') || ''}
  100. className={'appearance-none block w-full rounded-lg pl-[14px] px-3 py-2 border border-gray-200 hover:border-gray-300 hover:shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 placeholder-gray-400 caret-primary-600 sm:text-sm'}
  101. />
  102. </div>
  103. </div>
  104. <div className='mb-5'>
  105. <label htmlFor="name" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  106. {t('login.name')}
  107. </label>
  108. <div className="mt-1 relative rounded-md shadow-sm">
  109. <input
  110. id="name"
  111. type="text"
  112. value={name}
  113. onChange={e => setName(e.target.value)}
  114. placeholder={t('login.namePlaceholder') || ''}
  115. className={'appearance-none block w-full rounded-lg pl-[14px] px-3 py-2 border border-gray-200 hover:border-gray-300 hover:shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 placeholder-gray-400 caret-primary-600 sm:text-sm pr-10'}
  116. />
  117. </div>
  118. </div>
  119. <div className='mb-5'>
  120. <label htmlFor="password" className="my-2 flex items-center justify-between text-sm font-medium text-gray-900">
  121. {t('login.password')}
  122. </label>
  123. <div className="mt-1 relative rounded-md shadow-sm">
  124. <input
  125. id="password"
  126. type={showPassword ? 'text' : 'password'}
  127. value={password}
  128. onChange={e => setPassword(e.target.value)}
  129. placeholder={t('login.passwordPlaceholder') || ''}
  130. className={'appearance-none block w-full rounded-lg pl-[14px] px-3 py-2 border border-gray-200 hover:border-gray-300 hover:shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 placeholder-gray-400 caret-primary-600 sm:text-sm pr-10'}
  131. />
  132. <div className="absolute inset-y-0 right-0 flex items-center pr-3">
  133. <button
  134. type="button"
  135. onClick={() => setShowPassword(!showPassword)}
  136. className="text-gray-400 hover:text-gray-500 focus:outline-none focus:text-gray-500"
  137. >
  138. {showPassword ? '👀' : '😝'}
  139. </button>
  140. </div>
  141. </div>
  142. <div className='mt-1 text-xs text-gray-500'>{t('login.error.passwordInvalid')}</div>
  143. </div>
  144. {/* <div className="flex items-center justify-between">
  145. <div className="text-sm">
  146. <div className="flex items-center mb-4">
  147. <input id="default-checkbox" type="checkbox" className="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 rounded" />
  148. <label htmlFor="default-checkbox" className="ml-2 text-sm font-medium cursor-pointer text-primary-600 hover:text-gray-500">{t('login.acceptPP')}</label>
  149. </div>
  150. </div>
  151. </div> */}
  152. <div>
  153. <Button type='primary' className='w-full !fone-medium !text-sm' onClick={handleSetting}>
  154. {t('login.installBtn')}
  155. </Button>
  156. </div>
  157. </form>
  158. <div className="block w-hull mt-2 text-xs text-gray-600">
  159. {t('login.license.tip')}
  160. &nbsp;
  161. <Link
  162. className='text-primary-600'
  163. target='_blank' rel='noopener noreferrer'
  164. href={'https://docs.dify.ai/user-agreement/open-source'}
  165. >{t('login.license.link')}</Link>
  166. </div>
  167. </div>
  168. </div>
  169. </>
  170. )
  171. }
  172. export default InstallForm