From 41e40ada9b63a18d05e25c6b849f2895389149e5 Mon Sep 17 00:00:00 2001 From: ilya-bkv Date: Thu, 7 Mar 2024 10:20:50 +0300 Subject: [PATCH] Comment delete message --- public/locales/en/translation.json | 3 ++- public/locales/ru/translation.json | 3 ++- src/components/Article/Comment/Comment.tsx | 13 +++++++++---- src/context/reactions.tsx | 16 ++++++++-------- src/graphql/client/core.ts | 3 +-- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index 25a18e64..b021d2ce 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -525,5 +525,6 @@ "video": "video", "view": "view", "viewsWithCount": "{count} {count, plural, one {view} other {views}}", - "yesterday": "yesterday" + "yesterday": "yesterday", + "Failed to delete comment": "Failed to delete comment" } diff --git a/public/locales/ru/translation.json b/public/locales/ru/translation.json index 5ad79f28..33643991 100644 --- a/public/locales/ru/translation.json +++ b/public/locales/ru/translation.json @@ -552,5 +552,6 @@ "video": "видео", "view": "просмотр", "viewsWithCount": "{count} {count, plural, one {просмотр} few {просмотрa} other {просмотров}}", - "yesterday": "вчера" + "yesterday": "вчера", + "Failed to delete comment": "Не удалось удалить комментарий" } diff --git a/src/components/Article/Comment/Comment.tsx b/src/components/Article/Comment/Comment.tsx index a0fee501..d66e1767 100644 --- a/src/components/Article/Comment/Comment.tsx +++ b/src/components/Article/Comment/Comment.tsx @@ -64,14 +64,19 @@ export const Comment = (props: Props) => { }) if (isConfirmed) { - await deleteReaction(props.comment.id) - // TODO: Учесть то что deleteReaction может вернуть error - if (props.onDelete) { + const { error } = await deleteReaction(props.comment.id) + const notificationType = error ? 'error' : 'success' + const notificationMessage = error + ? t('Failed to delete comment') + : t('Comment successfully deleted') + await showSnackbar({ type: notificationType, body: notificationMessage }) + + if (!error && props.onDelete) { props.onDelete(props.comment.id) } - await showSnackbar({ body: t('Comment successfully deleted') }) } } catch (error) { + await showSnackbar({ body: 'error' }) console.error('[deleteReaction]', error) } } diff --git a/src/context/reactions.tsx b/src/context/reactions.tsx index 6e0e6756..2adbe2ef 100644 --- a/src/context/reactions.tsx +++ b/src/context/reactions.tsx @@ -21,7 +21,7 @@ type ReactionsContextType = { }) => Promise createReaction: (reaction: ReactionInput) => Promise updateReaction: (reaction: ReactionInput) => Promise - deleteReaction: (id: number) => Promise + deleteReaction: (id: number) => Promise<{ error: string }> } const ReactionsContext = createContext() @@ -84,15 +84,15 @@ export const ReactionsProvider = (props: { children: JSX.Element }) => { setReactionEntities(changes) } - const deleteReaction = async (reaction_id: number): Promise => { + const deleteReaction = async (reaction_id: number): Promise<{ error: string; reaction?: string }> => { if (reaction_id) { - const { error } = await apiClient.destroyReaction(reaction_id) - if (error) { - await showSnackbar({ type: 'error', body: t(error) }) + const result = await apiClient.destroyReaction(reaction_id) + if (!result.error) { + setReactionEntities({ + [reaction_id]: undefined, + }) } - setReactionEntities({ - [reaction_id]: undefined, - }) + return result } } diff --git a/src/graphql/client/core.ts b/src/graphql/client/core.ts index a45d1345..4cbe81c2 100644 --- a/src/graphql/client/core.ts +++ b/src/graphql/client/core.ts @@ -135,7 +135,6 @@ export const apiClient = { user?: string }): Promise => { const response = await publicGraphQLClient.query(authorFollows, params).toPromise() - console.log('!!! response:', response) return response.data.get_author_follows }, @@ -188,7 +187,7 @@ export const apiClient = { destroyReaction: async (reaction_id: number) => { const response = await apiClient.private.mutation(reactionDestroy, { reaction_id }).toPromise() console.debug('[graphql.client.core] destroyReaction:', response) - return response.data.delete_reaction.reaction + return response.data.delete_reaction }, updateReaction: async (reaction: ReactionInput) => { const response = await apiClient.private.mutation(reactionUpdate, { reaction }).toPromise()