You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import KFModal from '@/components/KFModal';
  2. import { type CodeConfigData } from '@/pages/CodeConfig/List';
  3. import { addCodeConfigReq, updateCodeConfigReq } from '@/services/codeConfig';
  4. import { to } from '@/utils/promise';
  5. import { Form, Input, Radio, message, type ModalProps } from 'antd';
  6. import { omit } from 'lodash';
  7. export enum RepoVisibleEnum {
  8. Private = 0, // 私有
  9. Public = 1, // 公开
  10. }
  11. export enum VerifyModeEnum {
  12. Password = 0, // 用户名密码
  13. SSH = 1, // SSH Key
  14. }
  15. export enum OperationTypeEnum {
  16. Create = 0, // 新建
  17. Update = 1, // 更新
  18. }
  19. type FormData = Partial<CodeConfigData>;
  20. interface AddCodeConfigModalProps extends Omit<ModalProps, 'onOk'> {
  21. opType: OperationTypeEnum;
  22. codeConfigData?: CodeConfigData;
  23. onOk: () => void;
  24. }
  25. function AddCodeConfigModal({ opType, codeConfigData, onOk, ...rest }: AddCodeConfigModalProps) {
  26. // 上传请求
  27. const createCodeConfig = async (formData: FormData) => {
  28. const params: FormData & { id?: number } = {
  29. ...formData,
  30. };
  31. // 清除多余的信息
  32. if (formData.code_repo_vis === RepoVisibleEnum.Public) {
  33. omit(params, ['verify_mode', 'git_user_name', 'git_password', 'ssh_key']);
  34. }
  35. if (formData.verify_mode === VerifyModeEnum.Password) {
  36. omit(params, ['ssh_key']);
  37. } else if (formData.verify_mode === VerifyModeEnum.SSH) {
  38. omit(params, ['git_user_name', 'git_password']);
  39. }
  40. if (opType === OperationTypeEnum.Update) {
  41. params.id = codeConfigData?.id;
  42. }
  43. const request = opType === OperationTypeEnum.Create ? addCodeConfigReq : updateCodeConfigReq;
  44. const [res] = await to(request(params));
  45. if (res) {
  46. message.success(opType === OperationTypeEnum.Create ? '创建成功' : '修改成功');
  47. onOk?.();
  48. }
  49. };
  50. // 提交
  51. const onFinish = (formData: FormData) => {
  52. createCodeConfig(formData);
  53. };
  54. // 设置初始值
  55. const initialValues: FormData = codeConfigData ?? {
  56. code_repo_vis: RepoVisibleEnum.Public,
  57. verify_mode: VerifyModeEnum.Password,
  58. };
  59. if (initialValues.verify_mode === undefined || initialValues.verify_mode === null) {
  60. initialValues.verify_mode = VerifyModeEnum.Password;
  61. }
  62. return (
  63. <KFModal
  64. {...rest}
  65. title="新建代码配置"
  66. image={require('@/assets/img/create-experiment.png')}
  67. width={825}
  68. okButtonProps={{
  69. htmlType: 'submit',
  70. form: 'form',
  71. }}
  72. destroyOnClose
  73. >
  74. <Form
  75. name="form"
  76. layout="vertical"
  77. onFinish={onFinish}
  78. initialValues={initialValues}
  79. autoComplete="off"
  80. >
  81. {/* 禁止 Chrome 自动填充 */}
  82. {/* <Input type="text" style={{ display: 'none' }} />
  83. <Input type="password" style={{ display: 'none' }} /> */}
  84. <Form.Item
  85. label="代码仓库名称"
  86. name="code_repo_name"
  87. required
  88. rules={[
  89. {
  90. required: true,
  91. message: '请输入代码仓库名称',
  92. },
  93. ]}
  94. >
  95. <Input placeholder="请输入代码仓库名称" showCount allowClear maxLength={64} />
  96. </Form.Item>
  97. <Form.Item
  98. label="代码仓库可见性"
  99. name="code_repo_vis"
  100. rules={[
  101. {
  102. required: true,
  103. message: '请选择代码仓库可见性',
  104. },
  105. ]}
  106. >
  107. <Radio.Group>
  108. <Radio value={RepoVisibleEnum.Public}>公开</Radio>
  109. <Radio value={RepoVisibleEnum.Private}>私有</Radio>
  110. </Radio.Group>
  111. </Form.Item>
  112. <Form.Item
  113. label="Git 地址"
  114. name="git_url"
  115. rules={[
  116. {
  117. required: true,
  118. message: '请输入 Git 地址',
  119. },
  120. {
  121. type: 'url',
  122. message: '请输入正确的 Git 地址',
  123. },
  124. ]}
  125. >
  126. <Input placeholder="请输入 Git 地址" showCount allowClear maxLength={256} />
  127. </Form.Item>
  128. <Form.Item
  129. label="代码分支/Tag"
  130. name="git_branch"
  131. rules={[
  132. {
  133. required: true,
  134. message: '请输入代码分支/Tag',
  135. },
  136. ]}
  137. >
  138. <Input placeholder="请输入代码分支/Tag" showCount allowClear maxLength={64} />
  139. </Form.Item>
  140. <Form.Item
  141. noStyle
  142. shouldUpdate={(prevValues, currentValues) =>
  143. prevValues?.code_repo_vis !== currentValues?.code_repo_vis
  144. }
  145. >
  146. {({ getFieldValue }) => {
  147. return getFieldValue('code_repo_vis') === RepoVisibleEnum.Private ? (
  148. <>
  149. <Form.Item
  150. label="验证方式"
  151. name="verify_mode"
  152. rules={[
  153. {
  154. required: true,
  155. message: '请选择验证方式',
  156. },
  157. ]}
  158. >
  159. <Radio.Group>
  160. <Radio value={VerifyModeEnum.Password}>用户名/密码</Radio>
  161. <Radio value={VerifyModeEnum.SSH}>SSH Key</Radio>
  162. </Radio.Group>
  163. </Form.Item>
  164. <Form.Item
  165. noStyle
  166. shouldUpdate={(prevValues, currentValues) =>
  167. prevValues?.verify_mode !== currentValues?.verify_mode
  168. }
  169. >
  170. {({ getFieldValue }) => {
  171. return getFieldValue('verify_mode') === VerifyModeEnum.Password ? (
  172. <>
  173. <Form.Item
  174. label="Git 用户名"
  175. name="git_user_name"
  176. rules={[
  177. {
  178. required: true,
  179. message: '请输入 Git 用户名',
  180. },
  181. ]}
  182. >
  183. <Input
  184. placeholder="请输入 Git 用户名"
  185. autoComplete="off"
  186. showCount
  187. allowClear
  188. maxLength={64}
  189. />
  190. </Form.Item>
  191. <Form.Item
  192. label="Git 密码"
  193. name="git_password"
  194. rules={[
  195. {
  196. required: true,
  197. message: '请输入 Git 密码',
  198. },
  199. ]}
  200. >
  201. <Input.Password
  202. autoComplete="new-password"
  203. placeholder="请输入 Git 密码"
  204. allowClear
  205. />
  206. </Form.Item>
  207. </>
  208. ) : (
  209. <Form.Item
  210. label="SSH Key"
  211. name="ssh_key"
  212. rules={[
  213. {
  214. required: true,
  215. message: '请输入 SSH Key',
  216. },
  217. ]}
  218. >
  219. <Input.TextArea
  220. placeholder="请输入 SSH Key"
  221. showCount
  222. maxLength={1024}
  223. autoSize={{ minRows: 3, maxRows: 6 }}
  224. allowClear
  225. />
  226. </Form.Item>
  227. );
  228. }}
  229. </Form.Item>
  230. </>
  231. ) : null;
  232. }}
  233. </Form.Item>
  234. </Form>
  235. </KFModal>
  236. );
  237. }
  238. export default AddCodeConfigModal;