installForm.tsx 6.4 KB

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