Skip to content

Commit ec82138

Browse files
committed
Fix #52
Post was initialized every time when user tempsaves the post. Wrong initialization happens when user types something in the middle of tempsave process. Fixed by setting a mutable `initialized` value
1 parent 8de7cb5 commit ec82138

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/containers/write/ActiveEditor.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState, useCallback } from 'react';
1+
import React, { useEffect, useState, useCallback, useRef } from 'react';
22
import { useSelector, useDispatch, shallowEqual } from 'react-redux';
33
import { RootState } from '../../modules';
44
import {
@@ -31,6 +31,7 @@ const ActiveEditor: React.FC<ActiveEditorProps> = () => {
3131
const mode = useSelector((state: RootState) => state.write.mode);
3232
const postId = useSelector((state: RootState) => state.write.postId);
3333
const [askLoadTemp, setAskLoadTemp] = useState(false);
34+
const initialized = useRef(false);
3435

3536
const dispatch = useDispatch();
3637
const location = useLocation();
@@ -75,6 +76,7 @@ const ActiveEditor: React.FC<ActiveEditorProps> = () => {
7576
const post = safe(() => readPostForEdit.data!.post);
7677
useEffect(() => {
7778
if (!post) return;
79+
if (initialized.current) return;
7880
dispatch(
7981
prepareEdit({
8082
id: post.id,
@@ -91,6 +93,7 @@ const ActiveEditor: React.FC<ActiveEditorProps> = () => {
9193
}),
9294
);
9395
dispatch(setInitialBody(post.body));
96+
initialized.current = true;
9497
}, [dispatch, post]);
9598

9699
const lastPostHistory = safe(() => getLastPostHistory.data!.lastPostHistory);
@@ -105,6 +108,7 @@ const ActiveEditor: React.FC<ActiveEditorProps> = () => {
105108
useEffect(() => {
106109
if (!lastPostHistory) return;
107110
if (!post) return;
111+
108112
const equals = shallowEqual(
109113
{
110114
title: lastPostHistory.title,

0 commit comments

Comments
 (0)