-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwidget-setup.js
70 lines (67 loc) · 2.24 KB
/
widget-setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* External App URL (must)
*/
const externalAppUrl = 'https://<external-app-link-here>'
/**
* Initial Path (optional)
*/
const path = props.path
/**
* Initial view height (optional)
*/
const initialViewHeight = 500
/**
* Initial Payload (optional) - Do not use async data here, it may fail to be ready before sending this initial payload.
* If you want to get some data, make a "request"
*
* Use "useInitialPayload()" hook inside the external app to get this data
*/
const initialPayload = {
myNiceProp: 'me gusta :D',
}
/**
* Request Handlers.
*
* - request: payload sent by External App
*
* - response: method to send the answer back to the External App
*
* - utils: Utils features like
* - promisify: (caller, resolve, reject)
* There's no Promisse for some features yet, So this is util for when you need to get cached data using DiscoveryAPI, e.g:
* utils.promisify(() => Social.getr(`${context.accountId}/profile`), (res) => console.log(res), (err) => console.log(err))
*
* @param {{type: string, payload: {}}} request request with payload sent by External App
* @param {(request) => {send: () => void}} response send the answer back to the External App
* @param {{promisify:(caller: () => void, resolve: (data) => void, reject: (error) => void)}} utils Utils features like
*/
const requestHandler = (request, response, Utils) => {
switch (request.type) {
case 'get-account-id':
getAccountIdHandler(request, response)
break
}
}
const getAccountIdHandler = (request, response) => {
// You have acces to the request payload
console.log(request.payload) // Any data sent by the External App
// You can use any Discovery API feature here
const accountId = context.accountId
// Send a response to the External App (React App)
// "response" needs the "request" object to know the type of the request
// you can read this as "a response to a request"
response(request).send({ accountId })
}
// use `wendersonpires.testnet/widget/NearSocialBridgeCore` as source if you want to use "testnet" environment
return (
<Widget
src={'wendersonpires.near/widget/NearSocialBridgeCore'}
props={{
externalAppUrl,
path,
initialViewHeight,
initialPayload,
requestHandler,
}}
/>
)