2021-01-03 09:26:26 +01:00
|
|
|
import React, { useState, useContext } from 'react';
|
2021-01-03 10:54:04 +01:00
|
|
|
import { Form, Switch } from 'antd';
|
2021-01-03 09:26:26 +01:00
|
|
|
import { FormItemProps } from 'antd/es/form';
|
|
|
|
|
2021-01-03 10:10:38 +01:00
|
|
|
import { TEXTFIELD_DEFAULTS, RESET_TIMEOUT, SUCCESS_STATES, postConfigUpdateToAPI } from './constants';
|
2021-01-03 09:26:26 +01:00
|
|
|
|
|
|
|
import { ToggleSwitchProps } from '../../../types/config-section';
|
|
|
|
import { ServerStatusContext } from '../../../utils/server-status-context';
|
2021-01-03 10:54:04 +01:00
|
|
|
import InfoTip from '../info-tip';
|
2021-01-03 09:26:26 +01:00
|
|
|
|
|
|
|
export const TEXTFIELD_TYPE_TEXT = 'default';
|
|
|
|
export const TEXTFIELD_TYPE_PASSWORD = 'password'; // Input.Password
|
|
|
|
export const TEXTFIELD_TYPE_NUMBER = 'numeric';
|
|
|
|
export const TEXTFIELD_TYPE_TEXTAREA = 'textarea';
|
|
|
|
|
|
|
|
|
|
|
|
export default function ToggleSwitch(props: ToggleSwitchProps) {
|
|
|
|
const [submitStatus, setSubmitStatus] = useState<FormItemProps['validateStatus']>('');
|
|
|
|
const [submitStatusMessage, setSubmitStatusMessage] = useState('');
|
|
|
|
|
|
|
|
let resetTimer = null;
|
|
|
|
|
|
|
|
const serverStatusData = useContext(ServerStatusContext);
|
2021-01-03 10:54:04 +01:00
|
|
|
const { setFieldInConfigState } = serverStatusData || {};
|
2021-01-03 09:26:26 +01:00
|
|
|
|
|
|
|
const {
|
|
|
|
fieldName,
|
|
|
|
initialValues = {},
|
|
|
|
configPath = '',
|
|
|
|
disabled = false,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const initialValue = initialValues[fieldName] || false;
|
|
|
|
|
|
|
|
const defaultDetails = TEXTFIELD_DEFAULTS[configPath] || TEXTFIELD_DEFAULTS;
|
|
|
|
|
|
|
|
const {
|
|
|
|
apiPath = '',
|
|
|
|
label = '',
|
|
|
|
tip = '',
|
|
|
|
} = defaultDetails[fieldName] || {};
|
|
|
|
|
|
|
|
const resetStates = () => {
|
|
|
|
setSubmitStatus('');
|
|
|
|
clearTimeout(resetTimer);
|
|
|
|
resetTimer = null;
|
|
|
|
}
|
|
|
|
|
2021-01-03 10:10:38 +01:00
|
|
|
const handleChange = async checked => {
|
2021-01-03 09:26:26 +01:00
|
|
|
setSubmitStatus('validating');
|
2021-01-03 10:10:38 +01:00
|
|
|
await postConfigUpdateToAPI({
|
|
|
|
apiPath,
|
|
|
|
data: { value: checked },
|
|
|
|
onSuccess: () => {
|
2021-01-03 10:54:04 +01:00
|
|
|
setFieldInConfigState({ fieldName, value: checked, path: configPath });
|
2021-01-03 10:10:38 +01:00
|
|
|
setSubmitStatus('success');
|
|
|
|
},
|
|
|
|
onError: (message: string) => {
|
|
|
|
setSubmitStatus('error');
|
|
|
|
setSubmitStatusMessage(`There was an error: ${message}`);
|
|
|
|
},
|
2021-01-03 09:26:26 +01:00
|
|
|
});
|
|
|
|
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
icon: newStatusIcon = null,
|
|
|
|
message: newStatusMessage = '',
|
|
|
|
} = SUCCESS_STATES[submitStatus] || {};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="toggleswitch-container">
|
|
|
|
<div className="toggleswitch">
|
|
|
|
<Form.Item
|
|
|
|
name={fieldName}
|
|
|
|
validateStatus={submitStatus}
|
|
|
|
>
|
|
|
|
<Switch
|
2021-01-03 10:10:38 +01:00
|
|
|
className={`switch field-${fieldName}`}
|
2021-01-03 09:26:26 +01:00
|
|
|
loading={submitStatus === 'validating'}
|
|
|
|
onChange={handleChange}
|
|
|
|
checked={initialValue}
|
|
|
|
checkedChildren="ON"
|
|
|
|
unCheckedChildren="OFF"
|
|
|
|
disabled={disabled}
|
|
|
|
/>
|
|
|
|
</Form.Item>
|
|
|
|
|
2021-01-03 10:54:04 +01:00
|
|
|
<span className="label">{label} <InfoTip tip={tip} /></span>
|
|
|
|
|
2021-01-03 09:26:26 +01:00
|
|
|
</div>
|
|
|
|
<div className={`status-message ${submitStatus || ''}`}>
|
|
|
|
{newStatusIcon} {newStatusMessage} {submitStatusMessage}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|