-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b25d00
commit fb04dec
Showing
10 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ComponentProps } from 'react' | ||
import { FaRegEye } from 'react-icons/fa' | ||
import { IconWithLabel } from 'src/components/utils' | ||
import { usePostViewCount } from 'src/rtk/app/hooks' | ||
|
||
export default function PostViewCount({ | ||
postId, | ||
...props | ||
}: { postId: string } & ComponentProps<'div'>) { | ||
const viewCount = usePostViewCount(postId) | ||
|
||
return ( | ||
<div {...props} className={props.className}> | ||
<IconWithLabel renderZeroCount icon={<FaRegEye />} count={viewCount} /> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit' | ||
import { getPostsViewCount } from 'src/components/utils/datahub/post-view' | ||
import { RootState } from 'src/rtk/app/rootReducer' | ||
import { createSimpleManyFetchWrapper } from 'src/rtk/app/wrappers' | ||
|
||
export type PostViewCount = { | ||
postId: string | ||
viewsCount: number | ||
} | ||
|
||
const sliceName = 'postsViewCount' | ||
|
||
const adapter = createEntityAdapter<PostViewCount>({ | ||
selectId: data => data.postId, | ||
}) | ||
const selectors = adapter.getSelectors<RootState>(state => state.postsViewCount) | ||
|
||
export const selectPostViewCount = selectors.selectById | ||
|
||
export const fetchPostsViewCount = createSimpleManyFetchWrapper< | ||
{ postIds: string[] }, | ||
PostViewCount | ||
>({ | ||
sliceName, | ||
fetchData: async function ({ postIds }) { | ||
return await getPostsViewCount(postIds) | ||
}, | ||
getCachedData: (state, id) => selectPostViewCount(state, id), | ||
saveToCacheAction: data => slice.actions.setPostsViewCount(data), | ||
shouldFetchCondition: ({ postIds }) => postIds?.length !== 0, | ||
filterNewArgs: ({ postIds }, isNewId) => { | ||
const newPostIds = postIds?.filter(postId => isNewId(postId)) | ||
return { postIds: newPostIds } | ||
}, | ||
}) | ||
|
||
const slice = createSlice({ | ||
name: sliceName, | ||
initialState: adapter.getInitialState(), | ||
reducers: { | ||
setPostsViewCount: adapter.upsertMany, | ||
}, | ||
}) | ||
|
||
export default slice.reducer |