installForm.tsx 5.7 KB

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