Skip to content

Commit

Permalink
fix: if a contributor is updated/deleted remove its contributions
Browse files Browse the repository at this point in the history
  • Loading branch information
QuCMGisaia committed Feb 25, 2025
1 parent 4de90e1 commit 605d7ff
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FormControl, FormGroup } from '@angular/forms';
import { FormArray, FormControl, FormGroup } from '@angular/forms';
import { IconPickerComponent } from '@gisaia-team/ngx-icon-picker';
import { createComponentFactory, mockProvider, Spectator } from '@ngneat/spectator';
import { AlertOnChangeDirective } from '@shared-directives/alert-on-change/alert-on-change.directive';
Expand All @@ -10,6 +10,8 @@ import {
} from 'arlas-wui-toolkit';
import { MockComponent } from 'ng-mocks';
import { EditGroupComponent } from './edit-group.component';
import { WIDGET_TYPE } from './models';
import { AnalyticConfig } from '@services/main-form-manager/models-config';

describe('EditGroupComponent', () => {
let spectator: Spectator<EditGroupComponent>;
Expand Down Expand Up @@ -38,10 +40,10 @@ describe('EditGroupComponent', () => {
formGroup: new FormGroup({
icon: new FormControl(''),
title: new FormControl(''),
itemPerLine: new FormControl(''),
contentType: new FormControl(''),
content: new FormControl([]),
preview: new FormControl([])
itemPerLine: new FormControl(0),
contentType: new FormControl(new Array<WIDGET_TYPE>()),
content: new FormArray([]),
preview: new FormControl({} as AnalyticConfig)
})
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,22 @@ import { AnalyticsInitService } from '@analytics-config/services/analytics-init/
import { ShortcutsService } from '@analytics-config/services/shortcuts/shortcuts.service';
import { CdkDragDrop, CdkDragEnter, CdkDragMove } from '@angular/cdk/drag-drop';
import {
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
Input,
OnDestroy,
OnInit,
Output,
ViewChild
ChangeDetectorRef, Component, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output, ViewChild
} from '@angular/core';
import { FormArray, FormGroup } from '@angular/forms';
import { FormArray, FormControl, FormGroup } from '@angular/forms';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { marker } from '@colsen1991/ngx-translate-extract-marker';
import { ConfigExportHelper } from '@services/main-form-manager/config-export-helper';
import { AnalyticConfig } from '@services/main-form-manager/models-config';
import { MainFormService } from '@services/main-form/main-form.service';
import { ConfigFormGroupComponent } from '@shared-components/config-form-group/config-form-group.component';
import { ConfirmModalComponent } from '@shared-components/confirm-modal/confirm-modal.component';
import { ConfigFormGroup } from '@shared-models/config-form';
import { WidgetConfigFormGroup } from '@shared-models/widget-config-form';
import { moveInFormArray } from '@utils/tools';
import { OperationEnum } from 'arlas-web-core';
import { Subscription } from 'rxjs';
import { Subject } from 'rxjs/internal/Subject';
import { ArlasCollaborativesearchService } from 'arlas-wui-toolkit';
import { Subject, Subscription } from 'rxjs';
import { EditWidgetDialogComponent } from '../edit-widget-dialog/edit-widget-dialog.component';
import { EditWidgetDialogData } from '../edit-widget-dialog/models';
import { ImportWidgetDialogComponent } from '../import-widget-dialog/import-widget-dialog.component';
Expand Down Expand Up @@ -88,7 +81,14 @@ export class AddWidgetDialogComponent {
})
export class EditGroupComponent implements OnInit, OnDestroy {

@Input() public formGroup: FormGroup;
@Input() public formGroup: FormGroup<{
content: FormArray<FormGroup<{widgetType: FormControl<WIDGET_TYPE>; widgetData: FormGroup;}>>;
contentType: FormControl<Array<WIDGET_TYPE>>;
icon: FormControl<string>;
itemPerLine: FormControl<number>;
preview: FormControl<AnalyticConfig>;
title: FormControl<string>;
}>;
@Input() public groupIndex: number;
@Input() public updateDisplay: Subject<any> = new Subject();
@Output() public remove = new EventEmitter();
Expand All @@ -99,8 +99,8 @@ export class EditGroupComponent implements OnInit, OnDestroy {
dragIndex: number;
dropIndex: number;
};
public content: FormArray<FormGroup>;
public itemPerLine;
public content: FormArray<FormGroup<{widgetType: FormControl<WIDGET_TYPE>; widgetData: FormGroup;}>>;
public itemPerLine: number;
public toUnsubscribe: Array<Subscription> = [];


Expand All @@ -115,11 +115,12 @@ export class EditGroupComponent implements OnInit, OnDestroy {
private readonly analyticsImportService: AnalyticsImportService,
private readonly analyticsInitService: AnalyticsInitService,
private readonly main: MainFormService,
private readonly shortcutsService: ShortcutsService
private readonly shortcutsService: ShortcutsService,
private readonly collaborativeSearchService: ArlasCollaborativesearchService
) { }

public ngOnInit() {
this.content = this.formGroup.controls.content as FormArray;
this.content = this.formGroup.controls.content;
this.itemPerLine = this.formGroup.value.itemPerLine;
this.resetWidgetsOnTypeChange();
}
Expand Down Expand Up @@ -194,7 +195,9 @@ export class EditGroupComponent implements OnInit, OnDestroy {
}

public editWidget(widgetIndex: number, collection: string, newWidget?: boolean) {
const widgetFg = this.content.get(widgetIndex.toString()) as FormGroup;
const widgetFg = this.content.get(widgetIndex.toString()) as FormGroup<{widgetType: FormControl; widgetData: FormControl;}>;
const oldWidgetContributorId = ConfigExportHelper.getContributorId(widgetFg.value.widgetData, widgetFg.value.widgetType);

this.afterClosedEditSub = this.dialog.open(EditWidgetDialogComponent, {
data: {
widgetType: widgetFg.value.widgetType,
Expand All @@ -210,6 +213,14 @@ export class EditGroupComponent implements OnInit, OnDestroy {
widgetFg.value.widgetData,
this.formGroup.controls.icon.value
);
// If the new id of the contributor is different from the old one and it had a collaboration, remove it
if (contrib.identifier !== oldWidgetContributorId && this.collaborativeSearchService.collaborations.has(oldWidgetContributorId)) {
this.collaborativeSearchService.collaborationBus.next({
id: oldWidgetContributorId,
operation: OperationEnum.remove,
all: true
});
}
this.updatePreview();
contrib.updateFromCollaboration({
id: '',
Expand All @@ -234,6 +245,17 @@ export class EditGroupComponent implements OnInit, OnDestroy {

this.afterClosedconfirmSub = dialogRef.afterClosed().subscribe(result => {
if (result) {
// If it had a collaboration, remove it
const widgetFg = this.content.get(widgetIndex.toString()) as FormGroup<{widgetType: FormControl; widgetData: FormControl;}>;
const oldWidgetContributorId = ConfigExportHelper.getContributorId(widgetFg.value.widgetData, widgetFg.value.widgetType);
if (this.collaborativeSearchService.collaborations.has(oldWidgetContributorId)) {
this.collaborativeSearchService.collaborationBus.next({
id: oldWidgetContributorId,
operation: OperationEnum.remove,
all: true
});
}

this.removeShortcut(widgetIndex);
this.content.removeAt(widgetIndex);
this.contentTypeValue.splice(widgetIndex, 1);
Expand Down Expand Up @@ -293,7 +315,7 @@ export class EditGroupComponent implements OnInit, OnDestroy {
}

public get contentTypeValue() {
return this.formGroup.controls.contentType.value as Array<string>;
return this.formGroup.controls.contentType.value;
}

public dragEntered(event: CdkDragEnter<number>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class AnalyticsInitService {
});
}

public initNewWidget(type: string) {
public initNewWidget(type: WIDGET_TYPE) {
return this.formBuilder.group({
widgetType: [type],
widgetData: new FormGroup({}, (fg: FormGroup) => ({validateWidget: {valid: !!fg.controls.length}}))
Expand Down
7 changes: 5 additions & 2 deletions src/app/services/main-form-manager/config-export-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ export class ConfigExportHelper {

const timelineComponent: AnalyticComponentConfig = {
contributorId: (isDetailed ? 'detailedTimeline' : 'timeline'),
componentType: 'histogram',
componentType: WIDGET_TYPE.histogram,
uuid: isDetailed ? detailedTimelineUuid.value : timelineUuid.value,
input: {
id: isDetailed ? 'histogram-detailed-timeline' : 'histogram-timeline',
Expand Down Expand Up @@ -1135,8 +1135,11 @@ export class ConfigExportHelper {
* generates an identifier based on the definition of the contributor:
* - aggregationmodel
* - metrics ...
* @param widgetData The widget's configuration from a form
* @param widgetType Type of the widget
* @returns The id of the resulting contributor
*/
public static getContributorId(widgetData: any, widgetType: any): string {
public static getContributorId(widgetData: any, widgetType: string): string {
let idString = widgetData.dataStep.collection + '-';
if (widgetType === WIDGET_TYPE.histogram || widgetType === WIDGET_TYPE.swimlane) {
const agg = widgetData.dataStep.aggregation;
Expand Down
3 changes: 2 additions & 1 deletion src/app/services/main-form-manager/models-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { VisualisationSetConfig, BasemapStyle } from 'arlas-map';
import { FieldsConfiguration, LayerSourceConfig } from 'arlas-web-contributors';
import { AnalyticsTabs, ZoomToDataStrategy } from 'arlas-wui-toolkit';
import { Layer } from './models-map-config';
import { WIDGET_TYPE } from '@analytics-config/components/edit-group/models';

export const JSONPATH_COUNT = '$.count';
export const JSONPATH_METRIC = '$.metrics[0].value';
Expand Down Expand Up @@ -214,7 +215,7 @@ export interface AnalyticComponentConfig {
usage?: WidgetUsage;
contributorId: string;
shortcutContributorId?: string;
componentType: string;
componentType: WIDGET_TYPE;
showExportCsv?: boolean;
input: AnalyticComponentInputConfig;
}
Expand Down

0 comments on commit 605d7ff

Please sign in to comment.