2021-02-13 10:55:59 +03:00
|
|
|
// EDIT CUSTOM DETAILS ON YOUR PAGE
|
2022-09-07 10:00:28 +03:00
|
|
|
import React, { useState, useEffect, useContext, FC } from 'react';
|
2021-01-31 12:38:20 +03:00
|
|
|
import { Typography, Button } from 'antd';
|
2021-01-04 10:32:47 +03:00
|
|
|
import dynamic from 'next/dynamic';
|
|
|
|
import MarkdownIt from 'markdown-it';
|
|
|
|
|
2021-02-13 10:55:59 +03:00
|
|
|
import { ServerStatusContext } from '../../utils/server-status-context';
|
2021-01-31 12:38:20 +03:00
|
|
|
import {
|
|
|
|
postConfigUpdateToAPI,
|
|
|
|
RESET_TIMEOUT,
|
|
|
|
API_CUSTOM_CONTENT,
|
2021-02-13 10:55:59 +03:00
|
|
|
} from '../../utils/config-constants';
|
2021-02-01 11:36:27 +03:00
|
|
|
import {
|
|
|
|
createInputStatus,
|
|
|
|
StatusState,
|
|
|
|
STATUS_ERROR,
|
|
|
|
STATUS_PROCESSING,
|
|
|
|
STATUS_SUCCESS,
|
2021-02-13 10:55:59 +03:00
|
|
|
} from '../../utils/input-statuses';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { FormStatusIndicator } from './FormStatusIndicator';
|
2021-01-04 10:32:47 +03:00
|
|
|
|
2021-02-13 10:55:59 +03:00
|
|
|
import 'react-markdown-editor-lite/lib/index.css';
|
2021-01-04 10:32:47 +03:00
|
|
|
|
|
|
|
const mdParser = new MarkdownIt(/* Markdown-it options */);
|
|
|
|
const MdEditor = dynamic(() => import('react-markdown-editor-lite'), {
|
2021-01-31 12:38:20 +03:00
|
|
|
ssr: false,
|
2021-01-04 10:32:47 +03:00
|
|
|
});
|
|
|
|
|
2021-02-13 10:55:59 +03:00
|
|
|
const { Title } = Typography;
|
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export const EditPageContent: FC = () => {
|
2021-01-31 12:38:20 +03:00
|
|
|
const [content, setContent] = useState('');
|
2021-02-01 11:36:27 +03:00
|
|
|
const [submitStatus, setSubmitStatus] = useState<StatusState>(null);
|
2021-01-31 12:38:20 +03:00
|
|
|
const [hasChanged, setHasChanged] = useState(false);
|
|
|
|
|
|
|
|
const serverStatusData = useContext(ServerStatusContext);
|
|
|
|
const { serverConfig, setFieldInConfigState } = serverStatusData || {};
|
|
|
|
|
|
|
|
const { instanceDetails } = serverConfig;
|
|
|
|
const { extraPageContent: initialContent } = instanceDetails;
|
2021-01-04 10:32:47 +03:00
|
|
|
|
2021-01-31 12:38:20 +03:00
|
|
|
let resetTimer = null;
|
|
|
|
|
|
|
|
function handleEditorChange({ text }) {
|
|
|
|
setContent(text);
|
|
|
|
if (text !== initialContent && !hasChanged) {
|
|
|
|
setHasChanged(true);
|
|
|
|
} else if (text === initialContent && hasChanged) {
|
2021-01-04 10:32:47 +03:00
|
|
|
setHasChanged(false);
|
|
|
|
}
|
2021-01-31 12:38:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear out any validation states and messaging
|
|
|
|
const resetStates = () => {
|
2021-02-01 11:36:27 +03:00
|
|
|
setSubmitStatus(null);
|
2021-01-31 12:38:20 +03:00
|
|
|
setHasChanged(false);
|
|
|
|
clearTimeout(resetTimer);
|
|
|
|
resetTimer = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// posts all the tags at once as an array obj
|
|
|
|
async function handleSave() {
|
2021-02-01 11:36:27 +03:00
|
|
|
setSubmitStatus(createInputStatus(STATUS_PROCESSING));
|
2021-01-31 12:38:20 +03:00
|
|
|
await postConfigUpdateToAPI({
|
|
|
|
apiPath: API_CUSTOM_CONTENT,
|
|
|
|
data: { value: content },
|
2021-02-01 11:36:27 +03:00
|
|
|
onSuccess: (message: string) => {
|
2021-01-31 12:38:20 +03:00
|
|
|
setFieldInConfigState({
|
|
|
|
fieldName: 'extraPageContent',
|
|
|
|
value: content,
|
|
|
|
path: 'instanceDetails',
|
|
|
|
});
|
2021-02-01 11:36:27 +03:00
|
|
|
setSubmitStatus(createInputStatus(STATUS_SUCCESS, message));
|
2021-01-31 12:38:20 +03:00
|
|
|
},
|
|
|
|
onError: (message: string) => {
|
2021-02-01 11:36:27 +03:00
|
|
|
setSubmitStatus(createInputStatus(STATUS_ERROR, message));
|
2021-01-31 12:38:20 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setContent(initialContent);
|
|
|
|
}, [instanceDetails]);
|
|
|
|
|
|
|
|
return (
|
2021-02-13 10:55:59 +03:00
|
|
|
<div className="edit-page-content">
|
|
|
|
<Title level={3} className="section-title">
|
|
|
|
Custom Page Content
|
|
|
|
</Title>
|
2021-01-31 12:38:20 +03:00
|
|
|
|
2021-02-13 10:55:59 +03:00
|
|
|
<p className="description">
|
2021-02-07 06:38:58 +03:00
|
|
|
Edit the content of your page by using simple{' '}
|
2021-02-26 23:38:55 +03:00
|
|
|
<a
|
|
|
|
href="https://www.markdownguide.org/basic-syntax/"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
|
|
|
Markdown syntax
|
|
|
|
</a>
|
|
|
|
.
|
2021-01-31 12:38:20 +03:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<MdEditor
|
|
|
|
style={{ height: '30em' }}
|
|
|
|
value={content}
|
|
|
|
renderHTML={(c: string) => mdParser.render(c)}
|
|
|
|
onChange={handleEditorChange}
|
|
|
|
config={{
|
|
|
|
htmlClass: 'markdown-editor-preview-pane',
|
|
|
|
markdownClass: 'markdown-editor-pane',
|
|
|
|
}}
|
|
|
|
/>
|
2021-02-14 12:30:42 +03:00
|
|
|
<br />
|
2021-01-31 12:38:20 +03:00
|
|
|
<div className="page-content-actions">
|
2021-02-14 12:30:42 +03:00
|
|
|
{hasChanged && (
|
2021-01-31 12:38:20 +03:00
|
|
|
<Button type="primary" onClick={handleSave}>
|
|
|
|
Save
|
|
|
|
</Button>
|
2021-02-14 12:30:42 +03:00
|
|
|
)}
|
2021-02-01 11:36:27 +03:00
|
|
|
<FormStatusIndicator status={submitStatus} />
|
2021-01-04 10:32:47 +03:00
|
|
|
</div>
|
2021-01-31 12:38:20 +03:00
|
|
|
</div>
|
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|