edit hierarchy for manager / suggest hierarchy for user added
This commit is contained in:
@@ -26,13 +26,34 @@
|
||||
[status]="200" />
|
||||
} -->
|
||||
|
||||
@if (can_edit()) {
|
||||
@if (can_edit() || can_suggest()) {
|
||||
<app-hierarchy-edit-dialog
|
||||
[data]="universityRoot"
|
||||
[numberOfSuggestions]="number_of_suggestion_slots()"
|
||||
[data]="editorTree()"
|
||||
[numberOfSuggestions]="
|
||||
can_edit() ? 1 : number_of_suggestion_slots()
|
||||
"
|
||||
[isManager]="can_edit()"
|
||||
(saved)="onHierarchySaved($event)">
|
||||
</app-hierarchy-edit-dialog>
|
||||
}
|
||||
@if (can_edit()) {
|
||||
<button
|
||||
mat-stroked-button
|
||||
color="primary"
|
||||
(click)="reviewSuggestions()">
|
||||
Vorschläge prüfen
|
||||
@if (suggestionCount() > 0) {
|
||||
({{ suggestionCount() }})
|
||||
}
|
||||
</button>
|
||||
} @else if (can_suggest()) {
|
||||
<button
|
||||
mat-stroked-button
|
||||
color="primary"
|
||||
(click)="viewMySuggestions()">
|
||||
Meine Vorschläge
|
||||
</button>
|
||||
}
|
||||
<button
|
||||
mat-icon-button
|
||||
(click)="toggleLanguage()"
|
||||
|
||||
@@ -21,22 +21,29 @@ import { MatButtonModule } from '@angular/material/button';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatChipsModule } from '@angular/material/chips';
|
||||
import { MatTooltipModule } from '@angular/material/tooltip';
|
||||
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
|
||||
import type { EChartsOption } from 'echarts';
|
||||
import { CredentialService } from '../../../shared/services/credential.service';
|
||||
import { ChartThemeService } from '../../../shared/services/chart-theme.service';
|
||||
import { HierarchyEditDialogComponent } from './hierarchy-edit-dialog/hierarchy-edit-dialog.component';
|
||||
import { HierarchyEditorDialogData } from './hierarchy-edit-dialog/hierarchy-edit-dialog.component';
|
||||
import { HierarchyTreeEditorComponent } from './hierarchy-tree-editor/hierarchy-tree-editor.component';
|
||||
import {
|
||||
HierarchySuggestionsDialogComponent,
|
||||
HierarchySuggestionDecision,
|
||||
} from './hierarchy-suggestions-dialog/hierarchy-suggestions-dialog.component';
|
||||
import { HierarchyEditorResult } from './hierarchy-tree-editor/hierarchy-tree-editor.component';
|
||||
import { OrgminerService } from '../../../api/api/orgminer.service';
|
||||
import { HierarchyResponse } from '../../../api/model/hierarchyResponse';
|
||||
import { SaveHierarchyResponse } from '../../../api/model/saveHierarchyResponse';
|
||||
import { HierarchyNode } from '../../../api/model/hierarchyNode';
|
||||
import { MatSnackBar } from '@angular/material/snack-bar';
|
||||
export type modifiedOption = 'created' | 'updated' | 'deleted' | '';
|
||||
import { HierarchySuggestion } from '../../../api/model/hierarchySuggestion';
|
||||
|
||||
export interface RawNode extends HierarchyNode {
|
||||
id: string;
|
||||
modified?: modifiedOption;
|
||||
children?: RawNode[];
|
||||
[key: string]: any; // faculty_de, institutes_aggregator, chairs_aggregator, ...
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
interface TreeLevel {
|
||||
@@ -64,36 +71,67 @@ const MIN_TREE_HEIGHT = 200;
|
||||
MatIconModule,
|
||||
MatChipsModule,
|
||||
MatTooltipModule,
|
||||
MatDialogModule,
|
||||
HierarchyEditDialogComponent,
|
||||
],
|
||||
templateUrl: './hierarchy-chart.component.html',
|
||||
styleUrls: ['./hierarchy-chart.component.scss'],
|
||||
})
|
||||
/**
|
||||
* Owns the hierarchy chart state and coordinates all hierarchy dialogs.
|
||||
*
|
||||
* The route provides the initial hierarchy. This component turns it into chart
|
||||
* levels, opens the edit/suggestion dialogs, and persists editor results
|
||||
* through OrgminerService. Child dialogs never mutate chart state directly;
|
||||
* they return a result and the chart decides whether to refresh or emit it.
|
||||
*/
|
||||
export class HierarchyChartComponent implements OnInit {
|
||||
private readonly router = inject(Router);
|
||||
private readonly route = inject(ActivatedRoute);
|
||||
private readonly destroyRef = inject(DestroyRef);
|
||||
private readonly credentialService = inject(CredentialService);
|
||||
private readonly orgminerService = inject(OrgminerService);
|
||||
private readonly dialog = inject(MatDialog);
|
||||
private readonly chartTheme = inject(ChartThemeService);
|
||||
readonly loading = signal<boolean>(true);
|
||||
readonly lang = signal<'de' | 'en'>('de');
|
||||
readonly levels = signal<TreeLevel[]>([]);
|
||||
|
||||
private readonly _snackBar = inject(MatSnackBar);
|
||||
private idMap = new Map<string, RawNode>();
|
||||
public universityRoot: RawNode | null = null;
|
||||
private readonly snackBar = inject(MatSnackBar);
|
||||
private readonly idMap = new Map<string, RawNode>();
|
||||
universityRoot: RawNode | null = null;
|
||||
|
||||
/**
|
||||
* Deep copy of `universityRoot` that is handed to the edit dialog.
|
||||
* The editor may mutate it freely; the chart is only updated after a
|
||||
* successful save (see `onHierarchySaved`).
|
||||
*/
|
||||
readonly editorTree = signal<RawNode | null>(null);
|
||||
|
||||
private isOwnNavigation = false;
|
||||
hierarchyData = input.required<HierarchyResponse>();
|
||||
can_edit = computed(() => this.hierarchyData().can_edit);
|
||||
can_edit = computed(() => this.hierarchyData().can_edit ?? false);
|
||||
can_suggest = computed(() => this.hierarchyData().can_suggest ?? true);
|
||||
readonly suggestionCount = signal(0);
|
||||
number_of_suggestion_slots = signal(0);
|
||||
university_name = computed(
|
||||
() => this.credentialService.organizationName() ?? 'Universität',
|
||||
);
|
||||
hierarchySaved = signal<boolean | null>(null); // null = no feedback, true = success, false = failure
|
||||
hierarchySaved = signal<boolean | null>(null);
|
||||
@Output() analyzeOrgEvent = new EventEmitter<{ name: string; url: string }>();
|
||||
|
||||
private initialized = false;
|
||||
|
||||
constructor() {
|
||||
// Rebuild when the resolver supplies fresh data while preserving the open path.
|
||||
effect(() => {
|
||||
const data = this.hierarchyData();
|
||||
untracked(() => {
|
||||
if (!this.initialized || !data?.hierarchy) return;
|
||||
this.setHierarchy(data.hierarchy as RawNode[]);
|
||||
this.rebuildLevelsKeepingPath();
|
||||
});
|
||||
});
|
||||
effect(() => {
|
||||
this.chartTheme.colors();
|
||||
untracked(() => {
|
||||
@@ -113,6 +151,9 @@ export class HierarchyChartComponent implements OnInit {
|
||||
this.hierarchyData().number_of_suggestion_slots ?? 0,
|
||||
);
|
||||
this.loadData();
|
||||
if (this.can_edit()) {
|
||||
this.loadSuggestionCount();
|
||||
}
|
||||
this.route.queryParamMap
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe((params) => {
|
||||
@@ -122,6 +163,7 @@ export class HierarchyChartComponent implements OnInit {
|
||||
}
|
||||
this.restoreFromUrl(params);
|
||||
});
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
/* ----------------------------- data loading ----------------------------- */
|
||||
@@ -137,8 +179,21 @@ export class HierarchyChartComponent implements OnInit {
|
||||
return;
|
||||
}
|
||||
|
||||
// assign unique IDs to every node in the hierarchy, so we can restore the path from the URL later
|
||||
this.assignIds(this.hierarchyData().hierarchy as RawNode[], 'root');
|
||||
this.setHierarchy(this.hierarchyData().hierarchy as RawNode[]);
|
||||
this.loading.set(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Single place that (re)builds the internal state from a list of top-level nodes.
|
||||
* Works on a deep copy, so neither the API response nor the editor's data is mutated.
|
||||
* Used for the initial load and after a successful save.
|
||||
*/
|
||||
private setHierarchy(children: RawNode[]): void {
|
||||
const copy = structuredClone(children ?? []) as RawNode[];
|
||||
|
||||
this.idMap.clear();
|
||||
this.assignIds(copy, 'root');
|
||||
|
||||
this.universityRoot = {
|
||||
id: 'root',
|
||||
name_de: this.university_name(),
|
||||
@@ -146,20 +201,26 @@ export class HierarchyChartComponent implements OnInit {
|
||||
link_de: '',
|
||||
link_en: '',
|
||||
type: 'university',
|
||||
children: this.hierarchyData().hierarchy as RawNode[],
|
||||
children: copy,
|
||||
};
|
||||
this.idMap.set('root', this.universityRoot);
|
||||
|
||||
this.loading.set(false);
|
||||
this.editorTree.set(structuredClone(this.universityRoot));
|
||||
}
|
||||
|
||||
private assignIds(nodes: RawNode[], parentId: string): void {
|
||||
private assignIds(
|
||||
nodes: RawNode[],
|
||||
parentId: string,
|
||||
registerInMap = true,
|
||||
): void {
|
||||
nodes.forEach((node, index) => {
|
||||
const id = `${parentId}-${index}`;
|
||||
node.id = id;
|
||||
this.idMap.set(id, node);
|
||||
if (registerInMap) {
|
||||
this.idMap.set(id, node);
|
||||
}
|
||||
if (node.children?.length) {
|
||||
this.assignIds(node.children, id);
|
||||
this.assignIds(node.children, id, registerInMap);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -215,6 +276,171 @@ export class HierarchyChartComponent implements OnInit {
|
||||
|
||||
/* ------------------------------- UI actions ------------------------------ */
|
||||
|
||||
reviewSuggestions(): void {
|
||||
this.orgminerService.getHierarchySuggestions().subscribe({
|
||||
next: (suggestions: HierarchySuggestion[]) => {
|
||||
this.suggestionCount.set(this.countNewSuggestions(suggestions));
|
||||
const dialogRef = this.dialog.open(
|
||||
HierarchySuggestionsDialogComponent,
|
||||
{
|
||||
data: { suggestions, canReview: true },
|
||||
width: '65%',
|
||||
maxWidth: '95vw',
|
||||
maxHeight: '90vh',
|
||||
},
|
||||
);
|
||||
dialogRef
|
||||
.afterClosed()
|
||||
.subscribe((decision: HierarchySuggestionDecision | undefined) => {
|
||||
if (!decision) return;
|
||||
if (decision.action === 'review') {
|
||||
this.openSuggestionEditor(decision.suggestion);
|
||||
} else {
|
||||
this.rejectSuggestion(decision.suggestion);
|
||||
}
|
||||
});
|
||||
},
|
||||
error: (error) => {
|
||||
console.error(
|
||||
'[HierarchyChart] error loading hierarchy suggestions:',
|
||||
error,
|
||||
);
|
||||
this.snackBar.open('Error loading hierarchy suggestions', 'Close', {
|
||||
duration: 3000,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
viewMySuggestions(): void {
|
||||
this.orgminerService.getMyHierarchySuggestions().subscribe({
|
||||
next: (suggestions) => {
|
||||
this.dialog.open(HierarchySuggestionsDialogComponent, {
|
||||
data: { suggestions, canReview: false },
|
||||
width: '65%',
|
||||
maxWidth: '95vw',
|
||||
maxHeight: '90vh',
|
||||
});
|
||||
},
|
||||
error: (error) => {
|
||||
console.error(
|
||||
'[HierarchyChart] error loading own hierarchy suggestions:',
|
||||
error,
|
||||
);
|
||||
this.snackBar.open('Error loading your suggestions', 'Close', {
|
||||
duration: 3000,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private acceptSuggestion(
|
||||
suggestion: HierarchySuggestion,
|
||||
hierarchy: RawNode[],
|
||||
): void {
|
||||
this.orgminerService
|
||||
.acceptHierarchySuggestion(
|
||||
suggestion.id,
|
||||
|
||||
hierarchy,
|
||||
)
|
||||
.subscribe({
|
||||
next: () => {
|
||||
this.snackBar.open('Hierarchy suggestion accepted', 'Close', {
|
||||
duration: 3000,
|
||||
});
|
||||
this.suggestionCount.update((count) => Math.max(0, count - 1));
|
||||
this.refreshHierarchy(hierarchy);
|
||||
},
|
||||
error: (error) => {
|
||||
console.error(
|
||||
'[HierarchyChart] error accepting hierarchy suggestion:',
|
||||
error,
|
||||
);
|
||||
this.snackBar.open('Error accepting hierarchy suggestion', 'Close', {
|
||||
duration: 3000,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private openSuggestionEditor(suggestion: HierarchySuggestion): void {
|
||||
const tree: RawNode = {
|
||||
id: 'suggestion-root',
|
||||
name_de: 'Universität',
|
||||
name_en: 'University',
|
||||
type: 'university',
|
||||
children: suggestion.hierarchy as RawNode[],
|
||||
};
|
||||
this.assignIds(tree.children ?? [], 'suggestion-root', false);
|
||||
|
||||
const dialogRef = this.dialog.open<
|
||||
HierarchyTreeEditorComponent,
|
||||
HierarchyEditorDialogData,
|
||||
RawNode | HierarchyEditorResult
|
||||
>(HierarchyTreeEditorComponent, {
|
||||
data: {
|
||||
tree,
|
||||
numberOfSuggestions: 1,
|
||||
isManager: true,
|
||||
isSuggestion: true,
|
||||
suggestionStatus: suggestion.status,
|
||||
},
|
||||
width: '65%',
|
||||
maxWidth: '95vw',
|
||||
maxHeight: '90vh',
|
||||
panelClass: 'hierarchy-edit-dialog-panel',
|
||||
});
|
||||
dialogRef.afterClosed().subscribe((result) => {
|
||||
if (!result) return;
|
||||
if (!('children' in result)) {
|
||||
if (suggestion.status === HierarchySuggestion.StatusEnum.New) {
|
||||
this.rejectSuggestion(suggestion);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.acceptSuggestion(suggestion, result.children ?? []);
|
||||
});
|
||||
}
|
||||
|
||||
private rejectSuggestion(suggestion: HierarchySuggestion): void {
|
||||
this.orgminerService.rejectHierarchySuggestion(suggestion.id).subscribe({
|
||||
next: () => {
|
||||
this.snackBar.open('Hierarchy suggestion rejected', 'Close', {
|
||||
duration: 3000,
|
||||
});
|
||||
this.suggestionCount.update((count) => Math.max(0, count - 1));
|
||||
},
|
||||
error: (error) => {
|
||||
console.error(
|
||||
'[HierarchyChart] error rejecting hierarchy suggestion:',
|
||||
error,
|
||||
);
|
||||
this.snackBar.open('Error rejecting hierarchy suggestion', 'Close', {
|
||||
duration: 3000,
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private loadSuggestionCount(): void {
|
||||
this.orgminerService.getHierarchySuggestions().subscribe({
|
||||
next: (suggestions) =>
|
||||
this.suggestionCount.set(this.countNewSuggestions(suggestions)),
|
||||
error: (error) =>
|
||||
console.error(
|
||||
'[HierarchyChart] error loading suggestion count:',
|
||||
error,
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
private countNewSuggestions(suggestions: HierarchySuggestion[]): number {
|
||||
return suggestions.filter(
|
||||
(suggestion) => suggestion.status === HierarchySuggestion.StatusEnum.New,
|
||||
).length;
|
||||
}
|
||||
|
||||
toggleLanguage(): void {
|
||||
this.lang.set(this.lang() === 'de' ? 'en' : 'de');
|
||||
// rebuild every currently opened level with the new language, keep the path
|
||||
@@ -272,7 +498,6 @@ export class HierarchyChartComponent implements OnInit {
|
||||
}
|
||||
|
||||
private onLeafSelect(raw: RawNode): void {
|
||||
console.log('[UniversityHierarchy] leaf node clicked:', raw);
|
||||
this.analyzeOrgEvent.emit({
|
||||
name: raw.name_de ?? raw.name_en ?? '',
|
||||
url: raw.link_de ?? raw.link_en ?? '',
|
||||
@@ -450,14 +675,23 @@ export class HierarchyChartComponent implements OnInit {
|
||||
],
|
||||
};
|
||||
}
|
||||
//save new hierarchy data from the editor dialog
|
||||
|
||||
/* --------------------------------- saving -------------------------------- */
|
||||
|
||||
/**
|
||||
* Saves the hierarchy coming from the editor dialog.
|
||||
* The chart is only updated AFTER the server confirmed the save, and only if the
|
||||
* user may edit the real hierarchy (a plain suggestion must not change the chart).
|
||||
*/
|
||||
onHierarchySaved(newData: RawNode): void {
|
||||
this.orgminerService.saveHierarchy(newData.children as any).subscribe({
|
||||
const children = (newData.children ?? []) as RawNode[];
|
||||
|
||||
this.orgminerService.saveHierarchy(children as any).subscribe({
|
||||
next: (response) => {
|
||||
this._snackBar.open('Hierarchy saved successfully', 'Close', {
|
||||
this.snackBar.open('Hierarchy saved successfully', 'Close', {
|
||||
duration: 3000,
|
||||
});
|
||||
console.log('[HierarchyChart] hierarchy saved successfully:', response);
|
||||
|
||||
const saveResponse = response as SaveHierarchyResponse;
|
||||
if (typeof saveResponse.remaining_today !== 'number') {
|
||||
console.error(
|
||||
@@ -468,9 +702,13 @@ export class HierarchyChartComponent implements OnInit {
|
||||
this.number_of_suggestion_slots.set(saveResponse.remaining_today);
|
||||
}
|
||||
this.hierarchySaved.set(true);
|
||||
|
||||
if (this.can_edit()) {
|
||||
this.refreshHierarchy(children);
|
||||
}
|
||||
},
|
||||
error: (error) => {
|
||||
this._snackBar.open('Error saving hierarchy', 'Close', {
|
||||
this.snackBar.open('Error saving hierarchy', 'Close', {
|
||||
duration: 3000,
|
||||
});
|
||||
console.error('[HierarchyChart] error saving hierarchy:', error);
|
||||
@@ -478,4 +716,40 @@ export class HierarchyChartComponent implements OnInit {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the persisted hierarchy again, using the same service call the resolver uses,
|
||||
* so the chart shows exactly what the server stored.
|
||||
* If the re-fetch fails, fall back to the payload we just saved.
|
||||
*/
|
||||
private refreshHierarchy(savedChildren: RawNode[]): void {
|
||||
this.orgminerService.getHierarchy().subscribe({
|
||||
next: (response: HierarchyResponse) => {
|
||||
this.setHierarchy(response.hierarchy as RawNode[]);
|
||||
this.rebuildLevelsKeepingPath();
|
||||
},
|
||||
error: (error) => {
|
||||
console.error('[HierarchyChart] error reloading hierarchy:', error);
|
||||
this.setHierarchy(savedChildren);
|
||||
this.rebuildLevelsKeepingPath();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuilds the open levels from the (new) idMap. Levels whose id no longer exists are
|
||||
* dropped together with everything below; falls back to the root level.
|
||||
*/
|
||||
private rebuildLevelsKeepingPath(): void {
|
||||
const rebuilt: TreeLevel[] = [];
|
||||
for (const level of this.levels()) {
|
||||
const raw = this.idMap.get(level.id);
|
||||
if (!raw) break;
|
||||
rebuilt.push(this.buildLevel(raw));
|
||||
}
|
||||
this.levels.set(
|
||||
rebuilt.length ? rebuilt : [this.buildLevel(this.universityRoot!)],
|
||||
);
|
||||
this.updateQueryParams();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user