miolo-react 3.0.0-beta.12 → 3.0.0-beta.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/miolo-react.mjs +1 -1
- package/dist/miolo-react.umd.js +1 -1
- package/package.json +3 -3
- package/src/AppBrowser.jsx +14 -0
- package/src/AppServer.jsx +17 -0
- package/src/context/MioloContext.mjs +5 -0
- package/src/context/MioloContextProvider.jsx +95 -0
- package/src/context/useMioloContext.jsx +6 -0
- package/src/context/withMioloContext.jsx +15 -0
- package/src/index.mjs +7 -0
- package/src/ssr/getSsrDataFromContext.mjs +33 -0
- package/src/ssr/useSsrDataOrReload.mjs +43 -0
package/dist/miolo-react.mjs
CHANGED
package/dist/miolo-react.umd.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "miolo-react",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.13",
|
|
4
4
|
"description": "React utils for miolo",
|
|
5
5
|
"author": "Donato Lorenzo <donato@afialapis.com>",
|
|
6
6
|
"contributors": [
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
|
-
"dist"
|
|
27
|
+
"dist",
|
|
28
|
+
"src"
|
|
28
29
|
],
|
|
29
30
|
"scripts": {
|
|
30
31
|
"reset": "rm -fr package-lock.json npm-lock.yaml dist/* && npm i",
|
|
@@ -32,7 +33,6 @@
|
|
|
32
33
|
"bundle": "npx xeira bundle --target=browser --source_index=./src/index.mjs --bundle_folder=./dist --bundle_name=miolo-react --bundle_node_polyfill=true --bundle_extension=umd,mjs",
|
|
33
34
|
"dist": "npm run clean && npm run bundle"
|
|
34
35
|
},
|
|
35
|
-
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"miolo-cli": "../miolo-cli"
|
|
38
38
|
},
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import MioloContextProvider from './context/MioloContextProvider.jsx'
|
|
3
|
+
|
|
4
|
+
const AppBrowser = ({ children }) => {
|
|
5
|
+
const context = typeof window !== 'undefined' && window.__CONTEXT ? window.__CONTEXT : {};
|
|
6
|
+
|
|
7
|
+
return (
|
|
8
|
+
<MioloContextProvider context={context}>
|
|
9
|
+
{children}
|
|
10
|
+
</MioloContextProvider>
|
|
11
|
+
)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default AppBrowser
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
import React from 'react'
|
|
3
|
+
import MioloContextProvider from './context/MioloContextProvider.jsx'
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const AppServer = ({context, children}) => {
|
|
7
|
+
|
|
8
|
+
return (
|
|
9
|
+
<MioloContextProvider
|
|
10
|
+
context= {context || {}}>
|
|
11
|
+
{children}
|
|
12
|
+
</MioloContextProvider>
|
|
13
|
+
)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default AppServer
|
|
17
|
+
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import React , {useState, useEffect, useCallback} from 'react'
|
|
2
|
+
import Context from './MioloContext.mjs'
|
|
3
|
+
import { miolo_client } from 'miolo-cli'
|
|
4
|
+
import { useSsrDataOrReload } from '../ssr/useSsrDataOrReload.mjs'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const MioloContextProvider = ({context, children}) => {
|
|
8
|
+
const [innerContext, setInnerContext]= useState(context)
|
|
9
|
+
const [mioloObj, setMioloObj]= useState(miolo_client(context))
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
setInnerContext(context)
|
|
13
|
+
setMioloObj(miolo_client(context))
|
|
14
|
+
}, [context])
|
|
15
|
+
|
|
16
|
+
const login = useCallback(async (credentials) => {
|
|
17
|
+
const { fetcher } = mioloObj
|
|
18
|
+
const { config } = innerContext
|
|
19
|
+
|
|
20
|
+
const url = config.login_url || '/login'
|
|
21
|
+
const resp = await fetcher.login(url, credentials)
|
|
22
|
+
|
|
23
|
+
if (resp?.data) {
|
|
24
|
+
if (resp?.data?.authenticated) {
|
|
25
|
+
setInnerContext(current => {
|
|
26
|
+
return {
|
|
27
|
+
...current,
|
|
28
|
+
...resp?.data,
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return resp?.data
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {}
|
|
37
|
+
}, [innerContext, mioloObj])
|
|
38
|
+
|
|
39
|
+
const logout = useCallback(async () => {
|
|
40
|
+
const { fetcher } = mioloObj
|
|
41
|
+
const { config } = innerContext
|
|
42
|
+
|
|
43
|
+
const url = config.logout_url || '/logout'
|
|
44
|
+
const _resp = await fetcher.logout(url)
|
|
45
|
+
// resp.redirected= true
|
|
46
|
+
|
|
47
|
+
setInnerContext(current => {
|
|
48
|
+
return {
|
|
49
|
+
...current,
|
|
50
|
+
user: undefined,
|
|
51
|
+
authenticated: false,
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
user: undefined,
|
|
57
|
+
authenticated: false,
|
|
58
|
+
}
|
|
59
|
+
}, [innerContext, mioloObj])
|
|
60
|
+
|
|
61
|
+
const updateUser = useCallback((user) => {
|
|
62
|
+
setInnerContext((current) => {
|
|
63
|
+
return {
|
|
64
|
+
...current,
|
|
65
|
+
user,
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
}, [])
|
|
69
|
+
|
|
70
|
+
const useSsrData = (name, defval, loader, modifier) => {
|
|
71
|
+
return useSsrDataOrReload(innerContext, mioloObj, name, defval, loader, modifier)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<Context.Provider
|
|
76
|
+
value={{
|
|
77
|
+
//context: innerContext,
|
|
78
|
+
//setContext: setInnerContext,
|
|
79
|
+
//miolo: mioloObj,
|
|
80
|
+
user: innerContext.user,
|
|
81
|
+
updateUser,
|
|
82
|
+
authenticated: innerContext.authenticated,
|
|
83
|
+
fetcher: mioloObj.fetcher,
|
|
84
|
+
//socket: mioloObj.socket,
|
|
85
|
+
login,
|
|
86
|
+
logout,
|
|
87
|
+
useSsrData
|
|
88
|
+
}}>
|
|
89
|
+
{children}
|
|
90
|
+
</Context.Provider>
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
export default MioloContextProvider
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React, {useContext} from 'react'
|
|
2
|
+
import MioloContext from './MioloContext.mjs'
|
|
3
|
+
|
|
4
|
+
/* eslint react/display-name:0 */
|
|
5
|
+
const withMioloContext = (BaseComponent) => (props) => {
|
|
6
|
+
const context = useContext(MioloContext)
|
|
7
|
+
|
|
8
|
+
return (
|
|
9
|
+
<BaseComponent {...props}
|
|
10
|
+
{...context}/>
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export default withMioloContext
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import withMioloContext from './context/withMioloContext.jsx'
|
|
2
|
+
import useMioloContext from './context/useMioloContext.jsx'
|
|
3
|
+
|
|
4
|
+
import AppBrowser from './AppBrowser.jsx'
|
|
5
|
+
import AppServer from './AppServer.jsx'
|
|
6
|
+
|
|
7
|
+
export {withMioloContext, useMioloContext, AppBrowser, AppServer}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
|
|
2
|
+
const _getDataFromWindow = (name) => {
|
|
3
|
+
try {
|
|
4
|
+
if (window != undefined) {
|
|
5
|
+
const ssr_data= window.__CONTEXT.ssr_data
|
|
6
|
+
|
|
7
|
+
if (ssr_data!=undefined) {
|
|
8
|
+
if (ssr_data[name]!=undefined) {
|
|
9
|
+
return ssr_data[name]
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
} catch(e) {}
|
|
14
|
+
|
|
15
|
+
return undefined
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const getSsrDataFromContext = (context, name) => {
|
|
19
|
+
let data= undefined
|
|
20
|
+
|
|
21
|
+
if (context?.ssr_data != undefined && context?.ssr_data[name]!=undefined) {
|
|
22
|
+
data= context.ssr_data[name]
|
|
23
|
+
} else {
|
|
24
|
+
const wdata= _getDataFromWindow(name)
|
|
25
|
+
if (wdata != undefined) {
|
|
26
|
+
data= wdata
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return data
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default getSsrDataFromContext
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {useState, useCallback, useEffect} from 'react'
|
|
2
|
+
import getSsrDataFromContext from './getSsrDataFromContext.mjs'
|
|
3
|
+
|
|
4
|
+
const useSsrDataOrReload = (context, miolo, name, defval, loader, modifier) => {
|
|
5
|
+
|
|
6
|
+
const _maybeModify = useCallback((value) => {
|
|
7
|
+
return modifier==undefined ? value : modifier(value)
|
|
8
|
+
}, [modifier])
|
|
9
|
+
|
|
10
|
+
const ssrDataFromContext = getSsrDataFromContext(context, name)
|
|
11
|
+
const [ssrData, setSsrData] = useState(_maybeModify(
|
|
12
|
+
ssrDataFromContext != undefined
|
|
13
|
+
? ssrDataFromContext : defval))
|
|
14
|
+
const [needToRefresh, setNeedToRefresh] = useState(ssrDataFromContext == undefined)
|
|
15
|
+
|
|
16
|
+
const refreshSsrData = useCallback(() => {
|
|
17
|
+
if (loader == undefined) {
|
|
18
|
+
return
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const {fetcher} = miolo
|
|
22
|
+
|
|
23
|
+
async function fetchData() {
|
|
24
|
+
let nSsrData = await loader(context, fetcher)
|
|
25
|
+
setSsrData(_maybeModify(nSsrData))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
fetchData()
|
|
29
|
+
}, [context, miolo, loader, _maybeModify])
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
try {
|
|
33
|
+
if (needToRefresh) {
|
|
34
|
+
setNeedToRefresh(false)
|
|
35
|
+
refreshSsrData()
|
|
36
|
+
}
|
|
37
|
+
} catch(e) {}
|
|
38
|
+
}, [needToRefresh, refreshSsrData])
|
|
39
|
+
|
|
40
|
+
return [ssrData, (data) => setSsrData(_maybeModify(data)), refreshSsrData]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export {useSsrDataOrReload}
|