|
| 1 | +import { useEffect, useMemo, useState } from "react"; |
| 2 | +import { |
| 3 | + messageInstance, |
| 4 | + CustomSelect, |
| 5 | + CloseEyeIcon, |
| 6 | + CustomModal, |
| 7 | + UnderlineCss, |
| 8 | +} from "lowcoder-design"; |
| 9 | +import { trans } from "i18n"; |
| 10 | +import { default as Form } from "antd/es/form"; |
| 11 | +import { default as Input } from "antd/es/input"; |
| 12 | +import { validateResponse } from "api/apiUtils"; |
| 13 | +import _ from "lodash"; |
| 14 | +import { styled } from "styled-components"; |
| 15 | +import UserApi, { ApiKeyPayload } from "api/userApi"; |
| 16 | + |
| 17 | +const CustomModalStyled = styled(CustomModal)` |
| 18 | + button { |
| 19 | + margin-top: 20px; |
| 20 | + } |
| 21 | +`; |
| 22 | + |
| 23 | +const FormStyled = styled(Form)` |
| 24 | + .ant-form-item-control-input-content > input, |
| 25 | + .ant-input-password { |
| 26 | + &:hover { |
| 27 | + border-color: #8b8fa3; |
| 28 | + } |
| 29 | +
|
| 30 | + &:focus, |
| 31 | + &.ant-input-affix-wrapper-focused { |
| 32 | + border-color: #3377ff; |
| 33 | + } |
| 34 | + } |
| 35 | +
|
| 36 | + .ant-form-item-label > label { |
| 37 | + font-size: 13px; |
| 38 | + line-height: 19px; |
| 39 | + .has-tip { |
| 40 | + ${UnderlineCss}; |
| 41 | + } |
| 42 | + } |
| 43 | +
|
| 44 | + .ant-input-password-icon.anticon { |
| 45 | + color: #8b8fa3; |
| 46 | +
|
| 47 | + &:hover { |
| 48 | + color: #222; |
| 49 | + } |
| 50 | + } |
| 51 | +
|
| 52 | + &.ant-form-vertical .ant-form-item-label { |
| 53 | + padding-bottom: 4px; |
| 54 | + } |
| 55 | +
|
| 56 | + .ant-form-item-explain-error { |
| 57 | + font-size: 12px; |
| 58 | + color: #f73131; |
| 59 | + line-height: 20px; |
| 60 | + } |
| 61 | +
|
| 62 | + .ant-form-item-label |
| 63 | + > label.ant-form-item-required:not(.ant-form-item-required-mark-optional)::before { |
| 64 | + color: #f73131; |
| 65 | + } |
| 66 | +
|
| 67 | + .ant-input-status-error:not(.ant-input-disabled):not(.ant-input-borderless).ant-input, |
| 68 | + .ant-input-status-error:not(.ant-input-disabled):not(.ant-input-borderless).ant-input:hover { |
| 69 | + border-color: #f73131; |
| 70 | + } |
| 71 | +
|
| 72 | + .register { |
| 73 | + margin: -4px 0 20px 0; |
| 74 | + } |
| 75 | +
|
| 76 | + .ant-input-prefix { |
| 77 | + margin-right: 8px; |
| 78 | + svg { |
| 79 | + path, |
| 80 | + rect:nth-of-type(1) { |
| 81 | + stroke: #8b8fa3; |
| 82 | + } |
| 83 | + rect:nth-of-type(2) { |
| 84 | + fill: #8b8fa3; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +`; |
| 89 | + |
| 90 | +type CreateApiKeyModalProps = { |
| 91 | + modalVisible: boolean; |
| 92 | + closeModal: () => void; |
| 93 | + onConfigCreate: () => void; |
| 94 | +}; |
| 95 | + |
| 96 | +function CreateApiKeyModal(props: CreateApiKeyModalProps) { |
| 97 | + const { |
| 98 | + modalVisible, |
| 99 | + closeModal, |
| 100 | + onConfigCreate |
| 101 | + } = props; |
| 102 | + const [form] = Form.useForm(); |
| 103 | + const [saveLoading, setSaveLoading] = useState(false); |
| 104 | + |
| 105 | + const handleOk = () => { |
| 106 | + form.validateFields().then(values => { |
| 107 | + // console.log(values) |
| 108 | + createApiKey(values) |
| 109 | + }) |
| 110 | + } |
| 111 | + function createApiKey(values: ApiKeyPayload) { |
| 112 | + setSaveLoading(true); |
| 113 | + |
| 114 | + UserApi.createApiKey(values) |
| 115 | + .then((resp) => { |
| 116 | + if (validateResponse(resp)) { |
| 117 | + messageInstance.success(trans("idSource.saveSuccess")); |
| 118 | + } |
| 119 | + }) |
| 120 | + .catch((e) => messageInstance.error(e.message)) |
| 121 | + .finally(() => { |
| 122 | + setSaveLoading(false); |
| 123 | + onConfigCreate(); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + function handleCancel() { |
| 128 | + closeModal(); |
| 129 | + form.resetFields(); |
| 130 | + } |
| 131 | + |
| 132 | + return ( |
| 133 | + <CustomModalStyled |
| 134 | + width="500px" |
| 135 | + title={"Create API Key"} |
| 136 | + open={modalVisible} |
| 137 | + okText={"Save"} |
| 138 | + okButtonProps={{ |
| 139 | + loading: saveLoading |
| 140 | + }} |
| 141 | + onOk={handleOk} |
| 142 | + onCancel={handleCancel} |
| 143 | + destroyOnClose |
| 144 | + afterClose={() => form.resetFields()} |
| 145 | + > |
| 146 | + <FormStyled |
| 147 | + form={form} |
| 148 | + name="basic" |
| 149 | + layout="vertical" |
| 150 | + style={{ maxWidth: 440 }} |
| 151 | + autoComplete="off" |
| 152 | + > |
| 153 | + <Form.Item |
| 154 | + name="name" |
| 155 | + label="Name" |
| 156 | + rules={[{ required: true }]} |
| 157 | + > |
| 158 | + <Input |
| 159 | + placeholder={trans("idSource.formPlaceholder", { |
| 160 | + label: 'Name' |
| 161 | + })} |
| 162 | + /> |
| 163 | + </Form.Item> |
| 164 | + <Form.Item |
| 165 | + name="description" |
| 166 | + label="Description" |
| 167 | + > |
| 168 | + <Input |
| 169 | + placeholder={trans("idSource.formPlaceholder", { |
| 170 | + label: 'Description' |
| 171 | + })} |
| 172 | + /> |
| 173 | + </Form.Item> |
| 174 | + </FormStyled> |
| 175 | + </CustomModalStyled> |
| 176 | + ); |
| 177 | +} |
| 178 | + |
| 179 | +export default CreateApiKeyModal; |
0 commit comments