Skip to content

Commit

Permalink
feat(ens): add redux hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
developerfred committed May 25, 2022
1 parent 123308a commit 89414f1
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 42 deletions.
21 changes: 0 additions & 21 deletions src/hooks/useEns.tsx

This file was deleted.

8 changes: 7 additions & 1 deletion src/hooks/useSearchAddressBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@ import {ethers} from "ethers";
import {networks} from "../redux/networks";
import {useAppSelector} from "../redux/hooks";
import {addressBookSelectors} from "../redux/slices/addressBook.slice";
import {ensApi} from "../redux/slices/ensResolver.slice";

export const useSearchAddressBook = (searchTerm: string) => {
const isSearchTermAddress = useMemo(
() => ethers.utils.isAddress(searchTerm),
[searchTerm]
);


const ensQuery = ensApi.useResolveNameQuery(
searchTerm
)

return networks.map((network) => {
const addressBookEntries = useAppSelector((state) =>
searchTerm !== "" && !isSearchTermAddress
searchTerm !== "" && !isSearchTermAddress && !ensQuery
? addressBookSelectors
.selectAll(state)
.filter((x) => x.chainId === network.chainId)
Expand Down
9 changes: 0 additions & 9 deletions src/hooks/useSearchEns.ts

This file was deleted.

15 changes: 14 additions & 1 deletion src/hooks/useSearchSubgraphByAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {useMemo} from "react";
import {ethers} from "ethers";
import {networks} from "../redux/networks";
import {sfSubgraph} from "../redux/store";
import {ensApi} from "../redux/slices/ensResolver.slice";
import {skipToken} from "@reduxjs/toolkit/query";
import {gql} from "graphql-request";

Expand Down Expand Up @@ -51,6 +52,10 @@ export const useSearchSubgraphByAddress = (searchTerm: string) => {
[searchTerm]
);

const ensQuery = ensApi.useResolveNameQuery(
searchTerm ? searchTerm : skipToken
)

return networks.map((network) =>
sfSubgraph.useCustomQuery(
isSearchTermAddress
Expand All @@ -62,7 +67,15 @@ export const useSearchSubgraphByAddress = (searchTerm: string) => {
addressBytes: searchTerm.toLowerCase(),
},
}
: skipToken
// : skipToken
: {
chainId: network.chainId,
document: searchByAddressDocument,
variables: {
addressId: ensQuery,
addressBytes: ensQuery
},
}
)
);
};
2 changes: 1 addition & 1 deletion src/pages/[_network]/supertokens/[_id].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useContext, useEffect, useState } from "react";
import { sfSubgraph } from "../../../redux/store";
import { sfSubgraph} from "../../../redux/store";
import OpenInNewIcon from "@mui/icons-material/OpenInNew";
import { TabContext, TabList, TabPanel } from "@mui/lab";
import {
Expand Down
4 changes: 1 addition & 3 deletions src/redux/slices/ensResolver.slice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import urlcat from 'urlcat'
import { fakeBaseQuery } from "@reduxjs/toolkit/dist/query";
import { createApi } from "@reduxjs/toolkit/dist/query/react";
import { ethers } from "ethers";
import { createApi, fetchBaseQuery, fakeBaseQuery } from "@reduxjs/toolkit/query/react";

export interface NameInfo {
rnsName: string
Expand Down Expand Up @@ -98,4 +97,3 @@ export const ensApi = createApi({
};
},
});

14 changes: 8 additions & 6 deletions src/redux/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ export const makeStore = wrapMakeStore(() => {
addressBookSlice.reducer
);

// const ensReducer = persistReducer(
// { key: "ens-address", version: 1, storage: storageLocal },
// ensResolverSlice.reducer
// )
const ensReducer = persistReducer(
{ key: "ens-lockup", version: 1, storage: storageLocal },
ensApi.reducer
)

const store = configureStore({
reducer: {
[rpcApi.reducerPath]: rpcApi.reducer,
[sfSubgraph.reducerPath]: sfSubgraph.reducer,
[themePreferenceSlice.name]: themePreferenceSlice.reducer,
[addressBookSlice.name]: addressBookReducer,
[ensApi.reducerPath]: ensApi.reducer,
[ensApi.reducerPath]: ensReducer,
},
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
Expand All @@ -84,6 +84,7 @@ export const makeStore = wrapMakeStore(() => {
], // Ignore redux-persist actions: https://stackoverflow.com/a/62610422
},
})

.prepend(
nextReduxCookieMiddleware({
compress: true,
Expand All @@ -92,7 +93,8 @@ export const makeStore = wrapMakeStore(() => {
})
)
.concat(rpcApi.middleware)
.concat(sfSubgraph.middleware),
.concat(sfSubgraph.middleware)
.concat(ensApi.middleware),
});

if (!isServer()) {
Expand Down

0 comments on commit 89414f1

Please sign in to comment.