owncast/web/components/config/form-toggleswitch-with-submit.tsx

117 lines
3.0 KiB
TypeScript
Raw Normal View History

// This is a wrapper for the Ant Switch component.
// onChange of the switch, it will automatically post a change to the config api.
import React, { useState, useContext } from 'react';
import { Switch } from 'antd';
2021-02-01 07:07:00 +01:00
import {
createInputStatus,
StatusState,
STATUS_ERROR,
STATUS_PROCESSING,
STATUS_SUCCESS,
} from '../../utils/input-statuses';
import FormStatusIndicator from './form-status-indicator';
import { RESET_TIMEOUT, postConfigUpdateToAPI } from '../../utils/config-constants';
import { ServerStatusContext } from '../../utils/server-status-context';
2021-01-29 19:26:55 +01:00
interface ToggleSwitchProps {
apiPath: string;
fieldName: string;
checked?: boolean;
configPath?: string;
disabled?: boolean;
label?: string;
tip?: string;
useSubmit?: boolean;
onChange?: (arg: boolean) => void;
2021-01-29 19:26:55 +01:00
}
export default function ToggleSwitch(props: ToggleSwitchProps) {
2021-02-01 07:07:00 +01:00
const [submitStatus, setSubmitStatus] = useState<StatusState>(null);
let resetTimer = null;
const serverStatusData = useContext(ServerStatusContext);
2021-01-03 10:54:04 +01:00
const { setFieldInConfigState } = serverStatusData || {};
2021-01-31 10:38:20 +01:00
const {
apiPath,
checked,
configPath = '',
disabled = false,
fieldName,
label,
tip,
useSubmit,
onChange,
} = props;
const resetStates = () => {
2021-02-01 07:07:00 +01:00
setSubmitStatus(null);
clearTimeout(resetTimer);
resetTimer = null;
2021-01-31 10:38:20 +01:00
};
2021-01-29 19:26:55 +01:00
const handleChange = async (isChecked: boolean) => {
if (useSubmit) {
setSubmitStatus(createInputStatus(STATUS_PROCESSING));
2021-02-01 07:07:00 +01:00
await postConfigUpdateToAPI({
apiPath,
data: { value: isChecked },
onSuccess: () => {
setFieldInConfigState({ fieldName, value: isChecked, path: configPath });
setSubmitStatus(createInputStatus(STATUS_SUCCESS));
},
onError: (message: string) => {
setSubmitStatus(createInputStatus(STATUS_ERROR, `There was an error: ${message}`));
},
});
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
}
if (onChange) {
onChange(isChecked);
}
2021-01-31 10:38:20 +01:00
};
2021-02-01 07:07:00 +01:00
const loading = submitStatus !== null && submitStatus.type === STATUS_PROCESSING;
return (
<div className="formfield-container toggleswitch-container">
{label && (
<div className="label-side">
<span className="formfield-label">{label}</span>
</div>
)}
<div className="input-side">
<div className="input-group">
<Switch
className={`switch field-${fieldName}`}
loading={loading}
onChange={handleChange}
defaultChecked={checked}
checked={checked}
checkedChildren="ON"
unCheckedChildren="OFF"
disabled={disabled}
/>
<FormStatusIndicator status={submitStatus} />
</div>
<p className="field-tip">{tip}</p>
</div>
</div>
2021-01-31 10:38:20 +01:00
);
}
2021-01-29 19:26:55 +01:00
ToggleSwitch.defaultProps = {
checked: false,
configPath: '',
disabled: false,
label: '',
tip: '',
useSubmit: false,
onChange: null,
2021-01-29 19:26:55 +01:00
};