Skip to content

Commit

Permalink
feat: show post score
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Jan 26, 2024
1 parent e8256a3 commit 79b27e3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/components/posts/LatestPostsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import config from 'src/config'
import { DEFAULT_PAGE_SIZE } from 'src/config/ListData.config'
import { useDfApolloClient } from 'src/graphql/ApolloProvider'
import { GetLatestPostIds } from 'src/graphql/__generated__/GetLatestPostIds'
import { setPostScores } from 'src/rtk/features/posts/postScoreSlice'
import { fetchPosts } from 'src/rtk/features/posts/postsSlice'
import { DataSourceTypes, PostId } from 'src/types'
import { PostKind } from 'src/types/graphql-global-types'
Expand Down Expand Up @@ -48,6 +49,11 @@ export const loadMorePostsFn = async (loadMoreValues: LoadMoreValues<PostFilterT
if (filter.type === 'hot') {
const posts = await getHotPosts({ offset, limit: DEFAULT_PAGE_SIZE })
postIds = posts.data.map(value => value.persistentPostId)
dispatch(
setPostScores(
posts.data.map(({ persistentPostId, score }) => ({ id: persistentPostId, score })),
),
)
} else if (!isSuggested(filter.type) && client) {
const data = await loadPostsByQuery({ client, kind, offset, filter })
const { posts } = data as GetLatestPostIds
Expand Down
5 changes: 5 additions & 0 deletions src/components/posts/view-post/ViewRegularPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { FC, useState } from 'react'
import { CommentSection } from 'src/components/comments/CommentsSection'
import { useAppSelector } from 'src/rtk/app/store'
import { selectPostScore } from 'src/rtk/features/posts/postScoreSlice'
import { SpaceData } from 'src/types'
import { InfoPostPreview, PostActionsPanel, PostNotFound } from './helpers'
import { PreviewProps } from './PostPreview'
Expand All @@ -17,6 +19,8 @@ export const RegularPreview: ComponentType = props => {

const { isSharedPost } = postDetails.post.struct

const score = useAppSelector(state => selectPostScore(state, postDetails.post.id))

return !isSharedPost ? (
<>
<InfoPostPreview
Expand All @@ -26,6 +30,7 @@ export const RegularPreview: ComponentType = props => {
withTags={withTags}
withMarginForCardType={!withActions}
/>
{score?.score}
{withActions && (
<PostActionsPanel
postDetails={postDetails}
Expand Down
2 changes: 2 additions & 0 deletions src/rtk/app/rootReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import onBoarding from '../features/onBoarding/onBoardingSlice'
import mySpacePermissions from '../features/permissions/mySpacePermissionsSlice'
import myFeed from '../features/posts/myFeedSlice'
import ownPostIds from '../features/posts/ownPostIdsSlice'
import postScores from '../features/posts/postScoreSlice'
import posts from '../features/posts/postsSlice'
import followedAccountIds from '../features/profiles/followedAccountIdsSlice'
import profileSpaces from '../features/profiles/profilesSlice'
Expand Down Expand Up @@ -75,6 +76,7 @@ const rootReducer = combineReducers({
userStatistics,
generalStatistics,
leaderboard,
postScores,
})

export type RootState = ReturnType<typeof rootReducer>
Expand Down
27 changes: 27 additions & 0 deletions src/rtk/features/posts/postScoreSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit'
import { RootState } from 'src/rtk/app/rootReducer'

// TODO: remove this slice when score is not needed to be displayed anymore
export type PostScore = {
id: string
score: number
}

const sliceName = 'postScore'

const adapter = createEntityAdapter<PostScore>()
const selectors = adapter.getSelectors<RootState>(state => state.postScores)

export const selectPostScore = selectors.selectById

const slice = createSlice({
name: sliceName,
initialState: adapter.getInitialState(),
reducers: {
setPostScores: adapter.upsertMany,
},
})

export const { setPostScores } = slice.actions

export default slice.reducer

0 comments on commit 79b27e3

Please sign in to comment.