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