react-hook-core 0.4.9 → 0.4.10
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/lib/common.js +30 -0
- package/lib/input.js +14 -6
- package/lib/reflect.js +38 -38
- package/lib/search.js +25 -25
- package/lib/update.js +26 -2
- package/lib/useEdit.js +27 -31
- package/lib/useSearch.js +3 -2
- package/package.json +1 -1
- package/src/common.ts +28 -0
- package/src/core.ts +14 -3
- package/src/input.ts +14 -6
- package/src/reflect.ts +38 -37
- package/src/search.ts +24 -28
- package/src/update.ts +27 -2
- package/src/useEdit.ts +26 -30
- package/src/useSearch.ts +3 -2
- package/lib/error.js +0 -53
- package/lib/merge.js +0 -28
- package/src/error.ts +0 -55
- package/src/merge.ts +0 -26
package/lib/common.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
3
|
+
function messageByHttpStatus(status, resource) {
|
|
4
|
+
var k = "error_" + status
|
|
5
|
+
var msg = resource[k]
|
|
6
|
+
if (!msg || msg.length === 0) {
|
|
7
|
+
msg = resource.error_500
|
|
8
|
+
}
|
|
9
|
+
return msg
|
|
10
|
+
}
|
|
11
|
+
exports.messageByHttpStatus = messageByHttpStatus
|
|
12
|
+
function error(err, resource, ae) {
|
|
13
|
+
var title = resource.error
|
|
14
|
+
var msg = resource.error_internal
|
|
15
|
+
if (!err) {
|
|
16
|
+
ae(msg, undefined, title)
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
var data = err && err.response ? err.response : err
|
|
20
|
+
if (data) {
|
|
21
|
+
var status_1 = data.status
|
|
22
|
+
if (status_1 && !isNaN(status_1)) {
|
|
23
|
+
msg = messageByHttpStatus(status_1, resource)
|
|
24
|
+
}
|
|
25
|
+
ae(msg, undefined, title)
|
|
26
|
+
} else {
|
|
27
|
+
ae(msg, undefined, title)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.error = error
|
package/lib/input.js
CHANGED
|
@@ -59,15 +59,23 @@ function getErrorFunc(p, showErr) {
|
|
|
59
59
|
return p.showError
|
|
60
60
|
}
|
|
61
61
|
exports.getErrorFunc = getErrorFunc
|
|
62
|
-
function showLoading(
|
|
63
|
-
if (
|
|
64
|
-
|
|
62
|
+
function showLoading(loading) {
|
|
63
|
+
if (loading) {
|
|
64
|
+
if (typeof loading === "function") {
|
|
65
|
+
loading()
|
|
66
|
+
} else {
|
|
67
|
+
loading.showLoading()
|
|
68
|
+
}
|
|
65
69
|
}
|
|
66
70
|
}
|
|
67
71
|
exports.showLoading = showLoading
|
|
68
|
-
function hideLoading(
|
|
69
|
-
if (
|
|
70
|
-
|
|
72
|
+
function hideLoading(loading) {
|
|
73
|
+
if (loading) {
|
|
74
|
+
if (typeof loading === "function") {
|
|
75
|
+
loading()
|
|
76
|
+
} else {
|
|
77
|
+
loading.hideLoading()
|
|
78
|
+
}
|
|
71
79
|
}
|
|
72
80
|
}
|
|
73
81
|
exports.hideLoading = hideLoading
|
package/lib/reflect.js
CHANGED
|
@@ -40,44 +40,6 @@ function clone(obj) {
|
|
|
40
40
|
return x
|
|
41
41
|
}
|
|
42
42
|
exports.clone = clone
|
|
43
|
-
function getDirectValue(obj, key) {
|
|
44
|
-
if (obj && obj.hasOwnProperty(key)) {
|
|
45
|
-
return obj[key]
|
|
46
|
-
}
|
|
47
|
-
return null
|
|
48
|
-
}
|
|
49
|
-
exports.getDirectValue = getDirectValue
|
|
50
|
-
function setValue(obj, key, value) {
|
|
51
|
-
var replaceKey = key.replace(/\[/g, ".[").replace(/\.\./g, ".")
|
|
52
|
-
if (replaceKey.indexOf(".") === 0) {
|
|
53
|
-
replaceKey = replaceKey.slice(1, replaceKey.length)
|
|
54
|
-
}
|
|
55
|
-
var keys = replaceKey.split(".")
|
|
56
|
-
var firstKey = keys.shift()
|
|
57
|
-
if (!firstKey) {
|
|
58
|
-
return
|
|
59
|
-
}
|
|
60
|
-
var isArrayKey = /\[([0-9]+)\]/.test(firstKey)
|
|
61
|
-
if (keys.length > 0) {
|
|
62
|
-
var firstKeyValue = obj[firstKey] || {}
|
|
63
|
-
var returnValue = setValue(firstKeyValue, keys.join("."), value)
|
|
64
|
-
return setKey(obj, isArrayKey, firstKey, returnValue)
|
|
65
|
-
}
|
|
66
|
-
return setKey(obj, isArrayKey, firstKey, value)
|
|
67
|
-
}
|
|
68
|
-
exports.setValue = setValue
|
|
69
|
-
var setKey = function (_object, _isArrayKey, _key, _nextValue) {
|
|
70
|
-
if (_isArrayKey) {
|
|
71
|
-
if (_object.length > _key) {
|
|
72
|
-
_object[_key] = _nextValue
|
|
73
|
-
} else {
|
|
74
|
-
_object.push(_nextValue)
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
_object[_key] = _nextValue
|
|
78
|
-
}
|
|
79
|
-
return _object
|
|
80
|
-
}
|
|
81
43
|
function isEmptyObject(obj) {
|
|
82
44
|
for (var key in obj) {
|
|
83
45
|
if (obj.hasOwnProperty(key)) {
|
|
@@ -275,3 +237,41 @@ function equalAll(list, name, v) {
|
|
|
275
237
|
return true
|
|
276
238
|
}
|
|
277
239
|
exports.equalAll = equalAll
|
|
240
|
+
function getDirectValue(obj, key) {
|
|
241
|
+
if (obj && obj.hasOwnProperty(key)) {
|
|
242
|
+
return obj[key]
|
|
243
|
+
}
|
|
244
|
+
return null
|
|
245
|
+
}
|
|
246
|
+
exports.getDirectValue = getDirectValue
|
|
247
|
+
function setValue(obj, key, value) {
|
|
248
|
+
var replaceKey = key.replace(/\[/g, ".[").replace(/\.\./g, ".")
|
|
249
|
+
if (replaceKey.indexOf(".") === 0) {
|
|
250
|
+
replaceKey = replaceKey.slice(1, replaceKey.length)
|
|
251
|
+
}
|
|
252
|
+
var keys = replaceKey.split(".")
|
|
253
|
+
var firstKey = keys.shift()
|
|
254
|
+
if (!firstKey) {
|
|
255
|
+
return
|
|
256
|
+
}
|
|
257
|
+
var isArrayKey = /\[([0-9]+)\]/.test(firstKey)
|
|
258
|
+
if (keys.length > 0) {
|
|
259
|
+
var firstKeyValue = obj[firstKey] || {}
|
|
260
|
+
var returnValue = setValue(firstKeyValue, keys.join("."), value)
|
|
261
|
+
return setKey(obj, isArrayKey, firstKey, returnValue)
|
|
262
|
+
}
|
|
263
|
+
return setKey(obj, isArrayKey, firstKey, value)
|
|
264
|
+
}
|
|
265
|
+
exports.setValue = setValue
|
|
266
|
+
var setKey = function (_object, _isArrayKey, _key, _nextValue) {
|
|
267
|
+
if (_isArrayKey) {
|
|
268
|
+
if (_object.length > _key) {
|
|
269
|
+
_object[_key] = _nextValue
|
|
270
|
+
} else {
|
|
271
|
+
_object.push(_nextValue)
|
|
272
|
+
}
|
|
273
|
+
} else {
|
|
274
|
+
_object[_key] = _nextValue
|
|
275
|
+
}
|
|
276
|
+
return _object
|
|
277
|
+
}
|
package/lib/search.js
CHANGED
|
@@ -2,17 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true })
|
|
3
3
|
var core_1 = require("./core")
|
|
4
4
|
var reflect_1 = require("./reflect")
|
|
5
|
-
function getOffset(limit, page, firstLimit) {
|
|
6
|
-
var p = page && page > 0 ? page : 1
|
|
7
|
-
if (firstLimit && firstLimit > 0) {
|
|
8
|
-
var offset = limit * (p - 2) + firstLimit
|
|
9
|
-
return offset < 0 ? 0 : offset
|
|
10
|
-
} else {
|
|
11
|
-
var offset = limit * (p - 1)
|
|
12
|
-
return offset < 0 ? 0 : offset
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
exports.getOffset = getOffset
|
|
16
5
|
function mergeFilter(obj, b, pageSizes, arrs) {
|
|
17
6
|
var a = b
|
|
18
7
|
if (!b) {
|
|
@@ -150,6 +139,20 @@ function getFields(form, arr) {
|
|
|
150
139
|
return fields.length > 0 ? fields : undefined
|
|
151
140
|
}
|
|
152
141
|
exports.getFields = getFields
|
|
142
|
+
function getPageTotal(pageSize, total) {
|
|
143
|
+
if (!pageSize || pageSize <= 0) {
|
|
144
|
+
return 1
|
|
145
|
+
} else {
|
|
146
|
+
if (!total) {
|
|
147
|
+
total = 0
|
|
148
|
+
}
|
|
149
|
+
if (total % pageSize === 0) {
|
|
150
|
+
return Math.floor(total / pageSize)
|
|
151
|
+
}
|
|
152
|
+
return Math.floor(total / pageSize + 1)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
exports.getPageTotal = getPageTotal
|
|
153
156
|
function formatText() {
|
|
154
157
|
var args = []
|
|
155
158
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
@@ -174,20 +177,6 @@ function formatText() {
|
|
|
174
177
|
return formatted
|
|
175
178
|
}
|
|
176
179
|
exports.formatText = formatText
|
|
177
|
-
function getPageTotal(pageSize, total) {
|
|
178
|
-
if (!pageSize || pageSize <= 0) {
|
|
179
|
-
return 1
|
|
180
|
-
} else {
|
|
181
|
-
if (!total) {
|
|
182
|
-
total = 0
|
|
183
|
-
}
|
|
184
|
-
if (total % pageSize === 0) {
|
|
185
|
-
return Math.floor(total / pageSize)
|
|
186
|
-
}
|
|
187
|
-
return Math.floor(total / pageSize + 1)
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
exports.getPageTotal = getPageTotal
|
|
191
180
|
function buildMessage(resource, results, limit, page, total) {
|
|
192
181
|
if (!results || results.length === 0) {
|
|
193
182
|
return resource.msg_no_data_found
|
|
@@ -420,3 +409,14 @@ function toggleSortStyle(target) {
|
|
|
420
409
|
return field
|
|
421
410
|
}
|
|
422
411
|
exports.toggleSortStyle = toggleSortStyle
|
|
412
|
+
function getOffset(limit, page, firstLimit) {
|
|
413
|
+
var p = page && page > 0 ? page : 1
|
|
414
|
+
if (firstLimit && firstLimit > 0) {
|
|
415
|
+
var offset = limit * (p - 2) + firstLimit
|
|
416
|
+
return offset < 0 ? 0 : offset
|
|
417
|
+
} else {
|
|
418
|
+
var offset = limit * (p - 1)
|
|
419
|
+
return offset < 0 ? 0 : offset
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
exports.getOffset = getOffset
|
package/lib/update.js
CHANGED
|
@@ -16,7 +16,6 @@ var __assign =
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true })
|
|
17
17
|
var react_1 = require("react")
|
|
18
18
|
var core_1 = require("./core")
|
|
19
|
-
var merge_1 = require("./merge")
|
|
20
19
|
var state_1 = require("./state")
|
|
21
20
|
function removePhoneFormat(phone) {
|
|
22
21
|
if (phone) {
|
|
@@ -50,8 +49,33 @@ var m = "model"
|
|
|
50
49
|
var _getModelName = function (f2) {
|
|
51
50
|
return getModelName(f2, m)
|
|
52
51
|
}
|
|
52
|
+
function useMergeState(initialState) {
|
|
53
|
+
var _a = react_1.useState(initialState ? initialState : {}),
|
|
54
|
+
state = _a[0],
|
|
55
|
+
_setState = _a[1]
|
|
56
|
+
var callbackRef = react_1.useRef()
|
|
57
|
+
var setState = react_1.useCallback(
|
|
58
|
+
function (newState, callback) {
|
|
59
|
+
callbackRef.current = callback
|
|
60
|
+
_setState(function (prevState) {
|
|
61
|
+
return Object.assign({}, prevState, newState)
|
|
62
|
+
})
|
|
63
|
+
},
|
|
64
|
+
[state],
|
|
65
|
+
)
|
|
66
|
+
react_1.useEffect(
|
|
67
|
+
function () {
|
|
68
|
+
if (callbackRef.current) {
|
|
69
|
+
callbackRef.current(state)
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
[state],
|
|
73
|
+
)
|
|
74
|
+
return [state, setState]
|
|
75
|
+
}
|
|
76
|
+
exports.useMergeState = useMergeState
|
|
53
77
|
exports.useUpdate = function (initialState, getName, getLocale, removeErr) {
|
|
54
|
-
var _a =
|
|
78
|
+
var _a = useMergeState(initialState),
|
|
55
79
|
state = _a[0],
|
|
56
80
|
setState = _a[1]
|
|
57
81
|
var _b = react_1.useState(false),
|
package/lib/useEdit.js
CHANGED
|
@@ -16,12 +16,11 @@ var __assign =
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true })
|
|
17
17
|
var react_1 = require("react")
|
|
18
18
|
var react_router_1 = require("react-router")
|
|
19
|
+
var common_1 = require("./common")
|
|
19
20
|
var core_1 = require("./core")
|
|
20
21
|
var edit_1 = require("./edit")
|
|
21
|
-
var error_1 = require("./error")
|
|
22
22
|
var formutil_1 = require("./formutil")
|
|
23
23
|
var input_1 = require("./input")
|
|
24
|
-
var merge_1 = require("./merge")
|
|
25
24
|
var reflect_1 = require("./reflect")
|
|
26
25
|
var state_1 = require("./state")
|
|
27
26
|
var update_1 = require("./update")
|
|
@@ -203,7 +202,7 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
203
202
|
var baseProps = update_1.useUpdate(initialState, getModelName, p1.getLocale)
|
|
204
203
|
var state = baseProps.state,
|
|
205
204
|
setState = baseProps.setState
|
|
206
|
-
var _b =
|
|
205
|
+
var _b = update_1.useMergeState({
|
|
207
206
|
newMode: false,
|
|
208
207
|
setBack: false,
|
|
209
208
|
readOnly: p ? p.readOnly : undefined,
|
|
@@ -226,16 +225,16 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
226
225
|
showModel(model)
|
|
227
226
|
}
|
|
228
227
|
var _handleNotFound = function (form) {
|
|
229
|
-
var msg = error_1.message(p1.resource.value, "error_404", "error")
|
|
230
228
|
if (form) {
|
|
231
229
|
formutil_1.setReadOnly(form)
|
|
232
230
|
}
|
|
231
|
+
var resource = p1.resource.resource()
|
|
233
232
|
p1.showError(
|
|
234
|
-
|
|
233
|
+
resource.error_404,
|
|
235
234
|
function () {
|
|
236
235
|
return window.history.back
|
|
237
236
|
},
|
|
238
|
-
|
|
237
|
+
resource.error,
|
|
239
238
|
)
|
|
240
239
|
}
|
|
241
240
|
var handleNotFound = p && p.handleNotFound ? p.handleNotFound : _handleNotFound
|
|
@@ -261,15 +260,15 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
261
260
|
if (objKeys.length === 0) {
|
|
262
261
|
navigate(-1)
|
|
263
262
|
} else {
|
|
264
|
-
var
|
|
263
|
+
var resource = p1.resource.resource()
|
|
265
264
|
p1.confirm(
|
|
266
|
-
|
|
265
|
+
resource.msg_confirm_back,
|
|
267
266
|
function () {
|
|
268
267
|
navigate(-1)
|
|
269
268
|
},
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
269
|
+
resource.confirm,
|
|
270
|
+
resource.no,
|
|
271
|
+
resource.yes,
|
|
273
272
|
)
|
|
274
273
|
}
|
|
275
274
|
}
|
|
@@ -297,13 +296,12 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
297
296
|
}
|
|
298
297
|
}
|
|
299
298
|
var _onSave = function (isBack) {
|
|
299
|
+
var resource = p1.resource.resource()
|
|
300
300
|
if (p && p.readOnly) {
|
|
301
301
|
if (flag.newMode === true) {
|
|
302
|
-
|
|
303
|
-
p1.showError(m.message, undefined, m.title)
|
|
302
|
+
p1.showError(resource.error_permission_add, undefined, resource.error_permission)
|
|
304
303
|
} else {
|
|
305
|
-
|
|
306
|
-
p1.showError(msg.message, undefined, msg.title)
|
|
304
|
+
p1.showError(resource.error_permission_edit, undefined, resource.error_permission)
|
|
307
305
|
}
|
|
308
306
|
} else {
|
|
309
307
|
if (running === true) {
|
|
@@ -320,15 +318,14 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
320
318
|
}
|
|
321
319
|
if (flag.newMode) {
|
|
322
320
|
validate(obj_1, function () {
|
|
323
|
-
var msg = error_1.message(p1.resource.value, "msg_confirm_save", "confirm", "yes", "no")
|
|
324
321
|
p1.confirm(
|
|
325
|
-
|
|
322
|
+
resource.msg_confirm_save,
|
|
326
323
|
function () {
|
|
327
324
|
doSave(obj_1, undefined, version_1, isBack)
|
|
328
325
|
},
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
326
|
+
resource.confirm,
|
|
327
|
+
resource.no,
|
|
328
|
+
resource.yes,
|
|
332
329
|
)
|
|
333
330
|
})
|
|
334
331
|
} else {
|
|
@@ -338,15 +335,14 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
338
335
|
p1.showMessage(p1.resource.value("msg_no_change"))
|
|
339
336
|
} else {
|
|
340
337
|
validate(obj_1, function () {
|
|
341
|
-
var msg = error_1.message(p1.resource.value, "msg_confirm_save", "confirm", "yes", "no")
|
|
342
338
|
p1.confirm(
|
|
343
|
-
|
|
339
|
+
resource.msg_confirm_save,
|
|
344
340
|
function () {
|
|
345
341
|
doSave(obj_1, diffObj_1, version_1, isBack)
|
|
346
342
|
},
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
343
|
+
resource.confirm,
|
|
344
|
+
resource.no,
|
|
345
|
+
resource.yes,
|
|
350
346
|
)
|
|
351
347
|
})
|
|
352
348
|
}
|
|
@@ -468,8 +464,8 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
468
464
|
}
|
|
469
465
|
var postSave = p && p.postSave ? p.postSave : _postSave
|
|
470
466
|
var _handleDuplicateKey = function (result) {
|
|
471
|
-
var
|
|
472
|
-
p1.showError(
|
|
467
|
+
var resource = p1.resource.resource()
|
|
468
|
+
p1.showError(resource.error_duplicate_key, undefined, resource.error)
|
|
473
469
|
}
|
|
474
470
|
var handleDuplicateKey = p && p.handleDuplicateKey ? p.handleDuplicateKey : _handleDuplicateKey
|
|
475
471
|
var _doSave = function (obj, body, version, isBack) {
|
|
@@ -527,9 +523,9 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
527
523
|
.catch(function (err) {
|
|
528
524
|
var _a, _b, _c
|
|
529
525
|
var data = err && err.response ? err.response : err
|
|
530
|
-
var
|
|
531
|
-
var title =
|
|
532
|
-
var msg =
|
|
526
|
+
var resource = p1.resource.resource()
|
|
527
|
+
var title = resource.error
|
|
528
|
+
var msg = resource.error_internal
|
|
533
529
|
if (data && data.status === 422) {
|
|
534
530
|
fail((_a = err.response) === null || _a === void 0 ? void 0 : _a.data)
|
|
535
531
|
var obj = (_c = (_b = err.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.value
|
|
@@ -541,7 +537,7 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
541
537
|
handleNotFound(refForm.current)
|
|
542
538
|
} else {
|
|
543
539
|
if (data.status && !isNaN(data.status)) {
|
|
544
|
-
msg =
|
|
540
|
+
msg = common_1.messageByHttpStatus(data.status, resource)
|
|
545
541
|
}
|
|
546
542
|
if (data && (data.status === 401 || data.status === 403)) {
|
|
547
543
|
formutil_1.setReadOnly(refForm.current)
|
package/lib/useSearch.js
CHANGED
|
@@ -22,8 +22,8 @@ var __spreadArrays =
|
|
|
22
22
|
}
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true })
|
|
24
24
|
var react_1 = require("react")
|
|
25
|
+
var common_1 = require("./common")
|
|
25
26
|
var core_1 = require("./core")
|
|
26
|
-
var error_1 = require("./error")
|
|
27
27
|
var input_1 = require("./input")
|
|
28
28
|
var merge_1 = require("./merge")
|
|
29
29
|
var reflect_1 = require("./reflect")
|
|
@@ -504,7 +504,8 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
504
504
|
}
|
|
505
505
|
var searchError = function (err) {
|
|
506
506
|
setComponent({ page: component.tmpPageIndex })
|
|
507
|
-
|
|
507
|
+
var resource = p1.resource.resource()
|
|
508
|
+
common_1.error(err, resource, p1.showError)
|
|
508
509
|
input_1.hideLoading(p1.loading)
|
|
509
510
|
}
|
|
510
511
|
var appendList = p && p.appendList ? p.appendList : appendListOfState
|
package/package.json
CHANGED
package/src/common.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { StringMap } from "./core"
|
|
2
|
+
|
|
3
|
+
export function messageByHttpStatus(status: number, resource: StringMap): string {
|
|
4
|
+
const k = "error_" + status
|
|
5
|
+
let msg = resource[k]
|
|
6
|
+
if (!msg || msg.length === 0) {
|
|
7
|
+
msg = resource.error_500
|
|
8
|
+
}
|
|
9
|
+
return msg
|
|
10
|
+
}
|
|
11
|
+
export function error(err: any, resource: StringMap, ae: (msg: string, callback?: () => void, header?: string) => void) {
|
|
12
|
+
const title = resource.error
|
|
13
|
+
let msg = resource.error_internal
|
|
14
|
+
if (!err) {
|
|
15
|
+
ae(msg, undefined, title)
|
|
16
|
+
return
|
|
17
|
+
}
|
|
18
|
+
const data = err && err.response ? err.response : err
|
|
19
|
+
if (data) {
|
|
20
|
+
const status = data.status
|
|
21
|
+
if (status && !isNaN(status)) {
|
|
22
|
+
msg = messageByHttpStatus(status, resource)
|
|
23
|
+
}
|
|
24
|
+
ae(msg, undefined, title)
|
|
25
|
+
} else {
|
|
26
|
+
ae(msg, undefined, title)
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/core.ts
CHANGED
|
@@ -46,7 +46,6 @@ export interface ErrorMessage {
|
|
|
46
46
|
}
|
|
47
47
|
export interface UIService {
|
|
48
48
|
getValue(el: HTMLInputElement, locale?: Locale, currencyCode?: string): string | number | boolean | null | undefined
|
|
49
|
-
// decodeFromForm(form: HTMLFormElement, locale?: Locale, currencyCode?: string | null): any;
|
|
50
49
|
|
|
51
50
|
validateForm(form?: HTMLFormElement, locale?: Locale, focusFirst?: boolean, scroll?: boolean): boolean
|
|
52
51
|
removeFormError(form: HTMLFormElement): void
|
|
@@ -57,7 +56,7 @@ export interface UIService {
|
|
|
57
56
|
registerEvents?(form: HTMLFormElement): void
|
|
58
57
|
}
|
|
59
58
|
|
|
60
|
-
export type
|
|
59
|
+
export type Type =
|
|
61
60
|
| "ObjectId"
|
|
62
61
|
| "date"
|
|
63
62
|
| "datetime"
|
|
@@ -79,9 +78,21 @@ export type DataType =
|
|
|
79
78
|
| "datetimes"
|
|
80
79
|
| "times"
|
|
81
80
|
|
|
81
|
+
export type Format = "currency" | "percentage" | "email" | "url" | "phone" | "fax" | "ipv4" | "ipv6"
|
|
82
|
+
|
|
83
|
+
export interface Attribute {
|
|
84
|
+
name?: string
|
|
85
|
+
type?: Type
|
|
86
|
+
format?: Format
|
|
87
|
+
key?: boolean
|
|
88
|
+
version?: boolean
|
|
89
|
+
typeof?: Attributes
|
|
90
|
+
scale?: number
|
|
91
|
+
noformat?: boolean
|
|
92
|
+
}
|
|
82
93
|
export interface Attribute {
|
|
83
94
|
name?: string
|
|
84
|
-
type?:
|
|
95
|
+
type?: Type
|
|
85
96
|
key?: boolean
|
|
86
97
|
version?: boolean
|
|
87
98
|
typeof?: Attributes
|
package/src/input.ts
CHANGED
|
@@ -82,14 +82,22 @@ export function getErrorFunc(
|
|
|
82
82
|
return (p as any).showError
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
export function showLoading(
|
|
86
|
-
if (
|
|
87
|
-
|
|
85
|
+
export function showLoading(loading?: LoadingService | ((firstTime?: boolean) => void)): void {
|
|
86
|
+
if (loading) {
|
|
87
|
+
if (typeof loading === "function") {
|
|
88
|
+
loading()
|
|
89
|
+
} else {
|
|
90
|
+
loading.showLoading()
|
|
91
|
+
}
|
|
88
92
|
}
|
|
89
93
|
}
|
|
90
|
-
export function hideLoading(
|
|
91
|
-
if (
|
|
92
|
-
|
|
94
|
+
export function hideLoading(loading?: LoadingService | (() => void)): void {
|
|
95
|
+
if (loading) {
|
|
96
|
+
if (typeof loading === "function") {
|
|
97
|
+
loading()
|
|
98
|
+
} else {
|
|
99
|
+
loading.hideLoading()
|
|
100
|
+
}
|
|
93
101
|
}
|
|
94
102
|
}
|
|
95
103
|
|
package/src/reflect.ts
CHANGED
|
@@ -36,43 +36,6 @@ export function clone(obj: any): any {
|
|
|
36
36
|
return x
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
export function getDirectValue(obj: any, key: string): any {
|
|
40
|
-
if (obj && obj.hasOwnProperty(key)) {
|
|
41
|
-
return obj[key]
|
|
42
|
-
}
|
|
43
|
-
return null
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function setValue(obj: any, key: string, value: any): any {
|
|
47
|
-
let replaceKey = key.replace(/\[/g, ".[").replace(/\.\./g, ".")
|
|
48
|
-
if (replaceKey.indexOf(".") === 0) {
|
|
49
|
-
replaceKey = replaceKey.slice(1, replaceKey.length)
|
|
50
|
-
}
|
|
51
|
-
const keys = replaceKey.split(".")
|
|
52
|
-
const firstKey = keys.shift()
|
|
53
|
-
if (!firstKey) {
|
|
54
|
-
return
|
|
55
|
-
}
|
|
56
|
-
const isArrayKey = /\[([0-9]+)\]/.test(firstKey)
|
|
57
|
-
if (keys.length > 0) {
|
|
58
|
-
const firstKeyValue = obj[firstKey] || {}
|
|
59
|
-
const returnValue = setValue(firstKeyValue, keys.join("."), value)
|
|
60
|
-
return setKey(obj, isArrayKey, firstKey, returnValue)
|
|
61
|
-
}
|
|
62
|
-
return setKey(obj, isArrayKey, firstKey, value)
|
|
63
|
-
}
|
|
64
|
-
const setKey = (_object: any, _isArrayKey: boolean, _key: string, _nextValue: any) => {
|
|
65
|
-
if (_isArrayKey) {
|
|
66
|
-
if (_object.length > _key) {
|
|
67
|
-
_object[_key] = _nextValue
|
|
68
|
-
} else {
|
|
69
|
-
_object.push(_nextValue)
|
|
70
|
-
}
|
|
71
|
-
} else {
|
|
72
|
-
_object[_key] = _nextValue
|
|
73
|
-
}
|
|
74
|
-
return _object
|
|
75
|
-
}
|
|
76
39
|
export function isEmptyObject(obj: any): boolean {
|
|
77
40
|
for (let key in obj) {
|
|
78
41
|
if (obj.hasOwnProperty(key)) {
|
|
@@ -254,3 +217,41 @@ export function equalAll<T>(list: T[] | undefined | null, name: string, v: boole
|
|
|
254
217
|
}
|
|
255
218
|
return true
|
|
256
219
|
}
|
|
220
|
+
|
|
221
|
+
export function getDirectValue(obj: any, key: string): any {
|
|
222
|
+
if (obj && obj.hasOwnProperty(key)) {
|
|
223
|
+
return obj[key]
|
|
224
|
+
}
|
|
225
|
+
return null
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function setValue(obj: any, key: string, value: any): any {
|
|
229
|
+
let replaceKey = key.replace(/\[/g, ".[").replace(/\.\./g, ".")
|
|
230
|
+
if (replaceKey.indexOf(".") === 0) {
|
|
231
|
+
replaceKey = replaceKey.slice(1, replaceKey.length)
|
|
232
|
+
}
|
|
233
|
+
const keys = replaceKey.split(".")
|
|
234
|
+
const firstKey = keys.shift()
|
|
235
|
+
if (!firstKey) {
|
|
236
|
+
return
|
|
237
|
+
}
|
|
238
|
+
const isArrayKey = /\[([0-9]+)\]/.test(firstKey)
|
|
239
|
+
if (keys.length > 0) {
|
|
240
|
+
const firstKeyValue = obj[firstKey] || {}
|
|
241
|
+
const returnValue = setValue(firstKeyValue, keys.join("."), value)
|
|
242
|
+
return setKey(obj, isArrayKey, firstKey, returnValue)
|
|
243
|
+
}
|
|
244
|
+
return setKey(obj, isArrayKey, firstKey, value)
|
|
245
|
+
}
|
|
246
|
+
const setKey = (_object: any, _isArrayKey: boolean, _key: string, _nextValue: any) => {
|
|
247
|
+
if (_isArrayKey) {
|
|
248
|
+
if (_object.length > _key) {
|
|
249
|
+
_object[_key] = _nextValue
|
|
250
|
+
} else {
|
|
251
|
+
_object.push(_nextValue)
|
|
252
|
+
}
|
|
253
|
+
} else {
|
|
254
|
+
_object[_key] = _nextValue
|
|
255
|
+
}
|
|
256
|
+
return _object
|
|
257
|
+
}
|
package/src/search.ts
CHANGED
|
@@ -44,16 +44,6 @@ export interface Pagination {
|
|
|
44
44
|
|
|
45
45
|
interface Searchable extends Pagination, Sortable {}
|
|
46
46
|
|
|
47
|
-
export function getOffset(limit: number, page?: number, firstLimit?: number): number {
|
|
48
|
-
const p = page && page > 0 ? page : 1
|
|
49
|
-
if (firstLimit && firstLimit > 0) {
|
|
50
|
-
const offset = limit * (p - 2) + firstLimit
|
|
51
|
-
return offset < 0 ? 0 : offset
|
|
52
|
-
} else {
|
|
53
|
-
const offset = limit * (p - 1)
|
|
54
|
-
return offset < 0 ? 0 : offset
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
47
|
export function mergeFilter<S extends Filter>(obj: S, b?: S, pageSizes?: number[], arrs?: string[] | any) {
|
|
58
48
|
let a: any = b
|
|
59
49
|
if (!b) {
|
|
@@ -148,11 +138,6 @@ export function initFilter<S extends Filter>(m: S, com: Searchable): S {
|
|
|
148
138
|
com.sortType = ""
|
|
149
139
|
}
|
|
150
140
|
}
|
|
151
|
-
/*
|
|
152
|
-
delete m.page;
|
|
153
|
-
delete m.limit;
|
|
154
|
-
delete m.firstLimit;
|
|
155
|
-
*/
|
|
156
141
|
return m
|
|
157
142
|
}
|
|
158
143
|
|
|
@@ -195,6 +180,19 @@ export function getFields(form?: HTMLFormElement, arr?: string[]): string[] | un
|
|
|
195
180
|
return fields.length > 0 ? fields : undefined
|
|
196
181
|
}
|
|
197
182
|
|
|
183
|
+
export function getPageTotal(pageSize?: number, total?: number): number {
|
|
184
|
+
if (!pageSize || pageSize <= 0) {
|
|
185
|
+
return 1
|
|
186
|
+
} else {
|
|
187
|
+
if (!total) {
|
|
188
|
+
total = 0
|
|
189
|
+
}
|
|
190
|
+
if (total % pageSize === 0) {
|
|
191
|
+
return Math.floor(total / pageSize)
|
|
192
|
+
}
|
|
193
|
+
return Math.floor(total / pageSize + 1)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
198
196
|
export function formatText(...args: any[]): string {
|
|
199
197
|
let formatted = args[0]
|
|
200
198
|
if (!formatted || formatted === "") {
|
|
@@ -214,19 +212,6 @@ export function formatText(...args: any[]): string {
|
|
|
214
212
|
}
|
|
215
213
|
return formatted
|
|
216
214
|
}
|
|
217
|
-
export function getPageTotal(pageSize?: number, total?: number): number {
|
|
218
|
-
if (!pageSize || pageSize <= 0) {
|
|
219
|
-
return 1
|
|
220
|
-
} else {
|
|
221
|
-
if (!total) {
|
|
222
|
-
total = 0
|
|
223
|
-
}
|
|
224
|
-
if (total % pageSize === 0) {
|
|
225
|
-
return Math.floor(total / pageSize)
|
|
226
|
-
}
|
|
227
|
-
return Math.floor(total / pageSize + 1)
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
215
|
export function buildMessage<T>(resource: StringMap, results: T[], limit: number, page: number | undefined, total?: number): string {
|
|
231
216
|
if (!results || results.length === 0) {
|
|
232
217
|
return resource.msg_no_data_found
|
|
@@ -456,3 +441,14 @@ export function toggleSortStyle(target: HTMLElement): string {
|
|
|
456
441
|
}
|
|
457
442
|
return field
|
|
458
443
|
}
|
|
444
|
+
|
|
445
|
+
export function getOffset(limit: number, page?: number, firstLimit?: number): number {
|
|
446
|
+
const p = page && page > 0 ? page : 1
|
|
447
|
+
if (firstLimit && firstLimit > 0) {
|
|
448
|
+
const offset = limit * (p - 2) + firstLimit
|
|
449
|
+
return offset < 0 ? 0 : offset
|
|
450
|
+
} else {
|
|
451
|
+
const offset = limit * (p - 1)
|
|
452
|
+
return offset < 0 ? 0 : offset
|
|
453
|
+
}
|
|
454
|
+
}
|
package/src/update.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { useEffect, useState } from "react"
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react"
|
|
2
2
|
import { Locale, resources } from "./core"
|
|
3
|
-
import { useMergeState } from "./merge"
|
|
4
3
|
import { buildFlatState, buildState, handleEvent, localeOf } from "./state"
|
|
5
4
|
|
|
6
5
|
export function removePhoneFormat(phone: string): string {
|
|
@@ -34,6 +33,32 @@ const m = "model"
|
|
|
34
33
|
const _getModelName = (f2?: HTMLFormElement | null): string => {
|
|
35
34
|
return getModelName(f2, m)
|
|
36
35
|
}
|
|
36
|
+
|
|
37
|
+
export type Callback<T> = (value?: T) => void
|
|
38
|
+
export type DispatchWithCallback<T> = (value: T, callback?: Callback<T>) => void
|
|
39
|
+
|
|
40
|
+
export function useMergeState<T>(initialState?: T | (() => T)): [T, DispatchWithCallback<Partial<T>>] {
|
|
41
|
+
const [state, _setState] = useState(initialState ? initialState : ({} as any))
|
|
42
|
+
|
|
43
|
+
const callbackRef = useRef<Callback<T>>()
|
|
44
|
+
|
|
45
|
+
const setState = useCallback(
|
|
46
|
+
(newState: Partial<T>, callback?: Callback<T>): void => {
|
|
47
|
+
callbackRef.current = callback
|
|
48
|
+
_setState((prevState: any) => Object.assign({}, prevState, newState))
|
|
49
|
+
},
|
|
50
|
+
[state],
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (callbackRef.current) {
|
|
55
|
+
callbackRef.current(state)
|
|
56
|
+
}
|
|
57
|
+
}, [state])
|
|
58
|
+
|
|
59
|
+
return [state, setState]
|
|
60
|
+
}
|
|
61
|
+
|
|
37
62
|
export const useUpdate = <T>(
|
|
38
63
|
initialState: T,
|
|
39
64
|
getName?: ((f?: HTMLFormElement | null) => string) | string,
|
package/src/useEdit.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { useEffect, useState } from "react"
|
|
2
2
|
import { Params, useNavigate, useParams } from "react-router"
|
|
3
|
+
import { messageByHttpStatus } from "./common"
|
|
3
4
|
import { Attribute, Attributes, ErrorMessage, LoadingService, Locale, resources, ResourceService, UIService } from "./core"
|
|
4
5
|
import { createModel as createModel2 } from "./edit"
|
|
5
|
-
import { message, messageByHttpStatus } from "./error"
|
|
6
6
|
import { focusFirstError, setReadOnly } from "./formutil"
|
|
7
7
|
import { hideLoading, initForm, showLoading } from "./input"
|
|
8
|
-
import { DispatchWithCallback, useMergeState } from "./merge"
|
|
9
8
|
import { clone, makeDiff } from "./reflect"
|
|
10
9
|
import { localeOf } from "./state"
|
|
11
|
-
import { getModelName as getModelName2, useUpdate } from "./update"
|
|
10
|
+
import { DispatchWithCallback, getModelName as getModelName2, useMergeState, useUpdate } from "./update"
|
|
12
11
|
|
|
13
12
|
export function buildKeys(attributes: Attributes): string[] {
|
|
14
13
|
if (!attributes) {
|
|
@@ -337,11 +336,11 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
337
336
|
}
|
|
338
337
|
|
|
339
338
|
const _handleNotFound = (form?: any): void => {
|
|
340
|
-
const msg = message(p1.resource.value, "error_404", "error")
|
|
341
339
|
if (form) {
|
|
342
340
|
setReadOnly(form)
|
|
343
341
|
}
|
|
344
|
-
|
|
342
|
+
const resource = p1.resource.resource()
|
|
343
|
+
p1.showError(resource.error_404, () => window.history.back, resource.error)
|
|
345
344
|
}
|
|
346
345
|
const handleNotFound = p && p.handleNotFound ? p.handleNotFound : _handleNotFound
|
|
347
346
|
|
|
@@ -368,15 +367,15 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
368
367
|
if (objKeys.length === 0) {
|
|
369
368
|
navigate(-1)
|
|
370
369
|
} else {
|
|
371
|
-
const
|
|
370
|
+
const resource = p1.resource.resource()
|
|
372
371
|
p1.confirm(
|
|
373
|
-
|
|
372
|
+
resource.msg_confirm_back,
|
|
374
373
|
() => {
|
|
375
374
|
navigate(-1)
|
|
376
375
|
},
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
376
|
+
resource.confirm,
|
|
377
|
+
resource.no,
|
|
378
|
+
resource.yes,
|
|
380
379
|
)
|
|
381
380
|
}
|
|
382
381
|
}
|
|
@@ -406,13 +405,12 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
406
405
|
}
|
|
407
406
|
|
|
408
407
|
const _onSave = (isBack?: boolean) => {
|
|
408
|
+
const resource = p1.resource.resource()
|
|
409
409
|
if (p && p.readOnly) {
|
|
410
410
|
if (flag.newMode === true) {
|
|
411
|
-
|
|
412
|
-
p1.showError(m.message, undefined, m.title)
|
|
411
|
+
p1.showError(resource.error_permission_add, undefined, resource.error_permission)
|
|
413
412
|
} else {
|
|
414
|
-
|
|
415
|
-
p1.showError(msg.message, undefined, msg.title)
|
|
413
|
+
p1.showError(resource.error_permission_edit, undefined, resource.error_permission)
|
|
416
414
|
}
|
|
417
415
|
} else {
|
|
418
416
|
if (running === true) {
|
|
@@ -429,15 +427,14 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
429
427
|
}
|
|
430
428
|
if (flag.newMode) {
|
|
431
429
|
validate(obj, () => {
|
|
432
|
-
const msg = message(p1.resource.value, "msg_confirm_save", "confirm", "yes", "no")
|
|
433
430
|
p1.confirm(
|
|
434
|
-
|
|
431
|
+
resource.msg_confirm_save,
|
|
435
432
|
() => {
|
|
436
433
|
doSave(obj, undefined, version, isBack)
|
|
437
434
|
},
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
435
|
+
resource.confirm,
|
|
436
|
+
resource.no,
|
|
437
|
+
resource.yes,
|
|
441
438
|
)
|
|
442
439
|
})
|
|
443
440
|
} else {
|
|
@@ -447,15 +444,14 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
447
444
|
p1.showMessage(p1.resource.value("msg_no_change"))
|
|
448
445
|
} else {
|
|
449
446
|
validate(obj, () => {
|
|
450
|
-
const msg = message(p1.resource.value, "msg_confirm_save", "confirm", "yes", "no")
|
|
451
447
|
p1.confirm(
|
|
452
|
-
|
|
448
|
+
resource.msg_confirm_save,
|
|
453
449
|
() => {
|
|
454
450
|
doSave(obj, diffObj as any, version, isBack)
|
|
455
451
|
},
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
452
|
+
resource.confirm,
|
|
453
|
+
resource.no,
|
|
454
|
+
resource.yes,
|
|
459
455
|
)
|
|
460
456
|
})
|
|
461
457
|
}
|
|
@@ -584,8 +580,8 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
584
580
|
const postSave = p && p.postSave ? p.postSave : _postSave
|
|
585
581
|
|
|
586
582
|
const _handleDuplicateKey = (result?: T) => {
|
|
587
|
-
const
|
|
588
|
-
p1.showError(
|
|
583
|
+
const resource = p1.resource.resource()
|
|
584
|
+
p1.showError(resource.error_duplicate_key, undefined, resource.error)
|
|
589
585
|
}
|
|
590
586
|
const handleDuplicateKey = p && p.handleDuplicateKey ? p.handleDuplicateKey : _handleDuplicateKey
|
|
591
587
|
|
|
@@ -641,9 +637,9 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
641
637
|
})
|
|
642
638
|
.catch((err: any) => {
|
|
643
639
|
const data = err && err.response ? err.response : err
|
|
644
|
-
const
|
|
645
|
-
const title =
|
|
646
|
-
let msg =
|
|
640
|
+
const resource = p1.resource.resource()
|
|
641
|
+
const title = resource.error
|
|
642
|
+
let msg = resource.error_internal
|
|
647
643
|
if (data && data.status === 422) {
|
|
648
644
|
fail(err.response?.data)
|
|
649
645
|
const obj = err.response?.data?.value
|
|
@@ -655,7 +651,7 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
655
651
|
handleNotFound(refForm.current)
|
|
656
652
|
} else {
|
|
657
653
|
if (data.status && !isNaN(data.status)) {
|
|
658
|
-
msg = messageByHttpStatus(data.status,
|
|
654
|
+
msg = messageByHttpStatus(data.status, resource)
|
|
659
655
|
}
|
|
660
656
|
if (data && (data.status === 401 || data.status === 403)) {
|
|
661
657
|
setReadOnly(refForm.current)
|
package/src/useSearch.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useEffect, useState } from "react"
|
|
2
|
+
import { error } from "./common"
|
|
2
3
|
import { LoadingService, Locale, pageSizes, resources, ResourceService, UIService } from "./core"
|
|
3
|
-
import { error } from "./error"
|
|
4
4
|
import { hideLoading, initForm, showLoading } from "./input"
|
|
5
5
|
import { DispatchWithCallback, useMergeState } from "./merge"
|
|
6
6
|
import { clone } from "./reflect"
|
|
@@ -656,7 +656,8 @@ export const useCoreSearch = <T, S extends Filter, ST>(
|
|
|
656
656
|
|
|
657
657
|
const searchError = (err: any): void => {
|
|
658
658
|
setComponent({ page: component.tmpPageIndex })
|
|
659
|
-
|
|
659
|
+
const resource = p1.resource.resource()
|
|
660
|
+
error(err, resource, p1.showError)
|
|
660
661
|
hideLoading(p1.loading)
|
|
661
662
|
}
|
|
662
663
|
const appendList = p && p.appendList ? p.appendList : appendListOfState
|
package/lib/error.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict"
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true })
|
|
3
|
-
function getString(key, gv) {
|
|
4
|
-
if (typeof gv === "function") {
|
|
5
|
-
return gv(key)
|
|
6
|
-
} else {
|
|
7
|
-
return gv[key]
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
exports.getString = getString
|
|
11
|
-
function message(gv, msg, title, yes, no) {
|
|
12
|
-
var m2 = msg && msg.length > 0 ? getString(msg, gv) : ""
|
|
13
|
-
var m = { message: m2, title: "" }
|
|
14
|
-
if (title && title.length > 0) {
|
|
15
|
-
m.title = getString(title, gv)
|
|
16
|
-
}
|
|
17
|
-
if (yes && yes.length > 0) {
|
|
18
|
-
m.yes = getString(yes, gv)
|
|
19
|
-
}
|
|
20
|
-
if (no && no.length > 0) {
|
|
21
|
-
m.no = getString(no, gv)
|
|
22
|
-
}
|
|
23
|
-
return m
|
|
24
|
-
}
|
|
25
|
-
exports.message = message
|
|
26
|
-
function messageByHttpStatus(status, gv) {
|
|
27
|
-
var k = "error_" + status
|
|
28
|
-
var msg = getString(k, gv)
|
|
29
|
-
if (!msg || msg.length === 0) {
|
|
30
|
-
msg = getString("error_500", gv)
|
|
31
|
-
}
|
|
32
|
-
return msg
|
|
33
|
-
}
|
|
34
|
-
exports.messageByHttpStatus = messageByHttpStatus
|
|
35
|
-
function error(err, gv, ae) {
|
|
36
|
-
var title = getString("error", gv)
|
|
37
|
-
var msg = getString("error_internal", gv)
|
|
38
|
-
if (!err) {
|
|
39
|
-
ae(msg, undefined, title)
|
|
40
|
-
return
|
|
41
|
-
}
|
|
42
|
-
var data = err && err.response ? err.response : err
|
|
43
|
-
if (data) {
|
|
44
|
-
var status_1 = data.status
|
|
45
|
-
if (status_1 && !isNaN(status_1)) {
|
|
46
|
-
msg = messageByHttpStatus(status_1, gv)
|
|
47
|
-
}
|
|
48
|
-
ae(msg, undefined, title)
|
|
49
|
-
} else {
|
|
50
|
-
ae(msg, undefined, title)
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
exports.error = error
|
package/lib/merge.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict"
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true })
|
|
3
|
-
var react_1 = require("react")
|
|
4
|
-
function useMergeState(initialState) {
|
|
5
|
-
var _a = react_1.useState(initialState ? initialState : {}),
|
|
6
|
-
state = _a[0],
|
|
7
|
-
_setState = _a[1]
|
|
8
|
-
var callbackRef = react_1.useRef()
|
|
9
|
-
var setState = react_1.useCallback(
|
|
10
|
-
function (newState, callback) {
|
|
11
|
-
callbackRef.current = callback
|
|
12
|
-
_setState(function (prevState) {
|
|
13
|
-
return Object.assign({}, prevState, newState)
|
|
14
|
-
})
|
|
15
|
-
},
|
|
16
|
-
[state],
|
|
17
|
-
)
|
|
18
|
-
react_1.useEffect(
|
|
19
|
-
function () {
|
|
20
|
-
if (callbackRef.current) {
|
|
21
|
-
callbackRef.current(state)
|
|
22
|
-
}
|
|
23
|
-
},
|
|
24
|
-
[state],
|
|
25
|
-
)
|
|
26
|
-
return [state, setState]
|
|
27
|
-
}
|
|
28
|
-
exports.useMergeState = useMergeState
|
package/src/error.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { StringMap } from "./core"
|
|
2
|
-
|
|
3
|
-
export interface Message {
|
|
4
|
-
message: string
|
|
5
|
-
title: string
|
|
6
|
-
yes?: string
|
|
7
|
-
no?: string
|
|
8
|
-
}
|
|
9
|
-
export function getString(key: string, gv: StringMap | ((key: string) => string)): string {
|
|
10
|
-
if (typeof gv === "function") {
|
|
11
|
-
return gv(key)
|
|
12
|
-
} else {
|
|
13
|
-
return gv[key]
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
export function message(gv: StringMap | ((key: string) => string), msg: string, title?: string, yes?: string, no?: string): Message {
|
|
17
|
-
const m2 = msg && msg.length > 0 ? getString(msg, gv) : ""
|
|
18
|
-
const m: Message = { message: m2, title: "" }
|
|
19
|
-
if (title && title.length > 0) {
|
|
20
|
-
m.title = getString(title, gv)
|
|
21
|
-
}
|
|
22
|
-
if (yes && yes.length > 0) {
|
|
23
|
-
m.yes = getString(yes, gv)
|
|
24
|
-
}
|
|
25
|
-
if (no && no.length > 0) {
|
|
26
|
-
m.no = getString(no, gv)
|
|
27
|
-
}
|
|
28
|
-
return m
|
|
29
|
-
}
|
|
30
|
-
export function messageByHttpStatus(status: number, gv: StringMap | ((key: string) => string)): string {
|
|
31
|
-
const k = "error_" + status
|
|
32
|
-
let msg = getString(k, gv)
|
|
33
|
-
if (!msg || msg.length === 0) {
|
|
34
|
-
msg = getString("error_500", gv)
|
|
35
|
-
}
|
|
36
|
-
return msg
|
|
37
|
-
}
|
|
38
|
-
export function error(err: any, gv: StringMap | ((key: string) => string), ae: (msg: string, callback?: () => void, header?: string) => void) {
|
|
39
|
-
const title = getString("error", gv)
|
|
40
|
-
let msg = getString("error_internal", gv)
|
|
41
|
-
if (!err) {
|
|
42
|
-
ae(msg, undefined, title)
|
|
43
|
-
return
|
|
44
|
-
}
|
|
45
|
-
const data = err && err.response ? err.response : err
|
|
46
|
-
if (data) {
|
|
47
|
-
const status = data.status
|
|
48
|
-
if (status && !isNaN(status)) {
|
|
49
|
-
msg = messageByHttpStatus(status, gv)
|
|
50
|
-
}
|
|
51
|
-
ae(msg, undefined, title)
|
|
52
|
-
} else {
|
|
53
|
-
ae(msg, undefined, title)
|
|
54
|
-
}
|
|
55
|
-
}
|
package/src/merge.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { useCallback, useEffect, useRef, useState } from "react"
|
|
2
|
-
|
|
3
|
-
export type Callback<T> = (value?: T) => void
|
|
4
|
-
export type DispatchWithCallback<T> = (value: T, callback?: Callback<T>) => void
|
|
5
|
-
|
|
6
|
-
export function useMergeState<T>(initialState?: T | (() => T)): [T, DispatchWithCallback<Partial<T>>] {
|
|
7
|
-
const [state, _setState] = useState(initialState ? initialState : ({} as any))
|
|
8
|
-
|
|
9
|
-
const callbackRef = useRef<Callback<T>>()
|
|
10
|
-
|
|
11
|
-
const setState = useCallback(
|
|
12
|
-
(newState: Partial<T>, callback?: Callback<T>): void => {
|
|
13
|
-
callbackRef.current = callback
|
|
14
|
-
_setState((prevState: any) => Object.assign({}, prevState, newState))
|
|
15
|
-
},
|
|
16
|
-
[state],
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
useEffect(() => {
|
|
20
|
-
if (callbackRef.current) {
|
|
21
|
-
callbackRef.current(state)
|
|
22
|
-
}
|
|
23
|
-
}, [state])
|
|
24
|
-
|
|
25
|
-
return [state, setState]
|
|
26
|
-
}
|