ts cleanup on some config components

This commit is contained in:
gingervitis
2021-01-29 10:26:55 -08:00
parent 84f001c344
commit 97b2e00eee
6 changed files with 77 additions and 21 deletions

View File

@ -4,10 +4,6 @@ import { CheckCircleFilled, ExclamationCircleFilled } from '@ant-design/icons';
import { fetchData, SERVER_CONFIG_UPDATE_URL } from '../../../utils/apis'; import { fetchData, SERVER_CONFIG_UPDATE_URL } from '../../../utils/apis';
import { ApiPostArgs, VideoVariant, SocialHandle } from '../../../types/config-section'; import { ApiPostArgs, VideoVariant, SocialHandle } from '../../../types/config-section';
export const DEFAULT_NAME = 'Owncast User';
export const DEFAULT_TITLE = 'Owncast Server';
export const DEFAULT_SUMMARY = '';
export const TEXT_MAXLENGTH = 255; export const TEXT_MAXLENGTH = 255;
export const RESET_TIMEOUT = 3000; export const RESET_TIMEOUT = 3000;

View File

@ -5,6 +5,7 @@ import { ServerStatusContext } from '../../../utils/server-status-context';
import { postConfigUpdateToAPI, TEXTFIELD_PROPS_USERNAME, TEXTFIELD_PROPS_INSTANCE_URL, TEXTFIELD_PROPS_SERVER_TITLE, TEXTFIELD_PROPS_STREAM_TITLE, TEXTFIELD_PROPS_SERVER_SUMMARY, TEXTFIELD_PROPS_LOGO, API_YP_SWITCH } from './constants'; import { postConfigUpdateToAPI, TEXTFIELD_PROPS_USERNAME, TEXTFIELD_PROPS_INSTANCE_URL, TEXTFIELD_PROPS_SERVER_TITLE, TEXTFIELD_PROPS_STREAM_TITLE, TEXTFIELD_PROPS_SERVER_SUMMARY, TEXTFIELD_PROPS_LOGO, API_YP_SWITCH } from './constants';
import configStyles from '../../../styles/config-pages.module.scss'; import configStyles from '../../../styles/config-pages.module.scss';
import { UpdateArgs } from '../../../types/config-section';
export default function EditInstanceDetails() { export default function EditInstanceDetails() {
const [formDataValues, setFormDataValues] = useState(null); const [formDataValues, setFormDataValues] = useState(null);
@ -36,7 +37,7 @@ export default function EditInstanceDetails() {
} }
} }
const handleFieldChange = (fieldName: string, value: string) => { const handleFieldChange = ({ fieldName, value }: UpdateArgs) => {
setFormDataValues({ setFormDataValues({
...formDataValues, ...formDataValues,
[fieldName]: value, [fieldName]: value,

View File

@ -1,11 +1,10 @@
import React, { useState, useContext } from 'react'; import React, { useState, useContext } from 'react';
import { Button, Input, InputNumber } from 'antd'; import { Button, Input, InputNumber } from 'antd';
import { FormItemProps } from 'antd/es/form'; import { FormItemProps } from 'antd/es/form';
import { RESET_TIMEOUT, postConfigUpdateToAPI } from './constants'; import { RESET_TIMEOUT, postConfigUpdateToAPI } from './constants';
import { TextFieldProps } from '../../../types/config-section'; import { FieldUpdaterFunc } from '../../../types/config-section';
import { ServerStatusContext } from '../../../utils/server-status-context'; import { ServerStatusContext } from '../../../utils/server-status-context';
import InfoTip from '../info-tip'; import InfoTip from '../info-tip';
@ -15,6 +14,25 @@ export const TEXTFIELD_TYPE_NUMBER = 'numeric';
export const TEXTFIELD_TYPE_TEXTAREA = 'textarea'; export const TEXTFIELD_TYPE_TEXTAREA = 'textarea';
export const TEXTFIELD_TYPE_URL = 'url'; export const TEXTFIELD_TYPE_URL = 'url';
interface TextFieldProps {
apiPath: string;
fieldName: string;
configPath?: string;
disabled?: boolean;
initialValue?: string;
label?: string;
maxLength?: number;
placeholder?: string;
required?: boolean;
tip?: string;
type?: string;
value?: string | number;
onSubmit?: () => void;
onBlur?: () => void;
onChange?: FieldUpdaterFunc;
}
export default function TextField(props: TextFieldProps) { export default function TextField(props: TextFieldProps) {
const [submitStatus, setSubmitStatus] = useState<FormItemProps['validateStatus']>(''); const [submitStatus, setSubmitStatus] = useState<FormItemProps['validateStatus']>('');
@ -45,7 +63,6 @@ export default function TextField(props: TextFieldProps) {
value, value,
} = props; } = props;
// Clear out any validation states and messaging // Clear out any validation states and messaging
const resetStates = () => { const resetStates = () => {
setSubmitStatus(''); setSubmitStatus('');
@ -71,7 +88,7 @@ export default function TextField(props: TextFieldProps) {
} }
// if an extra onChange handler was sent in as a prop, let's run that too. // if an extra onChange handler was sent in as a prop, let's run that too.
if (onChange) { if (onChange) {
onChange(fieldName, val); onChange({ fieldName, value: val });
} }
}; };
@ -82,7 +99,7 @@ export default function TextField(props: TextFieldProps) {
} }
const val = e.target.value; const val = e.target.value;
if (required && val === '') { if (required && val === '') {
onChange(fieldName, initialValue); onChange({ fieldName, value: initialValue });
} }
// if an extra onBlur handler was sent in as a prop, let's run that too. // if an extra onBlur handler was sent in as a prop, let's run that too.
if (onBlur) { if (onBlur) {
@ -176,3 +193,19 @@ export default function TextField(props: TextFieldProps) {
</div> </div>
); );
} }
TextField.defaultProps = {
configPath: '',
disabled: false,
initialValue: '',
label: '',
maxLength: null,
placeholder: '',
required: false,
tip: '',
type: TEXTFIELD_TYPE_TEXT,
value: '',
onSubmit: () => {},
onBlur: () => {},
onChange: () => {},
};

View File

@ -4,10 +4,19 @@ import { FormItemProps } from 'antd/es/form';
import { RESET_TIMEOUT, SUCCESS_STATES, postConfigUpdateToAPI } from './constants'; import { RESET_TIMEOUT, SUCCESS_STATES, postConfigUpdateToAPI } from './constants';
import { ToggleSwitchProps } from '../../../types/config-section';
import { ServerStatusContext } from '../../../utils/server-status-context'; import { ServerStatusContext } from '../../../utils/server-status-context';
import InfoTip from '../info-tip'; import InfoTip from '../info-tip';
interface ToggleSwitchProps {
apiPath: string;
fieldName: string;
checked?: boolean;
configPath?: string;
disabled?: boolean;
label?: string;
tip?: string;
}
export default function ToggleSwitch(props: ToggleSwitchProps) { export default function ToggleSwitch(props: ToggleSwitchProps) {
const [submitStatus, setSubmitStatus] = useState<FormItemProps['validateStatus']>(''); const [submitStatus, setSubmitStatus] = useState<FormItemProps['validateStatus']>('');
@ -34,7 +43,7 @@ export default function ToggleSwitch(props: ToggleSwitchProps) {
resetTimer = null; resetTimer = null;
} }
const handleChange = async isChecked => { const handleChange = async (isChecked: boolean) => {
setSubmitStatus('validating'); setSubmitStatus('validating');
await postConfigUpdateToAPI({ await postConfigUpdateToAPI({
apiPath, apiPath,
@ -64,6 +73,7 @@ export default function ToggleSwitch(props: ToggleSwitchProps) {
loading={submitStatus === 'validating'} loading={submitStatus === 'validating'}
onChange={handleChange} onChange={handleChange}
defaultChecked={checked} defaultChecked={checked}
checked={checked}
checkedChildren="ON" checkedChildren="ON"
unCheckedChildren="OFF" unCheckedChildren="OFF"
disabled={disabled} disabled={disabled}
@ -77,3 +87,11 @@ export default function ToggleSwitch(props: ToggleSwitchProps) {
</div> </div>
); );
} }
ToggleSwitch.defaultProps = {
checked: false,
configPath: '',
disabled: false,
label: '',
tip: '',
};

View File

@ -1,24 +1,33 @@
// TS types for elements on the Config pages // TS types for elements on the Config pages
export interface TextFieldProps { export interface TextFieldProps {
handleResetValue?: (fieldName: string) => void; apiPath: string;
fieldName: string; fieldName: string;
initialValues?: any;
placeholder?: string;
type?: string;
configPath?: string; configPath?: string;
required?: boolean;
disabled?: boolean; disabled?: boolean;
initialValue?: string;
label?: string;
maxLength?: number;
placeholder?: string;
required?: boolean;
tip?: string;
type?: string;
value?: string;
onSubmit?: () => void; onSubmit?: () => void;
onBlur?: () => void; onBlur?: () => void;
onChange?: () => void; onChange?: () => void;
} }
export interface ToggleSwitchProps { export interface ToggleSwitchProps {
apiPath: string;
fieldName: string; fieldName: string;
initialValues?: any;
checked?: boolean;
configPath?: string; configPath?: string;
disabled?: boolean; disabled?: boolean;
label?: string;
tip?: string;
} }
// for dropdown // for dropdown

View File

@ -1,7 +1,7 @@
// TODO: add a notication after updating info that changes will take place either on a new stream or server restart. may be different for each field. // TODO: add a notication after updating info that changes will take place either on a new stream or server restart. may be different for each field.
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import PropTypes, { any } from 'prop-types'; import PropTypes from 'prop-types';
import { STATUS, fetchData, FETCH_INTERVAL, SERVER_CONFIG } from './apis'; import { STATUS, fetchData, FETCH_INTERVAL, SERVER_CONFIG } from './apis';
import { ConfigDetails, UpdateArgs } from '../types/config-section'; import { ConfigDetails, UpdateArgs } from '../types/config-section';
@ -50,7 +50,7 @@ export const ServerStatusContext = React.createContext({
...initialServerStatusState, ...initialServerStatusState,
serverConfig: initialServerConfigState, serverConfig: initialServerConfigState,
setFieldInConfigState: any, setFieldInConfigState: (args: UpdateArgs) => { return args },
}); });
const ServerStatusProvider = ({ children }) => { const ServerStatusProvider = ({ children }) => {
@ -88,7 +88,6 @@ const ServerStatusProvider = ({ children }) => {
...config, ...config,
[fieldName]: value, [fieldName]: value,
}; };
console.log({updatedConfig, fieldName, value, path})
setConfig(updatedConfig); setConfig(updatedConfig);
}; };