-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NO-CHANGELOG] feat: primary revenue widget hooks - part 2 #900
Changes from 29 commits
bac6541
0fe667c
004d8bf
e92e3fd
9b4a84a
8cf95f5
71b122b
8daf07b
8ea4f30
b27b867
836b5ea
ed6e3d2
6869ba3
887f2c7
d7b62d0
37ba9be
2c975d0
9577dab
381e0c1
efa2f6c
2b9b1c0
04353fd
5619743
3c210ff
5cc937d
ee8bda2
dd5ccbb
33252fe
a1ad3df
016ae71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { | ||
useContext, | ||
createContext, | ||
useMemo, | ||
ReactNode, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
|
||
import { PrimaryRevenueSuccess } from '@imtbl/checkout-widgets'; | ||
|
||
import { Item, PaymentTypes, SignResponse } from '../types'; | ||
import { useSignOrder } from '../hooks/useSignOrder'; | ||
import { ConnectLoaderState } from '../../../context/connect-loader-context/ConnectLoaderContext'; | ||
|
||
type SharedContextProps = { | ||
env: string; | ||
environmentId: string; | ||
items: Item[]; | ||
amount: string; | ||
fromContractAddress: string; | ||
provider: ConnectLoaderState['provider']; | ||
checkout: ConnectLoaderState['checkout']; | ||
}; | ||
|
||
type SharedContextValues = SharedContextProps & { | ||
sign: (paymentType: PaymentTypes) => Promise<SignResponse | undefined> | ||
execute: () => Promise<PrimaryRevenueSuccess>; | ||
recipientAddress: string; | ||
signResponse: SignResponse | undefined; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const SharedContext = createContext<SharedContextValues>({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a question on naming, any reason why you are not calling this PrimaryRevenueContext? Or something like that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No reason, we can name to |
||
items: [], | ||
amount: '', | ||
fromContractAddress: '', | ||
provider: undefined, | ||
checkout: undefined, | ||
environmentId: '', | ||
env: '', | ||
recipientAddress: '', | ||
sign: () => Promise.resolve(undefined), | ||
execute: () => Promise.resolve({} as PrimaryRevenueSuccess), | ||
signResponse: undefined, | ||
}); | ||
|
||
SharedContext.displayName = 'PrimaryRevenueSharedContext'; | ||
|
||
export function SharedContextProvider(props: { | ||
children: ReactNode; | ||
value: SharedContextProps; | ||
}) { | ||
const { | ||
children, | ||
value: { | ||
env, environmentId, items, amount, fromContractAddress, provider, checkout, | ||
}, | ||
} = props; | ||
|
||
const [recipientAddress, setRecipientAddress] = useState<string>(''); | ||
|
||
// Get recipient address | ||
useEffect(() => { | ||
const getRecipientAddress = async () => { | ||
const signer = provider?.getSigner(); | ||
const address = await signer?.getAddress(); | ||
if (address) { | ||
setRecipientAddress(address); | ||
} | ||
}; | ||
|
||
getRecipientAddress(); | ||
}, [provider]); | ||
|
||
const { sign, execute, signResponse } = useSignOrder({ | ||
items, | ||
provider, | ||
fromContractAddress, | ||
recipientAddress, | ||
environmentId, | ||
env, | ||
}); | ||
|
||
const values = useMemo( | ||
() => ({ | ||
items, | ||
amount, | ||
fromContractAddress, | ||
sign, | ||
execute, | ||
signResponse, | ||
environmentId, | ||
env, | ||
provider, | ||
checkout, | ||
recipientAddress, | ||
}), | ||
[env, environmentId, items, amount, fromContractAddress, provider, checkout, recipientAddress, signResponse], | ||
); | ||
|
||
return ( | ||
<SharedContext.Provider value={values}>{children}</SharedContext.Provider> | ||
); | ||
} | ||
|
||
export function useSharedContext() { | ||
return useContext(SharedContext); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keeping it to skip eslint check, gets remove in the next PRs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add an eslint ignore above the console log
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks.