react-hook-core 0.1.18 → 0.1.20

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/components.js CHANGED
@@ -808,19 +808,18 @@ var BaseEditComponent = (function (_super) {
808
808
  };
809
809
  BaseEditComponent.prototype.doSave = function (obj, dif, isBack) {
810
810
  };
811
- BaseEditComponent.prototype.succeed = function (msg, isBack, result) {
812
- if (result) {
813
- var model = result.value;
811
+ BaseEditComponent.prototype.succeed = function (msg, origin, isBack, model) {
812
+ if (model) {
814
813
  this.newMode = false;
815
814
  if (model && this.setBack) {
816
815
  this.resetState(false, model, reflect_1.clone(model));
817
816
  }
818
817
  else {
819
- edit_1.handleVersion(this.getModel(), this.version);
818
+ edit_1.handleVersion(origin, this.version);
820
819
  }
821
820
  }
822
821
  else {
823
- edit_1.handleVersion(this.getModel(), this.version);
822
+ edit_1.handleVersion(origin, this.version);
824
823
  }
825
824
  var isBackO = (isBack == null || isBack === undefined ? this.backOnSuccess : isBack);
826
825
  this.showMessage(msg);
@@ -831,55 +830,31 @@ var BaseEditComponent = (function (_super) {
831
830
  BaseEditComponent.prototype.fail = function (result) {
832
831
  var f = this.form;
833
832
  var u = this.ui;
834
- if (Array.isArray(result)) {
835
- if (u && f) {
836
- var unmappedErrors = u.showFormError(f, result);
837
- formutil_1.focusFirstError(f);
838
- if (unmappedErrors && unmappedErrors.length > 0) {
839
- var t = this.resourceService.value('error');
840
- if (u && u.buildErrorMessage) {
841
- var msg = u.buildErrorMessage(unmappedErrors);
842
- this.showError(msg, t);
843
- }
844
- else {
845
- this.showError(unmappedErrors[0].field + ' ' + unmappedErrors[0].code + ' ' + unmappedErrors[0].message, t);
846
- }
847
- }
848
- }
849
- else {
833
+ if (u && f) {
834
+ var unmappedErrors = u.showFormError(f, result);
835
+ formutil_1.focusFirstError(f);
836
+ if (unmappedErrors && unmappedErrors.length > 0) {
850
837
  var t = this.resourceService.value('error');
851
- if (result.length > 0) {
852
- this.showError(result[0].field + ' ' + result[0].code + ' ' + result[0].message, t);
838
+ if (u && u.buildErrorMessage) {
839
+ var msg = u.buildErrorMessage(unmappedErrors);
840
+ this.showError(msg, t);
853
841
  }
854
842
  else {
855
- this.showError(t, t);
843
+ this.showError(unmappedErrors[0].field + ' ' + unmappedErrors[0].code + ' ' + unmappedErrors[0].message, t);
856
844
  }
857
845
  }
858
846
  }
859
847
  else {
860
- var errors = result.errors;
861
- if (u && f) {
862
- var unmappedErrors = u.showFormError(f, errors);
863
- if (!result.message) {
864
- if (errors && errors.length === 1) {
865
- result.message = errors[0].message;
866
- }
867
- else {
868
- result.message = u.buildErrorMessage(unmappedErrors);
869
- }
870
- }
871
- formutil_1.focusFirstError(f);
872
- }
873
- else if (errors && errors.length === 1) {
874
- result.message = errors[0].message;
848
+ var t = this.resourceService.value('error');
849
+ if (result.length > 0) {
850
+ this.showError(result[0].field + ' ' + result[0].code + ' ' + result[0].message, t);
875
851
  }
876
- if (result.message) {
877
- var t = this.resourceService.value('error');
878
- this.showError(result.message, t);
852
+ else {
853
+ this.showError(t, t);
879
854
  }
880
855
  }
881
856
  };
882
- BaseEditComponent.prototype.postSave = function (res, backOnSave) {
857
+ BaseEditComponent.prototype.postSave = function (res, origin, isPatch, backOnSave) {
883
858
  this.running = false;
884
859
  core_1.hideLoading(this.loading);
885
860
  var st = this.status;
@@ -892,7 +867,7 @@ var BaseEditComponent = (function (_super) {
892
867
  }
893
868
  else if (!isNaN(x)) {
894
869
  if (x === st.success) {
895
- this.succeed(successMsg, backOnSave);
870
+ this.succeed(successMsg, origin, backOnSave);
896
871
  }
897
872
  else {
898
873
  if (newMod && x === st.duplicate_key) {
@@ -908,22 +883,19 @@ var BaseEditComponent = (function (_super) {
908
883
  }
909
884
  else {
910
885
  var result = x;
911
- if (result.status === st.success) {
912
- this.succeed(successMsg, backOnSave, result);
913
- this.showMessage(successMsg);
914
- }
915
- else if (result.errors && result.errors.length > 0) {
916
- this.fail(result);
917
- }
918
- else if (newMod && result.status === st.duplicate_key) {
919
- this.handleDuplicateKey(result);
920
- }
921
- else if (!newMod && x === st.not_found) {
922
- this.handleNotFound();
886
+ if (isPatch) {
887
+ var keys = Object.keys(result);
888
+ var a = origin;
889
+ for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
890
+ var k = keys_1[_i];
891
+ a[k] = result[k];
892
+ }
893
+ this.succeed(successMsg, a, backOnSave);
923
894
  }
924
895
  else {
925
- edit_1.handleStatus(result.status, st, r.value, this.showError);
896
+ this.succeed(successMsg, origin, backOnSave, result);
926
897
  }
898
+ this.showMessage(successMsg);
927
899
  }
928
900
  };
929
901
  BaseEditComponent.prototype.handleDuplicateKey = function (result) {
@@ -1027,14 +999,16 @@ var EditComponent = (function (_super) {
1027
999
  var com = this;
1028
1000
  var m = obj;
1029
1001
  var fn = this.newMode ? this.service.insert : this.service.update;
1002
+ var isPatch = false;
1030
1003
  if (!this.newMode) {
1031
1004
  if (this.patchable === true && this.service.patch && body && Object.keys(body).length > 0) {
1032
1005
  m = body;
1006
+ isPatch = true;
1033
1007
  fn = this.service.patch;
1034
1008
  }
1035
1009
  }
1036
1010
  fn(m).then(function (result) {
1037
- com.postSave(result, isBackO);
1011
+ com.postSave(result, obj, isBackO);
1038
1012
  com.running = false;
1039
1013
  core_1.hideLoading(com.loading);
1040
1014
  }).then(function (err) {
package/lib/core.js CHANGED
@@ -11,9 +11,9 @@ function createEditStatus(status) {
11
11
  duplicate_key: 0,
12
12
  not_found: 0,
13
13
  success: 1,
14
- version_error: 2,
15
- error: 4,
16
- data_corrupt: 8
14
+ version_error: -1,
15
+ error: 2,
16
+ data_corrupt: 4
17
17
  };
18
18
  return s;
19
19
  }
package/lib/index.js CHANGED
@@ -4,6 +4,7 @@ function __export(m) {
4
4
  }
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  var React = require("react");
7
+ var react_1 = require("react");
7
8
  __export(require("./formutil"));
8
9
  __export(require("./util"));
9
10
  __export(require("./core"));
@@ -20,6 +21,21 @@ __export(require("./components"));
20
21
  __export(require("./search"));
21
22
  __export(require("./reflect"));
22
23
  __export(require("./com"));
24
+ exports.useCallbackState = function (initialValue) {
25
+ var _a = react_1.useState(initialValue), state = _a[0], _setState = _a[1];
26
+ var callbackQueue = react_1.useRef([]);
27
+ react_1.useEffect(function () {
28
+ callbackQueue.current.forEach(function (cb) { return cb(state); });
29
+ callbackQueue.current = [];
30
+ }, [state]);
31
+ var setState = function (newValue, callback) {
32
+ _setState(newValue);
33
+ if (callback && typeof callback === "function") {
34
+ callbackQueue.current.push(callback);
35
+ }
36
+ };
37
+ return [state, setState];
38
+ };
23
39
  function checked(s, v) {
24
40
  if (s) {
25
41
  if (Array.isArray(s)) {
@@ -51,3 +67,49 @@ exports.Loading = function (props) {
51
67
  return (React.createElement('div', { className: 'loader-wrapper' }, React.createElement('div', { className: 'loader-sign', style: loadingStyle }, React.createElement('div', { className: 'loader' }))));
52
68
  }
53
69
  };
70
+ function formatDate(date, format) {
71
+ if (!date) {
72
+ return '';
73
+ }
74
+ var opts = {
75
+ year: "numeric",
76
+ month: "2-digit",
77
+ day: "2-digit",
78
+ hour: "numeric",
79
+ minute: "numeric",
80
+ second: "numeric",
81
+ hour12: false,
82
+ };
83
+ var d2 = new Date(date).toLocaleString("en-US", opts);
84
+ var od = format.replace("YYYY", d2.slice(6, 10));
85
+ od = od.replace("MM", d2.slice(0, 2));
86
+ od = od.replace("DD", d2.slice(3, 5));
87
+ od = od.replace("HH", d2.slice(12, 14));
88
+ od = od.replace("mm", d2.slice(15, 17));
89
+ od = od.replace("ss", d2.slice(18, 20));
90
+ return od;
91
+ }
92
+ exports.formatDate = formatDate;
93
+ ;
94
+ function dateToString(date) {
95
+ var d2 = typeof date !== "string" ? date : new Date(date);
96
+ var year = d2.getFullYear();
97
+ var month = String(d2.getMonth() + 1).padStart(2, "0");
98
+ var day = String(d2.getDate()).padStart(2, "0");
99
+ return year + "-" + month + "-" + day;
100
+ }
101
+ exports.dateToString = dateToString;
102
+ ;
103
+ function datetimeToString(date) {
104
+ if (date === void 0) { date = ""; }
105
+ var d2 = typeof date !== "string" ? date : new Date(date);
106
+ var year = d2.getFullYear();
107
+ var month = String(d2.getMonth() + 1).padStart(2, "0");
108
+ var day = String(d2.getDate()).padStart(2, "0");
109
+ var hours = String(d2.getHours()).padStart(2, "0");
110
+ var minutes = String(d2.getMinutes()).padStart(2, "0");
111
+ var seconds = String(d2.getSeconds()).padStart(2, "0");
112
+ return year + "-" + month + "-" + day + "T" + hours + ":" + minutes + ":" + seconds;
113
+ }
114
+ exports.datetimeToString = datetimeToString;
115
+ ;
package/lib/reflect.js CHANGED
@@ -81,6 +81,16 @@ var setKey = function (_object, _isArrayKey, _key, _nextValue) {
81
81
  }
82
82
  return _object;
83
83
  };
84
+ function isEmptyObject(obj) {
85
+ for (var key in obj) {
86
+ if (obj.hasOwnProperty(key)) {
87
+ return false;
88
+ }
89
+ }
90
+ return true;
91
+ }
92
+ exports.isEmptyObject = isEmptyObject;
93
+ ;
84
94
  function diff(obj1, obj2) {
85
95
  var fields = [];
86
96
  var key1s = Object.keys(obj1);
package/lib/useEdit.js CHANGED
@@ -245,19 +245,18 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
245
245
  }
246
246
  };
247
247
  var validate = (p && p.validate ? p.validate : _validate);
248
- var _succeed = function (obj, msg, version, isBack, result) {
249
- if (result) {
250
- var model = result.value;
248
+ var _succeed = function (origin, msg, version, isBack, model) {
249
+ if (model) {
251
250
  setFlag({ newMode: false });
252
251
  if (model && flag.setBack === true) {
253
252
  resetState(false, model, reflect_1.clone(model));
254
253
  }
255
254
  else {
256
- edit_1.handleVersion(obj, version);
255
+ edit_1.handleVersion(origin, version);
257
256
  }
258
257
  }
259
258
  else {
260
- edit_1.handleVersion(obj, version);
259
+ edit_1.handleVersion(origin, version);
261
260
  }
262
261
  p1.showMessage(msg);
263
262
  if (isBack) {
@@ -268,48 +267,27 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
268
267
  var _fail = function (result) {
269
268
  var f = refForm.current;
270
269
  var u = p1.ui;
271
- if (Array.isArray(result)) {
272
- if (u && f) {
273
- var unmappedErrors = u.showFormError(f, result);
274
- formutil_1.focusFirstError(f);
275
- if (unmappedErrors && unmappedErrors.length > 0) {
276
- var t = p1.resource.value('error');
277
- if (p1.ui && p1.ui.buildErrorMessage) {
278
- var msg = p1.ui.buildErrorMessage(unmappedErrors);
279
- p1.showError(msg, t);
280
- }
281
- else {
282
- p1.showError(unmappedErrors[0].field + ' ' + unmappedErrors[0].code + ' ' + unmappedErrors[0].message, t);
283
- }
284
- }
285
- }
286
- else {
270
+ if (u && f) {
271
+ var unmappedErrors = u.showFormError(f, result);
272
+ formutil_1.focusFirstError(f);
273
+ if (unmappedErrors && unmappedErrors.length > 0) {
287
274
  var t = p1.resource.value('error');
288
- if (result.length > 0) {
289
- p1.showError(result[0].field + ' ' + result[0].code + ' ' + result[0].message, t);
275
+ if (p1.ui && p1.ui.buildErrorMessage) {
276
+ var msg = p1.ui.buildErrorMessage(unmappedErrors);
277
+ p1.showError(msg, t);
290
278
  }
291
279
  else {
292
- p1.showError(t, t);
280
+ p1.showError(unmappedErrors[0].field + ' ' + unmappedErrors[0].code + ' ' + unmappedErrors[0].message, t);
293
281
  }
294
282
  }
295
283
  }
296
284
  else {
297
- var errors = result.errors;
298
- if (errors && u) {
299
- var unmappedErrors = u.showFormError(f, errors);
300
- formutil_1.focusFirstError(f);
301
- if (!result.message) {
302
- if (p1.ui && p1.ui.buildErrorMessage) {
303
- result.message = p1.ui.buildErrorMessage(unmappedErrors);
304
- }
305
- else {
306
- result.message = errors[0].message;
307
- }
308
- }
309
- if (result.message) {
310
- var t = p1.resource.value('error');
311
- p1.showError(result.message, t);
312
- }
285
+ var t = p1.resource.value('error');
286
+ if (result.length > 0) {
287
+ p1.showError(result[0].field + ' ' + result[0].code + ' ' + result[0].message, t);
288
+ }
289
+ else {
290
+ p1.showError(t, t);
313
291
  }
314
292
  }
315
293
  };
@@ -330,7 +308,7 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
330
308
  }
331
309
  };
332
310
  var handleError = (p && p.handleError ? p.handleError : _handleError);
333
- var _postSave = function (obj, r, version, backOnSave) {
311
+ var _postSave = function (r, origin, version, isPatch, backOnSave) {
334
312
  setRunning(false);
335
313
  core_1.hideLoading(p1.loading);
336
314
  var x = r;
@@ -342,7 +320,7 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
342
320
  }
343
321
  else if (!isNaN(x)) {
344
322
  if (x === st.success) {
345
- succeed(obj, successMsg, version, backOnSave);
323
+ succeed(origin, successMsg, version, backOnSave);
346
324
  }
347
325
  else {
348
326
  if (newMod && x === st.duplicate_key) {
@@ -351,29 +329,31 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
351
329
  else if (!newMod && x === st.not_found) {
352
330
  handleNotFound();
353
331
  }
332
+ else if (!newMod && x === st.version_error) {
333
+ var title = p1.resource.value('error');
334
+ var err = p1.resource.value('error_version');
335
+ p1.showError(err, title);
336
+ }
354
337
  else {
355
338
  edit_1.handleStatus(x, st, p1.resource.value, p1.showError);
356
339
  }
357
340
  }
358
341
  }
359
342
  else {
360
- var result = x;
361
- if (result.status === st.success) {
362
- succeed(obj, successMsg, version, backOnSave, result);
363
- p1.showMessage(successMsg);
364
- }
365
- else if (result.errors && result.errors.length > 0) {
366
- fail(result);
367
- }
368
- else if (newMod && result.status === st.duplicate_key) {
369
- handleDuplicateKey(result);
370
- }
371
- else if (!newMod && x === st.not_found) {
372
- handleNotFound();
343
+ var result = r;
344
+ if (isPatch) {
345
+ var keys = Object.keys(result);
346
+ var a = origin;
347
+ for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
348
+ var k = keys_1[_i];
349
+ a[k] = result[k];
350
+ }
351
+ succeed(a, successMsg, undefined, backOnSave);
373
352
  }
374
353
  else {
375
- edit_1.handleStatus(result.status, st, p1.resource.value, p1.showError);
354
+ succeed(origin, successMsg, version, backOnSave, r);
376
355
  }
356
+ p1.showMessage(successMsg);
377
357
  }
378
358
  };
379
359
  var postSave = (p && p.postSave ? p.postSave : _postSave);
@@ -389,14 +369,16 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
389
369
  var patchable = (p ? p.patchable : true);
390
370
  if (flag.newMode === false) {
391
371
  if (service.patch && patchable !== false && body && Object.keys(body).length > 0) {
392
- service.patch(body).then(function (result) { return postSave(obj, result, version, isBackO); }).catch(handleError);
372
+ service.patch(body).then(function (result) {
373
+ postSave(result, obj, version, true, isBackO);
374
+ }).catch(handleError);
393
375
  }
394
376
  else {
395
- service.update(obj).then(function (result) { return postSave(obj, result, version, isBackO); }).catch(handleError);
377
+ service.update(obj).then(function (result) { return postSave(result, obj, version, false, isBackO); }).catch(handleError);
396
378
  }
397
379
  }
398
380
  else {
399
- service.insert(obj).then(function (result) { return postSave(obj, result, version, isBackO); }).catch(handleError);
381
+ service.insert(obj).then(function (result) { return postSave(result, obj, version, false, isBackO); }).catch(handleError);
400
382
  }
401
383
  };
402
384
  var doSave = (p && p.doSave ? p.doSave : _doSave);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-hook-core",
3
- "version": "0.1.18",
3
+ "version": "0.1.20",
4
4
  "description": "react",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/components.ts CHANGED
@@ -885,17 +885,16 @@ export abstract class BaseEditComponent<T, P, S> extends BaseComponent<P, S> {
885
885
  doSave(obj: T, dif?: Partial<T>, isBack?: boolean) {
886
886
  }
887
887
 
888
- succeed(msg: string, isBack?: boolean, result?: ResultInfo<T>) {
889
- if (result) {
890
- const model = result.value;
888
+ succeed(msg: string, origin: T, isBack?: boolean, model?: T) {
889
+ if (model) {
891
890
  this.newMode = false;
892
891
  if (model && this.setBack) {
893
892
  this.resetState(false, model, clone(model));
894
893
  } else {
895
- handleVersion(this.getModel(), this.version);
894
+ handleVersion(origin, this.version);
896
895
  }
897
896
  } else {
898
- handleVersion(this.getModel(), this.version);
897
+ handleVersion(origin, this.version);
899
898
  }
900
899
  const isBackO = (isBack == null || isBack === undefined ? this.backOnSuccess : isBack);
901
900
  this.showMessage(msg);
@@ -903,53 +902,32 @@ export abstract class BaseEditComponent<T, P, S> extends BaseComponent<P, S> {
903
902
  this.back(null);
904
903
  }
905
904
  }
906
- fail(result: ResultInfo<T> | ErrorMessage[]) {
905
+ fail(result: ErrorMessage[]) {
907
906
  const f = this.form;
908
907
  const u = this.ui;
909
- if (Array.isArray(result)) {
910
- if (u && f) {
911
- const unmappedErrors = u.showFormError(f, result);
912
- focusFirstError(f);
913
- if (unmappedErrors && unmappedErrors.length > 0) {
914
- const t = this.resourceService.value('error');
915
- if (u && u.buildErrorMessage) {
916
- const msg = u.buildErrorMessage(unmappedErrors);
917
- this.showError(msg, t);
918
- } else {
919
- this.showError(unmappedErrors[0].field + ' ' + unmappedErrors[0].code + ' ' + unmappedErrors[0].message, t);
920
- }
921
- }
922
- } else {
908
+ if (u && f) {
909
+ const unmappedErrors = u.showFormError(f, result);
910
+ focusFirstError(f);
911
+ if (unmappedErrors && unmappedErrors.length > 0) {
923
912
  const t = this.resourceService.value('error');
924
- if (result.length > 0) {
925
- this.showError(result[0].field + ' ' + result[0].code + ' ' + result[0].message, t);
913
+ if (u && u.buildErrorMessage) {
914
+ const msg = u.buildErrorMessage(unmappedErrors);
915
+ this.showError(msg, t);
926
916
  } else {
927
- this.showError(t, t);
917
+ this.showError(unmappedErrors[0].field + ' ' + unmappedErrors[0].code + ' ' + unmappedErrors[0].message, t);
928
918
  }
929
919
  }
930
920
  } else {
931
- const errors = result.errors;
932
- if (u && f) {
933
- const unmappedErrors = u.showFormError(f, errors);
934
- if (!result.message) {
935
- if (errors && errors.length === 1) {
936
- result.message = errors[0].message;
937
- } else {
938
- result.message = u.buildErrorMessage(unmappedErrors);
939
- }
940
- }
941
- focusFirstError(f);
942
- } else if (errors && errors.length === 1) {
943
- result.message = errors[0].message;
944
- }
945
- if (result.message) {
946
- const t = this.resourceService.value('error');
947
- this.showError(result.message, t);
921
+ const t = this.resourceService.value('error');
922
+ if (result.length > 0) {
923
+ this.showError(result[0].field + ' ' + result[0].code + ' ' + result[0].message, t);
924
+ } else {
925
+ this.showError(t, t);
948
926
  }
949
927
  }
950
928
  }
951
929
 
952
- postSave(res: number|string|ResultInfo<T>|ErrorMessage[], backOnSave?: boolean) {
930
+ postSave(res: number|string|T|ErrorMessage[], origin: T, isPatch: boolean, backOnSave?: boolean) {
953
931
  this.running = false;
954
932
  hideLoading(this.loading);
955
933
  const st = this.status;
@@ -961,7 +939,7 @@ export abstract class BaseEditComponent<T, P, S> extends BaseComponent<P, S> {
961
939
  this.fail(x);
962
940
  } else if (!isNaN(x)) {
963
941
  if (x === st.success) {
964
- this.succeed(successMsg, backOnSave);
942
+ this.succeed(successMsg, origin, backOnSave);
965
943
  } else {
966
944
  if (newMod && x === st.duplicate_key) {
967
945
  this.handleDuplicateKey();
@@ -972,28 +950,27 @@ export abstract class BaseEditComponent<T, P, S> extends BaseComponent<P, S> {
972
950
  }
973
951
  }
974
952
  } else {
975
- const result: ResultInfo<T> = x;
976
- if (result.status === st.success) {
977
- this.succeed(successMsg, backOnSave, result);
978
- this.showMessage(successMsg);
979
- } else if (result.errors && result.errors.length > 0) {
980
- this.fail(result);
981
- } else if (newMod && result.status === st.duplicate_key) {
982
- this.handleDuplicateKey(result);
983
- } else if (!newMod && x === st.not_found) {
984
- this.handleNotFound();
953
+ const result: T = x;
954
+ if (isPatch) {
955
+ const keys = Object.keys(result);
956
+ const a: any = origin;
957
+ for (const k of keys) {
958
+ a[k] = (result as any)[k];
959
+ }
960
+ this.succeed(successMsg, a, backOnSave);
985
961
  } else {
986
- handleStatus(result.status, st, r.value, this.showError);
962
+ this.succeed(successMsg, origin, backOnSave, result);
987
963
  }
964
+ this.showMessage(successMsg);
988
965
  }
989
966
  }
990
- handleDuplicateKey(result?: ResultInfo<T>) {
967
+ handleDuplicateKey(result?: T) {
991
968
  const msg = message(this.resourceService.value, 'error_duplicate_key', 'error');
992
969
  this.showError(msg.message, msg.title);
993
970
  }
994
971
  }
995
972
  export class EditComponent<T, ID, P, S> extends BaseEditComponent<T, P, S> {
996
- constructor(props: P, protected service: GenericService<T, ID, number|ResultInfo<T>|ErrorMessage[]>,
973
+ constructor(props: P, protected service: GenericService<T, ID, number|T|ErrorMessage[]>,
997
974
  param: ResourceService|EditParameter,
998
975
  showMessage?: (msg: string, option?: string) => void,
999
976
  showError?: (m: string, title?: string, detail?: string, callback?: () => void) => void,
@@ -1086,14 +1063,16 @@ export class EditComponent<T, ID, P, S> extends BaseEditComponent<T, P, S> {
1086
1063
  const com = this;
1087
1064
  let m: T|Partial<T> = obj;
1088
1065
  let fn = this.newMode ? this.service.insert : this.service.update;
1066
+ let isPatch = false;
1089
1067
  if (!this.newMode) {
1090
1068
  if (this.patchable === true && this.service.patch && body && Object.keys(body).length > 0) {
1091
1069
  m = body;
1070
+ isPatch = true;
1092
1071
  fn = this.service.patch;
1093
1072
  }
1094
1073
  }
1095
1074
  fn(m as any).then(result => {
1096
- com.postSave(result, isBackO);
1075
+ com.postSave(result, obj, isBackO);
1097
1076
  com.running = false;
1098
1077
  hideLoading(com.loading);
1099
1078
  }).then(err => {
package/src/core.ts CHANGED
@@ -51,9 +51,9 @@ export function createEditStatus(status?: EditStatusConfig): EditStatusConfig {
51
51
  duplicate_key: 0,
52
52
  not_found: 0,
53
53
  success: 1,
54
- version_error: 2,
55
- error: 4,
56
- data_corrupt: 8
54
+ version_error: -1,
55
+ error: 2,
56
+ data_corrupt: 4
57
57
  };
58
58
  return s;
59
59
  }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as React from 'react';
2
+ import { useEffect, useRef, useState } from 'react';
2
3
  export * from './formutil';
3
4
  export * from './util';
4
5
  export * from './core';
@@ -16,6 +17,28 @@ export * from './search';
16
17
  export * from './reflect';
17
18
  export * from './com';
18
19
 
20
+ type CallBackType<T> = (updatedValue: T) => void;
21
+ type SetStateType<T> = T | ((prev: T) => T);
22
+ type RetType = <T>(initialValue: T | (() => T)) => [T, (newValue: SetStateType<T>, callback?: CallBackType<T>) => void];
23
+
24
+ export const useCallbackState: RetType = <T>(initialValue: T | (() => T)) => {
25
+ const [state, _setState] = useState<T>(initialValue);
26
+ const callbackQueue = useRef<CallBackType<T>[]>([]);
27
+
28
+ useEffect(() => {
29
+ callbackQueue.current.forEach((cb) => cb(state));
30
+ callbackQueue.current = [];
31
+ }, [state]);
32
+
33
+ const setState = (newValue: SetStateType<T>, callback?: CallBackType<T>) => {
34
+ _setState(newValue);
35
+ if (callback && typeof callback === "function") {
36
+ callbackQueue.current.push(callback);
37
+ }
38
+ };
39
+ return [state, setState];
40
+ };
41
+
19
42
  export function checked(s: string[]|string|undefined, v: string): boolean|undefined {
20
43
  if (s) {
21
44
  if (Array.isArray(s)) {
@@ -55,3 +78,42 @@ export const Loading = (props: LoadingProps) => {
55
78
  }
56
79
  };
57
80
  export type OnClick = React.MouseEvent<HTMLElement, MouseEvent>;
81
+ export function formatDate(date: Date | null | undefined, format: string): string {
82
+ if (!date) {
83
+ return '';
84
+ }
85
+ const opts: any = {
86
+ year: "numeric",
87
+ month: "2-digit",
88
+ day: "2-digit",
89
+ hour: "numeric",
90
+ minute: "numeric",
91
+ second: "numeric",
92
+ hour12: false,
93
+ };
94
+ const d2 = new Date(date).toLocaleString("en-US", opts);
95
+ let od = format.replace("YYYY", d2.slice(6, 10));
96
+ od = od.replace("MM", d2.slice(0, 2));
97
+ od = od.replace("DD", d2.slice(3, 5));
98
+ od = od.replace("HH", d2.slice(12, 14));
99
+ od = od.replace("mm", d2.slice(15, 17));
100
+ od = od.replace("ss", d2.slice(18, 20));
101
+ return od;
102
+ };
103
+ export function dateToString(date: Date | string): string {
104
+ const d2 = typeof date !== "string" ? date : new Date(date);
105
+ const year = d2.getFullYear();
106
+ const month = String(d2.getMonth() + 1).padStart(2, "0");
107
+ const day = String(d2.getDate()).padStart(2, "0");
108
+ return `${year}-${month}-${day}`;
109
+ };
110
+ export function datetimeToString(date: Date | string = ""): string {
111
+ const d2 = typeof date !== "string" ? date : new Date(date);
112
+ const year = d2.getFullYear();
113
+ const month = String(d2.getMonth() + 1).padStart(2, "0");
114
+ const day = String(d2.getDate()).padStart(2, "0");
115
+ const hours = String(d2.getHours()).padStart(2, "0");
116
+ const minutes = String(d2.getMinutes()).padStart(2, "0");
117
+ const seconds = String(d2.getSeconds()).padStart(2, "0");
118
+ return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`;
119
+ };
package/src/reflect.ts CHANGED
@@ -73,7 +73,14 @@ const setKey = (_object: any, _isArrayKey: boolean, _key: string, _nextValue: an
73
73
  }
74
74
  return _object;
75
75
  };
76
-
76
+ export function isEmptyObject(obj: any): boolean {
77
+ for (let key in obj) {
78
+ if (obj.hasOwnProperty(key)) {
79
+ return false;
80
+ }
81
+ }
82
+ return true;
83
+ };
77
84
  export function diff(obj1: any, obj2: any): string[] {
78
85
  const fields = [];
79
86
  const key1s = Object.keys(obj1);
@@ -159,7 +166,7 @@ export function equal(obj1: any, obj2: any): boolean {
159
166
  return (obj1.getTime() === obj2.getTime());
160
167
  }
161
168
  if ((Array.isArray(obj1) && !Array.isArray(obj2))
162
- || (!Array.isArray(obj1) && Array.isArray(obj2))) {
169
+ || (!Array.isArray(obj1) && Array.isArray(obj2))) {
163
170
  return false;
164
171
  }
165
172
  if (!Array.isArray(obj1) && !Array.isArray(obj2)) {
@@ -205,7 +212,7 @@ export function equalArrays<T>(ar1: T[], ar2: T[]): boolean {
205
212
  return true;
206
213
  }
207
214
 
208
- export function getArray<T>(list: T[]|undefined|null, name: string, v: boolean|string|number): T[] {
215
+ export function getArray<T>(list: T[] | undefined | null, name: string, v: boolean | string | number): T[] {
209
216
  const arrs = [];
210
217
  if (list) {
211
218
  for (const obj of list) {
@@ -216,7 +223,7 @@ export function getArray<T>(list: T[]|undefined|null, name: string, v: boolean|s
216
223
  }
217
224
  return arrs;
218
225
  }
219
- export function getDiffArray<T>(list: T[]|undefined|null, name: string, v: boolean|string|number): T[] {
226
+ export function getDiffArray<T>(list: T[] | undefined | null, name: string, v: boolean | string | number): T[] {
220
227
  const arrs = [];
221
228
  if (list) {
222
229
  for (const obj of list) {
@@ -227,14 +234,14 @@ export function getDiffArray<T>(list: T[]|undefined|null, name: string, v: boole
227
234
  }
228
235
  return arrs;
229
236
  }
230
- export function setAll<T>(list: T[]|undefined|null, name: string, v: boolean|string|number): void {
237
+ export function setAll<T>(list: T[] | undefined | null, name: string, v: boolean | string | number): void {
231
238
  if (list) {
232
239
  for (const obj of list) {
233
240
  (obj as any)[name] = v;
234
241
  }
235
242
  }
236
243
  }
237
- export function equalAll<T>(list: T[]|undefined|null, name: string, v: boolean|string|number): boolean {
244
+ export function equalAll<T>(list: T[] | undefined | null, name: string, v: boolean | string | number): boolean {
238
245
  if (list) {
239
246
  for (const obj of list) {
240
247
  if ((obj as any)[name] !== v) {
package/src/useEdit.ts CHANGED
@@ -32,11 +32,11 @@ export interface BaseEditComponentParam<T, ID> {
32
32
  createModel?: () => T;
33
33
  onSave?: (isBack?: boolean) => void;
34
34
  validate?: (obj: T, callback: (obj2?: T) => void) => void;
35
- succeed?: (obj: T, msg: string, version?: string, isBack?: boolean, result?: ResultInfo<T>) => void;
36
- fail?: (result: ResultInfo<T>|ErrorMessage[]) => void;
37
- postSave?: (obj: T, res: number|ResultInfo<T>|ErrorMessage[], version?: string, backOnSave?: boolean) => void;
35
+ succeed?: (origin: T, msg: string, version?: string, isBack?: boolean, model?: T) => void;
36
+ fail?: (result: ErrorMessage[]) => void;
37
+ postSave?: (res: number|T|ErrorMessage[], origin: T, version?: string, isPatch?: boolean, backOnSave?: boolean) => void;
38
38
  handleError?: (error: any) => void;
39
- handleDuplicateKey?: (result?: ResultInfo<T>) => void;
39
+ handleDuplicateKey?: (result?: T) => void;
40
40
  load?: (i: ID|null, callback?: (m: T, showM: (m2: T) => void) => void) => void;
41
41
  doSave?: (obj: T, diff?: T, version?: string, isBack?: boolean) => void;
42
42
  // prepareCustomData?: (data: any) => void; // need to review
@@ -44,7 +44,7 @@ export interface BaseEditComponentParam<T, ID> {
44
44
  export interface HookBaseEditParameter<T, ID, S> extends BaseEditComponentParam<T, ID> {
45
45
  refForm: any;
46
46
  initialState: S;
47
- service: GenericService<T, ID, number|ResultInfo<T>>;
47
+ service: GenericService<T, ID, number|T|ErrorMessage[]>;
48
48
  resource: ResourceService;
49
49
  showMessage: (msg: string) => void;
50
50
  showError: (m: string, header?: string, detail?: string, callback?: () => void) => void;
@@ -68,7 +68,7 @@ export interface HookPropsBaseEditParameter<T, ID, S, P> extends HookBaseEditPar
68
68
  export const useEdit = <T, ID, S>(
69
69
  refForm: any,
70
70
  initialState: S,
71
- service: GenericService<T, ID, number|ResultInfo<T> | ErrorMessage[]>,
71
+ service: GenericService<T, ID, number|T|ErrorMessage[]>,
72
72
 
73
73
  p2: EditParameter,
74
74
  p?: EditComponentParam<T, ID, S>
@@ -114,7 +114,7 @@ export const useEditProps = <T, ID, S, P>(
114
114
  props: P,
115
115
  refForm: any,
116
116
  initialState: S,
117
- service: GenericService<T, ID, number|ResultInfo<T>>,
117
+ service: GenericService<T, ID, number|T|ErrorMessage[]>,
118
118
  p2: EditParameter,
119
119
  p?: EditComponentParam<T, ID, S>
120
120
  ) => {
@@ -159,7 +159,7 @@ export const useEditOne = <T, ID, S>(p: HookBaseEditParameter<T, ID, S>) => {
159
159
  export const useCoreEdit = <T, ID, S, P>(
160
160
  refForm: any,
161
161
  initialState: S,
162
- service: GenericService<T, ID, number|ResultInfo<T>|ErrorMessage[]>,
162
+ service: GenericService<T, ID, number|T|ErrorMessage[]>,
163
163
  p1: EditParameter,
164
164
  p?: BaseEditComponentParam<T, ID>,
165
165
  props?: P
@@ -322,17 +322,16 @@ export const useCoreEdit = <T, ID, S, P>(
322
322
  };
323
323
  const validate = (p && p.validate ? p.validate : _validate);
324
324
 
325
- const _succeed = (obj: T, msg: string, version?: string, isBack?: boolean, result?: ResultInfo<T>) => {
326
- if (result) {
327
- const model = result.value;
325
+ const _succeed = (origin: T, msg: string, version?: string, isBack?: boolean, model?: T) => {
326
+ if (model) {
328
327
  setFlag({ newMode: false });
329
328
  if (model && flag.setBack === true) {
330
329
  resetState(false, model, clone(model));
331
330
  } else {
332
- handleVersion(obj, version);
331
+ handleVersion(origin, version);
333
332
  }
334
333
  } else {
335
- handleVersion(obj, version);
334
+ handleVersion(origin, version);
336
335
  }
337
336
  p1.showMessage(msg);
338
337
  if (isBack) {
@@ -341,46 +340,27 @@ export const useCoreEdit = <T, ID, S, P>(
341
340
  };
342
341
  const succeed = (p && p.succeed ? p.succeed : _succeed);
343
342
 
344
- const _fail = (result: ResultInfo<T> | ErrorMessage[]) => {
343
+ const _fail = (result: ErrorMessage[]) => {
345
344
  const f = refForm.current;
346
345
  const u = p1.ui;
347
- if (Array.isArray(result)) {
348
- if (u && f) {
349
- const unmappedErrors = u.showFormError(f, result);
350
- focusFirstError(f);
351
- if (unmappedErrors && unmappedErrors.length > 0) {
352
- const t = p1.resource.value('error');
353
- if (p1.ui && p1.ui.buildErrorMessage) {
354
- const msg = p1.ui.buildErrorMessage(unmappedErrors);
355
- p1.showError(msg, t);
356
- } else {
357
- p1.showError(unmappedErrors[0].field + ' ' + unmappedErrors[0].code + ' ' + unmappedErrors[0].message, t);
358
- }
359
- }
360
- } else {
346
+ if (u && f) {
347
+ const unmappedErrors = u.showFormError(f, result);
348
+ focusFirstError(f);
349
+ if (unmappedErrors && unmappedErrors.length > 0) {
361
350
  const t = p1.resource.value('error');
362
- if (result.length > 0) {
363
- p1.showError(result[0].field + ' ' + result[0].code + ' ' + result[0].message, t);
351
+ if (p1.ui && p1.ui.buildErrorMessage) {
352
+ const msg = p1.ui.buildErrorMessage(unmappedErrors);
353
+ p1.showError(msg, t);
364
354
  } else {
365
- p1.showError(t, t);
355
+ p1.showError(unmappedErrors[0].field + ' ' + unmappedErrors[0].code + ' ' + unmappedErrors[0].message, t);
366
356
  }
367
357
  }
368
358
  } else {
369
- const errors = result.errors;
370
- if (errors && u) {
371
- const unmappedErrors = u.showFormError(f, errors);
372
- focusFirstError(f);
373
- if (!result.message) {
374
- if (p1.ui && p1.ui.buildErrorMessage) {
375
- result.message = p1.ui.buildErrorMessage(unmappedErrors);
376
- } else {
377
- result.message = errors[0].message;
378
- }
379
- }
380
- if (result.message) {
381
- const t = p1.resource.value('error');
382
- p1.showError(result.message, t);
383
- }
359
+ const t = p1.resource.value('error');
360
+ if (result.length > 0) {
361
+ p1.showError(result[0].field + ' ' + result[0].code + ' ' + result[0].message, t);
362
+ } else {
363
+ p1.showError(t, t);
384
364
  }
385
365
  }
386
366
  };
@@ -402,7 +382,7 @@ export const useCoreEdit = <T, ID, S, P>(
402
382
  };
403
383
  const handleError = (p && p.handleError ? p.handleError : _handleError);
404
384
 
405
- const _postSave = (obj: T, r: number | ResultInfo<T>|ErrorMessage[], version?: string, backOnSave?: boolean) => {
385
+ const _postSave = (r: number | T|ErrorMessage[], origin: T, version?: string, isPatch?: boolean, backOnSave?: boolean) => {
406
386
  setRunning(false);
407
387
  hideLoading(p1.loading);
408
388
  const x: any = r;
@@ -413,35 +393,38 @@ export const useCoreEdit = <T, ID, S, P>(
413
393
  fail(x);
414
394
  } else if (!isNaN(x)) {
415
395
  if (x === st.success) {
416
- succeed(obj, successMsg, version, backOnSave);
396
+ succeed(origin, successMsg, version, backOnSave);
417
397
  } else {
418
398
  if (newMod && x === st.duplicate_key) {
419
399
  handleDuplicateKey();
420
400
  } else if (!newMod && x === st.not_found) {
421
401
  handleNotFound();
402
+ } else if (!newMod && x === st.version_error) {
403
+ const title = p1.resource.value('error');
404
+ const err = p1.resource.value('error_version');
405
+ p1.showError(err, title);
422
406
  } else {
423
407
  handleStatus(x as number, st, p1.resource.value, p1.showError);
424
408
  }
425
409
  }
426
410
  } else {
427
- const result: ResultInfo<any> = x;
428
- if (result.status === st.success) {
429
- succeed(obj, successMsg, version, backOnSave, result);
430
- p1.showMessage(successMsg);
431
- } else if (result.errors && result.errors.length > 0) {
432
- fail(result);
433
- } else if (newMod && result.status === st.duplicate_key) {
434
- handleDuplicateKey(result);
435
- } else if (!newMod && x === st.not_found) {
436
- handleNotFound();
411
+ const result = r as T;
412
+ if (isPatch) {
413
+ const keys = Object.keys(result);
414
+ const a: any = origin;
415
+ for (const k of keys) {
416
+ a[k] = (result as any)[k];
417
+ }
418
+ succeed(a, successMsg, undefined, backOnSave);
437
419
  } else {
438
- handleStatus(result.status, st, p1.resource.value, p1.showError);
420
+ succeed(origin, successMsg, version, backOnSave, r as T);
439
421
  }
422
+ p1.showMessage(successMsg);
440
423
  }
441
424
  };
442
425
  const postSave = (p && p.postSave ? p.postSave : _postSave);
443
426
 
444
- const _handleDuplicateKey = (result?: ResultInfo<any>) => {
427
+ const _handleDuplicateKey = (result?: T) => {
445
428
  const msg = message(p1.resource.value, 'error_duplicate_key', 'error');
446
429
  p1.showError(msg.message, msg.title);
447
430
  };
@@ -454,12 +437,14 @@ export const useCoreEdit = <T, ID, S, P>(
454
437
  const patchable = (p ? p.patchable : true);
455
438
  if (flag.newMode === false) {
456
439
  if (service.patch && patchable !== false && body && Object.keys(body).length > 0) {
457
- service.patch(body).then(result => postSave(obj, result, version, isBackO)).catch(handleError);
440
+ service.patch(body).then(result => {
441
+ postSave(result, obj, version, true, isBackO);
442
+ }).catch(handleError);
458
443
  } else {
459
- service.update(obj).then(result => postSave(obj, result, version, isBackO)).catch(handleError);
444
+ service.update(obj).then(result => postSave(result, obj, version, false, isBackO)).catch(handleError);
460
445
  }
461
446
  } else {
462
- service.insert(obj).then(result => postSave(obj, result, version, isBackO)).catch(handleError);
447
+ service.insert(obj).then(result => postSave(result, obj, version, false, isBackO)).catch(handleError);
463
448
  }
464
449
  };
465
450