esoftplay 0.0.142-d → 0.0.142-e
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/modules/lib/curl_view.tsx +68 -0
- package/package.json +1 -1
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// withHooks
|
|
2
|
+
|
|
3
|
+
import { ComponentMessage } from 'esoftplay/cache/component/message/import';
|
|
4
|
+
import { LibCurl } from 'esoftplay/cache/lib/curl/import';
|
|
5
|
+
import { LibLoading } from 'esoftplay/cache/lib/loading/import';
|
|
6
|
+
import { LibUtils } from 'esoftplay/cache/lib/utils/import';
|
|
7
|
+
import { UserData } from 'esoftplay/cache/user/data/import';
|
|
8
|
+
import FastStorage from 'esoftplay/mmkv';
|
|
9
|
+
import useSafeState from 'esoftplay/state';
|
|
10
|
+
import React, { ReactElement, useEffect } from 'react';
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
export interface LibCurl_viewArgs {
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface LibCurl_viewProps {
|
|
18
|
+
url: string;
|
|
19
|
+
post?: any;
|
|
20
|
+
cache?: boolean;
|
|
21
|
+
isUserData?: boolean;
|
|
22
|
+
onSuccess?: (res: any, message: string) => ReactElement;
|
|
23
|
+
onError?: (err: any, retry: Function) => ReactElement;
|
|
24
|
+
onLoading?: ReactElement;
|
|
25
|
+
}
|
|
26
|
+
export default function m(props: LibCurl_viewProps): any {
|
|
27
|
+
|
|
28
|
+
let key = ""
|
|
29
|
+
let initialData: any = undefined
|
|
30
|
+
if (props.cache) {
|
|
31
|
+
|
|
32
|
+
key = "curl-view" + LibUtils.shorten(props.url + JSON.stringify(props.post))
|
|
33
|
+
if (props.isUserData) {
|
|
34
|
+
UserData.register(key)
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
initialData = JSON.parse(FastStorage.getItemSync(key))
|
|
38
|
+
} catch (error) { }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const [data, setData] = useSafeState(initialData)
|
|
42
|
+
|
|
43
|
+
function fetchData() {
|
|
44
|
+
new LibCurl(props.url, props.post, (result, message) => {
|
|
45
|
+
if (props.cache) {
|
|
46
|
+
FastStorage.setItem(key, JSON.stringify({ result, message }))
|
|
47
|
+
}
|
|
48
|
+
setData({ result, message })
|
|
49
|
+
}, (err) => {
|
|
50
|
+
setData(err)
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function retry() {
|
|
55
|
+
setData(initialData)
|
|
56
|
+
fetchData()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
useEffect(fetchData, []);
|
|
60
|
+
|
|
61
|
+
if (!data) {
|
|
62
|
+
return props.onLoading ? props.onLoading : <LibLoading />
|
|
63
|
+
}
|
|
64
|
+
if (data.ok == 0) {
|
|
65
|
+
return props.onError ? props.onError(data, retry) : <ComponentMessage message={data.message} />
|
|
66
|
+
}
|
|
67
|
+
return props.onSuccess ? props.onSuccess(data.result, data.message) : null
|
|
68
|
+
}
|