2021-07-25 14:52:45 +02:00
|
|
|
// @flow
|
2022-02-04 03:56:44 +01:00
|
|
|
import { COMMENT_SERVER_API, COMMENT_SERVER_NAME } from 'config';
|
2021-07-25 14:52:45 +02:00
|
|
|
import React from 'react';
|
|
|
|
import Comments from 'comments';
|
2022-02-03 16:13:54 +01:00
|
|
|
import ItemInputRow from './internal/input-radio-panel-addCommentServer';
|
2022-02-02 07:19:39 +01:00
|
|
|
import Button from 'component/button';
|
2022-02-04 03:56:44 +01:00
|
|
|
import { FormField } from 'component/common/form-components/form-field';
|
2021-07-25 14:52:45 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
customServerEnabled: boolean,
|
|
|
|
customServerUrl: string,
|
|
|
|
setCustomServerEnabled: (boolean) => void,
|
|
|
|
setCustomServerUrl: (string) => void,
|
2022-02-02 07:19:39 +01:00
|
|
|
setCustomServers: (Array<CommentServerDetails>) => void,
|
|
|
|
customCommentServers: Array<CommentServerDetails>,
|
2021-07-25 14:52:45 +02:00
|
|
|
};
|
2022-02-04 03:56:44 +01:00
|
|
|
const defaultServer = { name: COMMENT_SERVER_NAME, url: COMMENT_SERVER_API };
|
2021-07-25 14:52:45 +02:00
|
|
|
function SettingCommentsServer(props: Props) {
|
2022-02-02 07:19:39 +01:00
|
|
|
const {
|
|
|
|
customServerEnabled,
|
|
|
|
customServerUrl,
|
|
|
|
setCustomServerEnabled,
|
|
|
|
setCustomServerUrl,
|
|
|
|
customCommentServers,
|
|
|
|
setCustomServers,
|
|
|
|
} = props;
|
|
|
|
const [addServer, setAddServer] = React.useState(false);
|
2021-07-25 14:52:45 +02:00
|
|
|
|
2022-02-02 07:19:39 +01:00
|
|
|
const customServersString = JSON.stringify(customCommentServers);
|
|
|
|
|
|
|
|
// "migrate" to make sure any currently set custom server is in saved list
|
2021-07-25 14:52:45 +02:00
|
|
|
React.useEffect(() => {
|
2022-02-02 07:19:39 +01:00
|
|
|
// const servers = JSON.parse(customServersString);
|
|
|
|
// if customServerUrl is not in servers, make sure it is.
|
|
|
|
}, [customServerUrl, customServersString, setCustomServers]);
|
|
|
|
|
|
|
|
const handleSelectServer = (serverItem: CommentServerDetails) => {
|
|
|
|
if (serverItem.url !== COMMENT_SERVER_API) {
|
|
|
|
Comments.setServerUrl(serverItem.url);
|
|
|
|
setCustomServerUrl(serverItem.url);
|
|
|
|
setCustomServerEnabled(true);
|
|
|
|
} else {
|
|
|
|
Comments.setServerUrl(undefined);
|
|
|
|
setCustomServerEnabled(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleAddServer = (serverItem: CommentServerDetails) => {
|
|
|
|
const newCustomServers = customCommentServers.slice();
|
|
|
|
newCustomServers.push(serverItem);
|
|
|
|
setCustomServers(newCustomServers);
|
|
|
|
handleSelectServer(serverItem);
|
2022-02-02 22:49:18 +01:00
|
|
|
setAddServer(false);
|
2022-02-02 07:19:39 +01:00
|
|
|
};
|
|
|
|
|
2022-02-04 03:56:44 +01:00
|
|
|
const handleRemoveServer = (serverItem: CommentServerDetails) => {
|
2022-02-02 07:19:39 +01:00
|
|
|
handleSelectServer(defaultServer);
|
|
|
|
const newCustomServers = customCommentServers.slice().filter((server) => {
|
|
|
|
return server.url !== serverItem.url;
|
|
|
|
});
|
|
|
|
setCustomServers(newCustomServers);
|
|
|
|
};
|
2021-07-25 14:52:45 +02:00
|
|
|
|
2022-02-04 03:56:44 +01:00
|
|
|
const commentServerLabel = (serverDetails, onRemove) => (
|
|
|
|
<span className={'compound-label'}>
|
|
|
|
<span>{serverDetails.name}</span>
|
|
|
|
<span className="compound-label__details">{serverDetails.url}</span>
|
|
|
|
{onRemove && <Button button={'inverse'} label={__('Remove')} onClick={() => onRemove(serverDetails)} />}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
const commentServerRadio = (customServerEnabled, customServerUrl, onClick, onRemove, serverDetails) => (
|
|
|
|
<FormField
|
|
|
|
type="radio"
|
|
|
|
checked={(!customServerEnabled && !onRemove) || (customServerEnabled && customServerUrl === serverDetails.url)}
|
|
|
|
onClick={() => onClick(serverDetails)}
|
|
|
|
label={commentServerLabel(serverDetails, onRemove)}
|
|
|
|
name={`${serverDetails.name}${serverDetails.url}`}
|
|
|
|
key={`${serverDetails.name}${serverDetails.url}`}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2021-07-25 14:52:45 +02:00
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2022-02-02 07:19:39 +01:00
|
|
|
<div className={'fieldset-section'}>
|
2022-02-04 03:56:44 +01:00
|
|
|
{commentServerRadio(customServerEnabled, customServerUrl, handleSelectServer, null, defaultServer)}
|
|
|
|
{customCommentServers.map((server) =>
|
|
|
|
commentServerRadio(customServerEnabled, customServerUrl, handleSelectServer, handleRemoveServer, server)
|
|
|
|
)}
|
2022-02-02 07:19:39 +01:00
|
|
|
</div>
|
|
|
|
<div className={'fieldset-section'}>
|
|
|
|
{!addServer && (
|
|
|
|
<div className="section__actions">
|
2022-02-04 03:56:44 +01:00
|
|
|
<Button type="button" button="inverse" onClick={() => setAddServer(true)} label={__('Add A Server')} />
|
2021-07-25 14:52:45 +02:00
|
|
|
</div>
|
|
|
|
)}
|
2022-02-02 07:19:39 +01:00
|
|
|
{addServer && <ItemInputRow update={handleAddServer} onCancel={setAddServer} />}
|
|
|
|
</div>
|
2021-07-25 14:52:45 +02:00
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SettingCommentsServer;
|