From 86706622968056a35a8c1fbe92e607595cab723e Mon Sep 17 00:00:00 2001 From: Darya Plotnytska Date: Mon, 16 Oct 2023 13:55:54 +0200 Subject: [PATCH 01/28] console: Refactor packet broker view --- .../components/routing-policy-form/index.js | 44 +---- .../admin-packet-broker.js | 187 ++++++++++-------- .../default-routing-policy.js | 68 ++----- .../views/admin-packet-broker/messages.js | 27 ++- pkg/webui/locales/en.json | 24 +-- pkg/webui/locales/ja.json | 12 +- 6 files changed, 149 insertions(+), 213 deletions(-) diff --git a/pkg/webui/console/components/routing-policy-form/index.js b/pkg/webui/console/components/routing-policy-form/index.js index 17453e8319..cee2254d36 100644 --- a/pkg/webui/console/components/routing-policy-form/index.js +++ b/pkg/webui/console/components/routing-policy-form/index.js @@ -16,10 +16,8 @@ import React, { useState, useCallback } from 'react' import { Col, Row } from 'react-grid-system' import { defineMessages } from 'react-intl' -import Form from '@ttn-lw/components/form' +import Form, { useFormContext } from '@ttn-lw/components/form' import Checkbox from '@ttn-lw/components/checkbox' -import SubmitBar from '@ttn-lw/components/submit-bar' -import SubmitButton from '@ttn-lw/components/submit-button' import Radio from '@ttn-lw/components/radio-button' import Message from '@ttn-lw/lib/components/message' @@ -28,7 +26,6 @@ import RoutingPolicy from '@console/components/routing-policy' import PropTypes from '@ttn-lw/lib/prop-types' import sharedMessages from '@ttn-lw/lib/shared-messages' -import Yup from '@ttn-lw/lib/yup' import { isValidPolicy } from '@console/lib/packet-broker/utils' import policyMessages from '@console/lib/packet-broker/messages' @@ -36,36 +33,20 @@ import policyMessages from '@console/lib/packet-broker/messages' import style from './routing-policy-form.styl' const m = defineMessages({ - saveDefaultPolicy: 'Save default policy', useDefaultPolicy: 'Use default routing policy for this network', useSpecificPolicy: 'Use network specific routing policy', doNotUseADefaultPolicy: 'Do not use a default routing policy for this network', doNotUseAPolicy: 'Do not use a routing policy for this network', }) -const validationSchema = Yup.object({ - _use_default_policy: Yup.bool(), - policy: Yup.object({ - uplink: Yup.object({}), - downlink: Yup.object({}), - }).when('_use_default_policy', { is: 'default', then: schema => schema.strip() }), -}) - const policySourceEncode = val => val === 'default' const policySourceDecode = val => (val ? 'default' : 'specific') const useDefaultEncode = val => val === 'default' const useDefaultDecode = val => (val ? 'default' : 'no-default') -const RoutingPolicyForm = ({ - onSubmit, - initialValues, - error, - defaultPolicy, - networkLevel, - submitMessage, -}) => { - const handleSubmit = useCallback(values => onSubmit(validationSchema.cast(values)), [onSubmit]) - const [useDefault, setUseDefault] = useState(initialValues._use_default_policy || false) +const RoutingPolicyForm = ({ defaultPolicy, networkLevel }) => { + const { values } = useFormContext() + const [useDefault, setUseDefault] = useState(values._use_default_policy || false) const handlePolicySourceChange = useCallback(setUseDefault, [setUseDefault]) const hasDefaultPolicy = isValidPolicy(defaultPolicy) @@ -73,12 +54,7 @@ const RoutingPolicyForm = ({ const showPolicyCheckboxes = (useDefault && !networkLevel) || (!useDefault && networkLevel) return ( -
+ <> {networkLevel ? ( @@ -182,30 +158,22 @@ const RoutingPolicyForm = ({ )} - - - -
+ ) } RoutingPolicyForm.propTypes = { defaultPolicy: PropTypes.routingPolicy, - error: PropTypes.error, initialValues: PropTypes.shape({ _use_default_policy: PropTypes.bool, policy: PropTypes.shape({}), }), networkLevel: PropTypes.bool, - onSubmit: PropTypes.func.isRequired, - submitMessage: PropTypes.message, } RoutingPolicyForm.defaultProps = { - error: undefined, networkLevel: false, defaultPolicy: undefined, - submitMessage: m.saveDefaultPolicy, initialValues: { _use_default_policy: false, policy: { diff --git a/pkg/webui/console/views/admin-packet-broker/admin-packet-broker.js b/pkg/webui/console/views/admin-packet-broker/admin-packet-broker.js index c5d01fc26b..32e832f3f0 100644 --- a/pkg/webui/console/views/admin-packet-broker/admin-packet-broker.js +++ b/pkg/webui/console/views/admin-packet-broker/admin-packet-broker.js @@ -22,12 +22,15 @@ import PacketBrokerLogo from '@assets/misc/packet-broker.svg' import Link from '@ttn-lw/components/link' import PageTitle from '@ttn-lw/components/page-title' -import Icon from '@ttn-lw/components/icon' import Switch from '@ttn-lw/components/switch' import Tabs from '@ttn-lw/components/tabs' import PortalledModal from '@ttn-lw/components/modal/portalled' -import Notification from '@ttn-lw/components/notification' import ErrorNotification from '@ttn-lw/components/error-notification' +import Radio from '@ttn-lw/components/radio-button' +import Form from '@ttn-lw/components/form' +import toast from '@ttn-lw/components/toast' +import SubmitBar from '@ttn-lw/components/submit-bar' +import SubmitButton from '@ttn-lw/components/submit-button' import Message from '@ttn-lw/lib/components/message' import RequireRequest from '@ttn-lw/lib/components/require-request' @@ -35,15 +38,18 @@ import GenericNotFound from '@ttn-lw/lib/components/full-view-error/not-found' import SubViewErrorComponent from '@console/views/sub-view-error' +import attachPromise from '@ttn-lw/lib/store/actions/attach-promise' import sharedMessages from '@ttn-lw/lib/shared-messages' +import Yup from '@ttn-lw/lib/yup' -import { isNotEnabledError } from '@console/lib/packet-broker/utils' +import { isNotEnabledError, isValidPolicy } from '@console/lib/packet-broker/utils' import { registerPacketBroker, deregisterPacketBroker, getHomeNetworkDefaultRoutingPolicy, - getHomeNetworkDefaultGatewayVisibility, + setHomeNetworkDefaultRoutingPolicy, + deleteHomeNetworkDefaultRoutingPolicy, } from '@console/store/actions/packet-broker' import { @@ -51,17 +57,24 @@ import { selectRegisterEnabled, selectEnabled, selectListed, - selectInfo, selectInfoError, + selectHomeNetworkDefaultRoutingPolicy, } from '@console/store/selectors/packet-broker' import DefaultRoutingPolicyView from './default-routing-policy' import NetworkRoutingPoliciesView from './network-routing-policies' -import DefaultGatewayVisibilityView from './default-gateway-visibility' import m from './messages' import style from './admin-packet-broker.styl' +const validationSchema = Yup.object({ + _use_default_policy: Yup.bool(), + policy: Yup.object({ + uplink: Yup.object({}), + downlink: Yup.object({}), + }).when('_use_default_policy', { is: 'default', then: schema => schema.strip() }), +}) + const PacketBroker = () => { const [activeTab, setActiveTab] = useState('default-routing-policy') const [deregisterModalVisible, setDeregisterModalVisible] = useState(false) @@ -70,7 +83,6 @@ const PacketBroker = () => { const enabled = useSelector(selectEnabled) const [unlistModalVisible, setUnlistModalVisible] = useState(false) const listed = useSelector(selectListed) - const info = useSelector(selectInfo) const infoError = useSelector(selectInfoError) const dispatch = useDispatch() const showError = Boolean(infoError) && !isNotEnabledError(infoError) @@ -113,11 +125,11 @@ const PacketBroker = () => { const tabs = [ { title: m.defaultRoutingPolicy, link: '/admin-panel/packet-broker', name: 'default' }, - { + /* { title: m.defaultGatewayVisibility, link: '/admin-panel/packet-broker/default-gateway-visibility', name: 'default-gateway-visibility', - }, + }, */ { title: sharedMessages.networks, link: '/admin-panel/packet-broker/networks', @@ -126,7 +138,43 @@ const PacketBroker = () => { }, ] - const boldMessage = { b: msg => {msg} } + const defaultRoutingPolicy = useSelector(selectHomeNetworkDefaultRoutingPolicy) + const initialValues = { + _use_default_policy: isValidPolicy(defaultRoutingPolicy), + _routing_configuration: 'default', + } + initialValues.policy = initialValues._use_default_policy + ? defaultRoutingPolicy + : { uplink: {}, downlink: {} } + const [rountingConfig, setRoutingConfig] = useState(initialValues._routing_configuration) + const [formError, setFormError] = useState(undefined) + const handleDefaultRoutingPolicySubmit = useCallback( + async values => { + const vals = validationSchema.cast(values) + const { _use_default_policy, policy } = vals + try { + if (_use_default_policy) { + await dispatch(attachPromise(setHomeNetworkDefaultRoutingPolicy(policy))) + } else { + await dispatch(attachPromise(deleteHomeNetworkDefaultRoutingPolicy())) + } + toast({ + message: m.defaultRoutingPolicySet, + type: toast.types.SUCCESS, + }) + } catch (error) { + setFormError(error) + } + }, + [dispatch, setFormError], + ) + + const handleRoutingConfigChange = useCallback( + value => { + setRoutingConfig(value) + }, + [setRoutingConfig], + ) return ( @@ -138,29 +186,23 @@ const PacketBroker = () => { Packet Broker
- + - Packet Broker documentation + Packet Broker {' | '} - {' | '} - - -

- - {!enabled && } + + + + {showError && } {enabled && ( - + {registerEnabled && ( )} - {registered && ( -
- {info.forwarder_enabled ? ( - - - - - ) : ( - - - - - )} - {info.home_network_enabled ? ( - - - - - ) : ( - - - - - )} -
- )} { {registered && ( <> -