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

Homeworks 10 & 11 done #633

Open
wants to merge 4 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
"author": "",
"license": "ISC",
"dependencies": {
"@angular/common": "^2.4.6",
"@angular/common": "^2.4.7",
"@angular/compiler": "^2.4.6",
"@angular/core": "^2.4.6",
"@angular/platform-browser": "^2.4.6",
"@angular/core": "^2.4.7",
"@angular/forms": "^2.4.6",
"@angular/http": "^2.4.8",
"@angular/platform-browser": "^2.4.7",
"@angular/platform-browser-dynamic": "^2.4.6",
"@angular/router": "^3.4.7",
"bootstrap": "^4.0.0-alpha.6",
"bootstrap-v4-webpack": "^1.0.1",
"es6-shim": "^0.35.3",
Expand All @@ -24,7 +27,7 @@
"zone.js": "^0.7.6"
},
"devDependencies": {
"@angular/forms": "^2.4.6",
"angular-in-memory-web-api": "^0.2.4",
"angular2-template-loader": "^0.6.1",
"awesome-typescript-loader": "^3.0.0-beta.20",
"css-loader": "^0.26.1",
Expand All @@ -34,6 +37,7 @@
"raw-loader": "^0.5.1",
"sass-loader": "^4.1.1",
"style-loader": "^0.13.1",
"to-string-loader": "^1.1.5",
"ts-loader": "^2.0.0",
"tslint": "^4.4.2",
"tslint-loader": "^3.3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>Title</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,98 +1,18 @@
import { Component, OnInit } from "@angular/core";
import { Hero } from "./hero";
import { HeroService } from "./hero.service";
import { Component } from "@angular/core";

@Component({
selector: "my-app",
template:`
<main class="row">
<div class="col-4">
<h2>My Heroes</h2>
<ul class="heroes">
<li *ngFor="let hero of heroes" (click)="onSelect(hero)" [class.selected]="hero === selectedHero">
<span class="badge">{{hero.id}}</span> {{hero.name}}
</li>
</ul>
</div>
<div class="col-8">
<my-hero-detail [hero]="selectedHero"></my-hero-detail>
</div>
</main>
`,
styles: [`
main {
width: 100%;
max-width: 991px;
margin: 30px auto;
}
.selected {
background-color: #CFD8DC !important;
color: white;
}
.heroes {
margin: 0 0 2em 0;
list-style-type: none;
padding: 0;
width: 15em;
}
.heroes li {
cursor: pointer;
position: relative;
left: 0;
background-color: #EEE;
margin: .5em;
padding: .3em 0 .3em 3em;
border-radius: 4px;
}
.heroes li.selected:hover {
background-color: #BBD8DC !important;
color: white;
}
.heroes li:hover {
color: #607D8B;
background-color: #DDD;
left: .1em;
}
.heroes .text {
position: relative;
top: -3px;
}
.heroes .badge {
display: inline-block;
font-size: small;
color: white;
padding: 0.8em 0.7em 0 0.7em;
background-color: #607D8B;
line-height: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
border-radius: 4px 0 0 4px;
}
`],
providers: [HeroService]
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<nav>
<a routerLink="/dashboard" routerLinkActive="active">Dashboard</a>
<a routerLink="/heroes" routerLinkActive="active">Heroes</a>
</nav>
<main>
<router-outlet></router-outlet>
</main>
`
})

export class AppComponent implements OnInit {
title = "Tour of Heroes";
selectedHero: Hero;
heroes: Hero[];

constructor (private heroService: HeroService) {}

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

getHeroes(): void {
this.heroService.getHeroes()
.then((heroes) => {
this.heroes = heroes;
});
}

onSelect = (hero:Hero) => {
this.selectedHero = hero;
}
export class AppComponent {
title = 'Tour of Heroes';
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { AppComponent } from "./app.component";
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { HttpModule } from '@angular/http';
import { ReactiveFormsModule } from '@angular/forms';

import { LocationStrategy, HashLocationStrategy } from '@angular/common';

import { HeroesComponent } from "./heroes.component";
import { AppComponent } from "./app.component";
import { DashboardComponent } from "./dashboard.component";
import { HeroDetailComponent } from "./hero-detail.component";
import { HeroEditComponent } from "./hero-edit.component";
import { HeroEditComponent } from "./hero-edit.component";
import { HeroSearchComponent } from "./hero-search.component";
import { HeroService } from './hero.service';
import { AppRoutingModule } from './app.routing.module';

// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from './in-memory-data.service';

@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent, HeroDetailComponent, HeroEditComponent ],
bootstrap: [ AppComponent ]
imports: [
BrowserModule,
FormsModule,
AppRoutingModule,
HttpModule,
InMemoryWebApiModule.forRoot(InMemoryDataService),
ReactiveFormsModule
],
declarations: [
AppComponent,
HeroesComponent,
DashboardComponent,
HeroDetailComponent,
HeroEditComponent,
HeroSearchComponent
],
providers: [
HeroService
],
bootstrap: [
AppComponent
]
})

export default class AppModule { }
export default class AppModule { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { DashboardComponent } from './dashboard.component';
import { HeroesComponent } from './heroes.component';
import { HeroDetailComponent } from './hero-detail.component';

const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'detail/:id', component: HeroDetailComponent },
{ path: 'heroes', component: HeroesComponent }
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h3>Top Heroes</h3>
<div class="row">
<a *ngFor="let hero of heroes" [routerLink]="['/detail', hero.id]" class="col-1-4">
<div class="module hero">
<h4>{{hero.name}}</h4>
</div>
</a>
</div>
<div class="row">
<div class="col-12">
<hero-search></hero-search>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Component, OnInit } from '@angular/core';

import { Hero } from './hero';
import { HeroService } from './hero.service';

@Component({
// moduleId: module.id,
selector: 'my-dashboard',
templateUrl: './dashboard.component.html',
})

export class DashboardComponent implements OnInit {

heroes: Hero[] = [];

constructor(private heroService: HeroService) { }

ngOnInit(): void {
this.heroService.getHeroes()
.then(heroes => this.heroes = heroes.slice(1, 5));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div *ngIf="hero" class="row">
<div class="col-4">
<h2>Hero details</h2>
<p>
<img [src]="hero.image" alt="{{hero.name}}">
</p>
<div><label>id: </label> {{hero.id}}</div>
<div><label>name: </label> {{hero.name}}</div>
<div><label>age: </label> {{hero.age}}</div>
<div><label>skill: </label> {{hero.skill}}</div>
<div>
<button type="button" (click)="allowEdit(true)" class="btn btn-info">Edit</button> <button type="button" (click)="delete(hero); $event.stopPropagation()" class="btn btn-danger">Delete</button>
</div>
</div>
<div class="col-4">
<my-hero-edit [hero]="hero" [canEdit]="canEdit" (goBack)="goBack()" (allowEdit)="allowEdit($event)"></my-hero-edit>
</div>
</div>
<br/>
<button class="btn btn-secondary" (click)="goBack()">Back</button>
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
import { Component, Input, OnChanges } from "@angular/core";
import { Component, Input, OnInit, OnChanges } from "@angular/core";
import { Hero } from "./hero";
import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';
import { HeroService } from './hero.service';
import 'rxjs/add/operator/switchMap';

@Component({
// moduleId: module.id,
selector: "my-hero-detail",
template: `
<div *ngIf="hero" class="row">
<div class="col-6">
<h2>Hero details</h2>
<p>
<img [src]="hero.image" alt="{{hero.name}}">
</p>
<div><label>id: </label> {{hero.id}}</div>
<div><label>name: </label> {{hero.name}}</div>
<div><label>age: </label> {{hero.age}}</div>
<div><label>skill: </label> {{hero.skill}}</div>
<div>
<button type="button" (click)="allowEdit(true)" class="btn btn-info">Edit</button>
</div>
</div>
<div class="col-6">
<my-hero-edit [hero]="hero" [canEdit]="canEdit" (allowEdit)="allowEdit($event)"></my-hero-edit>
</div>
</div>
`
templateUrl: "./hero-detail.component.html"
})

export class HeroDetailComponent {
export class HeroDetailComponent implements OnInit {

constructor(
private heroService: HeroService,
private route: ActivatedRoute,
private location: Location
) {}

ngOnInit(): void {
this.route.params
.switchMap((params: Params) => this.heroService.getHero(+params['id']))
.subscribe(hero => this.hero = hero);
}

@Input()
hero: Hero;
canEdit: boolean = false;
Expand All @@ -38,4 +37,8 @@ export class HeroDetailComponent {
this.canEdit = condition;
}

goBack(): void {
this.location.back();
}

}
Loading