From 5770b915ffdc6ea071c58e3ffd226004555b406b Mon Sep 17 00:00:00 2001 From: litesun <7sunmiao@gmail.com> Date: Thu, 14 Jan 2021 23:36:55 +0800 Subject: [PATCH 01/12] feat: user can skip upstream when select service_id --- web/src/components/Upstream/UpstreamForm.tsx | 220 +++++++++--------- web/src/pages/Route/Create.tsx | 7 +- .../components/Step2/RequestRewriteView.tsx | 3 + web/src/pages/Route/transform.ts | 3 +- web/src/pages/Route/typing.d.ts | 1 + 5 files changed, 122 insertions(+), 112 deletions(-) diff --git a/web/src/components/Upstream/UpstreamForm.tsx b/web/src/components/Upstream/UpstreamForm.tsx index 3e7cff7d7b..a632a31724 100644 --- a/web/src/components/Upstream/UpstreamForm.tsx +++ b/web/src/components/Upstream/UpstreamForm.tsx @@ -61,6 +61,7 @@ type Props = { showSelector?: boolean; // FIXME: use proper typing ref?: any; + required: boolean, }; const removeBtnStyle = { @@ -70,11 +71,12 @@ const removeBtnStyle = { }; const UpstreamForm: React.FC = forwardRef( - ({ form, disabled, list = [], showSelector }, ref) => { + ({ form, disabled, list = [], showSelector, required = true }, ref) => { const { formatMessage } = useIntl(); const [readonly, setReadonly] = useState( Boolean(form.getFieldValue('upstream_id')) || disabled, ); + const [hidenForm, setHidenForm] = useState(false); const timeoutFields = [ { @@ -610,6 +612,7 @@ const UpstreamForm: React.FC = forwardRef( name="upstream_id" shouldUpdate={(prev, next) => { setReadonly(Boolean(next.upstream_id)); + setHidenForm(!Boolean(next.upstream_id === 'None')); if (prev.upstream_id !== next.upstream_id) { const id = next.upstream_id; if (id) { @@ -639,120 +642,123 @@ const UpstreamForm: React.FC = forwardRef( {item.name} ))} + {Boolean(!required) && None} )} - - - - - {() => { - if (form.getFieldValue('type') === 'chash') { - return ; - } - return null; - }} - - {NodeList()} - - - - { - return prev.pass_host !== next.pass_host; - }} - > - {() => { - if (form.getFieldValue('pass_host') === 'rewrite') { - return ( - - - - ); - } - return null; - }} - - - {timeoutFields.map(({ label, name }) => ( - - - - - + {hidenForm && (<> + + + + + {() => { + if (form.getFieldValue('type') === 'chash') { + return ; + } + return null; + }} + + {NodeList()} + + + + { + return prev.pass_host !== next.pass_host; + }} + > + {() => { + if (form.getFieldValue('pass_host') === 'rewrite') { + return ( + + + + ); + } + return null; + }} - ))} - - {[ - { - label: formatMessage({ id: 'page.upstream.step.healthyCheck.active' }), - name: ['checks', 'active'], - component: , - }, - { - label: formatMessage({ id: 'page.upstream.step.healthyCheck.passive' }), - name: ['checks', 'passive'], - component: , - }, - ].map(({ label, name, component }) => ( -
- - - - - {() => { - if (form.getFieldValue(name)) { - return component; - } - return null; - }} + {timeoutFields.map(({ label, name }) => ( + + + -
+ +
))} - + + + {[ + { + label: formatMessage({ id: 'page.upstream.step.healthyCheck.active' }), + name: ['checks', 'active'], + component: , + }, + { + label: formatMessage({ id: 'page.upstream.step.healthyCheck.passive' }), + name: ['checks', 'passive'], + component: , + }, + ].map(({ label, name, component }) => ( +
+ + + + + {() => { + if (form.getFieldValue(name)) { + return component; + } + return null; + }} + +
+ ))} +
+ )} ); }, diff --git a/web/src/pages/Route/Create.tsx b/web/src/pages/Route/Create.tsx index ab1257f130..e4911803e9 100644 --- a/web/src/pages/Route/Create.tsx +++ b/web/src/pages/Route/Create.tsx @@ -135,7 +135,7 @@ const Page: React.FC = (props) => { ); } - return ; + return ; } if (step === 3) { @@ -256,11 +256,10 @@ const Page: React.FC = (props) => { return ( <> diff --git a/web/src/pages/Route/components/Step2/RequestRewriteView.tsx b/web/src/pages/Route/components/Step2/RequestRewriteView.tsx index a3bf8dcb0f..1bd428f02d 100644 --- a/web/src/pages/Route/components/Step2/RequestRewriteView.tsx +++ b/web/src/pages/Route/components/Step2/RequestRewriteView.tsx @@ -23,7 +23,9 @@ const RequestRewriteView: React.FC = ({ form, upstreamRef, disabled, + isServiceId = false }) => { + console.log('isServiceId: ', isServiceId); const [list, setList] = useState([]); useEffect(() => { fetchUpstreamList().then(({ data }) => setList(data)); @@ -35,6 +37,7 @@ const RequestRewriteView: React.FC = ({ disabled={disabled} list={list} showSelector + required={!isServiceId} key={1} /> ); diff --git a/web/src/pages/Route/transform.ts b/web/src/pages/Route/transform.ts index bb794d227f..2085890bc7 100644 --- a/web/src/pages/Route/transform.ts +++ b/web/src/pages/Route/transform.ts @@ -107,6 +107,7 @@ export const transformStepData = ({ 'ret_code', 'redirectOption', service_id.length === 0 ? 'service_id' : '', + form2Data.upstream_id === 'None'? 'upstream_id': '', !Object.keys(data.plugins || {}).length ? 'plugins' : '', !Object.keys(data.script || {}).length ? 'script' : '', form1Data.hosts.filter(Boolean).length === 0 ? 'hosts' : '', @@ -177,7 +178,7 @@ export const transformRouteData = (data: RouteModule.Body) => { vars, status, upstream, - upstream_id, + upstream_id = 'None', service_id = '', priority = 0, enable_websocket, diff --git a/web/src/pages/Route/typing.d.ts b/web/src/pages/Route/typing.d.ts index 992a845dcb..fd9af57119 100644 --- a/web/src/pages/Route/typing.d.ts +++ b/web/src/pages/Route/typing.d.ts @@ -163,6 +163,7 @@ declare namespace RouteModule { form: FormInstance; disabled?: boolean; upstreamRef: any; + isServiceId: boolean; }; type Form2Data = { From d561238c7ab0cea3a13c8d8ce3d22959e6a9874c Mon Sep 17 00:00:00 2001 From: litesun <7sunmiao@gmail.com> Date: Thu, 14 Jan 2021 23:58:04 +0800 Subject: [PATCH 02/12] feat: update code --- web/src/components/Upstream/UpstreamForm.tsx | 13 +++++++++---- web/src/pages/Route/transform.ts | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/web/src/components/Upstream/UpstreamForm.tsx b/web/src/components/Upstream/UpstreamForm.tsx index a632a31724..a0ee533883 100644 --- a/web/src/components/Upstream/UpstreamForm.tsx +++ b/web/src/components/Upstream/UpstreamForm.tsx @@ -98,13 +98,18 @@ const UpstreamForm: React.FC = forwardRef( })); useEffect(() => { - const id = form.getFieldValue('upstream_id'); + const formData = form.getFieldsValue(); + const { id } = formData; if (id) { setReadonly(true); requestAnimationFrame(() => { form.setFieldsValue(list.find((item) => item.id === id)); }); } + + if (!required && !id && !Object.keys(formData).length) { + form.setFieldsValue({ upstream_id: 'None' }); + } }, [list]); const CHash = () => ( @@ -612,7 +617,7 @@ const UpstreamForm: React.FC = forwardRef( name="upstream_id" shouldUpdate={(prev, next) => { setReadonly(Boolean(next.upstream_id)); - setHidenForm(!Boolean(next.upstream_id === 'None')); + setHidenForm(Boolean(next.upstream_id === 'None')); if (prev.upstream_id !== next.upstream_id) { const id = next.upstream_id; if (id) { @@ -631,6 +636,7 @@ const UpstreamForm: React.FC = forwardRef( form.setFieldsValue(list.find((item) => item.id === id)); }} > + {Boolean(!required) && None} {[ { name: formatMessage({ id: 'page.upstream.step.select.upstream.select.option' }), @@ -642,12 +648,11 @@ const UpstreamForm: React.FC = forwardRef( {item.name} ))} - {Boolean(!required) && None} )} - {hidenForm && (<> + {!hidenForm && (<> { vars, status, upstream, - upstream_id = 'None', + upstream_id, service_id = '', priority = 0, enable_websocket, From 63302190afa45c84bc8998fe80184dc84a1c7ec5 Mon Sep 17 00:00:00 2001 From: litesun <7sunmiao@gmail.com> Date: Fri, 15 Jan 2021 11:57:53 +0800 Subject: [PATCH 03/12] feat: update upstream --- web/src/components/Upstream/UpstreamForm.tsx | 50 ++++++++++--------- .../components/Step2/RequestRewriteView.tsx | 1 - 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/web/src/components/Upstream/UpstreamForm.tsx b/web/src/components/Upstream/UpstreamForm.tsx index a0ee533883..e31b07fb76 100644 --- a/web/src/components/Upstream/UpstreamForm.tsx +++ b/web/src/components/Upstream/UpstreamForm.tsx @@ -98,18 +98,34 @@ const UpstreamForm: React.FC = forwardRef( })); useEffect(() => { - const formData = form.getFieldsValue(); - const { id } = formData; - if (id) { - setReadonly(true); + const formData = transformRequest(form.getFieldsValue()) || {}; + const { upstream_id } = form.getFieldsValue(); + + if (required && upstream_id ==='None') { + requestAnimationFrame(() => { + form.resetFields(); + setHidenForm(false); + }); + } + + if (upstream_id === 'None') { + setHidenForm(true); + } + + if (upstream_id) { requestAnimationFrame(() => { - form.setFieldsValue(list.find((item) => item.id === id)); + form.setFieldsValue(list.find((item) => item.id === upstream_id)); }); } - if (!required && !id && !Object.keys(formData).length) { - form.setFieldsValue({ upstream_id: 'None' }); + if (!required && !Object.keys(formData).length && upstream_id !== 'None') { + requestAnimationFrame(() => { + form.setFieldsValue({ upstream_id: 'None' }); + setHidenForm(true); + }); } + + setReadonly(Boolean(upstream_id)); }, [list]); const CHash = () => ( @@ -615,25 +631,13 @@ const UpstreamForm: React.FC = forwardRef( { - setReadonly(Boolean(next.upstream_id)); - setHidenForm(Boolean(next.upstream_id === 'None')); - if (prev.upstream_id !== next.upstream_id) { - const id = next.upstream_id; - if (id) { - form.setFieldsValue(list.find((item) => item.id === id)); - form.setFieldsValue({ - upstream_id: id, - }); - } - } - return prev.upstream_id !== next.upstream_id; - }} >