Skip to content

Commit 41be6ce

Browse files
committed
feat: persist post data in local storage and remove unused tag handling functions
1 parent 5e3046a commit 41be6ce

4 files changed

Lines changed: 21 additions & 15 deletions

File tree

app/new/page.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,18 @@ export default function NewPostPage() {
5656
[e.target.name]: e.target.value,
5757
}) as TPostAPI,
5858
);
59+
localStorage.setItem("postData", JSON.stringify(data));
5960
}
6061

62+
useEffect(() => {
63+
if (data.title !== "" || data.sig !== "" || data.hashtag?.length !== 0) {
64+
localStorage.setItem("postData", JSON.stringify(data));
65+
}
66+
}, [data]);
67+
6168
useEffect(() => {
6269
const storedContent = localStorage?.getItem("editorContent");
70+
const storedData = localStorage?.getItem("postData");
6371
if (storedContent) {
6472
setPostData(
6573
(prev: TPostAPI | undefined) =>
@@ -69,6 +77,15 @@ export default function NewPostPage() {
6977
}) as TPostAPI,
7078
);
7179
}
80+
if (storedData) {
81+
setPostData(
82+
(prev: TPostAPI | undefined) =>
83+
({
84+
...prev,
85+
...JSON.parse(storedData),
86+
}) as TPostAPI,
87+
);
88+
}
7289
}, []);
7390

7491
async function NewPostAPI() {
@@ -96,6 +113,7 @@ export default function NewPostPage() {
96113
return Swal.fire(alertMessageConfigs.Success).then(() => {
97114
setPostButtonDisable(false);
98115
localStorage.removeItem("editorContent");
116+
localStorage.removeItem("postData");
99117
route.push(`/post/${res.data._id}`);
100118
});
101119
} else if (res.status === 4001) {
@@ -123,6 +141,7 @@ export default function NewPostPage() {
123141
hashtag: [],
124142
});
125143
localStorage.removeItem("editorContent");
144+
localStorage.removeItem("postData");
126145
}
127146

128147
async function handleFileChange(e: ChangeEvent<HTMLInputElement>) {

components/PostEditor/desktop/Editor.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const MdEditorSync = ({ data, setPostData }: Props) => {
8686
}) as TPostAPI,
8787
);
8888
localStorage.setItem("editorContent", newContent);
89+
localStorage.setItem("postData", JSON.stringify(data));
8990
};
9091

9192
return (

components/PostEditor/desktop/PostEditor.tsx

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,6 @@ export default function NewPost({
4848
}
4949
}, [data]);
5050

51-
function handleAddTag(tag: string) {
52-
if (tag === "" || tag.length > 20) return;
53-
if (!hashtag.includes(tag)) {
54-
const newTags = [...hashtag, tag];
55-
setHashtag(newTags);
56-
setPostData((prev) => ({ ...prev, hashtag: newTags }));
57-
}
58-
}
59-
60-
function handleRemoveTag(tag: string) {
61-
const newTags = hashtag.filter((t) => t !== tag);
62-
setHashtag(newTags);
63-
setPostData((prev) => ({ ...prev, hashtag: newTags }));
64-
}
65-
6651
return (
6752
<SplitBlock>
6853
<Suspense fallback={null}>

components/PostEditor/mobile/Editor.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const MdEditorSync = ({ data, setPostData, token }: Props) => {
5252
}) as TPostAPI,
5353
);
5454
localStorage.setItem("editorContent", newContent);
55+
localStorage.setItem("postData", JSON.stringify(data));
5556
};
5657

5758
return (

0 commit comments

Comments
 (0)