(
client: QueryClient,
id: string,
postUpdate:
| Partial<Omit<Post, 'id'>>
| ((current: Post) => Partial<Omit<Post, 'id'>>),
)
| 658 | }; |
| 659 | |
| 660 | export const updatePostCache = ( |
| 661 | client: QueryClient, |
| 662 | id: string, |
| 663 | postUpdate: |
| 664 | | Partial<Omit<Post, 'id'>> |
| 665 | | ((current: Post) => Partial<Omit<Post, 'id'>>), |
| 666 | ): PostData | undefined => { |
| 667 | const currentPost = client.getQueryData<PostData>(getPostByIdKey(id)); |
| 668 | |
| 669 | if (!currentPost?.post) { |
| 670 | return currentPost; |
| 671 | } |
| 672 | |
| 673 | return client.setQueryData<PostData>( |
| 674 | getPostByIdKey(id), |
| 675 | (node): PostData | undefined => { |
| 676 | if (!node?.post) { |
| 677 | return node; |
| 678 | } |
| 679 | |
| 680 | const update = |
| 681 | typeof postUpdate === 'function' ? postUpdate(node.post) : postUpdate; |
| 682 | const updatedPost = { ...node.post, ...update } as Post; |
| 683 | const bookmark = updatedPost.bookmark ?? { createdAt: new Date() }; |
| 684 | |
| 685 | return { |
| 686 | post: { |
| 687 | ...updatedPost, |
| 688 | id: node.post.id, |
| 689 | bookmark: !updatedPost.bookmarked ? undefined : bookmark, |
| 690 | }, |
| 691 | }; |
| 692 | }, |
| 693 | ) as PostData | undefined; |
| 694 | }; |
| 695 | |
| 696 | export const updateAdPostInCache = ( |
| 697 | postId: string, |
no test coverage detected