migrate node lock dialog to date-fns (#8970)

* migrate node lock dialog to date-fns

* cleanup
This commit is contained in:
Denys Vuika
2023-10-06 13:25:33 +01:00
committed by GitHub
parent ea65756660
commit 93f45062fe
2 changed files with 15 additions and 28 deletions

View File

@@ -15,19 +15,19 @@
* limitations under the License.
*/
import moment from 'moment';
import { TestBed, fakeAsync, tick, ComponentFixture } from '@angular/core/testing';
import { MatDialogRef } from '@angular/material/dialog';
import { NodeLockDialogComponent } from './node-lock.dialog';
import { ContentTestingModule } from '../testing/content.testing.module';
import { TranslateModule } from '@ngx-translate/core';
import { addMinutes } from 'date-fns';
describe('NodeLockDialogComponent', () => {
let fixture: ComponentFixture<NodeLockDialogComponent>;
let component: NodeLockDialogComponent;
let expiryDate;
let expiryDate: Date;
const dialogRef = {
close: jasmine.createSpy('close')
};
@@ -54,7 +54,7 @@ describe('NodeLockDialogComponent', () => {
beforeEach(() => {
jasmine.clock().mockDate(new Date());
expiryDate = moment().add(1, 'minutes');
expiryDate = addMinutes(new Date(), 1);
component.data = {
node: {
@@ -77,25 +77,12 @@ describe('NodeLockDialogComponent', () => {
expect(component.form.value.isLocked).toBe(true);
expect(component.form.value.allowOwner).toBe(true);
expect(component.form.value.isTimeLock).toBe(true);
expect(component.form.value.time.format()).toBe(expiryDate.format());
});
it('should update form inputs', () => {
const newTime = moment();
component.form.controls['isLocked'].setValue(false);
component.form.controls['allowOwner'].setValue(false);
component.form.controls['isTimeLock'].setValue(false);
component.form.controls['time'].setValue(newTime);
expect(component.form.value.isLocked).toBe(false);
expect(component.form.value.allowOwner).toBe(false);
expect(component.form.value.isTimeLock).toBe(false);
expect(component.form.value.time.format()).toBe(newTime.format());
expect(component.form.value.time).toEqual(expiryDate);
});
it('should call dialog to close with form data when submit is successfully', fakeAsync(() => {
const node: any = { entry: {} };
spyOn(component['nodesApi'], 'lockNode').and.returnValue(Promise.resolve(node));
spyOn(component.nodesApi, 'lockNode').and.returnValue(Promise.resolve(node));
component.submit();
tick();
@@ -105,7 +92,7 @@ describe('NodeLockDialogComponent', () => {
}));
it('should call onError if submit fails', fakeAsync(() => {
spyOn(component['nodesApi'], 'lockNode').and.returnValue(Promise.reject('error'));
spyOn(component.nodesApi, 'lockNode').and.returnValue(Promise.reject('error'));
spyOn(component.data, 'onError');
component.submit();

View File

@@ -15,12 +15,10 @@
* limitations under the License.
*/
import moment from 'moment';
import { Component, Inject, OnInit, Optional, ViewEncapsulation } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
import { differenceInSeconds } from 'date-fns';
import { NodeBodyLock, Node, NodeEntry, NodesApi } from '@alfresco/js-api';
import { AlfrescoApiService } from '@alfresco/adf-core';
@@ -35,7 +33,7 @@ export class NodeLockDialogComponent implements OnInit {
node: Node = null;
nodeName: string;
_nodesApi: NodesApi;
private _nodesApi: NodesApi;
get nodesApi(): NodesApi {
this._nodesApi = this._nodesApi ?? new NodesApi(this.alfrescoApi.getInstance());
return this._nodesApi;
@@ -55,18 +53,20 @@ export class NodeLockDialogComponent implements OnInit {
const { node } = this.data;
this.nodeName = node.name;
const isTimeLock = !!node.properties['cm:expiryDate'];
const time = isTimeLock ? new Date(node.properties['cm:expiryDate']) : new Date();
this.form = this.formBuilder.group({
isLocked: node.isLocked || false,
allowOwner: node.properties['cm:lockType'] === 'WRITE_LOCK',
isTimeLock: !!node.properties['cm:expiryDate'],
time: node.properties['cm:expiryDate'] ? moment(node.properties['cm:expiryDate']) : moment()
isTimeLock,
time
});
}
private get lockTimeInSeconds(): number {
if (this.form.value.isTimeLock) {
const duration = moment.duration(moment(this.form.value.time).diff(moment()));
return duration.asSeconds();
return differenceInSeconds(new Date(this.form.value.time), Date.now());
}
return 0;