From 9e500f240a76bd0e16c8c63b764dd81c01f46f78 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 31 Jul 2020 00:14:32 +0800 Subject: Update post list when create or delete post. --- Timeline/ClientApp/src/app/data/timeline.ts | 23 ++++++++++- .../src/app/timeline/TimelinePageTemplate.tsx | 44 +++++----------------- .../src/app/timeline/TimelinePageTemplateUI.tsx | 32 ++++++++++++---- 3 files changed, 55 insertions(+), 44 deletions(-) (limited to 'Timeline/ClientApp/src') diff --git a/Timeline/ClientApp/src/app/data/timeline.ts b/Timeline/ClientApp/src/app/data/timeline.ts index 88a13381..84eb3764 100644 --- a/Timeline/ClientApp/src/app/data/timeline.ts +++ b/Timeline/ClientApp/src/app/data/timeline.ts @@ -450,14 +450,33 @@ export class TimelineService { ): Observable { const user = checkLogin(); return from( - getHttpTimelineClient().postPost(timelineName, request, user.token) + getHttpTimelineClient() + .postPost(timelineName, request, user.token) + .then((res) => { + this._postListSubscriptionHub.update(timelineName, (_, old) => { + return Promise.resolve({ + ...old, + posts: [...old.posts, { ...res, timelineName }], + }); + }); + return res; + }) ).pipe(map((post) => ({ ...post, timelineName }))); } deletePost(timelineName: string, postId: number): Observable { const user = checkLogin(); return from( - getHttpTimelineClient().deletePost(timelineName, postId, user.token) + getHttpTimelineClient() + .deletePost(timelineName, postId, user.token) + .then(() => { + this._postListSubscriptionHub.update(timelineName, (_, old) => { + return Promise.resolve({ + ...old, + posts: old.posts.filter((post) => post.id != postId), + }); + }); + }) ); } diff --git a/Timeline/ClientApp/src/app/timeline/TimelinePageTemplate.tsx b/Timeline/ClientApp/src/app/timeline/TimelinePageTemplate.tsx index 88066b76..a68d08c6 100644 --- a/Timeline/ClientApp/src/app/timeline/TimelinePageTemplate.tsx +++ b/Timeline/ClientApp/src/app/timeline/TimelinePageTemplate.tsx @@ -14,7 +14,7 @@ import { usePostList, } from '../data/timeline'; -import { TimelinePostInfoEx, TimelineDeleteCallback } from './Timeline'; +import { TimelineDeleteCallback } from './Timeline'; import { TimelineMemberDialog } from './TimelineMember'; import TimelinePropertyChangeDialog from './TimelinePropertyChangeDialog'; import { TimelinePageTemplateUIProps } from './TimelinePageTemplateUI'; @@ -55,7 +55,7 @@ export default function TimelinePageTemplate< undefined ); - const rawPosts = usePostList(timeline?.name); + const postListState = usePostList(timeline?.name); const [error, setError] = React.useState(undefined); @@ -182,43 +182,19 @@ export default function TimelinePageTemplate< const onDelete: TimelineDeleteCallback = React.useCallback( (index, id) => { - service.deletePost(name, id).subscribe( - () => { - // TODO: Remove this. - setPosts((oldPosts) => - without( - oldPosts as TimelinePostInfoEx[], - (oldPosts as TimelinePostInfoEx[])[index] - ) - ); - }, - () => { - pushAlert({ - type: 'danger', - message: t('timeline.deletePostFailed'), - }); - } - ); + service.deletePost(name, id).subscribe(null, () => { + pushAlert({ + type: 'danger', + message: t('timeline.deletePostFailed'), + }); + }); }, [service, name, t] ); const onPost: TimelinePostSendCallback = React.useCallback( (req) => { - return service - .createPost(name, req) - .pipe( - map((newPost) => { - // TODO: Remove this. - setPosts((oldPosts) => - concat(oldPosts as TimelinePostInfoEx[], { - ...newPost, - deletable: true, - }) - ); - }) - ) - .toPromise(); + return service.createPost(name, req).toPromise().then(); }, [service, name] ); @@ -245,7 +221,7 @@ export default function TimelinePageTemplate< { timeline: TimelineInfo; @@ -29,7 +34,7 @@ export interface TimelineCardComponentProps { export interface TimelinePageTemplateUIProps { avatarKey?: string | number; timeline?: TimelineInfo; - posts?: TimelinePostInfoEx[] | 'forbid'; + postListState?: TimelinePostListState; CardComponent: React.ComponentType>; onMember: () => void; onManage?: (item: TManageItems | 'property') => void; @@ -41,7 +46,7 @@ export interface TimelinePageTemplateUIProps { export default function TimelinePageTemplateUI( props: TimelinePageTemplateUIProps ): React.ReactElement | null { - const { timeline } = props; + const { timeline, postListState } = props; const { t } = useTranslation(); @@ -116,7 +121,7 @@ export default function TimelinePageTemplateUI( subscriptions.forEach((s) => s.unsubscribe()); }; } - }, [getResizeEvent, triggerResizeEvent, timeline, props.posts]); + }, [getResizeEvent, triggerResizeEvent, timeline, postListState]); const [cardHeight, setCardHeight] = React.useState(0); @@ -142,16 +147,27 @@ export default function TimelinePageTemplateUI( } else { if (timeline != null) { let timelineBody: React.ReactElement; - if (props.posts != null) { - if (props.posts === 'forbid') { + if (postListState != null) { + if (postListState.state === 'forbid') { timelineBody = (

{t('timeline.messageCantSee')}

); } else { + const posts: TimelinePostInfoEx[] = postListState.posts.map( + (post) => ({ + ...post, + deletable: timelineService.hasModifyPostPermission( + userService.currentUser, + timeline, + post + ), + }) + ); + timelineBody = ( -- cgit v1.2.3