import get from 'lodash/get' ; import * as Constants from '../constants' ; import * as Copy from '../i18n' ; import type { WifiNetwork , WifiKey , EapOption , WifiAuthField , WifiConfigureRequest , WifiSecurityType , WifiEapConfig , ConnectFormValues , ConnectFormErrors , ConnectFormField , ConnectFormTextField , ConnectFormSecurityField , } from '../types' ; export const renderLabel = ( label , required ) : string => `${ required ? '* ' : '' }${ label }` ; const FIELD_SSID = { type : Constants . FIELD_TYPE_TEXT , name : Constants . CONFIGURE_FIELD_SSID , label : renderLabel ( Copy . LABEL_SSID , true ) , isPassword : false , } ; const FIELD_PSK = { type : Constants . FIELD_TYPE_TEXT , name : Constants . CONFIGURE_FIELD_PSK , label : renderLabel ( Copy . LABEL_PSK , true ) , isPassword : true , } ; const makeSecurityField = ( eapOptions , showAllOptions ) : ConnectFormSecurityField => ( { type : Constants . FIELD_TYPE_SECURITY , name : Constants . CONFIGURE_FIELD_SECURITY_TYPE , label : renderLabel ( Copy . LABEL_SECURITY , true ) , placeholder : Copy . SELECT_AUTHENTICATION_METHOD , eapOptions , showAllOptions , } ) ; const getEapIsSelected = ( formSecurityType ) : boolean => { return ( formSecurityType != null && formSecurityType !== Constants . SECURITY_NONE && formSecurityType !== Constants . SECURITY_WPA_PSK ) ; } ; const getEapFields = ( eapOptions , values , errors ? , touched ? ) : WifiAuthField [ ] => { const eapType = values . securityType ; return eapOptions . filter ( opt => opt . name === eapType ) . flatMap ( opt => opt . options ) ; } ; const getEapFieldName = ( baseName ) : string => `eapConfig.${ baseName }` ; export function getConnectFormFields ( network , robotName , eapOptions , wifiKeys , values ) { const { securityType : formSecurityType } = values ; const fields = [ ] ; if ( network === null ) { fields . push ( FIELD_SSID ) ; } if ( ! network || network . securityType === Constants . SECURITY_WPA_EAP ) { fields . push ( makeSecurityField ( eapOptions , ! network ) ) ; } if ( network ?. securityType === Constants . SECURITY_WPA_PSK || formSecurityType === Constants . SECURITY_WPA_PSK ) { fields . push ( FIELD_PSK ) ; } if ( network ?. securityType === Constants . SECURITY_WPA_EAP || getEapIsSelected ( formSecurityType ) ) { fields . push ( ... getEapFields ( eapOptions , values ) . map ( field => { const { type } = field ; const name = getEapFieldName ( field . name ) ; const label = renderLabel ( field . displayName , field . required ) ; if ( type === Constants . AUTH_TYPE_FILE ) { return { type : Constants . FIELD_TYPE_KEY_FILE , name , label , robotName , wifiKeys , placeholder : Copy . SELECT_FILE , } ; } return { type : Constants . FIELD_TYPE_TEXT , name , label , isPassword : type === Constants . AUTH_TYPE_PASSWORD , } ; } ) ) ; } return fields ; } export function validateConnectFormFields ( network , eapOptions , values ) { const { ssid : formSsid , securityType : formSecurityType , psk : formPsk , } = values ; const errors = { } ; if ( network === null && ! formSsid ) { errors . ssid = Copy . FIELD_IS_REQUIRED ( Copy . LABEL_SSID ) ; } if ( ( network === null || network . securityType === Constants . SECURITY_WPA_EAP ) && ! formSecurityType ) { errors . securityType = Copy . FIELD_IS_REQUIRED ( Copy . LABEL_SECURITY ) ; } if ( ( network ?. securityType === Constants . SECURITY_WPA_PSK || formSecurityType === Constants . SECURITY_WPA_PSK ) && ( ! formPsk || formPsk . length < Constants . CONFIGURE_PSK_MIN_LENGTH ) ) { errors . psk = Copy . FIELD_NOT_LONG_ENOUGH ( Copy . LABEL_PSK , Constants . CONFIGURE_PSK_MIN_LENGTH ) ; } if ( network ?. securityType === Constants . SECURITY_WPA_EAP || getEapIsSelected ( formSecurityType ) ) { getEapFields ( eapOptions , values ) . filter ( ( { name , required } ) => required && ! get ( values , getEapFieldName ( name ) ) ) . forEach ( ( { name , displayName } ) => { errors [ getEapFieldName ( name ) as keyof typeof errors ] = Copy . FIELD_IS_REQUIRED ( displayName ) ; } ) ; } return errors ; } export const connectFormToConfigureRequest = ( network , values ) : WifiConfigureRequest | null => { const { ssid : formSsid , securityType : formSecurityType , psk : formPsk , eapConfig : formEapConfig , } = values ; const ssid = network ?. ssid ?? formSsid ?? null ; let securityType = null ; let eapConfig = null ; if ( getEapIsSelected ( formSecurityType ) ) { securityType = Constants . SECURITY_WPA_EAP ; eapConfig = { eapType : formSecurityType } ; } else if ( network ) { securityType = network . securityType ; } else if ( values . securityType === Constants . SECURITY_NONE || values . securityType === Constants . SECURITY_WPA_PSK ) { securityType = values . securityType as WifiSecurityType ; } if ( ssid !== null && securityType !== null ) { const options = { ssid , securityType , hidden : network === null , } ; if ( formPsk != null ) options . psk = formPsk ; if ( eapConfig ) options . eapConfig = { ... eapConfig , ... formEapConfig } ; return options ; } return null ; } ;