2021-02-13 10:55:59 +03:00
|
|
|
// This is a wrapper for the Ant Switch component.
|
2021-02-14 12:45:08 +03:00
|
|
|
// This one is styled to match the form-textfield component.
|
|
|
|
// If `useSubmit` is true then it will automatically post to the config API onChange.
|
2021-02-13 10:55:59 +03:00
|
|
|
|
2021-01-03 11:26:26 +03:00
|
|
|
import React, { useState, useContext } from 'react';
|
2021-01-28 14:08:57 +03:00
|
|
|
import { Switch } from 'antd';
|
2021-02-01 09:07:00 +03:00
|
|
|
import {
|
|
|
|
createInputStatus,
|
|
|
|
StatusState,
|
|
|
|
STATUS_ERROR,
|
|
|
|
STATUS_PROCESSING,
|
|
|
|
STATUS_SUCCESS,
|
2021-02-07 06:38:58 +03:00
|
|
|
} from '../../utils/input-statuses';
|
2021-02-01 11:36:27 +03:00
|
|
|
import FormStatusIndicator from './form-status-indicator';
|
2021-01-03 11:26:26 +03:00
|
|
|
|
2021-02-07 06:38:58 +03:00
|
|
|
import { RESET_TIMEOUT, postConfigUpdateToAPI } from '../../utils/config-constants';
|
2021-01-03 11:26:26 +03:00
|
|
|
|
2021-02-07 06:38:58 +03:00
|
|
|
import { ServerStatusContext } from '../../utils/server-status-context';
|
2021-01-03 11:26:26 +03:00
|
|
|
|
2021-01-29 21:26:55 +03:00
|
|
|
interface ToggleSwitchProps {
|
|
|
|
fieldName: string;
|
|
|
|
|
2021-02-14 12:45:08 +03:00
|
|
|
apiPath?: string;
|
2021-01-29 21:26:55 +03:00
|
|
|
checked?: boolean;
|
|
|
|
configPath?: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
label?: string;
|
|
|
|
tip?: string;
|
2021-02-13 10:55:59 +03:00
|
|
|
useSubmit?: boolean;
|
|
|
|
onChange?: (arg: boolean) => void;
|
2021-01-29 21:26:55 +03:00
|
|
|
}
|
2021-01-03 11:26:26 +03:00
|
|
|
export default function ToggleSwitch(props: ToggleSwitchProps) {
|
2021-02-01 09:07:00 +03:00
|
|
|
const [submitStatus, setSubmitStatus] = useState<StatusState>(null);
|
2021-01-03 11:26:26 +03:00
|
|
|
|
|
|
|
let resetTimer = null;
|
|
|
|
|
|
|
|
const serverStatusData = useContext(ServerStatusContext);
|
2021-01-03 12:54:04 +03:00
|
|
|
const { setFieldInConfigState } = serverStatusData || {};
|
2021-01-31 12:38:20 +03:00
|
|
|
|
2021-02-13 10:55:59 +03:00
|
|
|
const {
|
|
|
|
apiPath,
|
|
|
|
checked,
|
|
|
|
configPath = '',
|
|
|
|
disabled = false,
|
|
|
|
fieldName,
|
|
|
|
label,
|
|
|
|
tip,
|
|
|
|
useSubmit,
|
|
|
|
onChange,
|
|
|
|
} = props;
|
2021-01-03 11:26:26 +03:00
|
|
|
|
|
|
|
const resetStates = () => {
|
2021-02-01 09:07:00 +03:00
|
|
|
setSubmitStatus(null);
|
2021-01-03 11:26:26 +03:00
|
|
|
clearTimeout(resetTimer);
|
|
|
|
resetTimer = null;
|
2021-01-31 12:38:20 +03:00
|
|
|
};
|
2021-01-03 11:26:26 +03:00
|
|
|
|
2021-01-29 21:26:55 +03:00
|
|
|
const handleChange = async (isChecked: boolean) => {
|
2021-02-13 10:55:59 +03:00
|
|
|
if (useSubmit) {
|
|
|
|
setSubmitStatus(createInputStatus(STATUS_PROCESSING));
|
2021-02-01 09:07:00 +03:00
|
|
|
|
2021-02-13 10:55:59 +03: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 12:38:20 +03:00
|
|
|
};
|
2021-01-03 11:26:26 +03:00
|
|
|
|
2021-02-01 09:07:00 +03:00
|
|
|
const loading = submitStatus !== null && submitStatus.type === STATUS_PROCESSING;
|
2021-01-03 11:26:26 +03:00
|
|
|
return (
|
2021-02-13 10:55:59 +03:00
|
|
|
<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>
|
2021-01-03 11:26:26 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-01-31 12:38:20 +03:00
|
|
|
);
|
2021-01-03 11:26:26 +03:00
|
|
|
}
|
2021-01-29 21:26:55 +03:00
|
|
|
|
|
|
|
ToggleSwitch.defaultProps = {
|
2021-02-14 12:45:08 +03:00
|
|
|
apiPath: '',
|
2021-01-29 21:26:55 +03:00
|
|
|
checked: false,
|
|
|
|
configPath: '',
|
|
|
|
disabled: false,
|
|
|
|
label: '',
|
|
|
|
tip: '',
|
2021-02-13 10:55:59 +03:00
|
|
|
useSubmit: false,
|
|
|
|
onChange: null,
|
2021-01-29 21:26:55 +03:00
|
|
|
};
|