Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
formControlName="name"
placeholder="Customer name"
[class.is-invalid]="customerForm.invalid && customerForm.touched"
(input)="onSearchChanges($event.target.value)"
required
/>
<span
Expand All @@ -23,6 +24,7 @@
rows="3"
formControlName="description"
placeholder="Customer description"
(input)="onSearchChanges($event.target.value)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a doubt. Why is the function called onSearchChanges? I remember the purpose is to check when the value Input field has changed. Maybe we need better name like onInputChange or something similar.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the purpose is to check when the Input value or the textArea has changed.
I'm going to rename onSearchChanges

></textarea>
</div>
<button type="submit" class="btn btn-primary" [disabled]="!customerForm.valid">Save</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,23 @@ describe('CreateCustomerComponent', () => {
expect(component.changeValueAreTabsActives.emit).toHaveBeenCalledWith(component.areTabsActive);
});

it('if detect changes in customer information, it should emit a true', () => {
component.haveChanges = true;
spyOn(component.isHaveChanges, 'emit');

component.onSearchChanges('changes text');

expect(component.haveChanges).toBe(true);
expect(component.isHaveChanges.emit).toHaveBeenCalledWith(component.haveChanges);
});

it('if not detect changes in customer information, it should emit a false', () => {
component.haveChanges = false;
spyOn(component.isHaveChanges, 'emit');

component.onSearchChanges('');

expect(component.haveChanges).toBe(false);
expect(component.isHaveChanges.emit).toHaveBeenCalledWith(component.haveChanges);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { LoadCustomerProjects, CleanCustomerProjects } from '../../../projects/c
export class CreateCustomerComponent implements OnInit, OnDestroy {
customerForm: FormGroup;
@Input() areTabsActive: boolean;
@Input() haveChanges: boolean;
@Output() isHaveChanges = new EventEmitter<boolean>();
@Output() changeValueAreTabsActives = new EventEmitter<boolean>();
@Output() closeCustomerComponent = new EventEmitter<boolean>();
customerToEdit: Customer;
Expand Down Expand Up @@ -68,6 +70,7 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
this.store.dispatch(new LoadProjectTypes(customerData.id));
this.store.dispatch(new LoadCustomerProjects(customerData.id));
this.changeValueAreTabsActives.emit(true);
this.isHaveChanges.emit(this.haveChanges = false);
this.customerForm.setValue({
name: customerData.name,
description: customerData.description,
Expand All @@ -94,4 +97,9 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
this.store.dispatch(new ResetCustomerToEdit());
this.closeCustomerComponent.emit(false);
}

onSearchChanges(searchValue: string): void {
return searchValue ? this.isHaveChanges.emit(this.haveChanges = true) :
this.isHaveChanges.emit(this.haveChanges = false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
<td class="col-3 text-break">{{ customer.name }}</td>
<td class="col-4 text-center">
<button
data-toggle="modal"
(click)="editCustomer(customer.id)"
type="button"
data-target="#editModal"
class="btn btn-sm btn-primary"
>
<i class="fa fa-pencil fa-xs"></i>
<i class="fa fa-pen fa-xs"></i>
</button>

<button
data-toggle="modal"
data-target="#deleteModal"
Expand Down Expand Up @@ -53,3 +56,16 @@
(closeModalEvent)="deleteCustomer()"
>
</app-dialog>

<app-dialog
*ngIf="showModal"
class="modal fade"
id="editModal"
tabindex="-1"
role="dialog"
aria-hidden="true"
[title]="'Edit Customer'"
[body]="message"
(closeModalEvent)="closeModal()"
>
</app-dialog>
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,27 @@ describe('CustomerTableListComponent', () => {
expect(store.dispatch).toHaveBeenCalledWith(new LoadCustomers());
});

it('onClick edit, dispatch SetCustomerToEdit and enable customer form', () => {

it('Onclick Edit, if there are changes, the modal must be presented ', () => {
component.haveChanges = true;
const expectMessage = 'Do you have changes in a client, do you want to discard them?';

component.editCustomer('1');

expect(component.message).toEqual(expectMessage);
expect(component.showModal).toBeTrue();
});

it('onClick edit, if there are not have changes dispatch SetCustomerToEdit, enable customer form and hidden modal', () => {
component.haveChanges = false;

spyOn(store, 'dispatch');

component.editCustomer('1');

expect(store.dispatch).toHaveBeenCalledWith(new SetCustomerToEdit('1'));
expect(component.showCustomerForm).toBeTruthy();
expect(component.showModal).toBeFalse();
});

it('onClick edit, dispatch clean Forms in project and project type', () => {
Expand All @@ -76,6 +90,18 @@ describe('CustomerTableListComponent', () => {
expect(store.dispatch).toHaveBeenCalledWith(new ResetProjectTypeToEdit());
});

it('when you click close modal, you should close the modal, discard the current changes and load a new client for edit', () => {
spyOn(component.changeValueShowCustomerForm, 'emit');
spyOn(store, 'dispatch');

component.editCustomer('1');
component.closeModal();

expect(component.showModal).toBeFalse();
expect(component.changeValueShowCustomerForm.emit).toHaveBeenCalledWith(true);
expect(store.dispatch).toHaveBeenCalledWith(new SetCustomerToEdit('1'));
});

it('onClick delete, dispatch DeleteCustomer', () => {
spyOn(store, 'dispatch');
component.idToDelete = '1';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ResetProjectTypeToEdit } from '../../../projects-type/store';
})
export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
@Input() showCustomerForm: boolean;
@Input() haveChanges: boolean;
@Output() changeValueShowCustomerForm = new EventEmitter<boolean>();
@Input()
customers: Customer[] = [];
Expand All @@ -31,6 +32,7 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
changeCustomerSubscription: Subscription;
showModal = false;
idToDelete: string;
idToEdit: string;
message: string;
isLoading$: Observable<boolean>;

Expand Down Expand Up @@ -77,10 +79,25 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
}

editCustomer(customerId: string) {
this.idToEdit = customerId;
if (this.haveChanges === true) {
this.message = 'Do you have changes in a client, do you want to discard them?';
this.showModal = true;
} else {
this.showCustomerForm = true;
this.showModal = false;
this.changeValueShowCustomerForm.emit(this.showCustomerForm);
this.resetProjectFieldsToEdit();
this.store.dispatch(new SetCustomerToEdit(customerId));
}
}

closeModal() {
this.showCustomerForm = true;
this.showModal = false;
this.changeValueShowCustomerForm.emit(this.showCustomerForm);
this.store.dispatch(new SetCustomerToEdit(customerId));
this.resetProjectFieldsToEdit();
this.store.dispatch(new SetCustomerToEdit(this.idToEdit));
}

private resetProjectFieldsToEdit() {
Expand Down Expand Up @@ -109,5 +126,4 @@ export class CustomerListComponent implements OnInit, OnDestroy, AfterViewInit {
this.message = `Are you sure you want to delete ${item.name}?`;
this.showModal = true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ <h4 class="navbar-brand nav-title">{{customerName}}</h4>
<app-create-customer
[areTabsActive]="areTabsActive"
(changeValueAreTabsActives)="activeTabs($event)"
(isHaveChanges)="getChanges($event)"
(closeCustomerComponent)="closeCustomer($event)"
></app-create-customer>
</div>
Expand All @@ -66,7 +67,7 @@ <h4 class="navbar-brand nav-title">{{customerName}}</h4>
aria-labelledby="projects-type-tab"
>
<div class="container">
<app-create-project-type></app-create-project-type>
<app-create-project-type (isHaveChanges)="getChanges($event)"></app-create-project-type>
<app-project-type-list></app-project-type-list>
</div>
</div>
Expand All @@ -78,7 +79,7 @@ <h4 class="navbar-brand nav-title">{{customerName}}</h4>
aria-labelledby="projects-tab"
>
<div class="container mb-1">
<app-create-project></app-create-project>
<app-create-project (isHaveChanges)="getChanges($event)"></app-create-project>
<app-project-list></app-project-list>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,18 @@ describe('ManagmentCustomerProjectsComponent', () => {
component.showTab('projects');
expect(component.activeTab).toEqual('projects');
});

it('should call close customer function', () => {
spyOn(component.closeCustemerForm, 'emit');
component.closeCustomer(false);
expect(component.closeCustemerForm.emit).toHaveBeenCalledWith(false);
});

it('with changes should emit a true ', () => {
spyOn(component.sendChanges, 'emit');

component.getChanges(true);

expect(component.sendChanges.emit).toHaveBeenCalledWith(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { Component, Output, EventEmitter, OnInit } from '@angular/core';
})
export class ManagementCustomerProjectsComponent implements OnInit {
@Output() closeCustemerForm = new EventEmitter<boolean>();
@Output() sendChanges = new EventEmitter<boolean>();
areTabsActive: boolean;
haveChanges: boolean;
activeTab: string;
customerName: string;

Expand Down Expand Up @@ -42,4 +44,8 @@ export class ManagementCustomerProjectsComponent implements OnInit {
this.activeTab = activeTab;
}

getChanges($haveChanges: boolean) {
this.haveChanges = $haveChanges;
this.sendChanges.emit($haveChanges);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
<form style="width: 600px;" [formGroup]="projectTypeForm" (ngSubmit)="onSubmit(projectTypeForm.value)">
<div class="form-group">
<input type="text" class="form-control form-control-sm" id="name" formControlName="name" aria-describedby=""
[class.is-invalid]="name.invalid && name.touched" required placeholder="Name" />
<input type="text"class="form-control form-control-sm"
id="name"
formControlName="name"
aria-describedby=""
[class.is-invalid]="name.invalid && name.touched"
required placeholder="Name"
(input)="onSearchChanges($event.target.value)"
/>
<div class="text-danger" *ngIf="(name.dirty || name.touched) && name.invalid && name.errors.required">
Name is required.
</div>
</div>
<div class="form-group">
<textarea class="form-control form-control-sm mt-2" id="descriptionTextArea" rows="3" placeholder="Description"
formControlName="description"></textarea>
<textarea class="form-control
form-control-sm mt-2"
id="descriptionTextArea"
rows="3"
placeholder="Description"
formControlName="description"
(input)="onSearchChanges($event.target.value)"
></textarea>
</div>
<button type="submit" class="btn btn-primary" [disabled]="!projectTypeForm.valid">
Save
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,24 @@ describe('InputProjectTypeComponent', () => {
expect(store.dispatch).toHaveBeenCalledTimes(1);
expect(store.dispatch).toHaveBeenCalledWith(new ResetProjectTypeToEdit());
});

it('if detect changes in create project type , it should emit a true', () => {
component.haveChanges = true;
spyOn(component.isHaveChanges, 'emit');

component.onSearchChanges('name project');

expect(component.haveChanges).toBe(true);
expect(component.isHaveChanges.emit).toHaveBeenCalledWith(true);
});

it('if not detect changes in create project type, it should emit a false', () => {
component.haveChanges = false;
spyOn(component.isHaveChanges, 'emit');

component.onSearchChanges('');

expect(component.haveChanges).toBe(false);
expect(component.isHaveChanges.emit).toHaveBeenCalledWith(false);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
import { Store, select } from '@ngrx/store';

Expand All @@ -14,6 +14,8 @@ import { Subscription } from 'rxjs';
styleUrls: ['./create-project-type.component.scss'],
})
export class CreateProjectTypeComponent implements OnInit, OnDestroy {
@Input() haveChanges: boolean;
@Output() isHaveChanges = new EventEmitter<boolean>();
projectTypeForm: FormGroup;
projectTypeToEdit: ProjectType;
customerId: string;
Expand Down Expand Up @@ -63,9 +65,11 @@ export class CreateProjectTypeComponent implements OnInit, OnDestroy {
id: this.projectTypeToEdit.id,
};
this.store.dispatch(new UpdateProjectType(projectType));
this.isHaveChanges.emit(this.haveChanges = false);
} else {
this.store.dispatch(new CreateProjectType({ ...projectTypeData, customer_id: this.customerId }));
this.projectTypeForm.get('description').setValue('');
this.isHaveChanges.emit(this.haveChanges = false);
}
}

Expand All @@ -76,4 +80,9 @@ export class CreateProjectTypeComponent implements OnInit, OnDestroy {
ngOnDestroy(): void {
this.getCustomerIdSubscription.unsubscribe();
}

onSearchChanges(searchValue: string): void {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is identical to onSearchChanges of create-customer.ts
Here

return searchValue ? this.isHaveChanges.emit(this.haveChanges = true) :
this.isHaveChanges.emit(this.haveChanges = false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
placeholder="Project name"
formControlName="name"
[class.is-invalid]="name.invalid && name.touched"
(input)="onSearchChanges($event.target.value)"
/>
<div class="text-danger" *ngIf="(name.dirty || name.touched) && name.invalid && name.errors.required">
Project name is required.
Expand All @@ -16,6 +17,7 @@
rows="3"
formControlName="description"
placeholder="Description"
(input)="onSearchChanges($event.target.value)"
></textarea>
<div class="form-group">
<select class="custom-select custom-select-sm mt-2" formControlName="project_type_id">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,24 @@ describe('InputProjectComponent', () => {
expect(store.dispatch).toHaveBeenCalledTimes(1);
expect(store.dispatch).toHaveBeenCalledWith(new ResetProjectToEdit());
});

it('if detect changes in create project , it should emit a true', () => {
component.haveChanges = true;
spyOn(component.isHaveChanges, 'emit');

component.onSearchChanges('name project');

expect(component.haveChanges).toBe(true);
expect(component.isHaveChanges.emit).toHaveBeenCalledWith(true);
});

it('if not detect changes in create project, it should emit a false', () => {
component.haveChanges = false;
spyOn(component.isHaveChanges, 'emit');

component.onSearchChanges('');

expect(component.haveChanges).toBe(false);
expect(component.isHaveChanges.emit).toHaveBeenCalledWith(false);
});
});
Loading