0

refactor the Admin Password Input field and add a new boolean field for it

This commit is contained in:
dorj222 2023-02-07 11:41:51 +01:00
parent c3caa0716f
commit 170f367538
3 changed files with 13 additions and 12 deletions

View File

@ -31,6 +31,7 @@ export type TextFieldProps = {
useTrim?: boolean; useTrim?: boolean;
useTrimLead?: boolean; useTrimLead?: boolean;
value?: string | number; value?: string | number;
isAdminPwdField?: boolean;
onBlur?: FieldUpdaterFunc; onBlur?: FieldUpdaterFunc;
onChange?: FieldUpdaterFunc; onChange?: FieldUpdaterFunc;
}; };
@ -53,6 +54,7 @@ export const TextField: FC<TextFieldProps> = ({
type, type,
useTrim, useTrim,
value, value,
isAdminPwdField,
}) => { }) => {
const [hasPwdChanged, setHasPwdChanged] = useState(false); const [hasPwdChanged, setHasPwdChanged] = useState(false);
const [showPwdButton, setShowPwdButton] = useState(false); const [showPwdButton, setShowPwdButton] = useState(false);
@ -63,7 +65,7 @@ export const TextField: FC<TextFieldProps> = ({
if (onChange) { if (onChange) {
const val = type === TEXTFIELD_TYPE_NUMBER ? e : e.target.value; const val = type === TEXTFIELD_TYPE_NUMBER ? e : e.target.value;
setShowPwdButton(true); setShowPwdButton(true);
if (fieldName === 'adminPassword' && regex.test(val)) { if (isAdminPwdField && regex.test(val)) {
setHasPwdChanged(true); setHasPwdChanged(true);
} else { } else {
setHasPwdChanged(false); setHasPwdChanged(false);
@ -144,7 +146,7 @@ export const TextField: FC<TextFieldProps> = ({
</div> </div>
) : null} ) : null}
{type !== TEXTFIELD_TYPE_PASSWORD ? ( {!isAdminPwdField ? (
<div className="input-side"> <div className="input-side">
<div className="input-group"> <div className="input-group">
<Field <Field
@ -247,6 +249,7 @@ TextField.defaultProps = {
pattern: '', pattern: '',
useTrim: false, useTrim: false,
useTrimLead: false, useTrimLead: false,
isAdminPwdField: false,
onSubmit: () => {}, onSubmit: () => {},
onBlur: () => {}, onBlur: () => {},

View File

@ -24,6 +24,7 @@ export type TextFieldWithSubmitProps = TextFieldProps & {
apiPath: string; apiPath: string;
configPath?: string; configPath?: string;
initialValue?: string; initialValue?: string;
isAdminPwdField?: boolean;
}; };
export const TextFieldWithSubmit: FC<TextFieldWithSubmitProps> = ({ export const TextFieldWithSubmit: FC<TextFieldWithSubmitProps> = ({
@ -38,13 +39,13 @@ export const TextFieldWithSubmit: FC<TextFieldWithSubmitProps> = ({
const [hasChanged, setHasChanged] = useState(false); const [hasChanged, setHasChanged] = useState(false);
const [isPwdInput, setPwdInputField] = useState(false);
const serverStatusData = useContext(ServerStatusContext); const serverStatusData = useContext(ServerStatusContext);
const { setFieldInConfigState } = serverStatusData || {}; const { setFieldInConfigState } = serverStatusData || {};
let resetTimer = null; let resetTimer = null;
const { fieldName, required, tip, status, value, onChange, onSubmit } = textFieldProps; const { fieldName, required, tip, status, value, isAdminPwdField, onChange, onSubmit } =
textFieldProps;
// Clear out any validation states and messaging // Clear out any validation states and messaging
const resetStates = () => { const resetStates = () => {
@ -67,13 +68,6 @@ export const TextFieldWithSubmit: FC<TextFieldWithSubmitProps> = ({
} }
}, [value]); }, [value]);
useEffect(() => {
if (fieldName === 'adminPassword') {
setPwdInputField(true);
}
setPwdInputField(false);
}, [fieldName]);
// if field is required but value is empty, or equals initial value, then don't show submit/update button. otherwise clear out any result messaging and display button. // if field is required but value is empty, or equals initial value, then don't show submit/update button. otherwise clear out any result messaging and display button.
const handleChange = ({ fieldName: changedFieldName, value: changedValue }: UpdateArgs) => { const handleChange = ({ fieldName: changedFieldName, value: changedValue }: UpdateArgs) => {
if (onChange) { if (onChange) {
@ -144,7 +138,7 @@ export const TextFieldWithSubmit: FC<TextFieldWithSubmitProps> = ({
<div className="field-tip">{tip}</div> <div className="field-tip">{tip}</div>
<FormStatusIndicator status={status || submitStatus} /> <FormStatusIndicator status={status || submitStatus} />
<div className="update-button-container"> <div className="update-button-container">
{isPwdInput && ( {!isAdminPwdField && (
<Button <Button
type="primary" type="primary"
size="small" size="small"

View File

@ -122,6 +122,7 @@ export const TEXTFIELD_PROPS_ADMIN_PASSWORD = {
label: 'Admin Password', label: 'Admin Password',
tip: 'Save this password somewhere safe, you will need it to login to the admin dashboard!', tip: 'Save this password somewhere safe, you will need it to login to the admin dashboard!',
required: true, required: true,
isAdminPwdField: true,
}; };
export const TEXTFIELD_PROPS_FFMPEG = { export const TEXTFIELD_PROPS_FFMPEG = {
apiPath: API_FFMPEG, apiPath: API_FFMPEG,
@ -131,6 +132,7 @@ export const TEXTFIELD_PROPS_FFMPEG = {
label: 'FFmpeg Path', label: 'FFmpeg Path',
tip: 'Absolute file path of the FFMPEG application on your server', tip: 'Absolute file path of the FFMPEG application on your server',
required: true, required: true,
isAdminPwdField: false,
}; };
export const TEXTFIELD_PROPS_WEB_PORT = { export const TEXTFIELD_PROPS_WEB_PORT = {
apiPath: API_WEB_PORT, apiPath: API_WEB_PORT,
@ -140,6 +142,7 @@ export const TEXTFIELD_PROPS_WEB_PORT = {
label: 'Owncast port', label: 'Owncast port',
tip: 'What port is your Owncast web server listening? Default is 8080', tip: 'What port is your Owncast web server listening? Default is 8080',
required: true, required: true,
isAdminPwdField: false,
}; };
export const TEXTFIELD_PROPS_RTMP_PORT = { export const TEXTFIELD_PROPS_RTMP_PORT = {
apiPath: API_RTMP_PORT, apiPath: API_RTMP_PORT,
@ -149,6 +152,7 @@ export const TEXTFIELD_PROPS_RTMP_PORT = {
label: 'RTMP port', label: 'RTMP port',
tip: 'What port should accept inbound broadcasts? Default is 1935', tip: 'What port should accept inbound broadcasts? Default is 1935',
required: true, required: true,
isAdminPwdField: false,
}; };
export const TEXTFIELD_PROPS_INSTANCE_URL = { export const TEXTFIELD_PROPS_INSTANCE_URL = {
apiPath: API_INSTANCE_URL, apiPath: API_INSTANCE_URL,