fix(newsfeed): load saved content when reopening the edit modal

Sync modal editor state from the updated feed on open, clear editFeed
on reset, and remount TextEditor so a second edit shows the latest HTML
instead of stale content.
This commit is contained in:
Rotimi Best
2026-06-11 20:32:39 +01:00
parent bbd243d214
commit 89e44d4fab
6 changed files with 116 additions and 31 deletions
@@ -51,12 +51,9 @@
let isDeleteFeedModal = $state(false);
const openEditFeed = () => {
newsfeedApi.openNewFeedModal();
editFeed = feed;
edit = true;
if (edit === true) {
editFeed = feed;
}
newsfeedApi.openNewFeedModal();
};
const handleReact = (reactionType: string) => addNewReaction(reactionType, feed.id, author.id);
@@ -21,7 +21,18 @@
newPost: ''
});
let newPost = $state(edit === true ? editFeed?.content : '');
let newPost = $state('');
$effect(() => {
if (!newsfeedApi.isNewFeedModalOpen) {
newPost = '';
return;
}
if (edit && editFeed) {
newPost = editFeed.content || '';
}
});
const onPost = async () => {
isLoading = true;
@@ -31,9 +42,7 @@
await newsfeedApi.update(courseId, editFeed.id, { content: newPost ?? '' });
if (newsfeedApi.success) {
edit = false;
newPost = '';
newsfeedApi.closeNewFeedModal();
resetEditor();
}
} else {
await newsfeedApi.create(courseId, {
@@ -59,6 +68,7 @@
const resetEditor = () => {
newPost = '';
edit = false;
editFeed = null;
newsfeedApi.closeNewFeedModal();
};
</script>
@@ -81,14 +91,16 @@
</Dialog.Title>
</Dialog.Header>
<section class="max-w-lg">
<TextEditor
content={newPost || editFeed?.content || ''}
onChange={(text) => {
newPost = text;
}}
editorClass="max-h-[400px]"
placeholder={$t('course.navItem.news_feed.heading_button.placeholder')}
/>
{#key edit ? editFeed?.id : 'new'}
<TextEditor
content={newPost}
onChange={(text) => {
newPost = text;
}}
editorClass="max-h-[400px]"
placeholder={$t('course.navItem.news_feed.heading_button.placeholder')}
/>
{/key}
{#if errors.newPost}
<p class="text-sm text-red-500">{errors.newPost}</p>
{/if}
@@ -20,13 +20,13 @@
let newPost = $state('');
$effect(() => {
if (edit && editFeed) {
newPost = editFeed.content || '';
if (!programNewsfeedApi.isNewFeedModalOpen) {
newPost = '';
return;
}
if (!programNewsfeedApi.isNewFeedModalOpen) {
newPost = '';
if (edit && editFeed) {
newPost = editFeed.content || '';
}
});
@@ -84,14 +84,16 @@
</Dialog.Title>
</Dialog.Header>
<section class="max-w-lg">
<TextEditor
content={newPost || editFeed?.content || ''}
onChange={(text) => {
newPost = text;
}}
editorClass="max-h-[400px]"
placeholder={$t('programs.newsfeed.editor_placeholder')}
/>
{#key edit ? editFeed?.id : 'new'}
<TextEditor
content={newPost}
onChange={(text) => {
newPost = text;
}}
editorClass="max-h-[400px]"
placeholder={$t('programs.newsfeed.editor_placeholder')}
/>
{/key}
</section>
<Dialog.Footer>
<Button variant="outline" onclick={resetEditor}>