|
| 1 | +import { useMemo, useState } from "react"; |
| 2 | +import { messageInstance, CloseEyeIcon } from "lowcoder-design"; |
| 3 | +import { trans } from "i18n"; |
| 4 | +import { |
| 5 | + FormStyled, |
| 6 | + PasswordLabel |
| 7 | +} from "../styledComponents"; |
| 8 | +import { default as Form } from "antd/es/form"; |
| 9 | +import { default as Input } from "antd/es/input"; |
| 10 | +import { default as Tooltip } from "antd/es/tooltip"; |
| 11 | +import IdSourceApi, { ConfigItem } from "api/idSourceApi"; |
| 12 | +import { validateResponse } from "api/apiUtils"; |
| 13 | +import { authConfig, AuthType, clientIdandSecretConfig, ItemType } from "../idSourceConstants"; |
| 14 | +import _ from "lodash"; |
| 15 | +import Flex from "antd/es/flex"; |
| 16 | +import Button from "antd/es/button"; |
| 17 | + |
| 18 | +type GeneralOAuthFormProp = { |
| 19 | + authType: AuthType, |
| 20 | + onSave: () => void; |
| 21 | + onCancel: () => void; |
| 22 | +}; |
| 23 | + |
| 24 | +function GeneralOAuthForm(props: GeneralOAuthFormProp) { |
| 25 | + const { |
| 26 | + authType, |
| 27 | + onSave, |
| 28 | + onCancel, |
| 29 | + } = props; |
| 30 | + const [form1] = Form.useForm(); |
| 31 | + const [saveLoading, setSaveLoading] = useState(false); |
| 32 | + |
| 33 | + function saveAuthProvider(values: ConfigItem) { |
| 34 | + setSaveLoading(true); |
| 35 | + const config = { |
| 36 | + ...values, |
| 37 | + authType, |
| 38 | + enableRegister: true, |
| 39 | + } |
| 40 | + IdSourceApi.saveConfig(config) |
| 41 | + .then((resp) => { |
| 42 | + if (validateResponse(resp)) { |
| 43 | + messageInstance.success(trans("idSource.saveSuccess")); |
| 44 | + } |
| 45 | + }) |
| 46 | + .catch((e) => messageInstance.error(e.message)) |
| 47 | + .finally(() => { |
| 48 | + setSaveLoading(false); |
| 49 | + onSave(); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + const handleSave = () => { |
| 54 | + form1.validateFields().then(values => { |
| 55 | + console.log(values); |
| 56 | + saveAuthProvider(values); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + function handleCancel() { |
| 61 | + onCancel(); |
| 62 | + } |
| 63 | + |
| 64 | + const authConfigForm = useMemo(() => { |
| 65 | + if(!authConfig[authType]) return clientIdandSecretConfig; |
| 66 | + return authConfig[authType].form; |
| 67 | + }, [authType]) |
| 68 | + |
| 69 | + return ( |
| 70 | + <FormStyled |
| 71 | + form={form1} |
| 72 | + name="general" |
| 73 | + layout="vertical" |
| 74 | + style={{ maxWidth: '100%' }} |
| 75 | + autoComplete="off" |
| 76 | + > |
| 77 | + {Object.entries(authConfigForm).map(([key, value]) => { |
| 78 | + const valueObject = _.isObject(value) ? (value as ItemType) : false; |
| 79 | + const required = true; |
| 80 | + const label = valueObject ? valueObject.label : value; |
| 81 | + const tip = valueObject && valueObject.tip; |
| 82 | + const isPassword = valueObject && valueObject.isPassword; |
| 83 | + return ( |
| 84 | + <div key={key}> |
| 85 | + <Form.Item |
| 86 | + key={key} |
| 87 | + name={key} |
| 88 | + rules={[ |
| 89 | + { |
| 90 | + required, |
| 91 | + message: trans("idSource.formPlaceholder", { |
| 92 | + label, |
| 93 | + }), |
| 94 | + }, |
| 95 | + ]} |
| 96 | + label={ |
| 97 | + isPassword ? ( |
| 98 | + <PasswordLabel> |
| 99 | + <span>{label}:</span> |
| 100 | + <CloseEyeIcon /> |
| 101 | + </PasswordLabel> |
| 102 | + ) : ( |
| 103 | + <Tooltip title={tip}> |
| 104 | + <span className={tip ? "has-tip" : ""}>{label}</span>: |
| 105 | + </Tooltip> |
| 106 | + ) |
| 107 | + } |
| 108 | + > |
| 109 | + {isPassword ? ( |
| 110 | + <Input |
| 111 | + type={"password"} |
| 112 | + placeholder={trans("idSource.encryptedServer")} |
| 113 | + autoComplete={"one-time-code"} |
| 114 | + /> |
| 115 | + ) : ( |
| 116 | + <Input |
| 117 | + placeholder={trans("idSource.formPlaceholder", { |
| 118 | + label, |
| 119 | + })} |
| 120 | + /> |
| 121 | + )} |
| 122 | + </Form.Item> |
| 123 | + </div> |
| 124 | + ); |
| 125 | + })} |
| 126 | + <Flex justify="end" gap={'8px'}> |
| 127 | + <Button |
| 128 | + type="default" |
| 129 | + style={{margin: 0}} |
| 130 | + onClick={handleCancel} |
| 131 | + > |
| 132 | + Cancel |
| 133 | + </Button> |
| 134 | + <Button |
| 135 | + type="primary" |
| 136 | + style={{margin: 0}} |
| 137 | + onClick={handleSave} |
| 138 | + loading={saveLoading} |
| 139 | + > |
| 140 | + Save |
| 141 | + </Button> |
| 142 | + </Flex> |
| 143 | + </FormStyled> |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +export default GeneralOAuthForm; |
0 commit comments