installForm.tsx 6.2 KB

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