Skip to content

Commit

Permalink
Merge pull request #939 from ita-social-projects/news-text-limit-add
Browse files Browse the repository at this point in the history
added limit for news text
  • Loading branch information
MementoMorj authored Oct 30, 2023
2 parents c38dc45 + c4335d0 commit 13a8024
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/features/AdminPage/NewsPage/NewsModal/NewsModal.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ const NewsModal: React.FC<{
open: boolean;
setIsModalOpen: React.Dispatch<React.SetStateAction<boolean>>;
afterSubmit?: (news: News) => void;
}> = observer(({ newsItem, open, setIsModalOpen, afterSubmit }) => {
initialValue: any;
limit: any;
}> = observer(({ newsItem, open, setIsModalOpen, afterSubmit, initialValue, limit }) => {
const [form] = Form.useForm();
const { newsStore } = useMobx();
const [previewOpen, setPreviewOpen] = useState(false);
Expand All @@ -46,6 +48,9 @@ const NewsModal: React.FC<{
const [textIsChanged, setTextIsChanged] = useState<boolean>(false);
const imageId = useRef<number | undefined>(0);
const editorRef = useRef<TinyMCEEditor>();
const sizeLimit = limit ?? 15000;
const [data, setData] = React.useState(initialValue ?? "");
const [count, setCount] = React.useState(0);

const handlePreview = async (file: UploadFile) => {
setFilePreview(file);
Expand Down Expand Up @@ -204,6 +209,25 @@ const NewsModal: React.FC<{
}
};

const handleInit = (value: any, editor: any) => {
setCount(editor.getContent({ format: "text" }).length);
};

const handleUpdate = (value: any, editor: any) => {
const cCount = editor.getContent({ format: "text" }).length;
if (cCount <= sizeLimit) {
setData(value);
setCount(cCount);
}
};

const handleBeforeAddUndo = (evt: any, editor: any) => {
const cCount = editor.getContent({ format: "text" }).length;
if (cCount > sizeLimit) {
evt.preventDefault();
}
};

return (
<div>
<ConfigProvider locale={ukUA}>
Expand Down Expand Up @@ -262,8 +286,7 @@ const NewsModal: React.FC<{
}
return Promise.reject(new Error('Посилання вже існує'));
},
},
]}
}, ]}
>
<Input maxLength={200} showCount />
</Form.Item>
Expand All @@ -273,9 +296,12 @@ const NewsModal: React.FC<{
<span>Текст:</span>
</div>
<Editor
onEditorChange={handleTextChange}
onEditorChange={handleUpdate}
value={data}
onBeforeAddUndo={handleBeforeAddUndo}
onInit={(evt, editor) => {
editorRef.current = editor;
handleInit;
}}
initialValue={newsItem ? newsItem.text : ''}
init={{
Expand All @@ -297,6 +323,7 @@ const NewsModal: React.FC<{
'body { font-family:Roboto,Helvetica Neue,sans-serif; font-size:14px }',
}}
/>
<p>Remaining: {sizeLimit - count}</p>
{!textIsPresent && textIsChanged && (
<p className="form-text">Введіть текст</p>
)}
Expand Down

0 comments on commit 13a8024

Please sign in to comment.