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

Prueba Técnica Angular #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 22 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const routes: Routes = [
path: '',
component: HomeComponent,
pathMatch: 'full'
},
{
path: ':filter',
component: HomeComponent,
}
];

Expand Down
13 changes: 10 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './pages/home/home.component';
import { FormsModule } from '@angular/forms';
import { TaskComponent } from './components/task/task.component';
import { AutofocusDirective } from './directives/autofocus.directive';
import { PathLocationStrategy, LocationStrategy } from '@angular/common';

@NgModule({
declarations: [
AppComponent,
HomeComponent
HomeComponent,
TaskComponent,
AutofocusDirective
],
imports: [
BrowserModule,
AppRoutingModule
AppRoutingModule,
FormsModule
],
providers: [],
providers: [{ provide: LocationStrategy, useClass: PathLocationStrategy }],
bootstrap: [AppComponent]
})
export class AppModule { }
Empty file.
20 changes: 20 additions & 0 deletions src/app/components/task/task.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<li [ngClass]="{ 'completed': task.completed, 'editing': editing }">
<div class="view">
<input
class="toggle"
type="checkbox"
[checked]="task.completed"
(click)="toggleTask()"/>
<label (dblclick)="editing = true">{{task.title}}</label>
<button
class="destroy"
(click)="removeTask()"></button>
</div>
<input
*ngIf="editing"
class="edit"
appAutofocus
[(ngModel)]="task.title"
(keydown.escape)="cancelEdition()"
(keydown.enter)="updateTitle()"/>
</li>
23 changes: 23 additions & 0 deletions src/app/components/task/task.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { TaskComponent } from './task.component';

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

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

fixture = TestBed.createComponent(TaskComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
44 changes: 44 additions & 0 deletions src/app/components/task/task.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { Task } from 'src/app/interface/task';
import { TaskManagerService } from 'src/app/services/task-manager.service';

@Component({
selector: 'app-task',
templateUrl: './task.component.html',
styleUrls: ['./task.component.css']
})
export class TaskComponent implements OnInit {

editing: boolean = false;
@Input() index: number = -1;
@Input() task: Task = {
id: -1,
title: '',
completed: false
}
@Output() updateTasks: EventEmitter<any> = new EventEmitter();

constructor(private taskManager: TaskManagerService) { }

ngOnInit(): void {
}

toggleTask() {
this.taskManager.completedTask(this.index);
}

removeTask() {
this.taskManager.removeTask(this.index);
this.updateTasks.emit();
}

updateTitle() {
this.taskManager.editTaskTitle(this.index, this.task.title);
this.editing = false;
}

cancelEdition() {
this.editing = false;
}

}
8 changes: 8 additions & 0 deletions src/app/directives/autofocus.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// import { AutofocusDirective } from './autofocus.directive';

// describe('AutofocusDirective', () => {
// it('should create an instance', () => {
// const directive = new AutofocusDirective();
// expect(directive).toBeTruthy();
// });
// });
14 changes: 14 additions & 0 deletions src/app/directives/autofocus.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Directive, ElementRef, AfterViewInit } from '@angular/core';

@Directive({
selector: '[appAutofocus]'
})
export class AutofocusDirective implements AfterViewInit{

constructor(private host: ElementRef) { }

ngAfterViewInit() {
this.host.nativeElement.focus();
}

}
5 changes: 5 additions & 0 deletions src/app/interface/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Task {
id: number;
title: string;
completed: boolean;
}
58 changes: 26 additions & 32 deletions src/app/pages/home/home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,53 @@ <h1>My Day</h1>
<input
class="new-todo"
placeholder="Type new todo"
autofocus
appAutofocus
type="text"
[(ngModel)]="taskTitle"
(keydown.enter)="addNewTask()"
/>
</div>
</header>
<div class="container todoapp-wrapper">
<!-- This section should be hidden by default and shown when there are todos -->
<section class="main">
<section class="main" *ngIf="tasks.length > 0">
<ul class="todo-list">
<li class="completed">
<div class="view">
<input class="toggle" type="checkbox" checked />
<label>Learn JavaScript</label>
<button class="destroy"></button>
</div>
<input class="edit" value="Learn JavaScript" />
</li>
<li>
<div class="view">
<input class="toggle" type="checkbox" />
<label>Buy a unicorn</label>
<button class="destroy"></button>
</div>
<input class="edit" value="Buy a unicorn" />
</li>
<li class="editing">
<div class="view">
<input class="toggle" type="checkbox" />
<label>Make dishes</label>
<button class="destroy"></button>
</div>
<input class="edit" value="Make dishes" />
</li>
<ng-container *ngFor="let task of tasks; index as i">
<app-task
(updateTasks)="updateTasks()"
[task]="task"
[index]="i">
</app-task>
</ng-container>
</ul>
</section>
<!-- This footer should be hidden by default and shown when there are todos -->
<footer class="footer">
<footer class="footer" *ngIf="getTotal() > 0">
<!-- This should be `0 items left` by default -->
<span class="todo-count"><strong>0</strong> item left</span>
<span class="todo-count"><strong>{{getPending()}}</strong> item<span *ngIf="getPending() != 1">s</span> left</span>
<!-- Remove this if you don't implement routing -->
<ul class="filters">
<li>
<a routerLink="/" class="selected">All</a>
<a
routerLink='/'
[class.selected]="currentRoute === 'all'">All</a>
</li>
<li>
<a routerLink="/pending">Pending</a>
<a
routerLink='/pending'
routerLinkActive="selected">Pending</a>
</li>
<li>
<a routerLink="/completed">Completed</a>
<a
routerLink='/completed'
routerLinkActive="selected">Completed</a>
</li>
</ul>
<!-- Hidden if no completed items are left ↓ -->
<button class="clear-completed">Clear completed</button>
<button
*ngIf="getCompleted() > 0"
class="clear-completed"
(click)="eraseCompleted()">Clear completed</button>
</footer>
</div>
</section>
Loading