From a5dfc2e03ba3c94cef200c938cb04292245896e4 Mon Sep 17 00:00:00 2001 From: dorj222 Date: Tue, 24 Jan 2023 14:33:56 +0100 Subject: [PATCH] update Input Validators for Streak Keys and Admin Password --- web/components/admin/TextField.tsx | 32 ++++++++++++++++--- web/components/admin/TextFieldWithSubmit.tsx | 21 ++++++------ .../admin/config/server/StreamKeys.tsx | 20 ++++++++++-- 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/web/components/admin/TextField.tsx b/web/components/admin/TextField.tsx index 9f649e2e9..8aee784ee 100644 --- a/web/components/admin/TextField.tsx +++ b/web/components/admin/TextField.tsx @@ -1,6 +1,6 @@ -import React, { FC, useEffect } from 'react'; +import React, { FC, useEffect, useState } from 'react'; import classNames from 'classnames'; -import { Input, Form, InputNumber } from 'antd'; +import { Input, Form, InputNumber, Button } from 'antd'; import { FieldUpdaterFunc } from '../../types/config-section'; // import InfoTip from '../info-tip'; import { StatusState } from '../../utils/input-statuses'; @@ -17,7 +17,7 @@ export type TextFieldProps = { onSubmit?: () => void; onPressEnter?: () => void; - + onHandleSubmit?: () => void; className?: string; disabled?: boolean; label?: string; @@ -44,6 +44,7 @@ export const TextField: FC = ({ onBlur, onChange, onPressEnter, + onHandleSubmit, pattern, placeholder, required, @@ -53,16 +54,25 @@ export const TextField: FC = ({ useTrim, value, }) => { + const [hasChanged, setHasChanged] = useState(false); + const regex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/; + const [form] = Form.useForm(); const handleChange = (e: any) => { // if an extra onChange handler was sent in as a prop, let's run that too. if (onChange) { const val = type === TEXTFIELD_TYPE_NUMBER ? e : e.target.value; + // console.log('val: ', val, 'fieldname: ', fieldName); + + if (fieldName === 'adminPassword' && regex.test(val)) { + setHasChanged(true); + } else { + setHasChanged(false); + } + onChange({ fieldName, value: useTrim ? val.trim() : val }); } }; - const [form] = Form.useForm(); - useEffect(() => { // console.log('value: ', value); form.setFieldsValue({ adminPassword: value }); @@ -199,6 +209,17 @@ export const TextField: FC = ({ value={value as number | (readonly string[] & number)} /> +
+ +

{tip}

@@ -231,4 +252,5 @@ TextField.defaultProps = { onBlur: () => {}, onChange: () => {}, onPressEnter: () => {}, + onHandleSubmit: () => {}, }; diff --git a/web/components/admin/TextFieldWithSubmit.tsx b/web/components/admin/TextFieldWithSubmit.tsx index ce89c7030..bf39ab9e9 100644 --- a/web/components/admin/TextFieldWithSubmit.tsx +++ b/web/components/admin/TextFieldWithSubmit.tsx @@ -126,6 +126,7 @@ export const TextFieldWithSubmit: FC = ({ onSubmit={null} onBlur={handleBlur} onChange={handleChange} + onHandleSubmit={handleSubmit} />
@@ -134,15 +135,17 @@ export const TextFieldWithSubmit: FC = ({
{tip}
- + {fieldName !== 'adminPassword' && ( + + )}
diff --git a/web/components/admin/config/server/StreamKeys.tsx b/web/components/admin/config/server/StreamKeys.tsx index 5c70d884d..b1077f4ff 100644 --- a/web/components/admin/config/server/StreamKeys.tsx +++ b/web/components/admin/config/server/StreamKeys.tsx @@ -6,7 +6,8 @@ import { ServerStatusContext } from '../../../../utils/server-status-context'; import { fetchData, UPDATE_STREAM_KEYS } from '../../../../utils/apis'; const { Paragraph } = Typography; -const { Item } = Form; + +const regex = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/; // Lazy loaded components @@ -36,6 +37,9 @@ const saveKeys = async (keys, setError) => { }; const AddKeyForm = ({ setShowAddKeyForm, setFieldInConfigState, streamKeys, setError }) => { + const [hasChanged, setHasChanged] = useState(false); + const [form] = Form.useForm(); + const { Item } = Form; const handleAddKey = (newkey: any) => { const updatedKeys = [...streamKeys, newkey]; @@ -49,11 +53,21 @@ const AddKeyForm = ({ setShowAddKeyForm, setFieldInConfigState, streamKeys, setE setShowAddKeyForm(false); }; + const handleInputChange = (event: any) => { + const val = event.target.value; + if (regex.test(val)) { + setHasChanged(true); + } else { + setHasChanged(false); + } + }; + return (
- + -