2021-06-22 06:19:00 +03:00
|
|
|
import { Typography } from 'antd';
|
2021-07-09 21:42:01 +03:00
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
|
|
|
import { TEXTFIELD_TYPE_TEXTAREA } from '../components/config/form-textfield';
|
|
|
|
import TextFieldWithSubmit from '../components/config/form-textfield-with-submit';
|
|
|
|
import ToggleSwitch from '../components/config/form-toggleswitch';
|
2021-07-26 08:12:50 +03:00
|
|
|
import EditValueArray from '../components/config/edit-string-array';
|
|
|
|
import { createInputStatus, STATUS_ERROR, STATUS_SUCCESS } from '../utils/input-statuses';
|
|
|
|
|
2021-07-09 21:42:01 +03:00
|
|
|
import { UpdateArgs } from '../types/config-section';
|
2021-06-22 06:19:00 +03:00
|
|
|
import {
|
|
|
|
FIELD_PROPS_DISABLE_CHAT,
|
2021-07-20 08:02:02 +03:00
|
|
|
TEXTFIELD_PROPS_CHAT_FORBIDDEN_USERNAMES,
|
|
|
|
TEXTFIELD_PROPS_SERVER_WELCOME_MESSAGE,
|
2021-07-26 08:12:50 +03:00
|
|
|
API_CHAT_FORBIDDEN_USERNAMES,
|
|
|
|
postConfigUpdateToAPI,
|
|
|
|
RESET_TIMEOUT,
|
2021-06-22 06:19:00 +03:00
|
|
|
} from '../utils/config-constants';
|
|
|
|
import { ServerStatusContext } from '../utils/server-status-context';
|
|
|
|
|
|
|
|
export default function ConfigChat() {
|
|
|
|
const { Title } = Typography;
|
|
|
|
const [formDataValues, setFormDataValues] = useState(null);
|
2021-07-26 08:12:50 +03:00
|
|
|
const [forbiddenUsernameSaveState, setForbidenUsernameSaveState] = useState(null);
|
2021-06-22 06:19:00 +03:00
|
|
|
const serverStatusData = useContext(ServerStatusContext);
|
2021-07-26 08:12:50 +03:00
|
|
|
const { serverConfig, setFieldInConfigState } = serverStatusData || {};
|
2021-06-22 06:19:00 +03:00
|
|
|
|
2021-07-20 08:02:02 +03:00
|
|
|
const { chatDisabled, forbiddenUsernames } = serverConfig;
|
|
|
|
const { instanceDetails } = serverConfig;
|
|
|
|
const { welcomeMessage } = instanceDetails;
|
2021-06-22 06:19:00 +03:00
|
|
|
|
2021-07-09 21:42:01 +03:00
|
|
|
const handleFieldChange = ({ fieldName, value }: UpdateArgs) => {
|
|
|
|
setFormDataValues({
|
|
|
|
...formDataValues,
|
|
|
|
[fieldName]: value,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-22 06:19:00 +03:00
|
|
|
function handleChatDisableChange(disabled: boolean) {
|
2021-09-11 01:08:15 +03:00
|
|
|
handleFieldChange({ fieldName: 'chatDisabled', value: !disabled });
|
2021-06-22 06:19:00 +03:00
|
|
|
}
|
|
|
|
|
2021-07-26 08:12:50 +03:00
|
|
|
function resetForbiddenUsernameState() {
|
|
|
|
setForbidenUsernameSaveState(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveForbiddenUsernames() {
|
|
|
|
postConfigUpdateToAPI({
|
|
|
|
apiPath: API_CHAT_FORBIDDEN_USERNAMES,
|
|
|
|
data: { value: formDataValues.forbiddenUsernames },
|
|
|
|
onSuccess: () => {
|
|
|
|
setFieldInConfigState({
|
|
|
|
fieldName: 'forbiddenUsernames',
|
|
|
|
value: formDataValues.forbiddenUsernames,
|
|
|
|
});
|
|
|
|
setForbidenUsernameSaveState(STATUS_SUCCESS);
|
|
|
|
setTimeout(resetForbiddenUsernameState, RESET_TIMEOUT);
|
|
|
|
},
|
|
|
|
onError: (message: string) => {
|
|
|
|
setForbidenUsernameSaveState(createInputStatus(STATUS_ERROR, message));
|
|
|
|
setTimeout(resetForbiddenUsernameState, RESET_TIMEOUT);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleDeleteUsername(index: number) {
|
|
|
|
formDataValues.forbiddenUsernames.splice(index, 1);
|
|
|
|
saveForbiddenUsernames();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleCreateUsername(tag: string) {
|
|
|
|
formDataValues.forbiddenUsernames.push(tag);
|
|
|
|
handleFieldChange({
|
|
|
|
fieldName: 'forbiddenUsernames',
|
|
|
|
value: formDataValues.forbiddenUsernames,
|
|
|
|
});
|
|
|
|
saveForbiddenUsernames();
|
2021-06-22 06:19:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setFormDataValues({
|
|
|
|
chatDisabled,
|
2021-07-20 08:02:02 +03:00
|
|
|
forbiddenUsernames,
|
|
|
|
welcomeMessage,
|
2021-06-22 06:19:00 +03:00
|
|
|
});
|
|
|
|
}, [serverConfig]);
|
|
|
|
|
|
|
|
if (!formDataValues) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="config-server-details-form">
|
|
|
|
<Title>Chat Settings</Title>
|
|
|
|
<div className="form-module config-server-details-container">
|
|
|
|
<ToggleSwitch
|
|
|
|
fieldName="chatDisabled"
|
|
|
|
{...FIELD_PROPS_DISABLE_CHAT}
|
2021-09-11 01:08:15 +03:00
|
|
|
checked={!formDataValues.chatDisabled}
|
|
|
|
reversed
|
2021-06-22 06:19:00 +03:00
|
|
|
onChange={handleChatDisableChange}
|
|
|
|
/>
|
2021-07-20 08:02:02 +03:00
|
|
|
<TextFieldWithSubmit
|
|
|
|
fieldName="welcomeMessage"
|
|
|
|
{...TEXTFIELD_PROPS_SERVER_WELCOME_MESSAGE}
|
|
|
|
type={TEXTFIELD_TYPE_TEXTAREA}
|
|
|
|
value={formDataValues.welcomeMessage}
|
2021-07-21 21:26:19 +03:00
|
|
|
initialValue={welcomeMessage}
|
2021-07-20 08:02:02 +03:00
|
|
|
onChange={handleFieldChange}
|
2021-06-22 06:19:00 +03:00
|
|
|
/>
|
2021-07-26 08:12:50 +03:00
|
|
|
<br />
|
|
|
|
<br />
|
|
|
|
<EditValueArray
|
|
|
|
title={TEXTFIELD_PROPS_CHAT_FORBIDDEN_USERNAMES.label}
|
|
|
|
placeholder={TEXTFIELD_PROPS_CHAT_FORBIDDEN_USERNAMES.placeholder}
|
|
|
|
description={TEXTFIELD_PROPS_CHAT_FORBIDDEN_USERNAMES.tip}
|
|
|
|
values={formDataValues.forbiddenUsernames}
|
|
|
|
handleDeleteIndex={handleDeleteUsername}
|
|
|
|
handleCreateString={handleCreateUsername}
|
|
|
|
submitStatus={createInputStatus(forbiddenUsernameSaveState)}
|
|
|
|
/>
|
2021-06-22 06:19:00 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|