-
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
c2b6e6b
commit 841ddd4
Showing
10 changed files
with
141 additions
and
19 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,43 @@ | ||
import { SocialCallDataArgs, socialCallName } from '@subsocial/data-hub-sdk' | ||
import axios from 'axios' | ||
import { gql } from 'graphql-request' | ||
import { createSocialDataEventPayload, DatahubParams, datahubQueryRequest } from './utils' | ||
|
||
// QUERIES | ||
const GET_SUPER_LIKE_COUNTS = gql` | ||
query GetSuperLikeCounts($postIds: [String!]!) { | ||
activeStakingSuperLikeCountsByPost(args: { postPersistentIds: $postIds }) { | ||
persistentPostId | ||
count | ||
} | ||
} | ||
` | ||
export async function getSuperLikeCounts(postIds: string[]) { | ||
const res = (await datahubQueryRequest({ | ||
document: GET_SUPER_LIKE_COUNTS, | ||
variables: { postIds }, | ||
})) as { | ||
activeStakingSuperLikeCountsByPost: { | ||
persistentPostId: string | ||
count: number | ||
}[] | ||
} | ||
|
||
return res.activeStakingSuperLikeCountsByPost.map(item => ({ | ||
postId: item.persistentPostId, | ||
count: item.count, | ||
})) | ||
} | ||
|
||
// MUTATIONS | ||
export async function createSuperLike( | ||
params: DatahubParams<SocialCallDataArgs<'synth_active_staking_create_super_like'>>, | ||
) { | ||
const input = createSocialDataEventPayload( | ||
socialCallName.synth_active_staking_create_super_like, | ||
params, | ||
) | ||
|
||
const res = await axios.post('/api/datahub/super-likes', input) | ||
return res.data | ||
} |
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,6 @@ | ||
import { useAppSelector } from 'src/rtk/app/store' | ||
import { selectPostSuperLikeCount } from './superLikeCountsSlice' | ||
|
||
export function useSuperLikeCount(postId: string) { | ||
return useAppSelector(state => selectPostSuperLikeCount(state, postId)?.count ?? 0) | ||
} |
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,60 @@ | ||
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit' | ||
import { getSuperLikeCounts } from 'src/components/utils/datahub/super-likes' | ||
import { RootState } from 'src/rtk/app/rootReducer' | ||
import { createSimpleFetchWrapper } from 'src/rtk/app/wrappers' | ||
|
||
export type SuperLikeCount = { | ||
postId: string | ||
count: number | ||
} | ||
|
||
const sliceName = 'superLikesCounts' | ||
|
||
const adapter = createEntityAdapter<SuperLikeCount>({ | ||
selectId: data => data.postId, | ||
}) | ||
const selectors = adapter.getSelectors<RootState>(state => state.superLikeCounts) | ||
|
||
export const selectPostSuperLikeCount = selectors.selectById | ||
export const selectPostSuperLikeCounts = selectors.selectEntities | ||
|
||
export const fetchSuperLikeCounts = createSimpleFetchWrapper< | ||
{ postIds: string[] }, | ||
SuperLikeCount[] | ||
>({ | ||
fetchData: async function ({ postIds }) { | ||
return await getSuperLikeCounts(postIds) | ||
}, | ||
saveToCacheAction: data => slice.actions.setSuperLikeCounts(data), | ||
getCachedData: (state, { postIds }) => { | ||
const entities = selectPostSuperLikeCounts(state) | ||
let isEveryDataCached = true | ||
|
||
const postEntities: SuperLikeCount[] = [] | ||
for (let i = 0; i < postIds.length; i++) { | ||
const postId = postIds[i] | ||
if (!entities[postId]) { | ||
isEveryDataCached = false | ||
break | ||
} else { | ||
postEntities.push(entities[postId]!) | ||
} | ||
} | ||
|
||
if (isEveryDataCached) { | ||
return postEntities | ||
} | ||
return undefined | ||
}, | ||
sliceName, | ||
}) | ||
|
||
const slice = createSlice({ | ||
name: sliceName, | ||
initialState: adapter.getInitialState(), | ||
reducers: { | ||
setSuperLikeCounts: adapter.upsertMany, | ||
}, | ||
}) | ||
|
||
export default slice.reducer |
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