-
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.
feat: use data from subid for creators list
fix issue where useFetchCreator if fetched multiple times are calling upsert everytime, changing the state, even though the call to api is only once, making it infinite loop
- Loading branch information
1 parent
50be25c
commit 285ba2b
Showing
15 changed files
with
175 additions
and
90 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
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
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,20 @@ | ||
import { useFetchWithoutApi } from 'src/rtk/app/hooksCommon' | ||
import { useAppSelector } from 'src/rtk/app/store' | ||
import { fetchCreators, selectCreators } from './creatorsListSlice' | ||
|
||
export function useFetchCreators(config?: { enabled?: boolean }) { | ||
const { enabled } = config || {} | ||
const data = useAppSelector(state => selectCreators(state)) | ||
|
||
const props = useFetchWithoutApi(fetchCreators, {}, { enabled }) | ||
|
||
return { | ||
...props, | ||
data, | ||
} | ||
} | ||
|
||
export function useIsCreatorSpace(spaceId?: string) { | ||
const { data } = useFetchCreators({ enabled: !!spaceId }) | ||
return data.map(({ spaceId }) => spaceId).includes(spaceId ?? '') | ||
} |
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,47 @@ | ||
import { createAsyncThunk, createEntityAdapter, createSlice } from '@reduxjs/toolkit' | ||
import { getCreatorList } from 'src/components/utils/OffchainUtils' | ||
import { ThunkApiConfig } from 'src/rtk/app/helpers' | ||
import { RootState } from 'src/rtk/app/rootReducer' | ||
|
||
export type Creator = { spaceId: string } | ||
const sliceName = 'creatorsList' | ||
|
||
const adapter = createEntityAdapter<Creator>({ | ||
selectId: data => data.spaceId, | ||
}) | ||
const selectors = adapter.getSelectors<RootState>(state => state.creatorsList) | ||
|
||
export const selectCreators = selectors.selectAll | ||
|
||
let pendingPromise: Promise<Creator[]> | null = null | ||
export const fetchCreators = createAsyncThunk<Creator[], { reload?: boolean }, ThunkApiConfig>( | ||
`${sliceName}/fetchMany`, | ||
async ({ reload }, { getState, dispatch }) => { | ||
if (!reload) { | ||
const fetchedData = selectCreators(getState()) | ||
if (fetchedData.length > 0) return fetchedData | ||
} | ||
if (pendingPromise) return pendingPromise | ||
|
||
async function fetchData() { | ||
const data = await getCreatorList() | ||
await dispatch(slice.actions.setCreators(data)) | ||
return data | ||
} | ||
const promise = fetchData() | ||
pendingPromise = promise | ||
await promise | ||
pendingPromise = null | ||
return promise | ||
}, | ||
) | ||
|
||
const slice = createSlice({ | ||
name: sliceName, | ||
initialState: adapter.getInitialState(), | ||
reducers: { | ||
setCreators: adapter.setAll, | ||
}, | ||
}) | ||
|
||
export default slice.reducer |
Oops, something went wrong.