From c158532cc1ce35e0ec6bedc3f7142380388970fa Mon Sep 17 00:00:00 2001 From: Untone Date: Sat, 3 Feb 2024 00:52:04 +0300 Subject: [PATCH] draft-remove-fix --- src/components/Views/DraftsView/DraftsView.tsx | 8 +++++--- src/context/editor.tsx | 6 +++--- src/stores/zine/articles.ts | 6 ++++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/components/Views/DraftsView/DraftsView.tsx b/src/components/Views/DraftsView/DraftsView.tsx index 1aed1a8e..16d977ec 100644 --- a/src/components/Views/DraftsView/DraftsView.tsx +++ b/src/components/Views/DraftsView/DraftsView.tsx @@ -22,8 +22,8 @@ export const DraftsView = () => { } } - createEffect(async () => { - if (isSessionLoaded()) await loadDrafts() + createEffect(() => { + if (isSessionLoaded()) loadDrafts() }) const { @@ -32,7 +32,9 @@ export const DraftsView = () => { const handleDraftDelete = async (shout: Shout) => { const result = deleteShout(shout.id) - if (result) await loadDrafts() + if (result) { + setDrafts((ddd) => ddd.filter((d) => d.id !== shout.id)) + } } const handleDraftPublish = (shout: Shout) => { diff --git a/src/context/editor.tsx b/src/context/editor.tsx index c630518d..824d9d78 100644 --- a/src/context/editor.tsx +++ b/src/context/editor.tsx @@ -9,7 +9,7 @@ import { apiClient } from '../graphql/client/core' import { Topic, TopicInput } from '../graphql/schema/core.gen' import { router, useRouter } from '../stores/router' import { slugify } from '../utils/slugify' - +import { addArticles } from '../stores/zine/articles' import { useLocalize } from './localize' import { useSnackbar } from './snackbar' @@ -197,11 +197,11 @@ export const EditorProvider = (props: { children: JSX.Element }) => { const publishShoutById = async (shout_id: number) => { try { - await apiClient.updateArticle({ + const newShout = await apiClient.updateArticle({ shout_id, publish: true, }) - + addArticles([newShout]) openPage(router, 'feed') } catch (error) { console.error('[publishShoutById]', error) diff --git a/src/stores/zine/articles.ts b/src/stores/zine/articles.ts index c126000d..6fdbea71 100644 --- a/src/stores/zine/articles.ts +++ b/src/stores/zine/articles.ts @@ -64,12 +64,14 @@ const topCommentedArticles = createLazyMemo(() => { }) // eslint-disable-next-line sonarjs/cognitive-complexity -const addArticles = (...args: Shout[][]) => { +export const addArticles = (...args: Shout[][]) => { const allArticles = args.flatMap((articles) => articles || []) const newArticleEntities = allArticles.reduce( (acc, article) => { - acc[article.slug] = article + if (!acc[article.slug]) { + acc[article.slug] = article + } return acc }, {} as { [articleSLug: string]: Shout },