react-hook-core 0.0.1 → 0.1.0
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 +370 -486
- package/lib/core.js +89 -27
- package/lib/diff.js +3 -0
- package/lib/edit.js +35 -33
- package/lib/formutil.js +5 -2
- package/lib/index.js +7 -0
- package/lib/merge.js +1 -1
- package/lib/state.js +17 -10
- package/lib/update.js +36 -55
- package/lib/useEdit.js +181 -327
- package/lib/useMessage.js +25 -0
- package/lib/useSearch.js +118 -238
- package/lib/useView.js +70 -161
- package/lib/util.js +6 -2
- package/package.json +7 -9
- package/src/components.ts +345 -301
- package/src/core.ts +110 -49
- package/src/diff.ts +4 -1
- package/src/edit.ts +44 -42
- package/src/formutil.ts +7 -4
- package/src/index.ts +8 -0
- package/src/merge.ts +3 -3
- package/src/state.ts +25 -19
- package/src/update.ts +35 -58
- package/src/useEdit.ts +165 -244
- package/src/useMessage.ts +37 -0
- package/src/useSearch.ts +145 -225
- package/src/useView.ts +64 -101
- package/src/util.ts +8 -4
- package/tsconfig.json +1 -0
package/src/components.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
+
import {RouteComponentProps} from 'react-router';
|
|
2
3
|
import {clone, diff, makeDiff} from 'reflectx';
|
|
3
|
-
import {addParametersIntoUrl, append,
|
|
4
|
-
import {BaseDiffState, createDiffStatus, DiffApprService, DiffParameter, DiffState, DiffStatusConfig} from './core';
|
|
5
|
-
import {Attributes, buildId, EditStatusConfig, error, getCurrencyCode, getModelName as getModelName2, HistoryProps, initForm, LoadingService, Locale, message, messageByHttpStatus, ModelHistoryProps, ModelProps, removePhoneFormat, ResourceService,
|
|
4
|
+
import {addParametersIntoUrl, append, buildMessage, changePage, changePageSize, formatResultsByComponent, getFieldsFromForm, getModel, handleAppend, handleSortEvent, initFilter, mergeFilter as mergeFilter2, more, reset, Searchable, showPaging, validate} from 'search-core';
|
|
5
|
+
import {BaseDiffState, createDiffStatus, createEditStatus, DiffApprService, DiffParameter, DiffState, DiffStatusConfig, hideLoading, showLoading} from './core';
|
|
6
|
+
import {Attributes, buildId, EditStatusConfig, error, ErrorMessage, Filter, getCurrencyCode, getModelName as getModelName2, HistoryProps, initForm, LoadingService, Locale, message, messageByHttpStatus, ModelHistoryProps, ModelProps, removePhoneFormat, ResourceService, SearchParameter, SearchResult, SearchService, SearchState, StringMap, UIService, ViewParameter, ViewService} from './core';
|
|
6
7
|
import {formatDiffModel, getDataFields} from './diff';
|
|
7
8
|
import {build, createModel as createModel2, EditParameter, GenericService, handleStatus, handleVersion, initPropertyNullInModel, ResultInfo} from './edit';
|
|
8
9
|
import {focusFirstError, readOnly} from './formutil';
|
|
9
10
|
import {getAutoSearch, getConfirmFunc, getDiffStatusFunc, getEditStatusFunc, getErrorFunc, getLoadingFunc, getLocaleFunc, getMsgFunc, getResource, getUIService} from './input';
|
|
10
11
|
import {buildFromUrl} from './route';
|
|
11
|
-
import {buildFlatState, buildState, handleEvent, handleProps, localeOf} from './state';
|
|
12
|
+
import {buildFlatState, buildState, enLocale, handleEvent, handleProps, localeOf} from './state';
|
|
12
13
|
|
|
13
|
-
export class ViewComponent<T, ID, P extends
|
|
14
|
+
export class ViewComponent<T, ID, P extends RouteComponentProps, S> extends React.Component<P, S> {
|
|
14
15
|
constructor(props: P, sv: ((id: ID, ctx?: any) => Promise<T>)|ViewService<T, ID>,
|
|
15
16
|
param: ResourceService|ViewParameter,
|
|
16
17
|
showError?: (msg: string, title?: string, detail?: string, callback?: () => void) => void,
|
|
@@ -26,13 +27,15 @@ export class ViewComponent<T, ID, P extends HistoryProps, S> extends React.Compo
|
|
|
26
27
|
if (typeof sv === 'function') {
|
|
27
28
|
this.loadData = sv;
|
|
28
29
|
} else {
|
|
29
|
-
this.
|
|
30
|
-
if (
|
|
31
|
-
const m =
|
|
30
|
+
this.loadData = sv.load;
|
|
31
|
+
if (sv.metadata) {
|
|
32
|
+
const m = sv.metadata();
|
|
32
33
|
if (m) {
|
|
33
34
|
this.metadata = m;
|
|
34
35
|
const meta = build(m);
|
|
35
|
-
|
|
36
|
+
if (meta) {
|
|
37
|
+
this.keys = meta.keys;
|
|
38
|
+
}
|
|
36
39
|
}
|
|
37
40
|
}
|
|
38
41
|
}
|
|
@@ -45,15 +48,15 @@ export class ViewComponent<T, ID, P extends HistoryProps, S> extends React.Compo
|
|
|
45
48
|
this.ref = React.createRef();
|
|
46
49
|
}
|
|
47
50
|
protected name?: string;
|
|
48
|
-
protected running
|
|
51
|
+
protected running?: boolean;
|
|
49
52
|
protected resourceService: ResourceService;
|
|
50
53
|
protected resource: StringMap;
|
|
51
54
|
protected loading?: LoadingService;
|
|
52
55
|
protected showError: (msg: string, title?: string, detail?: string, callback?: () => void) => void;
|
|
53
56
|
protected getLocale?: (profile?: string) => Locale;
|
|
54
|
-
protected loadData
|
|
55
|
-
protected service: ViewService<T, ID>;
|
|
56
|
-
protected form
|
|
57
|
+
protected loadData?: (id: ID, ctx?: any) => Promise<T>;
|
|
58
|
+
// protected service: ViewService<T, ID>;
|
|
59
|
+
protected form?: HTMLFormElement;
|
|
57
60
|
protected ref: any;
|
|
58
61
|
protected keys?: string[];
|
|
59
62
|
protected metadata?: Attributes;
|
|
@@ -71,50 +74,45 @@ export class ViewComponent<T, ID, P extends HistoryProps, S> extends React.Compo
|
|
|
71
74
|
const n = getModelName2(this.form);
|
|
72
75
|
if (!n || n.length === 0) {
|
|
73
76
|
return 'model';
|
|
77
|
+
} else {
|
|
78
|
+
return n;
|
|
74
79
|
}
|
|
75
80
|
}
|
|
76
81
|
componentDidMount() {
|
|
77
82
|
this.form = this.ref.current;
|
|
78
83
|
const id = buildId<ID>(this.props, this.keys);
|
|
79
|
-
|
|
84
|
+
if (id) {
|
|
85
|
+
this.load(id);
|
|
86
|
+
}
|
|
80
87
|
}
|
|
81
|
-
|
|
88
|
+
load(_id: ID, callback?: (m: T, showF: (model: T) => void) => void) {
|
|
82
89
|
const id: any = _id;
|
|
83
|
-
if (id != null && id !== '') {
|
|
90
|
+
if (id != null && id !== '' && this.loadData) {
|
|
84
91
|
this.running = true;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
try {
|
|
89
|
-
const ctx: any = {};
|
|
90
|
-
let obj: T;
|
|
91
|
-
if (this.loadData) {
|
|
92
|
-
obj = await this.loadData(id, ctx);
|
|
93
|
-
} else {
|
|
94
|
-
obj = await this.service.load(id, ctx);
|
|
95
|
-
}
|
|
92
|
+
showLoading(this.loading);
|
|
93
|
+
const com = this;
|
|
94
|
+
this.loadData(id).then(obj => {
|
|
96
95
|
if (!obj) {
|
|
97
|
-
|
|
96
|
+
com.handleNotFound(com.form);
|
|
98
97
|
} else {
|
|
99
98
|
if (callback) {
|
|
100
|
-
callback(obj,
|
|
99
|
+
callback(obj, com.showModel);
|
|
101
100
|
} else {
|
|
102
|
-
|
|
101
|
+
com.showModel(obj);
|
|
103
102
|
}
|
|
104
103
|
}
|
|
105
|
-
|
|
104
|
+
com.running = false;
|
|
105
|
+
hideLoading(com.loading);
|
|
106
|
+
}).catch(err => {
|
|
106
107
|
const data = (err && err.response) ? err.response : err;
|
|
107
108
|
if (data && data.status === 404) {
|
|
108
|
-
|
|
109
|
+
com.handleNotFound(com.form);
|
|
109
110
|
} else {
|
|
110
|
-
error(err,
|
|
111
|
+
error(err, com.resourceService.value, com.showError);
|
|
111
112
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
this.loading.hideLoading();
|
|
116
|
-
}
|
|
117
|
-
}
|
|
113
|
+
com.running = false;
|
|
114
|
+
hideLoading(com.loading);
|
|
115
|
+
});
|
|
118
116
|
}
|
|
119
117
|
}
|
|
120
118
|
protected handleNotFound(form?: HTMLFormElement): void {
|
|
@@ -125,7 +123,7 @@ export class ViewComponent<T, ID, P extends HistoryProps, S> extends React.Compo
|
|
|
125
123
|
this.showError(msg.message, msg.title);
|
|
126
124
|
}
|
|
127
125
|
getModel(): T {
|
|
128
|
-
return this.state[this.getModelName()];
|
|
126
|
+
return (this.state as any)[this.getModelName()];
|
|
129
127
|
}
|
|
130
128
|
showModel(model: T) {
|
|
131
129
|
const modelName = this.getModelName();
|
|
@@ -147,8 +145,8 @@ export class BaseComponent<P extends ModelProps, S> extends React.Component<P, S
|
|
|
147
145
|
this.updateDateState = this.updateDateState.bind(this);
|
|
148
146
|
this.prepareCustomData = this.prepareCustomData.bind(this);
|
|
149
147
|
}
|
|
150
|
-
protected running
|
|
151
|
-
protected form
|
|
148
|
+
protected running?: boolean;
|
|
149
|
+
protected form?: HTMLFormElement|null;
|
|
152
150
|
/*
|
|
153
151
|
protected handleSubmitForm(e) {
|
|
154
152
|
if (e.which === 13) {
|
|
@@ -187,7 +185,7 @@ export class BaseComponent<P extends ModelProps, S> extends React.Component<P, S
|
|
|
187
185
|
protected updateDateState = (name: string, value: any) => {
|
|
188
186
|
const props: any = this.props;
|
|
189
187
|
const modelName = this.getModelName(this.form);
|
|
190
|
-
const state = this.state[modelName];
|
|
188
|
+
const state = (this.state as any)[modelName];
|
|
191
189
|
if (props.setGlobalState) {
|
|
192
190
|
const data = props.shouldBeCustomized ? this.prepareCustomData({ [name]: value }) : { [name]: value };
|
|
193
191
|
props.setGlobalState({ [modelName]: { ...state, ...data } });
|
|
@@ -196,7 +194,7 @@ export class BaseComponent<P extends ModelProps, S> extends React.Component<P, S
|
|
|
196
194
|
this.setState(objSet);
|
|
197
195
|
}
|
|
198
196
|
}
|
|
199
|
-
protected getModelName(f?: HTMLFormElement): string {
|
|
197
|
+
protected getModelName(f?: HTMLFormElement|null): string {
|
|
200
198
|
let f2 = f;
|
|
201
199
|
if (!f2) {
|
|
202
200
|
f2 = this.form;
|
|
@@ -240,7 +238,60 @@ export class BaseComponent<P extends ModelProps, S> extends React.Component<P, S
|
|
|
240
238
|
}
|
|
241
239
|
}
|
|
242
240
|
}
|
|
243
|
-
export
|
|
241
|
+
export interface MessageOnlyState extends ModelProps {
|
|
242
|
+
message?: string;
|
|
243
|
+
}
|
|
244
|
+
export class MessageComponent<P extends MessageOnlyState, S extends MessageOnlyState> extends BaseComponent<P, S> {
|
|
245
|
+
constructor(props: P,
|
|
246
|
+
getLocale?: () => Locale,
|
|
247
|
+
removeErr?: (ctrl: HTMLInputElement) => void) {
|
|
248
|
+
super(props, getLocale, removeErr);
|
|
249
|
+
this.getModelName = this.getModelName.bind(this);
|
|
250
|
+
this.showMessage = this.showMessage.bind(this);
|
|
251
|
+
this.showError = this.showError.bind(this);
|
|
252
|
+
this.hideMessage = this.hideMessage.bind(this);
|
|
253
|
+
this.ref = React.createRef();
|
|
254
|
+
}
|
|
255
|
+
ref: any;
|
|
256
|
+
name?: string;
|
|
257
|
+
alertClass = '';
|
|
258
|
+
protected getModelName(f?: HTMLFormElement|null): string {
|
|
259
|
+
if (this.name && this.name.length > 0) {
|
|
260
|
+
return this.name;
|
|
261
|
+
}
|
|
262
|
+
let f2 = f;
|
|
263
|
+
if (!f2) {
|
|
264
|
+
f2 = this.form;
|
|
265
|
+
}
|
|
266
|
+
if (f2) {
|
|
267
|
+
const a = getModelName2(f2);
|
|
268
|
+
if (a && a.length > 0) {
|
|
269
|
+
return a;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return 'model';
|
|
273
|
+
}
|
|
274
|
+
showMessage = (msg: string) => {
|
|
275
|
+
this.alertClass = 'alert alert-info';
|
|
276
|
+
this.setState({ message: msg });
|
|
277
|
+
}
|
|
278
|
+
showError = (msg: string|ErrorMessage[]) => {
|
|
279
|
+
this.alertClass = 'alert alert-error';
|
|
280
|
+
if (typeof msg === 'string') {
|
|
281
|
+
this.setState({ message: msg });
|
|
282
|
+
} else if (Array.isArray(msg) && msg.length > 0) {
|
|
283
|
+
this.setState({ message: msg[0].message });
|
|
284
|
+
} else {
|
|
285
|
+
const x = JSON.stringify(msg);
|
|
286
|
+
this.setState({ message: x });
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
hideMessage = () => {
|
|
290
|
+
this.alertClass = '';
|
|
291
|
+
this.setState({ message: '' });
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
export class BaseSearchComponent<T, S extends Filter, P extends ModelHistoryProps, I extends SearchState<T, S>> extends BaseComponent<P, I> implements Searchable {
|
|
244
295
|
constructor(props: P,
|
|
245
296
|
protected resourceService: ResourceService,
|
|
246
297
|
protected showMessage: (msg: string) => void,
|
|
@@ -248,7 +299,7 @@ export class BaseSearchComponent<T, S extends SearchModel, P extends ModelHistor
|
|
|
248
299
|
protected ui?: UIService,
|
|
249
300
|
protected loading?: LoadingService,
|
|
250
301
|
protected listFormId?: string) {
|
|
251
|
-
super(props, getLocale, (ui ? ui.removeError :
|
|
302
|
+
super(props, getLocale, (ui ? ui.removeError : undefined));
|
|
252
303
|
this.resource = resourceService.resource();
|
|
253
304
|
this.getModelName = this.getModelName.bind(this);
|
|
254
305
|
this.showMessage = this.showMessage.bind(this);
|
|
@@ -259,12 +310,12 @@ export class BaseSearchComponent<T, S extends SearchModel, P extends ModelHistor
|
|
|
259
310
|
this.getSearchForm = this.getSearchForm.bind(this);
|
|
260
311
|
this.setSearchForm = this.setSearchForm.bind(this);
|
|
261
312
|
|
|
262
|
-
this.
|
|
263
|
-
this.
|
|
264
|
-
this.
|
|
313
|
+
this.setFilter = this.setFilter.bind(this);
|
|
314
|
+
this.getFilter = this.getFilter.bind(this);
|
|
315
|
+
this.getFields = this.getFields.bind(this);
|
|
265
316
|
|
|
266
317
|
this.pageSizeChanged = this.pageSizeChanged.bind(this);
|
|
267
|
-
this.
|
|
318
|
+
this.clearQ = this.clearQ.bind(this);
|
|
268
319
|
this.searchOnClick = this.searchOnClick.bind(this);
|
|
269
320
|
|
|
270
321
|
this.resetAndSearch = this.resetAndSearch.bind(this);
|
|
@@ -278,7 +329,7 @@ export class BaseSearchComponent<T, S extends SearchModel, P extends ModelHistor
|
|
|
278
329
|
this.showMore = this.showMore.bind(this);
|
|
279
330
|
this.pageChanged = this.pageChanged.bind(this);
|
|
280
331
|
|
|
281
|
-
this.url = (props.match ? props.match.url : props['props'].match.url);
|
|
332
|
+
this.url = (props.match ? props.match.url : (props as any)['props'].match.url);
|
|
282
333
|
/*
|
|
283
334
|
this.locationSearch = '';
|
|
284
335
|
const location = (props.location ? props.location : props['props'].location);
|
|
@@ -293,36 +344,36 @@ export class BaseSearchComponent<T, S extends SearchModel, P extends ModelHistor
|
|
|
293
344
|
// Pagination
|
|
294
345
|
initPageSize = 20;
|
|
295
346
|
pageSize = 20;
|
|
296
|
-
pageIndex = 1;
|
|
347
|
+
pageIndex?: number = 1;
|
|
297
348
|
nextPageToken?: string;
|
|
298
349
|
itemTotal = 0;
|
|
299
350
|
pageTotal = 0;
|
|
300
|
-
showPaging
|
|
301
|
-
append
|
|
302
|
-
appendMode
|
|
303
|
-
appendable
|
|
351
|
+
showPaging?: boolean;
|
|
352
|
+
append?: boolean;
|
|
353
|
+
appendMode?: boolean;
|
|
354
|
+
appendable?: boolean;
|
|
304
355
|
|
|
305
356
|
// Sortable
|
|
306
|
-
sortField
|
|
307
|
-
sortType
|
|
308
|
-
sortTarget
|
|
357
|
+
sortField?: string;
|
|
358
|
+
sortType?: string;
|
|
359
|
+
sortTarget?: HTMLElement;
|
|
309
360
|
|
|
310
|
-
keys
|
|
311
|
-
format?: (obj: T, locale
|
|
312
|
-
|
|
313
|
-
|
|
361
|
+
keys?: string[];
|
|
362
|
+
format?: (obj: T, locale?: Locale) => T;
|
|
363
|
+
fields?: string[];
|
|
364
|
+
initFields?: boolean;
|
|
314
365
|
sequenceNo = 'sequenceNo';
|
|
315
|
-
triggerSearch
|
|
316
|
-
tmpPageIndex = 1;
|
|
366
|
+
triggerSearch?: boolean;
|
|
367
|
+
tmpPageIndex?: number = 1;
|
|
317
368
|
|
|
318
369
|
pageMaxSize = 7;
|
|
319
370
|
pageSizes: number[] = [10, 20, 40, 60, 100, 200, 400, 800];
|
|
320
371
|
|
|
321
|
-
|
|
322
|
-
excluding
|
|
323
|
-
hideFilter
|
|
372
|
+
list?: T[];
|
|
373
|
+
excluding?: string[]|number[];
|
|
374
|
+
hideFilter?: boolean;
|
|
324
375
|
|
|
325
|
-
ignoreUrlParam
|
|
376
|
+
ignoreUrlParam?: boolean;
|
|
326
377
|
// locationSearch: string;
|
|
327
378
|
// _currentSortField: string;
|
|
328
379
|
|
|
@@ -333,7 +384,7 @@ export class BaseSearchComponent<T, S extends SearchModel, P extends ModelHistor
|
|
|
333
384
|
deletable?: boolean;
|
|
334
385
|
|
|
335
386
|
protected getModelName(): string {
|
|
336
|
-
return '
|
|
387
|
+
return 'filter';
|
|
337
388
|
}
|
|
338
389
|
|
|
339
390
|
toggleFilter(event: any): void {
|
|
@@ -341,12 +392,12 @@ export class BaseSearchComponent<T, S extends SearchModel, P extends ModelHistor
|
|
|
341
392
|
}
|
|
342
393
|
protected add = (event: any) => {
|
|
343
394
|
event.preventDefault();
|
|
344
|
-
const url = this.props['props'].match.url + '/add';
|
|
395
|
+
const url = (this.props as any)['props'].match.url + '/add';
|
|
345
396
|
this.props.history.push(url);
|
|
346
397
|
}
|
|
347
398
|
load(s: S, autoSearch: boolean): void {
|
|
348
|
-
const obj2 =
|
|
349
|
-
this.
|
|
399
|
+
const obj2 = initFilter(s, this);
|
|
400
|
+
this.setFilter(obj2);
|
|
350
401
|
const com = this;
|
|
351
402
|
if (autoSearch) {
|
|
352
403
|
setTimeout(() => {
|
|
@@ -359,45 +410,51 @@ export class BaseSearchComponent<T, S extends SearchModel, P extends ModelHistor
|
|
|
359
410
|
this.form = form;
|
|
360
411
|
}
|
|
361
412
|
|
|
362
|
-
protected getSearchForm(): HTMLFormElement {
|
|
413
|
+
protected getSearchForm(): HTMLFormElement|undefined|null {
|
|
363
414
|
if (!this.form && this.listFormId) {
|
|
364
415
|
this.form = document.getElementById(this.listFormId) as HTMLFormElement;
|
|
365
416
|
}
|
|
366
417
|
return this.form;
|
|
367
418
|
}
|
|
368
|
-
|
|
369
|
-
this.setState(
|
|
419
|
+
setFilter(filter: S): void {
|
|
420
|
+
this.setState(filter as any);
|
|
370
421
|
}
|
|
371
|
-
protected getCurrencyCode(): string {
|
|
422
|
+
protected getCurrencyCode(): string|undefined {
|
|
372
423
|
return getCurrencyCode(this.form);
|
|
373
424
|
}
|
|
374
|
-
|
|
425
|
+
getFilter(): S {
|
|
375
426
|
const name = this.getModelName();
|
|
376
|
-
|
|
427
|
+
let lc: Locale|undefined;
|
|
428
|
+
if (this.getLocale) {
|
|
429
|
+
lc = this.getLocale();
|
|
430
|
+
}
|
|
431
|
+
if (!lc) {
|
|
432
|
+
lc = enLocale;
|
|
433
|
+
}
|
|
377
434
|
const cc = this.getCurrencyCode();
|
|
378
|
-
const fields = this.
|
|
435
|
+
const fields = this.getFields();
|
|
379
436
|
const l = this.getList();
|
|
380
437
|
const f = this.getSearchForm();
|
|
381
|
-
const dc = (this.ui ? this.ui.decodeFromForm :
|
|
438
|
+
const dc = (this.ui ? this.ui.decodeFromForm : undefined);
|
|
382
439
|
const obj3 = getModel<T, S>(this.state, name, this, fields, this.excluding, this.keys, l, f, dc, lc, cc);
|
|
383
440
|
return obj3;
|
|
384
441
|
}
|
|
385
|
-
protected
|
|
386
|
-
const fs =
|
|
387
|
-
this.
|
|
442
|
+
protected getFields(): string[]|undefined {
|
|
443
|
+
const fs = getFieldsFromForm(this.fields, this.initFields, this.form);
|
|
444
|
+
this.initFields = true;
|
|
388
445
|
return fs;
|
|
389
446
|
}
|
|
390
|
-
|
|
447
|
+
/*
|
|
391
448
|
protected pagingOnClick = (size, e) => {
|
|
392
449
|
this.setState(prevState => ({ isPageSizeOpenDropDown: !(prevState as any).isPageSizeOpenDropDown } as any));
|
|
393
450
|
this.pageSizeChanged(size);
|
|
394
451
|
}
|
|
395
|
-
|
|
452
|
+
*/
|
|
396
453
|
protected pageSizeOnClick = () => {
|
|
397
454
|
this.setState(prevState => ({ isPageSizeOpenDropDown: !(prevState as any).isPageSizeOpenDropDown } as any));
|
|
398
455
|
}
|
|
399
456
|
|
|
400
|
-
protected
|
|
457
|
+
protected clearQ(): void {
|
|
401
458
|
const m = this.state.model;
|
|
402
459
|
if (m) {
|
|
403
460
|
m.q = '';
|
|
@@ -437,16 +494,14 @@ export class BaseSearchComponent<T, S extends SearchModel, P extends ModelHistor
|
|
|
437
494
|
if (listForm && this.ui) {
|
|
438
495
|
this.ui.removeFormError(listForm);
|
|
439
496
|
}
|
|
440
|
-
const s = this.
|
|
497
|
+
const s = this.getFilter();
|
|
441
498
|
const com = this;
|
|
442
499
|
this.validateSearch(s, () => {
|
|
443
500
|
if (com.running === true) {
|
|
444
501
|
return;
|
|
445
502
|
}
|
|
446
503
|
com.running = true;
|
|
447
|
-
|
|
448
|
-
this.loading.showLoading();
|
|
449
|
-
}
|
|
504
|
+
showLoading(this.loading);
|
|
450
505
|
if (!this.ignoreUrlParam) {
|
|
451
506
|
addParametersIntoUrl(s, isFirstLoad);
|
|
452
507
|
}
|
|
@@ -460,14 +515,14 @@ export class BaseSearchComponent<T, S extends SearchModel, P extends ModelHistor
|
|
|
460
515
|
|
|
461
516
|
validateSearch(se: S, callback: () => void): void {
|
|
462
517
|
const u = this.ui;
|
|
463
|
-
const vl = (u ? u.validateForm :
|
|
464
|
-
validate(se, callback, this.getSearchForm(), this.getLocale
|
|
518
|
+
const vl = (u ? u.validateForm : undefined);
|
|
519
|
+
validate(se, callback, this.getSearchForm(), localeOf(undefined, this.getLocale), vl);
|
|
465
520
|
}
|
|
466
521
|
showResults(s: S, sr: SearchResult<T>) {
|
|
467
522
|
const com = this;
|
|
468
523
|
const results = sr.list;
|
|
469
524
|
if (results && results.length > 0) {
|
|
470
|
-
const lc = this.getLocale
|
|
525
|
+
const lc = localeOf(undefined, this.getLocale);
|
|
471
526
|
formatResultsByComponent(results, com, lc);
|
|
472
527
|
}
|
|
473
528
|
const am = com.appendMode;
|
|
@@ -477,29 +532,28 @@ export class BaseSearchComponent<T, S extends SearchModel, P extends ModelHistor
|
|
|
477
532
|
}
|
|
478
533
|
if (am) {
|
|
479
534
|
let limit = s.limit;
|
|
480
|
-
if (s.page <= 1 && s.firstLimit && s.firstLimit > 0) {
|
|
535
|
+
if ((!s.page || s.page <= 1) && s.firstLimit && s.firstLimit > 0) {
|
|
481
536
|
limit = s.firstLimit;
|
|
482
537
|
}
|
|
483
538
|
com.nextPageToken = sr.nextPageToken;
|
|
484
|
-
handleAppend(com,
|
|
485
|
-
if (com.append && s.page > 1) {
|
|
539
|
+
handleAppend(com, sr.list, limit, sr.nextPageToken);
|
|
540
|
+
if (com.append && (s.page && s.page > 1)) {
|
|
486
541
|
com.appendList(results);
|
|
487
542
|
} else {
|
|
488
543
|
com.setList(results);
|
|
489
544
|
}
|
|
490
545
|
} else {
|
|
491
|
-
showPaging(com,
|
|
546
|
+
showPaging(com, sr.list, s.limit, sr.total);
|
|
492
547
|
com.setList(results);
|
|
493
548
|
com.tmpPageIndex = s.page;
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
com.running = false;
|
|
498
|
-
if (this.loading) {
|
|
499
|
-
this.loading.hideLoading();
|
|
549
|
+
if (s.limit) {
|
|
550
|
+
this.showMessage(buildMessage(this.resourceService, s.page, s.limit, sr.list, sr.total));
|
|
551
|
+
}
|
|
500
552
|
}
|
|
553
|
+
com.running = undefined;
|
|
554
|
+
hideLoading(com.loading);
|
|
501
555
|
if (com.triggerSearch) {
|
|
502
|
-
com.triggerSearch =
|
|
556
|
+
com.triggerSearch = undefined;
|
|
503
557
|
com.resetAndSearch();
|
|
504
558
|
}
|
|
505
559
|
}
|
|
@@ -530,7 +584,7 @@ export class BaseSearchComponent<T, S extends SearchModel, P extends ModelHistor
|
|
|
530
584
|
}
|
|
531
585
|
}
|
|
532
586
|
|
|
533
|
-
getList(): T[] {
|
|
587
|
+
getList(): T[]|undefined {
|
|
534
588
|
return this.list;
|
|
535
589
|
}
|
|
536
590
|
|
|
@@ -550,19 +604,19 @@ export class BaseSearchComponent<T, S extends SearchModel, P extends ModelHistor
|
|
|
550
604
|
this.doSearch();
|
|
551
605
|
}
|
|
552
606
|
pageSizeChanged = (event: any) => {
|
|
553
|
-
const size = parseInt((event.currentTarget as HTMLInputElement).value,
|
|
607
|
+
const size = parseInt((event.currentTarget as HTMLInputElement).value, 10);
|
|
554
608
|
changePageSize(this, size);
|
|
555
609
|
this.tmpPageIndex = 1;
|
|
556
610
|
this.doSearch();
|
|
557
611
|
}
|
|
558
612
|
|
|
559
|
-
pageChanged(data) {
|
|
613
|
+
pageChanged(data: any) {
|
|
560
614
|
const { currentPage, itemsPerPage } = data;
|
|
561
615
|
changePage(this, currentPage, itemsPerPage);
|
|
562
616
|
this.doSearch();
|
|
563
617
|
}
|
|
564
618
|
}
|
|
565
|
-
export class SearchComponent<T, S extends
|
|
619
|
+
export class SearchComponent<T, S extends Filter, P extends ModelHistoryProps, I extends SearchState<T, S>> extends BaseSearchComponent<T, S, P, I> {
|
|
566
620
|
constructor(props: P, sv: ((s: S, ctx?: any) => Promise<SearchResult<T>>) | SearchService<T, S>,
|
|
567
621
|
param: ResourceService|SearchParameter,
|
|
568
622
|
showMessage?: (msg: string, option?: string) => void,
|
|
@@ -578,76 +632,72 @@ export class SearchComponent<T, S extends SearchModel, P extends ModelHistoryPro
|
|
|
578
632
|
const x: any = sv;
|
|
579
633
|
this.search = x;
|
|
580
634
|
} else {
|
|
581
|
-
this.
|
|
582
|
-
if (
|
|
583
|
-
this.keys =
|
|
635
|
+
this.search = sv.search;
|
|
636
|
+
if (sv.keys) {
|
|
637
|
+
this.keys = sv.keys();
|
|
584
638
|
}
|
|
585
639
|
}
|
|
586
640
|
}
|
|
587
641
|
this.call = this.call.bind(this);
|
|
588
642
|
this.showError = getErrorFunc(param, showError);
|
|
589
643
|
this.componentDidMount = this.componentDidMount.bind(this);
|
|
590
|
-
this.
|
|
591
|
-
this.
|
|
644
|
+
this.mergeFilter = this.mergeFilter.bind(this);
|
|
645
|
+
this.createFilter = this.createFilter.bind(this);
|
|
592
646
|
this.ref = React.createRef();
|
|
593
647
|
}
|
|
594
648
|
protected showError: (m: string, header?: string, detail?: string, callback?: () => void) => void;
|
|
595
649
|
protected search?: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>;
|
|
596
|
-
protected service: SearchService<T, S>;
|
|
650
|
+
// protected service: SearchService<T, S>;
|
|
597
651
|
protected ref: any;
|
|
598
652
|
protected autoSearch: boolean;
|
|
599
653
|
componentDidMount() {
|
|
600
|
-
const k = (this.ui ? this.ui.registerEvents :
|
|
654
|
+
const k = (this.ui ? this.ui.registerEvents : undefined);
|
|
601
655
|
this.form = initForm(this.ref.current, k);
|
|
602
|
-
const s = this.
|
|
656
|
+
const s = this.mergeFilter(buildFromUrl<S>(), this.createFilter());
|
|
603
657
|
this.load(s, this.autoSearch);
|
|
604
658
|
}
|
|
605
|
-
|
|
606
|
-
return
|
|
659
|
+
mergeFilter(obj: S, b?: S, arrs?: string[]|any): S {
|
|
660
|
+
return mergeFilter2<S>(obj, b, this.pageSizes, arrs);
|
|
607
661
|
}
|
|
608
|
-
|
|
662
|
+
createFilter(): S {
|
|
609
663
|
const s: any = {};
|
|
610
664
|
return s;
|
|
611
665
|
}
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
666
|
+
call(se: S) {
|
|
667
|
+
this.running = true;
|
|
668
|
+
const s = clone(se);
|
|
669
|
+
let page = this.pageIndex;
|
|
670
|
+
if (!page || page < 1) {
|
|
671
|
+
page = 1;
|
|
672
|
+
}
|
|
673
|
+
let offset: number|undefined;
|
|
674
|
+
if (se.limit) {
|
|
621
675
|
if (se.firstLimit && se.firstLimit > 0) {
|
|
622
676
|
offset = se.limit * (page - 2) + se.firstLimit;
|
|
623
677
|
} else {
|
|
624
678
|
offset = se.limit * (page - 1);
|
|
625
679
|
}
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
this.running = false;
|
|
648
|
-
if (this.loading) {
|
|
649
|
-
this.loading.hideLoading();
|
|
650
|
-
}
|
|
680
|
+
}
|
|
681
|
+
const limit = (page <= 1 && se.firstLimit && se.firstLimit > 0 ? se.firstLimit : se.limit);
|
|
682
|
+
const next = (this.nextPageToken && this.nextPageToken.length > 0 ? this.nextPageToken : offset);
|
|
683
|
+
const fields = se.fields;
|
|
684
|
+
delete se['page'];
|
|
685
|
+
delete se['fields'];
|
|
686
|
+
delete se['limit'];
|
|
687
|
+
delete se['firstLimit'];
|
|
688
|
+
showLoading(this.loading);
|
|
689
|
+
const com = this;
|
|
690
|
+
if (this.search) {
|
|
691
|
+
this.search(s, limit, next, fields).then(sr => {
|
|
692
|
+
com.showResults(s, sr);
|
|
693
|
+
com.running = undefined;
|
|
694
|
+
hideLoading(com.loading);
|
|
695
|
+
}).catch(err => {
|
|
696
|
+
com.pageIndex = com.tmpPageIndex;
|
|
697
|
+
error(err, com.resourceService.value, com.showError);
|
|
698
|
+
com.running = undefined;
|
|
699
|
+
hideLoading(com.loading);
|
|
700
|
+
});
|
|
651
701
|
}
|
|
652
702
|
}
|
|
653
703
|
}
|
|
@@ -661,10 +711,11 @@ export abstract class BaseEditComponent<T, P extends ModelHistoryProps, S> exten
|
|
|
661
711
|
getLocale?: () => Locale,
|
|
662
712
|
protected ui?: UIService,
|
|
663
713
|
protected loading?: LoadingService,
|
|
664
|
-
|
|
714
|
+
status?: EditStatusConfig,
|
|
665
715
|
patchable?: boolean, backOnSaveSuccess?: boolean) {
|
|
666
|
-
super(props, getLocale, (ui ? ui.removeError :
|
|
716
|
+
super(props, getLocale, (ui ? ui.removeError : undefined));
|
|
667
717
|
this.resource = resourceService.resource();
|
|
718
|
+
this.status = createEditStatus(status);
|
|
668
719
|
if (patchable === false) {
|
|
669
720
|
this.patchable = patchable;
|
|
670
721
|
}
|
|
@@ -697,16 +748,17 @@ export abstract class BaseEditComponent<T, P extends ModelHistoryProps, S> exten
|
|
|
697
748
|
this.postSave = this.postSave.bind(this);
|
|
698
749
|
this.handleDuplicateKey = this.handleDuplicateKey.bind(this);
|
|
699
750
|
}
|
|
751
|
+
status: EditStatusConfig;
|
|
700
752
|
protected name?: string;
|
|
701
753
|
protected backOnSuccess = true;
|
|
702
754
|
protected resource: StringMap;
|
|
703
755
|
protected metadata?: Attributes;
|
|
704
756
|
protected keys?: string[];
|
|
705
757
|
protected version?: string;
|
|
706
|
-
protected newMode
|
|
707
|
-
protected setBack
|
|
758
|
+
protected newMode?: boolean;
|
|
759
|
+
protected setBack?: boolean;
|
|
708
760
|
protected patchable = true;
|
|
709
|
-
protected orginalModel
|
|
761
|
+
protected orginalModel?: T;
|
|
710
762
|
|
|
711
763
|
addable?: boolean = true;
|
|
712
764
|
readOnly?: boolean;
|
|
@@ -720,12 +772,12 @@ export abstract class BaseEditComponent<T, P extends ModelHistoryProps, S> exten
|
|
|
720
772
|
}
|
|
721
773
|
this.props.history.goBack();
|
|
722
774
|
}
|
|
723
|
-
protected resetState(newMod: boolean, model: T, originalModel
|
|
775
|
+
protected resetState(newMod: boolean, model: T, originalModel?: T) {
|
|
724
776
|
this.newMode = newMod;
|
|
725
777
|
this.orginalModel = originalModel;
|
|
726
778
|
this.showModel(model);
|
|
727
779
|
}
|
|
728
|
-
protected handleNotFound(form?: HTMLFormElement): void {
|
|
780
|
+
protected handleNotFound(form?: HTMLFormElement|null): void {
|
|
729
781
|
const msg = message(this.resourceService.value, 'error_not_found', 'error');
|
|
730
782
|
if (form) {
|
|
731
783
|
readOnly(form);
|
|
@@ -740,7 +792,7 @@ export abstract class BaseEditComponent<T, P extends ModelHistoryProps, S> exten
|
|
|
740
792
|
}
|
|
741
793
|
getModel(): T {
|
|
742
794
|
const n = this.getModelName();
|
|
743
|
-
return this.props[n] || this.state[n];
|
|
795
|
+
return (this.props as any)[n] || (this.state as any)[n];
|
|
744
796
|
}
|
|
745
797
|
showModel(model: T) {
|
|
746
798
|
const f = this.form;
|
|
@@ -773,10 +825,10 @@ export abstract class BaseEditComponent<T, P extends ModelHistoryProps, S> exten
|
|
|
773
825
|
this.form = (event.target as HTMLInputElement).form;
|
|
774
826
|
}
|
|
775
827
|
const obj = this.createModel();
|
|
776
|
-
this.resetState(true, obj,
|
|
828
|
+
this.resetState(true, obj, undefined);
|
|
777
829
|
const u = this.ui;
|
|
778
|
-
|
|
779
|
-
|
|
830
|
+
const f = this.form;
|
|
831
|
+
if (u && f) {
|
|
780
832
|
setTimeout(() => {
|
|
781
833
|
u.removeFormError(f);
|
|
782
834
|
}, 100);
|
|
@@ -830,8 +882,8 @@ export abstract class BaseEditComponent<T, P extends ModelHistoryProps, S> exten
|
|
|
830
882
|
}
|
|
831
883
|
}
|
|
832
884
|
protected validate(obj: T, callback: (obj2?: T) => void) {
|
|
833
|
-
if (this.ui) {
|
|
834
|
-
const valid = this.ui.validateForm(this.form, this.getLocale
|
|
885
|
+
if (this.ui && this.form) {
|
|
886
|
+
const valid = this.ui.validateForm(this.form, localeOf(undefined, this.getLocale));
|
|
835
887
|
if (valid) {
|
|
836
888
|
callback(obj);
|
|
837
889
|
}
|
|
@@ -865,7 +917,7 @@ export abstract class BaseEditComponent<T, P extends ModelHistoryProps, S> exten
|
|
|
865
917
|
const errors = result.errors;
|
|
866
918
|
const f = this.form;
|
|
867
919
|
const u = this.ui;
|
|
868
|
-
if (u) {
|
|
920
|
+
if (u && f) {
|
|
869
921
|
const unmappedErrors = u.showFormError(f, errors);
|
|
870
922
|
if (!result.message) {
|
|
871
923
|
if (errors && errors.length === 1) {
|
|
@@ -878,15 +930,15 @@ export abstract class BaseEditComponent<T, P extends ModelHistoryProps, S> exten
|
|
|
878
930
|
} else if (errors && errors.length === 1) {
|
|
879
931
|
result.message = errors[0].message;
|
|
880
932
|
}
|
|
881
|
-
|
|
882
|
-
|
|
933
|
+
if (result.message) {
|
|
934
|
+
const t = this.resourceService.value('error');
|
|
935
|
+
this.showError(result.message, t);
|
|
936
|
+
}
|
|
883
937
|
}
|
|
884
938
|
|
|
885
939
|
protected postSave(res: number|string|ResultInfo<T>, backOnSave?: boolean) {
|
|
886
940
|
this.running = false;
|
|
887
|
-
|
|
888
|
-
this.loading.hideLoading();
|
|
889
|
-
}
|
|
941
|
+
hideLoading(this.loading);
|
|
890
942
|
const st = this.status;
|
|
891
943
|
const newMod = this.newMode;
|
|
892
944
|
const successMsg = (newMod ? this.insertSuccessMsg : this.updateSuccessMsg);
|
|
@@ -925,7 +977,7 @@ export abstract class BaseEditComponent<T, P extends ModelHistoryProps, S> exten
|
|
|
925
977
|
this.showError(msg.message, msg.title);
|
|
926
978
|
}
|
|
927
979
|
}
|
|
928
|
-
export class EditComponent<T, ID, P extends
|
|
980
|
+
export class EditComponent<T, ID, P extends RouteComponentProps, S> extends BaseEditComponent<T, P, S> {
|
|
929
981
|
constructor(props: P, protected service: GenericService<T, ID, number|ResultInfo<T>>,
|
|
930
982
|
param: ResourceService|EditParameter,
|
|
931
983
|
showMessage?: (msg: string, option?: string) => void,
|
|
@@ -939,8 +991,10 @@ export class EditComponent<T, ID, P extends ModelHistoryProps, S> extends BaseEd
|
|
|
939
991
|
const metadata = service.metadata();
|
|
940
992
|
if (metadata) {
|
|
941
993
|
const meta = build(metadata);
|
|
942
|
-
|
|
943
|
-
|
|
994
|
+
if (meta) {
|
|
995
|
+
this.keys = meta.keys;
|
|
996
|
+
this.version = meta.version;
|
|
997
|
+
}
|
|
944
998
|
this.metadata = metadata;
|
|
945
999
|
}
|
|
946
1000
|
}
|
|
@@ -960,59 +1014,57 @@ export class EditComponent<T, ID, P extends ModelHistoryProps, S> extends BaseEd
|
|
|
960
1014
|
}
|
|
961
1015
|
protected ref: any;
|
|
962
1016
|
componentDidMount() {
|
|
963
|
-
const k = (this.ui ? this.ui.registerEvents :
|
|
1017
|
+
const k = (this.ui ? this.ui.registerEvents : undefined);
|
|
964
1018
|
this.form = initForm(this.ref.current, k);
|
|
965
1019
|
const id = buildId<ID>(this.props, this.keys);
|
|
966
|
-
|
|
1020
|
+
if (id) {
|
|
1021
|
+
this.load(id);
|
|
1022
|
+
}
|
|
967
1023
|
}
|
|
968
|
-
|
|
1024
|
+
load(_id: ID, callback?: (m: T, showM: (m2: T) => void) => void) {
|
|
969
1025
|
const id: any = _id;
|
|
970
1026
|
if (id != null && id !== '') {
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
}
|
|
976
|
-
const ctx: any = {};
|
|
977
|
-
const obj = await this.service.load(id, ctx);
|
|
1027
|
+
const com = this;
|
|
1028
|
+
this.running = true;
|
|
1029
|
+
showLoading(com.loading);
|
|
1030
|
+
this.service.load(id).then(obj => {
|
|
978
1031
|
if (!obj) {
|
|
979
|
-
|
|
1032
|
+
com.handleNotFound(this.form);
|
|
980
1033
|
} else {
|
|
981
|
-
|
|
982
|
-
|
|
1034
|
+
com.newMode = false;
|
|
1035
|
+
com.orginalModel = clone(obj);
|
|
983
1036
|
if (!callback) {
|
|
984
|
-
|
|
1037
|
+
com.showModel(obj);
|
|
985
1038
|
} else {
|
|
986
|
-
callback(obj,
|
|
1039
|
+
callback(obj, com.showModel);
|
|
987
1040
|
}
|
|
988
1041
|
}
|
|
989
|
-
|
|
1042
|
+
com.running = false;
|
|
1043
|
+
hideLoading(com.loading);
|
|
1044
|
+
}).catch(err => {
|
|
990
1045
|
const data = (err && err.response) ? err.response : err;
|
|
991
|
-
const r =
|
|
1046
|
+
const r = com.resourceService;
|
|
992
1047
|
const gv = r.value;
|
|
993
1048
|
const title = gv('error');
|
|
994
1049
|
let msg = gv('error_internal');
|
|
995
1050
|
if (data && data.status === 404) {
|
|
996
|
-
|
|
1051
|
+
com.handleNotFound(com.form);
|
|
997
1052
|
} else {
|
|
998
1053
|
if (data.status && !isNaN(data.status)) {
|
|
999
1054
|
msg = messageByHttpStatus(data.status, gv);
|
|
1000
1055
|
}
|
|
1001
1056
|
if (data && (data.status === 401 || data.status === 403)) {
|
|
1002
|
-
readOnly(
|
|
1057
|
+
readOnly(com.form);
|
|
1003
1058
|
}
|
|
1004
|
-
|
|
1059
|
+
com.showError(msg, title);
|
|
1005
1060
|
}
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
this.loading.hideLoading();
|
|
1010
|
-
}
|
|
1011
|
-
}
|
|
1061
|
+
com.running = false;
|
|
1062
|
+
hideLoading(com.loading);
|
|
1063
|
+
});
|
|
1012
1064
|
} else {
|
|
1013
1065
|
// Call service state
|
|
1014
1066
|
this.newMode = true;
|
|
1015
|
-
this.orginalModel =
|
|
1067
|
+
this.orginalModel = undefined;
|
|
1016
1068
|
const obj = this.createModel();
|
|
1017
1069
|
if (callback) {
|
|
1018
1070
|
callback(obj, this.showModel);
|
|
@@ -1021,35 +1073,28 @@ export class EditComponent<T, ID, P extends ModelHistoryProps, S> extends BaseEd
|
|
|
1021
1073
|
}
|
|
1022
1074
|
}
|
|
1023
1075
|
}
|
|
1024
|
-
protected
|
|
1076
|
+
protected save(obj: T, body?: T, isBack?: boolean) {
|
|
1025
1077
|
this.running = true;
|
|
1026
|
-
|
|
1027
|
-
this.loading.showLoading();
|
|
1028
|
-
}
|
|
1078
|
+
showLoading(this.loading);
|
|
1029
1079
|
const isBackO = (isBack == null || isBack === undefined ? this.backOnSuccess : isBack);
|
|
1030
1080
|
const com = this;
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
} else {
|
|
1038
|
-
const result = await this.service.update(obj, ctx);
|
|
1039
|
-
com.postSave(result, isBackO);
|
|
1040
|
-
}
|
|
1041
|
-
} else {
|
|
1042
|
-
const result = await this.service.insert(obj, ctx);
|
|
1043
|
-
com.postSave(result, isBackO);
|
|
1044
|
-
}
|
|
1045
|
-
} catch (err) {
|
|
1046
|
-
error(err, this.resourceService.value, this.showError);
|
|
1047
|
-
} finally {
|
|
1048
|
-
this.running = false;
|
|
1049
|
-
if (this.loading) {
|
|
1050
|
-
this.loading.hideLoading();
|
|
1081
|
+
let m = obj;
|
|
1082
|
+
let fn = this.newMode ? this.service.insert : this.service.update;
|
|
1083
|
+
if (!this.newMode) {
|
|
1084
|
+
if (this.patchable === true && this.service.patch && body && Object.keys(body).length > 0) {
|
|
1085
|
+
m = body;
|
|
1086
|
+
fn = this.service.patch;
|
|
1051
1087
|
}
|
|
1052
1088
|
}
|
|
1089
|
+
fn(m).then(result => {
|
|
1090
|
+
com.postSave(result, isBackO);
|
|
1091
|
+
com.running = false;
|
|
1092
|
+
hideLoading(com.loading);
|
|
1093
|
+
}).then(err => {
|
|
1094
|
+
error(err, com.resourceService.value, com.showError);
|
|
1095
|
+
com.running = false;
|
|
1096
|
+
hideLoading(com.loading);
|
|
1097
|
+
});
|
|
1053
1098
|
}
|
|
1054
1099
|
}
|
|
1055
1100
|
|
|
@@ -1058,7 +1103,7 @@ export class BaseDiffApprComponent<T, ID, P extends HistoryProps, S extends Base
|
|
|
1058
1103
|
protected showMessage: (msg: string, option?: string) => void,
|
|
1059
1104
|
protected showError: (m: string, title?: string, detail?: string, callback?: () => void) => void,
|
|
1060
1105
|
protected loading?: LoadingService,
|
|
1061
|
-
|
|
1106
|
+
status?: DiffStatusConfig,
|
|
1062
1107
|
) {
|
|
1063
1108
|
super(props);
|
|
1064
1109
|
// this._id = props['props'].match.params.id || props['props'].match.params.cId || props.match.params.cId;
|
|
@@ -1072,16 +1117,15 @@ export class BaseDiffApprComponent<T, ID, P extends HistoryProps, S extends Base
|
|
|
1072
1117
|
this.postReject = this.postReject.bind(this);
|
|
1073
1118
|
this.format = this.format.bind(this);
|
|
1074
1119
|
this.handleNotFound = this.handleNotFound.bind(this);
|
|
1075
|
-
|
|
1076
|
-
this.status = createDiffStatus();
|
|
1077
|
-
}
|
|
1120
|
+
this.status = createDiffStatus(status);
|
|
1078
1121
|
this.state = {
|
|
1079
1122
|
disabled: false
|
|
1080
1123
|
};
|
|
1081
1124
|
}
|
|
1082
|
-
protected
|
|
1083
|
-
protected
|
|
1084
|
-
protected
|
|
1125
|
+
protected status: DiffStatusConfig;
|
|
1126
|
+
protected id?: ID;
|
|
1127
|
+
protected form?: HTMLFormElement;
|
|
1128
|
+
protected running?: boolean;
|
|
1085
1129
|
protected resource: StringMap;
|
|
1086
1130
|
|
|
1087
1131
|
protected back(event: any) {
|
|
@@ -1135,12 +1179,15 @@ export class BaseDiffApprComponent<T, ID, P extends HistoryProps, S extends Base
|
|
|
1135
1179
|
const differentKeys = diff(diffModel.origin, diffModel.value);
|
|
1136
1180
|
const dataFields = getDataFields(this.form);
|
|
1137
1181
|
dataFields.forEach(e => {
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
(e.childNodes
|
|
1142
|
-
|
|
1143
|
-
|
|
1182
|
+
const x = e.getAttribute('data-field');
|
|
1183
|
+
if (x) {
|
|
1184
|
+
if (differentKeys.indexOf(x) >= 0) {
|
|
1185
|
+
if (e.childNodes.length === 3) {
|
|
1186
|
+
(e.childNodes[1] as HTMLElement).classList.add('highlight');
|
|
1187
|
+
(e.childNodes[2] as HTMLElement).classList.add('highlight');
|
|
1188
|
+
} else {
|
|
1189
|
+
e.classList.add('highlight');
|
|
1190
|
+
}
|
|
1144
1191
|
}
|
|
1145
1192
|
}
|
|
1146
1193
|
});
|
|
@@ -1149,12 +1196,15 @@ export class BaseDiffApprComponent<T, ID, P extends HistoryProps, S extends Base
|
|
|
1149
1196
|
const differentKeys = diff(origin, value);
|
|
1150
1197
|
const dataFields = getDataFields(this.form);
|
|
1151
1198
|
dataFields.forEach(e => {
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
(e.childNodes
|
|
1156
|
-
|
|
1157
|
-
|
|
1199
|
+
const x = e.getAttribute('data-field');
|
|
1200
|
+
if (x) {
|
|
1201
|
+
if (differentKeys.indexOf(x) >= 0) {
|
|
1202
|
+
if (e.childNodes.length === 3) {
|
|
1203
|
+
(e.childNodes[1] as HTMLElement).classList.add('highlight');
|
|
1204
|
+
(e.childNodes[2] as HTMLElement).classList.add('highlight');
|
|
1205
|
+
} else {
|
|
1206
|
+
e.classList.add('highlight');
|
|
1207
|
+
}
|
|
1158
1208
|
}
|
|
1159
1209
|
}
|
|
1160
1210
|
});
|
|
@@ -1167,8 +1217,7 @@ export class BaseDiffApprComponent<T, ID, P extends HistoryProps, S extends Base
|
|
|
1167
1217
|
this.showError(msg.message, msg.title);
|
|
1168
1218
|
}
|
|
1169
1219
|
}
|
|
1170
|
-
|
|
1171
|
-
export class DiffApprComponent<T, ID, P extends HistoryProps, S extends DiffState<T>> extends BaseDiffApprComponent<T, ID, P, S> {
|
|
1220
|
+
export class DiffApprComponent<T, ID, P extends RouteComponentProps, S extends DiffState<T>> extends BaseDiffApprComponent<T, ID, P, S> {
|
|
1172
1221
|
constructor(props: P, protected service: DiffApprService<T, ID>,
|
|
1173
1222
|
param: ResourceService|DiffParameter,
|
|
1174
1223
|
showMessage?: (msg: string, option?: string) => void,
|
|
@@ -1191,85 +1240,80 @@ export class DiffApprComponent<T, ID, P extends HistoryProps, S extends DiffStat
|
|
|
1191
1240
|
componentDidMount() {
|
|
1192
1241
|
this.form = this.ref.current;
|
|
1193
1242
|
const id = buildId<ID>(this.props, this.keys);
|
|
1194
|
-
|
|
1243
|
+
if (id) {
|
|
1244
|
+
this.load(id);
|
|
1245
|
+
}
|
|
1195
1246
|
}
|
|
1196
1247
|
|
|
1197
1248
|
formatFields(value: T): T {
|
|
1198
1249
|
return value;
|
|
1199
1250
|
}
|
|
1200
1251
|
|
|
1201
|
-
|
|
1252
|
+
load(_id: ID) {
|
|
1202
1253
|
const id: any = _id;
|
|
1203
1254
|
if (id != null && id !== '') {
|
|
1204
1255
|
this.id = _id;
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
}
|
|
1210
|
-
const dobj = await this.service.diff(id);
|
|
1256
|
+
const com = this;
|
|
1257
|
+
this.running = true;
|
|
1258
|
+
showLoading(this.loading);
|
|
1259
|
+
this.service.diff(id).then(dobj => {
|
|
1211
1260
|
if (!dobj) {
|
|
1212
|
-
|
|
1261
|
+
com.handleNotFound();
|
|
1213
1262
|
} else {
|
|
1214
|
-
const formatdDiff = formatDiffModel(dobj,
|
|
1215
|
-
|
|
1263
|
+
const formatdDiff = formatDiffModel(dobj, com.formatFields);
|
|
1264
|
+
com.setState({
|
|
1216
1265
|
origin: formatdDiff.origin,
|
|
1217
1266
|
value: formatdDiff.value
|
|
1218
|
-
},
|
|
1267
|
+
}, com.format);
|
|
1219
1268
|
}
|
|
1220
|
-
|
|
1269
|
+
com.running = false;
|
|
1270
|
+
hideLoading(com.loading);
|
|
1271
|
+
}).catch(err => {
|
|
1221
1272
|
const data = (err && err.response) ? err.response : err;
|
|
1222
1273
|
if (data && data.status === 404) {
|
|
1223
|
-
|
|
1274
|
+
com.handleNotFound();
|
|
1224
1275
|
} else {
|
|
1225
|
-
error(err,
|
|
1276
|
+
error(err, com.resourceService.value, com.showError);
|
|
1226
1277
|
}
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
this.loading.hideLoading();
|
|
1231
|
-
}
|
|
1232
|
-
}
|
|
1278
|
+
com.running = false;
|
|
1279
|
+
hideLoading(com.loading);
|
|
1280
|
+
});
|
|
1233
1281
|
}
|
|
1234
1282
|
}
|
|
1235
1283
|
|
|
1236
|
-
|
|
1284
|
+
approve(event: any) {
|
|
1237
1285
|
event.preventDefault();
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
this.loading.hideLoading();
|
|
1252
|
-
}
|
|
1286
|
+
const com = this;
|
|
1287
|
+
this.running = true;
|
|
1288
|
+
showLoading(this.loading);
|
|
1289
|
+
if (this.id) {
|
|
1290
|
+
this.service.approve(this.id).then(status => {
|
|
1291
|
+
com.postApprove(status, null);
|
|
1292
|
+
com.running = false;
|
|
1293
|
+
hideLoading(com.loading);
|
|
1294
|
+
}).catch(err => {
|
|
1295
|
+
com.postApprove(4, err);
|
|
1296
|
+
com.running = false;
|
|
1297
|
+
hideLoading(com.loading);
|
|
1298
|
+
});
|
|
1253
1299
|
}
|
|
1254
1300
|
}
|
|
1255
1301
|
|
|
1256
|
-
|
|
1302
|
+
reject(event: any) {
|
|
1257
1303
|
event.preventDefault();
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
this.loading.hideLoading();
|
|
1272
|
-
}
|
|
1304
|
+
const com = this;
|
|
1305
|
+
this.running = true;
|
|
1306
|
+
showLoading(this.loading);
|
|
1307
|
+
if (this.id) {
|
|
1308
|
+
this.service.reject(this.id).then(status => {
|
|
1309
|
+
com.postReject(status, null);
|
|
1310
|
+
com.running = false;
|
|
1311
|
+
hideLoading(com.loading);
|
|
1312
|
+
}).catch(err => {
|
|
1313
|
+
com.postReject(4, err);
|
|
1314
|
+
com.running = false;
|
|
1315
|
+
hideLoading(com.loading);
|
|
1316
|
+
});
|
|
1273
1317
|
}
|
|
1274
1318
|
}
|
|
1275
1319
|
}
|