Skip to content

Commit ee600a8

Browse files
committed
auto save changes
1 parent 3d613d6 commit ee600a8

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed
Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { Component, OnInit, Input, Output, EventEmitter, forwardRef, OnChanges } from '@angular/core';
1+
import { Component, OnInit, Input, Output, EventEmitter, forwardRef, OnChanges, OnDestroy } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33
import { FormGroup, ReactiveFormsModule } from '@angular/forms';
44
import { ReferenceComponent } from '../reference/reference.component';
55
import { ComponentMapperComponent } from '../../../_bridge/component-mapper/component-mapper.component';
6+
import { IdleDetectionService } from '../../../_services/idle-detection.service';
7+
import { ServerConfigService } from '../../../_services/server-config.service';
68

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

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

26+
constructor(
27+
private idleService: IdleDetectionService,
28+
private scservice: ServerConfigService
29+
) {}
30+
2431
ngOnInit(): void {
25-
// Children may contain 'reference' component, so we need to
26-
// normalize them
32+
// Children may contain 'reference' component, so we need to normalize them
2733
this.arChildren$ = ReferenceComponent.normalizePConnArray(this.arChildren$);
34+
35+
this.checkAndEnableAutoSave();
2836
}
2937

3038
ngOnChanges() {
31-
// Children may contain 'reference' component, so we need to
32-
// normalize them
39+
// Children may contain 'reference' component, so we need to normalize them
3340
this.arChildren$ = ReferenceComponent.normalizePConnArray(this.arChildren$);
3441
}
3542

43+
ngOnDestroy() {
44+
this.idleService.stopWatching();
45+
}
46+
47+
async checkAndEnableAutoSave() {
48+
const sdkConfig = await this.scservice.getSdkConfig();
49+
const autoSave = sdkConfig.serverConfig.autoSave;
50+
51+
if (autoSave) {
52+
this.idleService.startWatching(() => this.autoSave(), autoSave);
53+
}
54+
}
55+
3656
onActionButtonClick(oData: any) {
3757
this.actionButtonClick.emit(oData);
3858
}
59+
60+
autoSave() {
61+
const context = this.pConn$.getContextName();
62+
if (PCore.getFormUtils().isStateModified(context)) {
63+
this.pConn$.getActionsApi().saveAssignment(context);
64+
}
65+
}
3966
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Injectable } from '@angular/core';
2+
import { fromEvent, merge, Subscription, timer } from 'rxjs';
3+
import { switchMap, tap } from 'rxjs/operators';
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class IdleDetectionService {
9+
private activityEvents$ = merge(fromEvent(document, 'mousemove'), fromEvent(document, 'keydown'), fromEvent(document, 'click'));
10+
11+
private idleSubscription!: Subscription;
12+
13+
startWatching(idleCallback: () => void, idleTimeout: number = 10000) {
14+
this.idleSubscription = this.activityEvents$.pipe(switchMap(() => timer(idleTimeout).pipe(tap(() => idleCallback())))).subscribe();
15+
}
16+
17+
stopWatching() {
18+
if (this.idleSubscription) {
19+
this.idleSubscription.unsubscribe();
20+
}
21+
}
22+
}

packages/angular-sdk-components/src/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export * from './lib/_directives/thousand-seperator.directive';
123123
export * from './lib/_services/server-config.service';
124124
export * from './lib/_services/case.service';
125125
export * from './lib/_services/datapage.service';
126+
export * from './lib/_services/idle-detection.service';
126127
export * from './lib/_services/endpoints';
127128

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

0 commit comments

Comments
 (0)