Skip to content
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
8 changes: 5 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CommonModule } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';

import { AppRoutingModule } from './app-routing.module';

Expand All @@ -16,8 +17,7 @@ import { ProjectListComponent } from './components/shared/project-list/project-l
import { CreateProjectComponent } from './components/shared/create-project/create-project.component';
import { DetailsFieldsComponent } from './components/shared/details-fields/details-fields.component';
import { ProjectListHoverComponent } from './components/shared/project-list-hover/project-list-hover.component';


import { ModalComponent } from './components/shared/modal/modal.component';
@NgModule({
declarations: [
AppComponent,
Expand All @@ -32,13 +32,15 @@ import { ProjectListHoverComponent } from './components/shared/project-list-hove
TimeClockComponent,
DetailsFieldsComponent,
ProjectListHoverComponent,
ModalComponent
],
imports: [
CommonModule,
BrowserModule,
AppRoutingModule,
FormsModule,
ReactiveFormsModule
ReactiveFormsModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<app-project-list class="item"
[projects] = "projects"
(editProject) = "editProject($event)"
(deleteProject) = "deleteProject($event)"
>
</app-project-list>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { async, ComponentFixture, TestBed, inject } from '@angular/core/testing';

import { ProjectManagementComponent } from './project-management.component';
import { Project } from '../../../interfaces/project';
import { ProjectService } from '../../../services/project.service';
import { of } from 'rxjs';

describe('ProjectManagementComponent', () => {
let component: ProjectManagementComponent;
let fixture: ComponentFixture<ProjectManagementComponent>;
let projectService: ProjectService;

const projects: Project[] = [{
id: 1,
name: 'app 1',
Expand All @@ -29,9 +33,17 @@ describe('ProjectManagementComponent', () => {
}
];

const projectServiceStub = {
getProjects() {
const projectsMock = projects;
return of(projectsMock);
}
};

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ProjectManagementComponent ]
declarations: [ ProjectManagementComponent ],
providers: [ { provide: ProjectService, useValue: projectServiceStub }]
})
.compileComponents();
}));
Expand All @@ -40,9 +52,18 @@ describe('ProjectManagementComponent', () => {
fixture = TestBed.createComponent(ProjectManagementComponent);
component = fixture.componentInstance;
fixture.detectChanges();

projectService = TestBed.inject(ProjectService);
component.projects = projects;
});


it('Service injected via inject(...) and TestBed.get(...) should be the same instance',
inject([ProjectService], (injectService: ProjectService) => {
expect(injectService).toBe(projectService);
})
);

it('should create', () => {
expect(component).toBeTruthy();
});
Expand Down Expand Up @@ -97,4 +118,34 @@ describe('ProjectManagementComponent', () => {
component.cancelForm();
expect(component.project).toBe(null);
});

it('should call getProjects method on init', () => {
const preojectServiceSpy = spyOn(projectService, 'getProjects').and.callThrough();
const componentSpy = spyOn(component, 'getProjects').and.callThrough();

expect(preojectServiceSpy).not.toHaveBeenCalled();
expect(componentSpy).not.toHaveBeenCalled();

component.ngOnInit();

expect(preojectServiceSpy).toHaveBeenCalledTimes(1);
expect(componentSpy).toHaveBeenCalledTimes(1);
});


it('should call getProjects and return a list of projects', async(() => {

spyOn(projectService, 'getProjects').and.returnValue(of(projects));

component.ngOnInit();

expect(component.projects).toEqual(projects);
}));

it('should delete a project #deleteProject', () => {
const projectId = 1;

component.deleteProject(projectId);
expect(component.projects.length).toEqual(2);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { Project } from '../../../interfaces/project';
import { Input } from '@angular/core';
import { Output, EventEmitter } from '@angular/core';
import { ProjectService } from '../../../services/project.service';

@Component({
selector: 'app-project-management',
templateUrl: './project-management.component.html',
Expand All @@ -13,14 +13,13 @@ export class ProjectManagementComponent implements OnInit {

project: Project;

projects: Project[] = [
{ id: 1, name: 'GoSpace', details: 'Improve workspaces', status: 'Active', completed: false},
{ id: 2, name: 'MidoPlay', details: 'Lottery', status: 'Inactive', completed: true}
];
projects: Project[] = [];

constructor() { }
constructor(private projectService: ProjectService) {
}

ngOnInit(): void {
this.getProjects();
}

updateProject(projectData): void {
Expand All @@ -44,7 +43,17 @@ export class ProjectManagementComponent implements OnInit {
this.project = this.projects.filter((project) => project.id === projectId)[0];
}

deleteProject(projectId): void {
this.projects = this.projects.filter((project) => project.id !== projectId);
}

cancelForm() {
this.project = null;
}

getProjects() {
this.projectService.getProjects().subscribe(data => {
this.projects = data;
});
}
}
Empty file.
17 changes: 17 additions & 0 deletions src/app/components/shared/modal/modal.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="modal-dialog" role="document">
<div class="modal-content" *ngIf="project">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Delete Project</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
Are you sure you want to delete {{project.name}} project?
</div>
<div class="modal-footer">
<button #cancelDeleteModal type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button (click)="removedProject(project.id)" type="button" class="btn btn-primary">Delete</button>
</div>
</div>
</div>
25 changes: 25 additions & 0 deletions src/app/components/shared/modal/modal.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { ModalComponent } from './modal.component';

describe('ModalComponent', () => {
let component: ModalComponent;
let fixture: ComponentFixture<ModalComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ModalComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(ModalComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
26 changes: 26 additions & 0 deletions src/app/components/shared/modal/modal.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Output, EventEmitter } from '@angular/core';
import { Input } from '@angular/core';
import { Project } from '../../../interfaces/project'
@Component({
selector: 'app-modal',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.css']
})
export class ModalComponent implements OnInit {

@Input() project: Project;
@Output() removeProject = new EventEmitter();

@ViewChild('cancelDeleteModal') cancelDeleteModal: ElementRef;

constructor() { }

ngOnInit(): void {
}

removedProject(projectId) {
this.removeProject.emit(projectId);
this.cancelDeleteModal.nativeElement.click();
}
}
55 changes: 37 additions & 18 deletions src/app/components/shared/project-list/project-list.component.html
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
<div class="card-body">
<h1 class="card-title">Project List</h1>
<div class="scroll">
<div class="accordion" id="accordionExample">
<div class="card" *ngFor="let project of projects; let rowIndex = index">
<div class="card-header">
<h2 class="mb-0">
<a type="button" data-toggle="collapse" [attr.data-target]="'#row'+rowIndex" [attr.aria-controls]="'row'+rowIndex">
{{project.name}}
</a>
<button (click)="editProject.emit(project.id)" class="btn btn-outline-primary float-right">edit</button>
</h2>
</div>
<div class="accordion" id="accordionProject">
<div *ngIf="projects?.length > 0; else notShow">
<div class="card" *ngFor="let project of projects; let rowIndex = index">
<div class="card-header">
<h2 class="mb-0">
<a type="button" data-toggle="collapse" [attr.data-target]="'#row'+rowIndex" [attr.aria-controls]="'row'+rowIndex">
{{project.name}}
</a>
<div class="btn-group float-right" role="group" >
<i (click)="editProject.emit(project.id)" class="far fa-edit btn btn-link"></i>
<i (click)="openModal(project)" data-toggle="modal" data-target="#deleteModal" class="far fa-trash-alt btn btn-link"></i>
</div>
</h2>
</div>

<div [id]="'row'+rowIndex" class="collapse" data-parent="#accordionExample">
<div class="card-body">
<h5>Details:</h5>
<p>{{project.details}}</p>
<h5>Status:</h5>
<p>{{project.status}}</p>
<h5>Completed project:</h5>
<p>{{project.completed ? 'Yes' : 'No'}}</p>
<div [id]="'row'+rowIndex" class="collapse" data-parent="#accordionProject">
<div class="card-body">
<h5>Details:</h5>
<p>{{project.details}}</p>
<h5>Status:</h5>
<p>{{project.status}}</p>
<h5>Completed project:</h5>
<p>{{project.completed ? 'Yes' : 'No'}}</p>
</div>
</div>
</div>
</div>
<ng-template #notShow>
<div class="card">
<div class="card-body">
<h4 class="card-text">There is no any project.</h4>
<h5 class="card-text">Please, create one.</h5>
</div>
</div>
</ng-template>
</div>
</div>
</div>

<app-modal *ngIf = "openDeleteModal" class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-hidden="true"
[project] = "projectToDelete"
(removeProject) = "removeProject($event)"
>
</app-modal>
17 changes: 16 additions & 1 deletion src/app/components/shared/project-list/project-list.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { Input } from '@angular/core';
import { Output, EventEmitter } from '@angular/core';
import { Project } from '../../../interfaces/project';

@Component({
selector: 'app-project-list',
Expand All @@ -9,11 +10,25 @@ import { Output, EventEmitter } from '@angular/core';
})
export class ProjectListComponent implements OnInit {

@Input() projects: any[];
@Input() projects: Project[] = [];
@Output() editProject = new EventEmitter();
@Output() deleteProject = new EventEmitter();

projectToDelete: Project;
openDeleteModal: Boolean = false;

constructor() { }

ngOnInit(): void {
}

openModal(projectData) {
this.projectToDelete = projectData;
this.openDeleteModal = true;
}

removeProject(projectId) {
this.deleteProject.emit(projectId);
this.projectToDelete = null;
}
}
23 changes: 23 additions & 0 deletions src/app/services/project.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TestBed, inject, async } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';

import { ProjectService } from './project.service';

describe('ProjectService', () => {
let service: ProjectService;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule]
});
service = TestBed.inject(ProjectService);
});


it('should create', inject([HttpClientTestingModule, ProjectService],
(httpClient: HttpClientTestingModule, apiService: ProjectService) => {
expect(apiService).toBeTruthy();
expect(httpClient).toBeTruthy();
}));

});
19 changes: 19 additions & 0 deletions src/app/services/project.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Injectable } from '@angular/core';
import { Project } from '../interfaces/project';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class ProjectService {

projects: Project[] = [];
url = 'assets/project.json';

constructor(private http: HttpClient) {}

getProjects(): Observable<Project[]> {
return this.http.get<Project[]>(this.url);
}
}
Loading