Skip to content

Commit

Permalink
fix: FavoritesService updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfaron committed Apr 15, 2024
1 parent 19a7552 commit 5e2cdb8
Show file tree
Hide file tree
Showing 38 changed files with 709 additions and 505 deletions.
8 changes: 4 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
"CONTENT_ROOT": "projects/aas-server/build",
"WEB_ROOT": "projects/aas-portal/dist",
"ASSETS": "projects/aas-server/src/assets",
"USER_STORAGE": "mongodb://localhost:27017/aasportal-users",
"TEMPLATE_STORAGE": "http://localhost:8080/templates",
"AAS_INDEX": "mysql://localhost:3306",
"ENDPOINTS": "[\"file:///endpoints/samples?name=Samples\",\"http://localhost:5001?name=AASX%20Server\",\"http://localhost:8080/endpoints/idta?name=Cloud\"]",
// "USER_STORAGE": "mongodb://localhost:27017/aasportal-users",
// "TEMPLATE_STORAGE": "http://localhost:8080/templates",
// "AAS_INDEX": "mysql://localhost:3306",
"ENDPOINTS": "[\"file:///endpoints/samples?name=Samples\"]",
}
},
{
Expand Down
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@
"ZVEI"
],
"files.eol": "\n",
"jest.debugMode": true,
"jest.virtualFolders": [
{"name": "common", "rootPath": "./projects/common", "runMode": "on-demand", "jestCommandLine": "node --experimental-vm-modules --no-warnings ../../node_modules/jest/bin/jest.js"},
{"name": "aas-server", "rootPath": "./projects/aas-server", "runMode": "on-demand", "jestCommandLine": "node --experimental-vm-modules --no-warnings ../../node_modules/jest/bin/jest.js"}
],
}
4 changes: 2 additions & 2 deletions projects/aas-lib/src/lib/aas-table/aas-table.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@
[checked]="row.selected" [name]="row.name" />
</td>
<td>
<div class="d-flex" style="overflow-x: hidden">
<div [ngStyle]="{'width': (row.level * 16) + 'px'}"></div>
<div class="d-flex align-items-center" style="overflow-x: hidden">
<div [style.width]="(row.level * 16) + 'px'"></div>
@if (row.isLeaf) {
<div class="wh-4"></div>
}@else {
Expand Down
5 changes: 5 additions & 0 deletions projects/aas-lib/src/lib/aas-table/aas-table.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ th.th-w-icon {

th.th-w-checkbox {
width: 32px;
}

div.wh-4 {
width: 16px;
height: 16px;
}
4 changes: 2 additions & 2 deletions projects/aas-lib/src/lib/aas-table/aas-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class AASTableComponent implements OnInit, OnChanges, OnDestroy {
}

public open(row: AASTableRow): void {
this.clipboard.set('AASDocument', row.document);
this.clipboard.set('AASDocument', row.element);
this.router.navigate(['/aas'], {
skipLocationChange: true,
queryParams: {
Expand All @@ -159,7 +159,7 @@ export class AASTableComponent implements OnInit, OnChanges, OnDestroy {
}

public getToolTip(row: AASTableRow): string {
return `${row.endpoint}, ${row.document.address}`;
return `${row.endpoint}, ${row.element.address}`;
}

public toggleSelected(row: AASTableRow): void {
Expand Down
48 changes: 29 additions & 19 deletions projects/aas-lib/src/lib/aas-table/aas-table.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class AASTableEffects {
const rows = documents.map(document => {
const row = map.get(`${document.endpoint}:${document.id}`);
if (row) {
return row.document === document ? row : this.cloneWithNewDocument(row, document);
return row.element === document ? row : this.cloneWithNewDocument(row, document);
}

return new AASTableRow(document, -1, false, false, false, false, -1, -1, -1);
Expand All @@ -69,7 +69,7 @@ export class AASTableEffects {
documents.forEach(document => {
const row = map.get(`${document.endpoint}:${document.id}`);
if (row) {
rows.push(row.document === document ? row : this.cloneWithNewDocument(row, document));
rows.push(row.element === document ? row : this.cloneWithNewDocument(row, document));
} else {
nodes.push(document);
}
Expand All @@ -86,19 +86,20 @@ export class AASTableEffects {
const root = nodes.find(node => !node.parentId);
const index = findLastIndex(rows, row => row.level === 0);
if (root) {
const children = nodes.filter(node => this.isChild(root, node));
const hasChildren = this.hasChildren(root, nodes);
const rootRow = new AASTableRow(
root,
-1,
false,
false,
false,
children.length === 0,
!hasChildren,
0,
children.length > 0 ? rows.length + 1 : -1,
hasChildren ? rows.length + 1 : -1,
-1,
);

const parentIndex = rows.length;
rows.push(rootRow);

if (index >= 0) {
Expand All @@ -107,7 +108,7 @@ export class AASTableEffects {
rows[index] = previous;
}

this.traverse(root, nodes, rows, 0, 1);
this.traverse(root, nodes, rows, parentIndex, 1);
}

return rows;
Expand All @@ -121,15 +122,14 @@ export class AASTableEffects {
level: number,
): void {
let previous: AASTableRow | null = null;
const children = nodes.filter(node => this.isChild(parent, node));
for (const child of children) {
for (const child of this.getChildren(parent, nodes)) {
const row = new AASTableRow(
child,
parentIndex,
false,
false,
false,
!nodes.some(node => this.isChild(child, node)),
!this.hasChildren(child, nodes),
level,
-1,
-1,
Expand All @@ -141,26 +141,36 @@ export class AASTableEffects {
previous.nextSibling = index;
}

if (children.length > 0) {
row.firstChild = rows.length;
this.traverse(child, nodes, rows, index, level + 1);
}

row.firstChild = rows.length;
this.traverse(child, nodes, rows, index, level + 1);
previous = row;
}
}

private isChild(parent: AASDocument, node: AASDocument): boolean {
if (!node.parentId) {
return false;
private getChildren(parent: AASDocument, nodes: AASDocument[]): AASDocument[] {
const children: AASDocument[] = [];
for (const node of nodes) {
if (node.parentId === parent.id) {
children.push(node);
}
}

return children;
}

private hasChildren(parent: AASDocument, nodes: AASDocument[]): boolean {
for (const node of nodes) {
if (node.parentId === parent.id) {
return true;
}
}

return node.parentId === parent.id;
return false;
}

private clone(row: AASTableRow): AASTableRow {
return new AASTableRow(
row.document,
row.element,
row.parent,
row.selected,
row.expanded,
Expand Down
4 changes: 2 additions & 2 deletions projects/aas-lib/src/lib/aas-table/aas-table.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const getState = (state: AASTableFeatureState) => state.aasTable;
export const selectState = createSelector(getState, state => state);

export const selectSelectedDocuments = createSelector(getRows, (rows: AASTableRow[]): AASDocument[] => {
return rows.filter(row => row.selected).map(row => row.document);
return rows.filter(row => row.selected).map(row => row.element);
});

export const selectSomeSelected = createSelector(getRows, (rows: AASTableRow[]): boolean => {
Expand All @@ -34,7 +34,7 @@ export const selectRows = (translate: TranslateService) => {
if (state.viewMode === 'list') {
if (state.filter) {
const filter = new AASTableFilter(state.filter, translate.currentLang);
return state.rows.filter(row => filter.match(row.document));
return state.rows.filter(row => filter.match(row.element));
}

return state.rows;
Expand Down
16 changes: 8 additions & 8 deletions projects/aas-lib/src/lib/aas-table/aas-table.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Tree, TreeNode } from '../tree';

export class AASTableRow extends TreeNode<AASDocument> {
public constructor(
public readonly document: AASDocument,
document: AASDocument,
parent: number,
selected: boolean,
expanded: boolean,
Expand All @@ -26,27 +26,27 @@ export class AASTableRow extends TreeNode<AASDocument> {
}

public get id(): string {
return this.document.id;
return this.element.id;
}

public get name(): string {
return this.document.idShort;
return this.element.idShort;
}

public get thumbnail(): string {
return this.document.thumbnail ?? '/assets/resources/aas.32.png';
return this.element.thumbnail ?? '/assets/resources/aas.32.png';
}

public get endpoint(): string {
return this.document.endpoint;
return this.element.endpoint;
}

public get state(): 'loaded' | 'unloaded' | 'unavailable' {
if (this.document.content === null) {
if (this.element.content === null) {
return 'unloaded';
}

if (this.document.content) {
if (this.element.content) {
return 'loaded';
}

Expand Down Expand Up @@ -77,7 +77,7 @@ export class AASTableTree extends Tree<AASDocument, AASTableRow> {

protected override cloneNode(node: AASTableRow): AASTableRow {
return new AASTableRow(
node.document,
node.element,
node.parent,
node.selected,
node.expanded,
Expand Down
4 changes: 4 additions & 0 deletions projects/aas-lib/src/lib/auth/auth-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export class AuthApiService {
return this.http.get<Cookie[]>(`/api/v1/users/${encodeBase64Url(id)}/cookies`);
}

public getCookie(id: string, name: string): Observable<Cookie | undefined> {
return this.http.get<Cookie>(`/api/v1/users/${encodeBase64Url(id)}/cookies/${name}`);
}

public setCookie(id: string, cookie: Cookie): Observable<void> {
return this.http.post<void>(`/api/v1/users/${encodeBase64Url(id)}/cookies/${cookie.name}`, cookie);
}
Expand Down
55 changes: 16 additions & 39 deletions projects/aas-lib/src/lib/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { Injectable } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { TranslateService } from '@ngx-translate/core';
import { BehaviorSubject, from, last, map, mergeMap, Observable, of, throwError } from 'rxjs';
import { BehaviorSubject, from, map, mergeMap, Observable, of, throwError } from 'rxjs';
import { jwtDecode } from 'jwt-decode';
import {
ApplicationError,
Expand All @@ -36,7 +36,6 @@ import { WindowService } from '../window.service';
export class AuthService {
private readonly payload$ = new BehaviorSubject<JWTPayload>({ role: 'guest' });
private readonly ready$ = new BehaviorSubject<boolean>(false);
private readonly cookies = new Map<string, string>();

public constructor(
private modal: NgbModal,
Expand Down Expand Up @@ -235,52 +234,43 @@ export class AuthService {
* @param name The cookie name.
* @returns `true` if the cookie exists; otherwise, `false`.
*/
public checkCookie(name: string): boolean {
if (this.authenticated) {
return this.cookies.has(name);
public checkCookie(name: string): Observable<boolean> {
const payload = this.payload$.getValue();
if (payload && payload.sub) {
const id = payload.sub;
return this.api.getCookie(id, name).pipe(map(cookie => cookie != null));
} else {
return of(this.window.getLocalStorageItem(name) != null);
}

return this.window.getLocalStorageItem(name) != null;
}

/**
* Gets the value of the cookie with the specified name.
* @param name The cookie name.
* @returns The cookie value.
*/
public getCookie(name: string): string | null {
let data: string | null;
public getCookie(name: string): Observable<string | undefined> {
const payload = this.payload$.getValue();
if (payload) {
data = this.cookies.get(name) ?? null;
if (payload && payload.sub) {
return this.api.getCookie(payload.sub, name).pipe(map(cookie => cookie?.data));
} else {
data = this.window.getLocalStorageItem(name);
return of(this.window.getLocalStorageItem(name) ?? undefined);
}

return data;
}

/**
* Sets the value of the cookie with the specified name.
* @param name The cookie name.
* @param data The cookie value.
*/
public setCookie(name: string, data: string): void {
public setCookie(name: string, data: string): Observable<void> {
const payload = this.payload$.getValue();
if (payload && payload.sub) {
const id = payload.sub;
this.api
.setCookie(id, { name, data })
.pipe(
last(),
mergeMap(() => this.api.getCookies(id)),
)
.subscribe({
next: cookies => cookies.forEach(cookie => this.cookies.set(cookie.name, cookie.data)),
error: error => this.notify.error(error),
});
return this.api.setCookie(id, { name, data });
} else {
this.window.setLocalStorageItem(name, data);
return of(void 0);
}
}

Expand All @@ -292,13 +282,7 @@ export class AuthService {
const payload = this.payload$.getValue();
if (payload && payload.sub) {
const id = payload.sub;
return this.api.deleteCookie(id, name).pipe(
mergeMap(() => this.api.getCookies(id)),
map(cookies => {
this.cookies.clear();
cookies.forEach(cookie => this.cookies.set(cookie.name, cookie.data));
}),
);
return this.api.deleteCookie(id, name);
} else {
this.window.removeLocalStorageItem(name);
return of(void 0);
Expand All @@ -324,12 +308,5 @@ export class AuthService {
this.window.setLocalStorageItem('.Token', token);
const payload = jwtDecode(token) as JWTPayload;
this.payload$.next(payload);
this.cookies.clear();
if (payload.sub) {
this.api.getCookies(payload.sub).subscribe({
next: cookies => cookies.forEach(cookie => this.cookies.set(cookie.name, cookie.data)),
error: error => this.notify.error(error),
});
}
}
}
5 changes: 3 additions & 2 deletions projects/aas-lib/src/lib/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,13 @@ export abstract class Tree<TElement, TNode extends TreeNode<TElement>> {
}

private traverseNodes(node: TNode, expanded: TNode[]): TNode[] {
const nodes = this.getNodes();
if (node.firstChild >= 0 && node.expanded) {
let child = this.getNodes()[node.firstChild];
let child = nodes[node.firstChild];
expanded.push(child);
this.traverseNodes(child, expanded);
while (child.nextSibling >= 0) {
child = this.getNodes()[child.nextSibling];
child = nodes[child.nextSibling];
expanded.push(child);
this.traverseNodes(child, expanded);
}
Expand Down
Loading

0 comments on commit 5e2cdb8

Please sign in to comment.