@fto-consult/expo-ui 2.12.5 → 2.12.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fto-consult/expo-ui",
3
- "version": "2.12.5",
3
+ "version": "2.12.6",
4
4
  "description": "Bibliothèque de composants UI Expo,react-native",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,179 @@
1
+ // Copyright 2022 @fto-consult/Boris Fouomene. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ import Datagrid from "./IndexComponent";
6
+ import {defaultStr,defaultObj,defaultVal,extendObj} from "$utils";
7
+ import React from "$react";
8
+ import Auth from "$cauth";
9
+ import DateLib from "$lib/date";
10
+ import {getFetchOptions} from "$cutils/filters";
11
+ import {setQueryParams} from "$cutils/uri";
12
+ import Icon from "$ecomponents/Icon";
13
+ import Label from "$ecomponents/Label";
14
+ import { StyleSheet,View } from "react-native";
15
+ import theme from "$theme";
16
+ import useSWR,{useInfinite} from "$swr";
17
+ import appConfig from "$capp/config";
18
+ import APP from "$capp/instance";
19
+ import cAction from "$cactions";
20
+ import PropTypes from "prop-types";
21
+
22
+ const timeout = 5000*60*60;
23
+ export const swrOptions = {
24
+ refreshInterval : timeout, //5 minutes
25
+ shouldRetryOnError : false, //retry when fetcher has an error
26
+ dedupingInterval : timeout,
27
+ errorRetryInterval : timeout*2,
28
+ errorRetryCount : 5,
29
+ }
30
+
31
+ const SWRDatagridComponent = React.forwardRef((props,ref)=>{
32
+ let {
33
+ table,
34
+ data,
35
+ saveButton,
36
+ title,
37
+ fab,
38
+ rowKey,
39
+ actions,
40
+ sessionName,
41
+ server,
42
+ columns,
43
+ canMakePhoneCall,
44
+ makePhoneCallProps,
45
+ fetchData,
46
+ fetchPath,
47
+ fetcher,
48
+ ...rest
49
+ } = props;
50
+ rest = defaultObj(rest);
51
+ rest.exportTableProps = defaultObj(rest.exportTableProps)
52
+ const tableName = defaultStr(table.tableName,table.table).trim().toUpperCase();
53
+ canMakePhoneCall = defaultBool(canMakePhoneCall,table.canMakePhoneCall);
54
+ makePhoneCallProps = defaultObj(makePhoneCallProps,rest.makePhoneCallProps,table.makePhoneCallProps);
55
+ const isExportable = !!Auth.isTableDataAllowed({table:tableName,action:'export'});
56
+ rest.exportable = isExportable;
57
+ rowKey = defaultStr(rowKey,table.rowKey,table.primaryKeyColumnName);
58
+ title = defaultStr(title,table.label,table.text)
59
+ columns = table.fields;
60
+ const fetchFields = [];
61
+ Object.map(columns,(column,i)=>{
62
+ if(isObj(column)){
63
+ fetchFields.push(defaultStr(column.field,i));
64
+ }
65
+ })
66
+ actions = defaultVal(table.actions,actions);
67
+ for(let i in Datagrid.propTypes){
68
+ if(i in table){
69
+ rest[i] = isObj(rest[i])? extendObj(true,{},rest[i],table[i]) : table[i];
70
+ }
71
+ }
72
+ rest.actions = actions;
73
+ rest.columns = columns || [];
74
+ const icon = defaultStr(table.icon);
75
+ rest.tableName = tableName;
76
+ rest.canMakePhoneCall = canMakePhoneCall;
77
+ rest.makePhoneCallProps = makePhoneCallProps;
78
+ rest.exportTableProps.fileName = defaultStr(rest.exportTableProps.fileName,title+"-"+DateLib.format(DateLib.toObj(),'dd-mm-yyyy HH-MM'))
79
+ rest.exportTableProps.pdf = defaultObj(rest.exportTableProps.pdf);
80
+ rest.exportTableProps.pdf = extendObj(true,{},{
81
+ fileName : rest.exportTableProps.fileName,
82
+ title
83
+ },rest.exportTableProps.pdf);
84
+ const fetchOptionsRef = React.useRef({});
85
+ fetchPath = defaultStr(fetchPath,table.queryPath,tableName.toLowerCase()).trim();
86
+ const innerRef = React.useRef(null);
87
+ let {data:fetchedData, error, isValidating,size, setSize,isLoading,refresh} = useSWR(fetchPath,{
88
+ fetchOptionsMutator : (opts)=>{
89
+ const {url} = opts;
90
+ const fo = fetchOptionsRef.current;
91
+ if(Object.size(fo.queryParams,true)){
92
+ return {url : setQueryParams(url,fo.queryParams)};
93
+ }
94
+ },
95
+ fetcher,
96
+ showError : false,
97
+ swrOptions : {
98
+ ...swrOptions,
99
+ ...defaultObj(appConfig.swr),
100
+ },
101
+ });
102
+ React.useEffect(()=>{
103
+ innerRef.current && innerRef.current.setIsLoading && innerRef.current.setIsLoading(isLoading);
104
+ },[isLoading])
105
+ React.useEffect(()=>{
106
+ const upsert = cAction.upsert(tableName);
107
+ const remove = cAction.remove(tableName);
108
+ APP.on(remove,refresh);
109
+ APP.on(upsert,refresh);
110
+ return ()=>{
111
+ APP.off(upsert,refresh);
112
+ APP.off(remove,refresh);
113
+ }
114
+ },[])
115
+ return (
116
+ <Datagrid
117
+ {...rest}
118
+ {...defaultObj(table.datagrid)}
119
+ isLoading = {isLoading|| isValidating}
120
+ beforeFetchData = {({fetchOptions:opts})=>{
121
+ opts.fields = fetchFields;
122
+ opts = getFetchOptions({fetcher,...opts});
123
+ fetchOptionsRef.current = opts;
124
+ refresh();
125
+ }}
126
+ fetchData = {undefined}
127
+ data = {error ? data : defaultObj(fetchedData).data}
128
+ canMakePhoneCall={canMakePhoneCall}
129
+ key={tableName}
130
+ sessionName={defaultStr(sessionName,'list-data')}
131
+ ref={React.useMergeRefs(ref,innerRef)}
132
+ rowKey={rowKey}
133
+ renderEmpty = {(p)=>{
134
+ return <View style={styles.emptyAccordion}>
135
+ {icon ? <Icon name={icon} color={theme.colors.primaryOnSurface} size={80}/> : null}
136
+ {<Label secondary style={styles.labelTitle}>{title}</Label>}
137
+ <Label style={[styles.emptyText]}>
138
+ {"Aucune données enrégistrée!!"}
139
+ </Label>
140
+ </View>
141
+ }}
142
+ />
143
+ )
144
+ });
145
+
146
+ export default SWRDatagridComponent;
147
+
148
+ SWRDatagridComponent.displayName = "SWRDatagridComponent";
149
+
150
+ SWRDatagridComponent.propTypes = {
151
+ ...Datagrid.propTypes,
152
+ fetchPath : PropTypes.string,
153
+ fetchData : PropTypes.func,
154
+ table : PropTypes.shape({
155
+ tableName : PropTypes.string,
156
+ table : PropTypes.string,
157
+ fields : PropTypes.oneOfType([
158
+ PropTypes.objectOf(PropTypes.object),
159
+ PropTypes.arrayOf(PropTypes.object),
160
+ ])
161
+ }).isRequired,
162
+ }
163
+
164
+ const styles = StyleSheet.create({
165
+ emptyAccordion : {
166
+ alignSelf : 'center',
167
+ alignItems:'center'
168
+ },
169
+ labelTitle: {
170
+ fontSize : 18,
171
+ },
172
+ emptyText : {
173
+ fontSize : 16,
174
+ fontWeight : 'bold',
175
+ flexWrap : 'wrap',
176
+ marginVertical : 10,
177
+ textAlign : 'center'
178
+ }
179
+ })
@@ -4,4 +4,6 @@
4
4
 
5
5
  export * from "./Common";
6
6
 
7
- export {default} from "./IndexComponent";
7
+ export {default} from "./IndexComponent";
8
+
9
+ export {default as SWRDatagrid} from "./SWRDatagrid";