@wawjs/ngx-bos 22.0.6 → 22.0.7
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/README.md +1 -1
- package/RELEASES.md +38 -0
- package/fesm2022/file.service-C8Erkqrm.mjs +48 -0
- package/fesm2022/file.service-C8Erkqrm.mjs.map +1 -0
- package/fesm2022/{wawjs-ngx-bos-ngx-bos.config-B6tWU_X3.mjs → ngx-bos.config-DjtgUjR8.mjs} +3 -5
- package/fesm2022/ngx-bos.config-DjtgUjR8.mjs.map +1 -0
- package/fesm2022/{wawjs-ngx-bos-user.service-DFgHcvNR.mjs → user-crud.component-B1_SlIDS.mjs} +146 -16
- package/fesm2022/user-crud.component-B1_SlIDS.mjs.map +1 -0
- package/fesm2022/wawjs-ngx-bos.mjs +201 -184
- package/fesm2022/wawjs-ngx-bos.mjs.map +1 -1
- package/package.json +11 -9
- package/src/feature/file/file.component.html +1 -0
- package/src/feature/file/file.component.ts +34 -8
- package/src/feature/user/pages/clients/clients.component.ts +19 -45
- package/src/feature/user/pages/user-crud.component.ts +119 -0
- package/src/feature/user/pages/users/users.component.ts +33 -86
- package/src/feature/user/user.service.ts +102 -15
- package/src/form-components/ace/ace.component.ts +15 -1
- package/src/form-components/file/file.component.html +2 -2
- package/src/form-components/form-config-cache.ts +70 -0
- package/src/form-components/input/input.form.ts +1 -1
- package/src/form-components/select/select.component.html +24 -24
- package/src/form-components/tinymce/tinymce.component.ts +12 -0
- package/src/ngx-bos.config.ts +6 -4
- package/src/pages/form/form.component.ts +44 -7
- package/src/pages/forms/forms.component.ts +14 -2
- package/src/public-api.ts +1 -1
- package/types/wawjs-ngx-bos.d.ts +52 -15
- package/fesm2022/wawjs-ngx-bos-ngx-bos.config-B6tWU_X3.mjs.map +0 -1
- package/fesm2022/wawjs-ngx-bos-user.service-DFgHcvNR.mjs.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
{
|
|
2
|
-
"name": "@wawjs/ngx-bos",
|
|
3
|
-
"version": "22.0.6",
|
|
4
2
|
"license": "MIT",
|
|
5
3
|
"peerDependencies": {
|
|
6
4
|
"@angular/common": "^22.0.0",
|
|
@@ -23,16 +21,20 @@
|
|
|
23
21
|
"tslib": "2.8.1"
|
|
24
22
|
},
|
|
25
23
|
"sideEffects": false,
|
|
26
|
-
"
|
|
27
|
-
"
|
|
24
|
+
"name": "@wawjs/ngx-bos",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "./fesm2022/wawjs-ngx-bos.mjs",
|
|
27
|
+
"module": "./fesm2022/wawjs-ngx-bos.mjs",
|
|
28
|
+
"typings": "./types/wawjs-ngx-bos.d.ts",
|
|
29
|
+
"types": "./types/wawjs-ngx-bos.d.ts",
|
|
28
30
|
"exports": {
|
|
29
|
-
"./package.json": {
|
|
30
|
-
"default": "./package.json"
|
|
31
|
-
},
|
|
32
31
|
".": {
|
|
33
32
|
"types": "./types/wawjs-ngx-bos.d.ts",
|
|
34
33
|
"default": "./fesm2022/wawjs-ngx-bos.mjs"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": {
|
|
36
|
+
"default": "./package.json"
|
|
35
37
|
}
|
|
36
38
|
},
|
|
37
|
-
"
|
|
38
|
-
}
|
|
39
|
+
"version": "22.0.7"
|
|
40
|
+
}
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
input,
|
|
8
8
|
model,
|
|
9
9
|
} from '@angular/core';
|
|
10
|
-
import { ButtonComponent, ModalService } from '@wawjs/ngx-ui';
|
|
10
|
+
import { AlertService, ButtonComponent, ModalService } from '@wawjs/ngx-ui';
|
|
11
11
|
import { fileToDataUrl } from './file.util';
|
|
12
12
|
import { TranslateDirective } from '@wawjs/ngx-translate';
|
|
13
13
|
import type { FabricCropResult } from '@wawjs/ngx-fabric';
|
|
@@ -61,6 +61,7 @@ export class FileComponent {
|
|
|
61
61
|
|
|
62
62
|
private readonly _modal = inject(ModalService);
|
|
63
63
|
private readonly _fs = inject(FileService);
|
|
64
|
+
private readonly _alert = inject(AlertService);
|
|
64
65
|
|
|
65
66
|
readonly isMultiple = computed(() => this.mode().startsWith('multi-'));
|
|
66
67
|
readonly isImageMode = computed(() => this.mode().includes('image'));
|
|
@@ -74,9 +75,9 @@ export class FileComponent {
|
|
|
74
75
|
case 'single-file':
|
|
75
76
|
return this.wFile() ? [this.wFile() as string] : [];
|
|
76
77
|
case 'multi-image':
|
|
77
|
-
return this.wImages();
|
|
78
|
+
return this.wImages() ?? [];
|
|
78
79
|
case 'multi-file':
|
|
79
|
-
return this.wFiles();
|
|
80
|
+
return this.wFiles() ?? [];
|
|
80
81
|
default:
|
|
81
82
|
return [];
|
|
82
83
|
}
|
|
@@ -100,7 +101,6 @@ export class FileComponent {
|
|
|
100
101
|
this.isImageMode() && !!this.cropWidth() && !!this.cropHeight();
|
|
101
102
|
const urls: string[] = [];
|
|
102
103
|
const container = this.container();
|
|
103
|
-
const name = this.name();
|
|
104
104
|
|
|
105
105
|
try {
|
|
106
106
|
for (let i = 0; i < list.length; i++) {
|
|
@@ -129,7 +129,7 @@ export class FileComponent {
|
|
|
129
129
|
outputHeight: cropHeight,
|
|
130
130
|
onCrop: (result: FabricCropResult) => {
|
|
131
131
|
this._fs
|
|
132
|
-
.uploadBase64(result.base64, container, name)
|
|
132
|
+
.uploadBase64(result.base64, container, this._uploadName(result.base64, f.name))
|
|
133
133
|
.then((url) => {
|
|
134
134
|
if (url) {
|
|
135
135
|
urls.push(url);
|
|
@@ -139,13 +139,15 @@ export class FileComponent {
|
|
|
139
139
|
}, reject);
|
|
140
140
|
},
|
|
141
141
|
onCancel: () => resolve(),
|
|
142
|
+
// Escape, backdrop and the close button only call onClose.
|
|
143
|
+
onClose: () => resolve(),
|
|
142
144
|
});
|
|
143
145
|
});
|
|
144
146
|
} else {
|
|
145
147
|
const url = await this._fs.uploadBase64(
|
|
146
148
|
dataUrl,
|
|
147
149
|
container,
|
|
148
|
-
name,
|
|
150
|
+
this._uploadName(dataUrl, f.name),
|
|
149
151
|
);
|
|
150
152
|
|
|
151
153
|
if (url) {
|
|
@@ -158,6 +160,8 @@ export class FileComponent {
|
|
|
158
160
|
break;
|
|
159
161
|
}
|
|
160
162
|
}
|
|
163
|
+
} catch {
|
|
164
|
+
this._alert.error({ text: 'File upload failed' });
|
|
161
165
|
} finally {
|
|
162
166
|
// Keep uploads that succeeded before a failure and always reset the input.
|
|
163
167
|
this._applyNewUrls(urls);
|
|
@@ -202,6 +206,28 @@ export class FileComponent {
|
|
|
202
206
|
}
|
|
203
207
|
}
|
|
204
208
|
|
|
209
|
+
/**
|
|
210
|
+
* The backend takes the stored file's extension from `name`; without one, images are saved
|
|
211
|
+
* with no extension and are later shown as plain links.
|
|
212
|
+
*/
|
|
213
|
+
private _uploadName(dataUrl: string, fileName: string): string {
|
|
214
|
+
const configured = this.name();
|
|
215
|
+
|
|
216
|
+
if (configured) {
|
|
217
|
+
return configured;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const mime = /^data:([^;,]+)/.exec(dataUrl)?.[1] ?? '';
|
|
221
|
+
const extension: Record<string, string> = {
|
|
222
|
+
'image/jpeg': 'jpg',
|
|
223
|
+
'image/png': 'png',
|
|
224
|
+
'image/webp': 'webp',
|
|
225
|
+
'image/gif': 'gif',
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
return extension[mime] ? `file.${extension[mime]}` : fileName;
|
|
229
|
+
}
|
|
230
|
+
|
|
205
231
|
private _cropStageWidth(width: number): number {
|
|
206
232
|
return Math.min(820, Math.max(width, Math.round(width * 1.45)));
|
|
207
233
|
}
|
|
@@ -228,12 +254,12 @@ export class FileComponent {
|
|
|
228
254
|
}
|
|
229
255
|
|
|
230
256
|
if (mode === 'multi-image') {
|
|
231
|
-
this.wImages.set([...this.wImages(), ...urls]);
|
|
257
|
+
this.wImages.set([...(this.wImages() ?? []), ...urls]);
|
|
232
258
|
return;
|
|
233
259
|
}
|
|
234
260
|
|
|
235
261
|
if (mode === 'multi-file') {
|
|
236
|
-
this.wFiles.set([...this.wFiles(), ...urls]);
|
|
262
|
+
this.wFiles.set([...(this.wFiles() ?? []), ...urls]);
|
|
237
263
|
}
|
|
238
264
|
}
|
|
239
265
|
}
|
|
@@ -1,45 +1,19 @@
|
|
|
1
|
-
import { Component
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
protected override configType: 'server' | 'local' = 'server';
|
|
21
|
-
|
|
22
|
-
/** Basic columns for clients table */
|
|
23
|
-
columns = ['name', 'email'];
|
|
24
|
-
|
|
25
|
-
/** wtable config built by CrudComponent, aware of server mode */
|
|
26
|
-
config = this.getConfig();
|
|
27
|
-
|
|
28
|
-
protected override allowUrl(): boolean {
|
|
29
|
-
// no URL-driven filters for now, same as UsersComponent
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
constructor() {
|
|
34
|
-
// 'user' docType reuses existing user form components
|
|
35
|
-
super(
|
|
36
|
-
userFormComponentsFactory(),
|
|
37
|
-
inject(FormService),
|
|
38
|
-
inject(UserService),
|
|
39
|
-
'user',
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
// fetch first page from server using CrudComponent logic
|
|
43
|
-
this.setDocuments();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
1
|
+
import { Component } from '@angular/core';
|
|
2
|
+
import { TableComponent } from '@wawjs/ngx-ui';
|
|
3
|
+
import { UserCrudComponent } from '../user-crud.component';
|
|
4
|
+
|
|
5
|
+
@Component({
|
|
6
|
+
imports: [TableComponent],
|
|
7
|
+
templateUrl: './clients.component.html',
|
|
8
|
+
styleUrl: './clients.component.scss',
|
|
9
|
+
})
|
|
10
|
+
export class ClientsComponent extends UserCrudComponent {
|
|
11
|
+
/** Basic columns for clients table */
|
|
12
|
+
columns = ['name', 'email'];
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Local mode reads the shared user list. Server mode would replace that list with one page and
|
|
16
|
+
* shrink the Users table and the user selectors until reload.
|
|
17
|
+
*/
|
|
18
|
+
config = this.withoutBulkEdit(this.getConfig());
|
|
19
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { effect, inject, untracked } from '@angular/core';
|
|
2
|
+
import { CrudComponent, TableConfig } from '@wawjs/ngx-crud';
|
|
3
|
+
import { FormInterface, FormService } from '@wawjs/ngx-form';
|
|
4
|
+
import { AlertService } from '@wawjs/ngx-ui';
|
|
5
|
+
import { userFormComponentsFactory } from '../user.form';
|
|
6
|
+
import { User } from '../user.interface';
|
|
7
|
+
import { UserService } from '../user.service';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Shared admin behavior for the Users and Clients tables.
|
|
11
|
+
*
|
|
12
|
+
* The backend's unnamed `/api/user/update` always targets the signed-in user, and user delete only
|
|
13
|
+
* exists as the `admin` variant, so every mutation here goes through `updateAdmin` / `deleteAdmin`.
|
|
14
|
+
* Email is not an updatable key on the backend, so the edit form leaves it out.
|
|
15
|
+
*/
|
|
16
|
+
export abstract class UserCrudComponent extends CrudComponent<UserService, User, FormInterface> {
|
|
17
|
+
protected readonly userService = inject(UserService);
|
|
18
|
+
|
|
19
|
+
private readonly _userFormService = inject(FormService);
|
|
20
|
+
|
|
21
|
+
private readonly _userAlertService = inject(AlertService);
|
|
22
|
+
|
|
23
|
+
private readonly _createForm = userFormComponentsFactory();
|
|
24
|
+
|
|
25
|
+
private readonly _editForm: FormInterface = {
|
|
26
|
+
...this._createForm,
|
|
27
|
+
formId: 'user-edit',
|
|
28
|
+
components: this._createForm.components.filter((component) => component.key !== 'email'),
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
protected override configType: 'server' | 'local' = 'local';
|
|
32
|
+
|
|
33
|
+
constructor() {
|
|
34
|
+
super(userFormComponentsFactory(), inject(FormService), inject(UserService), 'user');
|
|
35
|
+
|
|
36
|
+
// The user list arrives after startup; keep the table in sync with it. setDocuments() reads
|
|
37
|
+
// and writes document signals, so it runs untracked to keep the effect from re-dirtying itself.
|
|
38
|
+
effect(() => {
|
|
39
|
+
this.userService.documents();
|
|
40
|
+
|
|
41
|
+
if (this.userService.isLoaded()) {
|
|
42
|
+
untracked(() => void this.setDocuments());
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected override allowUrl(): boolean {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Bulk edit would post every row through the self-update route, so only bulk create stays. */
|
|
52
|
+
protected withoutBulkEdit(config: TableConfig<User>): TableConfig<User> {
|
|
53
|
+
return {
|
|
54
|
+
...config,
|
|
55
|
+
headerButtons: (config.headerButtons || []).filter((button) => button.class !== 'edit'),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected override create(): void {
|
|
60
|
+
if (this._createForm.formId) {
|
|
61
|
+
this._userFormService.reset(this._createForm.formId, this.createObject());
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
super.create();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected override update(doc: User): void {
|
|
68
|
+
this._userFormService.reset(
|
|
69
|
+
this._editForm.formId as string,
|
|
70
|
+
doc as unknown as Record<string, unknown>,
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
this._userFormService.modal(
|
|
74
|
+
this._editForm,
|
|
75
|
+
{
|
|
76
|
+
label: 'Update',
|
|
77
|
+
click: (updated: unknown, close: () => void) => {
|
|
78
|
+
close();
|
|
79
|
+
|
|
80
|
+
const { email: _email, ...changes } = updated as User;
|
|
81
|
+
|
|
82
|
+
// Send a copy; mutating the row would also change the cached document on failure.
|
|
83
|
+
const next: User = { ...doc, ...changes };
|
|
84
|
+
|
|
85
|
+
this.userService.updateAdmin(next).subscribe({
|
|
86
|
+
next: () => {
|
|
87
|
+
void this.setDocuments();
|
|
88
|
+
},
|
|
89
|
+
error: () => {
|
|
90
|
+
void this.setDocuments();
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
doc,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
protected override async delete(doc: User): Promise<void> {
|
|
100
|
+
this._userAlertService.question({
|
|
101
|
+
text: 'Delete this user?',
|
|
102
|
+
timeout: 0,
|
|
103
|
+
progress: false,
|
|
104
|
+
buttons: [
|
|
105
|
+
{
|
|
106
|
+
text: 'Delete',
|
|
107
|
+
callback: () => {
|
|
108
|
+
this.userService.deleteAdmin(doc).subscribe(() => {
|
|
109
|
+
void this.setDocuments();
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
text: 'Cancel',
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -1,86 +1,33 @@
|
|
|
1
|
-
import { Component
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this._formService.reset(
|
|
35
|
-
this.form.formId,
|
|
36
|
-
doc as unknown as Record<string, unknown>,
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
this._formService.modal(
|
|
41
|
-
this.form,
|
|
42
|
-
{
|
|
43
|
-
label: 'Update',
|
|
44
|
-
click: (updated: unknown, close: () => void) => {
|
|
45
|
-
close();
|
|
46
|
-
Object.assign(doc, updated as User);
|
|
47
|
-
this.userService.updateAdmin(doc).subscribe(() => {
|
|
48
|
-
this.setDocuments();
|
|
49
|
-
});
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
doc,
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
userService = inject(UserService);
|
|
57
|
-
|
|
58
|
-
/** Every role gets a checkbox column; matches the columns added in the constructor. */
|
|
59
|
-
readonly roles = this.userService.roles;
|
|
60
|
-
|
|
61
|
-
columns = ['name', 'email'];
|
|
62
|
-
|
|
63
|
-
config = this.getConfig();
|
|
64
|
-
|
|
65
|
-
constructor() {
|
|
66
|
-
super(
|
|
67
|
-
userFormComponentsFactory(),
|
|
68
|
-
inject(FormService),
|
|
69
|
-
inject(UserService),
|
|
70
|
-
'user',
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
this.setDocuments();
|
|
74
|
-
|
|
75
|
-
for (const role of this.userService.roles) {
|
|
76
|
-
this.columns.push(role);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
updateRole(user: User, role: string, value: boolean): void {
|
|
81
|
-
user.is[role] = value;
|
|
82
|
-
this.userService.updateAdmin(user).subscribe(() => {
|
|
83
|
-
this.setDocuments();
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
}
|
|
1
|
+
import { Component } from '@angular/core';
|
|
2
|
+
import { CellDirective, InputComponent, TableComponent } from '@wawjs/ngx-ui';
|
|
3
|
+
import { User } from '../../user.interface';
|
|
4
|
+
import { UserCrudComponent } from '../user-crud.component';
|
|
5
|
+
|
|
6
|
+
@Component({
|
|
7
|
+
imports: [TableComponent, CellDirective, InputComponent],
|
|
8
|
+
templateUrl: './users.component.html',
|
|
9
|
+
styleUrl: './users.component.scss',
|
|
10
|
+
})
|
|
11
|
+
export class UsersComponent extends UserCrudComponent {
|
|
12
|
+
/** Every role gets a checkbox column; matches the columns added in the constructor. */
|
|
13
|
+
readonly roles = this.userService.roles;
|
|
14
|
+
|
|
15
|
+
columns = ['name', 'email', ...this.userService.roles];
|
|
16
|
+
|
|
17
|
+
config = this.withoutBulkEdit(this.getConfig());
|
|
18
|
+
|
|
19
|
+
/** Sends a copy: the row shares its `is` object with the cached user document. */
|
|
20
|
+
updateRole(user: User, role: string, value: boolean): void {
|
|
21
|
+
const next: User = { ...user, is: { ...(user.is || {}), [role]: value } };
|
|
22
|
+
|
|
23
|
+
this.userService.updateAdmin(next).subscribe({
|
|
24
|
+
next: () => {
|
|
25
|
+
void this.setDocuments();
|
|
26
|
+
},
|
|
27
|
+
error: () => {
|
|
28
|
+
// The service rolls back a rejected change; refresh so the table shows the real state.
|
|
29
|
+
void this.setDocuments();
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -3,10 +3,19 @@ import { Router } from '@angular/router';
|
|
|
3
3
|
import { AlertService } from '@wawjs/ngx-ui';
|
|
4
4
|
import { CrudService } from '@wawjs/ngx-crud';
|
|
5
5
|
import { EmitterService } from '@wawjs/ngx-core';
|
|
6
|
+
import { FormcomponentService, FormService } from '@wawjs/ngx-form';
|
|
6
7
|
import { HttpService } from '@wawjs/ngx-http';
|
|
7
8
|
import { NGX_BOS_CONFIG } from '../../ngx-bos.config';
|
|
9
|
+
import { FileService } from '../file/file.service';
|
|
10
|
+
import { NEW_USER } from './user.const';
|
|
8
11
|
import { User } from './user.interface';
|
|
9
12
|
import { readStoredUser, USER_STORAGE_KEY } from './user.storage';
|
|
13
|
+
|
|
14
|
+
/** Signed-out user: no `_id`, no roles. A fresh copy so nested objects are never shared. */
|
|
15
|
+
function guestUser(): User {
|
|
16
|
+
return { ...NEW_USER, data: {}, is: {}, roles: [] };
|
|
17
|
+
}
|
|
18
|
+
|
|
10
19
|
@Service()
|
|
11
20
|
export class UserService extends CrudService<User> {
|
|
12
21
|
private readonly _userHttpService = inject(HttpService);
|
|
@@ -19,6 +28,13 @@ export class UserService extends CrudService<User> {
|
|
|
19
28
|
|
|
20
29
|
private readonly _bosConfig = inject(NGX_BOS_CONFIG);
|
|
21
30
|
|
|
31
|
+
/** Other CRUD collections whose cached documents and queued mutations belong to one user. */
|
|
32
|
+
private readonly _userScopedServices: Pick<CrudService<User>, 'checkUser' | 'clearDocs'>[] = [
|
|
33
|
+
inject(FileService),
|
|
34
|
+
inject(FormService),
|
|
35
|
+
inject(FormcomponentService),
|
|
36
|
+
];
|
|
37
|
+
|
|
22
38
|
readonly url = this._bosConfig.url;
|
|
23
39
|
|
|
24
40
|
roles = this._bosConfig.roles.concat(['admin']);
|
|
@@ -27,7 +43,7 @@ export class UserService extends CrudService<User> {
|
|
|
27
43
|
|
|
28
44
|
readonly users = this.documents;
|
|
29
45
|
|
|
30
|
-
user = signal<User>(readStoredUser() ??
|
|
46
|
+
user = signal<User>(readStoredUser() ?? guestUser());
|
|
31
47
|
|
|
32
48
|
thumb = computed(() => {
|
|
33
49
|
const thumb = this.user().thumb;
|
|
@@ -43,6 +59,36 @@ export class UserService extends CrudService<User> {
|
|
|
43
59
|
return !!(this.user().is || {})[role];
|
|
44
60
|
}
|
|
45
61
|
|
|
62
|
+
/** True when the user has at least one role (admin or any configured staff role). */
|
|
63
|
+
hasAnyRole(user: User = this.user()): boolean {
|
|
64
|
+
const is = user.is || {};
|
|
65
|
+
|
|
66
|
+
return Object.keys(is).some((role) => !!is[role]);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private _usersRequested = false;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Loads the user list once. The backend only gives it to users with a role (it holds emails
|
|
73
|
+
* and phones, and anyone can register), so plain accounts do not request it at all.
|
|
74
|
+
*/
|
|
75
|
+
loadUsers(): void {
|
|
76
|
+
if (this._usersRequested || !this.hasAnyRole()) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
this._usersRequested = true;
|
|
81
|
+
|
|
82
|
+
this.get({
|
|
83
|
+
query: this._bosConfig.appId ? 'appId=' + this._bosConfig.appId : '',
|
|
84
|
+
}).subscribe({
|
|
85
|
+
error: () => {
|
|
86
|
+
// Allow a retry, for example after the session or the roles are refreshed.
|
|
87
|
+
this._usersRequested = false;
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
46
92
|
readonly usersByRole = computed<Record<string, User[]>>(() => {
|
|
47
93
|
const splitted: Record<string, User[]> = {};
|
|
48
94
|
|
|
@@ -79,23 +125,50 @@ export class UserService extends CrudService<User> {
|
|
|
79
125
|
});
|
|
80
126
|
|
|
81
127
|
if (localStorage.getItem(USER_STORAGE_KEY)) {
|
|
82
|
-
this.fetch({}, { name: 'me' }).subscribe(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
128
|
+
this.fetch({}, { name: 'me' }).subscribe({
|
|
129
|
+
next: (user: User) => {
|
|
130
|
+
if (user) {
|
|
131
|
+
this.setUser(user);
|
|
132
|
+
// The stored roles may be stale; a role granted since the last visit counts.
|
|
133
|
+
this.loadUsers();
|
|
134
|
+
} else if (localStorage.getItem(USER_STORAGE_KEY)) {
|
|
135
|
+
this.logout();
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
error: (error: unknown) => {
|
|
139
|
+
// Keep the saved user while offline or while the API is down; any other error
|
|
140
|
+
// means the session is no longer valid.
|
|
141
|
+
const status = (error as { status?: number } | null)?.status;
|
|
142
|
+
|
|
143
|
+
if (status === 0 || status === 502 || status === 503 || status === 504) {
|
|
144
|
+
this._emitterService.complete('us.user');
|
|
145
|
+
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
89
148
|
|
|
90
|
-
|
|
91
|
-
|
|
149
|
+
if (localStorage.getItem(USER_STORAGE_KEY)) {
|
|
150
|
+
this.logout();
|
|
151
|
+
}
|
|
152
|
+
},
|
|
92
153
|
});
|
|
154
|
+
|
|
155
|
+
this.loadUsers();
|
|
93
156
|
}
|
|
94
157
|
}
|
|
95
158
|
|
|
96
159
|
setUser(user: User) {
|
|
97
160
|
this.user.set(user);
|
|
98
161
|
|
|
162
|
+
// Cached CRUD documents and queued mutations belong to the previous user on this browser:
|
|
163
|
+
// each collection drops them when the stored owner differs from this user.
|
|
164
|
+
if (user._id) {
|
|
165
|
+
void this.checkUser(user._id);
|
|
166
|
+
|
|
167
|
+
for (const service of this._userScopedServices) {
|
|
168
|
+
void service.checkUser(user._id);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
99
172
|
localStorage.setItem(USER_STORAGE_KEY, JSON.stringify(user));
|
|
100
173
|
|
|
101
174
|
this._emitterService.complete('us.user');
|
|
@@ -104,10 +177,17 @@ export class UserService extends CrudService<User> {
|
|
|
104
177
|
updateMe() {
|
|
105
178
|
this.setUser(this.user());
|
|
106
179
|
|
|
107
|
-
return this.update(this.user()).subscribe(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
180
|
+
return this.update(this.user()).subscribe({
|
|
181
|
+
next: () => {
|
|
182
|
+
this._alertService.info({
|
|
183
|
+
text: 'Profile information has been updated',
|
|
184
|
+
});
|
|
185
|
+
},
|
|
186
|
+
error: () => {
|
|
187
|
+
this._alertService.error({
|
|
188
|
+
text: 'Something went wrong',
|
|
189
|
+
});
|
|
190
|
+
},
|
|
111
191
|
});
|
|
112
192
|
}
|
|
113
193
|
|
|
@@ -139,10 +219,17 @@ export class UserService extends CrudService<User> {
|
|
|
139
219
|
}
|
|
140
220
|
|
|
141
221
|
logout() {
|
|
142
|
-
this.user.set(
|
|
222
|
+
this.user.set(guestUser());
|
|
143
223
|
|
|
144
224
|
localStorage.removeItem(USER_STORAGE_KEY);
|
|
145
225
|
|
|
226
|
+
// Do not leave this user's cached documents and queued mutations to the next person.
|
|
227
|
+
this.clearDocs();
|
|
228
|
+
|
|
229
|
+
for (const service of this._userScopedServices) {
|
|
230
|
+
service.clearDocs();
|
|
231
|
+
}
|
|
232
|
+
|
|
146
233
|
// Reload only after the server has cleared the session cookie: reloading right away can
|
|
147
234
|
// cancel the request and leave the session alive. Do not wait longer than 3s if the API is down.
|
|
148
235
|
let finished = false;
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
import { AceComponent, AceConfigInterface } from '@wawjs/ngx-ace';
|
|
9
9
|
import { FormService } from '@wawjs/ngx-form';
|
|
10
10
|
import { FormBridgeData } from '../form-bridge.interface';
|
|
11
|
+
import { FormConfigCache } from '../form-config-cache';
|
|
11
12
|
|
|
12
13
|
interface Interface {}
|
|
13
14
|
|
|
@@ -91,8 +92,22 @@ export class AceFormComponent implements OnInit {
|
|
|
91
92
|
|
|
92
93
|
/* ------------ config / props helpers ------------ */
|
|
93
94
|
|
|
95
|
+
/** The same inputs must give back the same config object, or ace re-applies every option. */
|
|
96
|
+
private readonly _configCache = new FormConfigCache<AceConfigInterface>();
|
|
97
|
+
|
|
94
98
|
getConfig(data: FormBridgeData): AceConfigInterface {
|
|
95
99
|
const props = (data?.props ?? {}) as Record<string, unknown>;
|
|
100
|
+
const fieldConfig = data?.field?.['Config'];
|
|
101
|
+
|
|
102
|
+
return this._configCache.get([props, fieldConfig], () =>
|
|
103
|
+
this._buildConfig(props, fieldConfig),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private _buildConfig(
|
|
108
|
+
props: Record<string, unknown>,
|
|
109
|
+
fieldConfig: unknown,
|
|
110
|
+
): AceConfigInterface {
|
|
96
111
|
const config: AceConfigInterface = { ...DEFAULT_ACE_CONFIG };
|
|
97
112
|
|
|
98
113
|
const toNumber = (v: unknown): number | undefined => {
|
|
@@ -145,7 +160,6 @@ export class AceFormComponent implements OnInit {
|
|
|
145
160
|
}
|
|
146
161
|
|
|
147
162
|
// Per-field config (code-level override)
|
|
148
|
-
const fieldConfig = data?.field?.['Config'];
|
|
149
163
|
if (
|
|
150
164
|
fieldConfig &&
|
|
151
165
|
typeof fieldConfig === 'object' &&
|