@@ -45,12 +45,14 @@ export default function Index({ device, deviceOs }: IFuncProps) { const { error, showError } = useShowData() const { data } = useSession() const dispatch = useDispatch() + const [isGenerateStart, setIsGenerateStart] = useState(false) const settingsHandler = (value: boolean) => { setIsSettings(value) } const startGenerateHandler = () => { + setIsGenerateStart(true) if (data?.access && query['uuid'] !== undefined) { const uuid = query['uuid'] const req_body = { @@ -58,6 +60,7 @@ export default function Index({ device, deviceOs }: IFuncProps) { } editorEndpoints.UpdateCopywriteContent(uuid, req_body, data?.access).then(() => { CopyEndpoints.GenerateResponse(uuid, data?.access).then(() => { + setIsGenerateStart(false) addQueryParams('resp') }) }) @@ -152,7 +155,13 @@ export default function Index({ device, deviceOs }: IFuncProps) { {copywrite && ( - + )} @@ -4,6 +4,7 @@ import { Box } from '@mui/material' import { EditorState } from 'draft-js' import { stateFromHTML } from 'draft-js-import-html' import dynamic from 'next/dynamic' +import { useRouter } from 'next/router' import { useSession } from 'next-auth/react' import { setTextInputContent } from '@/src/features/use-copy/copy-store' @@ -19,15 +20,17 @@ interface IProps { id: string contentState: string | null saveEditorValue: (content: string) => void + isGenerateStart: boolean } -export const EditorWrap: React.FC = ({ theme, id, contentState, saveEditorValue }) => { +export const EditorWrap: React.FC = ({ theme, id, contentState, saveEditorValue, isGenerateStart }) => { const Editor = dynamic(() => import('react-draft-wysiwyg').then((res) => res.Editor), { ssr: false }) const [editorState, setEditorState] = useState(() => { if (contentState) return EditorState.createWithContent(stateFromHTML(contentState)) if (!contentState) return EditorState.createEmpty() }) const dispatch = useDispatch() + const [test, setTest] = useState() useEffect(() => { if (editorState) { @@ -36,21 +39,26 @@ export const EditorWrap: React.FC = ({ theme, id, contentState, saveEdit const timeout = setTimeout(() => { dispatch(setTextInputContent(htmlText)) }, 250) - return () => clearTimeout(timeout) + + const saveTimeout = setTimeout(() => { + saveEditorValue(htmlText) + }, 1000) + + return () => { + clearTimeout(timeout) + clearTimeout(saveTimeout) + } } }, [editorState]) - useEffect(() => {}, [editorState]) - - useBeforeUnload(() => { - if (editorState) { + useEffect(() => { + if (isGenerateStart && editorState) { saveEditorValue(getHtmlText(editorState)) } - }) + }, [isGenerateStart]) + useChangeRouter(() => { - if (editorState) { - saveEditorValue(getHtmlText(editorState)) - } + if (editorState) saveEditorValue(getHtmlText(editorState)) }) return (