Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate node lock dialog to date-fns #8970

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions lib/content-services/src/lib/dialogs/node-lock.dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
};
Expand All @@ -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: {
Expand All @@ -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();
Expand All @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions lib/content-services/src/lib/dialogs/node-lock.dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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());
swapnil-verma-gl marked this conversation as resolved.
Show resolved Hide resolved
return this._nodesApi;
Expand All @@ -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();
swapnil-verma-gl marked this conversation as resolved.
Show resolved Hide resolved

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;
Expand Down
Loading