Skip to content

Commit

Permalink
auto save changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tumms2021389 committed Dec 9, 2024
1 parent 3d613d6 commit ee600a8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Component, OnInit, Input, Output, EventEmitter, forwardRef, OnChanges } from '@angular/core';
import { Component, OnInit, Input, Output, EventEmitter, forwardRef, OnChanges, OnDestroy } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormGroup, ReactiveFormsModule } from '@angular/forms';
import { ReferenceComponent } from '../reference/reference.component';
import { ComponentMapperComponent } from '../../../_bridge/component-mapper/component-mapper.component';
import { IdleDetectionService } from '../../../_services/idle-detection.service';
import { ServerConfigService } from '../../../_services/server-config.service';

@Component({
selector: 'app-assignment-card',
Expand All @@ -11,7 +13,7 @@ import { ComponentMapperComponent } from '../../../_bridge/component-mapper/comp
standalone: true,
imports: [CommonModule, ReactiveFormsModule, forwardRef(() => ComponentMapperComponent)]
})
export class AssignmentCardComponent implements OnInit, OnChanges {
export class AssignmentCardComponent implements OnInit, OnChanges, OnDestroy {
@Input() pConn$: typeof PConnect;
@Input() formGroup$: FormGroup;
@Input() arMainButtons$: any[];
Expand All @@ -21,19 +23,44 @@ export class AssignmentCardComponent implements OnInit, OnChanges {

@Output() actionButtonClick: EventEmitter<any> = new EventEmitter();

constructor(
private idleService: IdleDetectionService,
private scservice: ServerConfigService
) {}

ngOnInit(): void {
// Children may contain 'reference' component, so we need to
// normalize them
// Children may contain 'reference' component, so we need to normalize them
this.arChildren$ = ReferenceComponent.normalizePConnArray(this.arChildren$);

this.checkAndEnableAutoSave();
}

ngOnChanges() {
// Children may contain 'reference' component, so we need to
// normalize them
// Children may contain 'reference' component, so we need to normalize them
this.arChildren$ = ReferenceComponent.normalizePConnArray(this.arChildren$);
}

ngOnDestroy() {
this.idleService.stopWatching();
}

async checkAndEnableAutoSave() {
const sdkConfig = await this.scservice.getSdkConfig();
const autoSave = sdkConfig.serverConfig.autoSave;

if (autoSave) {
this.idleService.startWatching(() => this.autoSave(), autoSave);
}
}

onActionButtonClick(oData: any) {
this.actionButtonClick.emit(oData);
}

autoSave() {
const context = this.pConn$.getContextName();
if (PCore.getFormUtils().isStateModified(context)) {
this.pConn$.getActionsApi().saveAssignment(context);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Injectable } from '@angular/core';
import { fromEvent, merge, Subscription, timer } from 'rxjs';
import { switchMap, tap } from 'rxjs/operators';

@Injectable({
providedIn: 'root'
})
export class IdleDetectionService {
private activityEvents$ = merge(fromEvent(document, 'mousemove'), fromEvent(document, 'keydown'), fromEvent(document, 'click'));

private idleSubscription!: Subscription;

startWatching(idleCallback: () => void, idleTimeout: number = 10000) {
this.idleSubscription = this.activityEvents$.pipe(switchMap(() => timer(idleTimeout).pipe(tap(() => idleCallback())))).subscribe();
}

stopWatching() {
if (this.idleSubscription) {
this.idleSubscription.unsubscribe();
}
}
}
1 change: 1 addition & 0 deletions packages/angular-sdk-components/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export * from './lib/_directives/thousand-seperator.directive';
export * from './lib/_services/server-config.service';
export * from './lib/_services/case.service';
export * from './lib/_services/datapage.service';
export * from './lib/_services/idle-detection.service';
export * from './lib/_services/endpoints';

export * from './lib/_helpers/case-utils';
Expand Down

0 comments on commit ee600a8

Please sign in to comment.