Skip to content

Commit ac8a7aa

Browse files
committed
feat: 优化 CallbackURL.tsx
1 parent 29a7755 commit ac8a7aa

File tree

6 files changed

+38
-46
lines changed

6 files changed

+38
-46
lines changed

src/pages/IdentityProvider/Create/CallbackURL.tsx

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import React from 'react';
2-
import { Typography } from 'antd';
1+
import React, { useEffect } from 'react';
2+
import { Form } from 'antd';
33
import { useIntl } from '@umijs/max';
44
import { getRedirectURL } from '@/services/system/oauth';
5-
import { ProFormDependency, ProFormText } from '@ant-design/pro-components';
5+
import { ProFormText } from '@ant-design/pro-components';
66

7-
const { Paragraph } = Typography;
7+
import { FormProps } from './_hooks';
88

99
const INTL = {
1010
NAME: {
@@ -15,38 +15,27 @@ const INTL = {
1515
},
1616
};
1717

18-
export const CallbackURL = () => {
18+
export const CallbackURL = (props: FormProps) => {
1919
const intl = useIntl();
20+
const { form } = props;
21+
const name = Form.useWatch('name', form);
22+
const redirectURL = getRedirectURL(`${name ? name : `{${intl.formatMessage(INTL.NAME)}}`}`);
23+
24+
useEffect(() => {
25+
const oldVal = form?.getFieldValue(['config', 'redirectURL']);
26+
if (oldVal && oldVal.endsWith('}')) {
27+
form?.setFieldValue(['config', 'redirectURL'], redirectURL);
28+
}
29+
}, [name]);
2030

2131
return (
22-
<ProFormDependency name={['name']}>
23-
{({ name }) => {
24-
const redirectURL = getRedirectURL(`${name ? name : `{${intl.formatMessage(INTL.NAME)}}`}`);
25-
return (
26-
<ProFormText
27-
width={300}
28-
label={intl.formatMessage(INTL.AUTH_URL)}
29-
name={['config', 'redirectURL']}
30-
readonly
31-
fieldProps={{ autoComplete: 'off' }}
32-
initialValue={redirectURL}
33-
proFieldProps={{
34-
render: () => {
35-
return (
36-
<Paragraph copyable={{ text: redirectURL }} style={{ marginBottom: '0' }}>
37-
<span
38-
dangerouslySetInnerHTML={{
39-
__html: `<span>${redirectURL}</span>`,
40-
}}
41-
/>
42-
</Paragraph>
43-
);
44-
},
45-
}}
46-
></ProFormText>
47-
);
48-
}}
49-
</ProFormDependency>
32+
<ProFormText
33+
width="lg"
34+
label={intl.formatMessage(INTL.AUTH_URL)}
35+
name={['config', 'redirectURL']}
36+
fieldProps={{ autoComplete: 'off' }}
37+
initialValue={redirectURL}
38+
></ProFormText>
5039
);
5140
};
5241

src/pages/IdentityProvider/Create/Coding.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Checkbox, Col, Row } from 'antd';
44
import { ProForm, ProFormText } from '@ant-design/pro-components';
55

66
import CallbackURL from './CallbackURL';
7+
import { FormProps } from './_hooks';
78

89
const INTL = {
910
CLIENT_ID: {
@@ -35,7 +36,7 @@ const INTL = {
3536
},
3637
};
3738

38-
export const Coding: React.FC = () => {
39+
export const Coding: React.FC<FormProps> = (props: FormProps) => {
3940
const intl = useIntl();
4041

4142
const scopes = [
@@ -110,7 +111,7 @@ export const Coding: React.FC = () => {
110111
</ProForm.Item>
111112
</ProForm.Group>
112113
<ProForm.Group align="center">
113-
<CallbackURL />
114+
<CallbackURL form={props.form} />
114115
</ProForm.Group>
115116
</>
116117
);

src/pages/IdentityProvider/Create/GitHub.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useIntl } from '@umijs/max';
33
import { ProForm, ProFormText } from '@ant-design/pro-components';
44

55
import CallbackURL from './CallbackURL';
6+
import { FormProps } from './_hooks';
67

78
const INTL = {
89
CLIENT_ID: {
@@ -25,7 +26,7 @@ const INTL = {
2526
},
2627
};
2728

28-
export const GitHub = () => {
29+
export const GitHub: React.FC<FormProps> = (props: FormProps) => {
2930
const intl = useIntl();
3031

3132
return (
@@ -59,7 +60,7 @@ export const GitHub = () => {
5960
/>
6061
</ProForm.Group>
6162
<ProForm.Group align="center">
62-
<CallbackURL />
63+
<CallbackURL form={props.form} />
6364
</ProForm.Group>
6465
</>
6566
);

src/pages/IdentityProvider/Create/Gitee.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Checkbox, Col, Row } from 'antd';
44
import { ProForm, ProFormText } from '@ant-design/pro-components';
55

66
import CallbackURL from './CallbackURL';
7+
import { FormProps } from './_hooks';
78

89
const INTL = {
910
CLIENT_ID: {
@@ -29,7 +30,7 @@ const INTL = {
2930
},
3031
};
3132

32-
export const Gitee = () => {
33+
export const Gitee: React.FC<FormProps> = (props: FormProps) => {
3334
const intl = useIntl();
3435

3536
const giteeScopes = [
@@ -95,7 +96,7 @@ export const Gitee = () => {
9596
</ProForm.Item>
9697
</ProForm.Group>
9798
<ProForm.Group align="center">
98-
<CallbackURL />
99+
<CallbackURL form={props.form} />
99100
</ProForm.Group>
100101
</>
101102
);

src/pages/IdentityProvider/Create/_hooks.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ import {
1212
UpdateIdentityProviderRequest,
1313
} from '@/services/idp';
1414
import { IdentityProviderType, ProviderCategory } from '@/enums';
15-
import { getRedirectURL } from '@/services/system/oauth';
1615

1716
export type FormType = UpdateIdentityProviderRequest & CreateIdentityProviderRequest;
1817

18+
export type FormProps = {
19+
form?: ProFormInstance<FormType>;
20+
};
21+
1922
export default function useIdentityProviderHook() {
2023
const intl = useIntl();
2124
const { message } = App.useApp();
@@ -82,9 +85,6 @@ export default function useIdentityProviderHook() {
8285
const handleSubmit = async () => {
8386
const values = await formRef.current?.validateFieldsReturnFormatValue?.();
8487
if (values) {
85-
if (values.config.redirectURL) {
86-
values.config.redirectURL = getRedirectURL(values.name);
87-
}
8888
return isEdit ? doUpdateIdp(instanceId, values) : doCreateIdp(values);
8989
}
9090
};

src/pages/IdentityProvider/Create/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,16 @@ const CreatePolicy: React.FC = () => {
142142
<ProFormDependency name={['type']}>
143143
{({ type }) => {
144144
if (type === IdentityProviderType.Gitee) {
145-
return <Gitee key={type} />;
145+
return <Gitee key={type} form={formRef.current} />;
146146
}
147147
if (type === IdentityProviderType.GitHub) {
148-
return <GitHub key={type} />;
148+
return <GitHub key={type} form={formRef.current} />;
149149
}
150150
if (type === IdentityProviderType.WeChatMiniProgram) {
151151
return <WeChatMiniProgram key={type} />;
152152
}
153153
if (type === IdentityProviderType.Coding) {
154-
return <Coding key={type} />;
154+
return <Coding key={type} form={formRef.current} />;
155155
}
156156
return null;
157157
}}

0 commit comments

Comments
 (0)