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

Homework 9-10-11 #634

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 @@ -19,13 +19,15 @@
"@angular/platform-browser": "~2.4.0",
"@angular/platform-browser-dynamic": "~2.4.0",
"@angular/router": "~3.4.0",
"@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.20",
"angular-in-memory-web-api": "^0.2.4",
"core-js": "^2.4.1",
"rxjs": "5.0.1",
"zone.js": "^0.7.4"
},
"devDependencies": {
"@types/jasmine": "2.5.38",
"@types/node": "^6.0.45",
"@types/jasmine": "^2.5.35",
"angular2-template-loader": "^0.6.0",
"awesome-typescript-loader": "^3.0.0-beta.18",
"css-loader": "^0.26.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { HeroesComponent } from './components/heroes/heroes.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
import { HeroDetailComponent } from './components/hero-detail/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 {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,50 +1,34 @@
import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';

import { HeroesComponent } from './components/heroes/heroes.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';

@Component({
selector: 'tour-of-heroes',
templateUrl: './app.tour-of-heroes.html',
styleUrls: ['./app.tour-of-heroes.css'],
//moduleId: module.id,
selector: 'my-app',
template: `
<h1 class="my-3">{{title}}</h1>
<div class="col-xs-12">
<nav>
<a routerLink="/dashboard" routerLinkActive="active" class="btn btn-primary">Dashboard</a>
<a routerLink="/heroes" routerLinkActive="active" class="btn btn-primary">Heroes</a>
</nav>
</div>
<router-outlet></router-outlet>
`
})

export class AppComponent {
title = `Tour of Heroes`;
selectedHero: Hero;
heroes = HEROES;
badheroes = BADHEROES;

onSelect(hero: Hero):void {
this.selectedHero = hero;
}
title = 'Tour of Heroes';
}

const HEROES: Hero[] = [
{ id: 11, name: 'Mr. Nice', img: 'https://upload.wikimedia.org/wikipedia/en/7/79/Mr._Nice.jpg' },
{ id: 12, name: 'Narco', img: 'https://s-media-cache-ak0.pinimg.com/236x/cb/1e/21/cb1e217af38d3c8e6658ff0322fca52b.jpg' },
{ id: 13, name: 'Bombasto', img: 'https://hydra-media.cursecdn.com/solsticearena.gamepedia.com/thumb/2/2e/Portrait_bombast.png/225px-Portrait_bombast.png?version=5684639074fe5ec7f58083d0b9e1e04e'},
{ id: 14, name: 'Celeritas', img: 'http://www.cardiacathletes.com/images/superheroes/superhero-3.png' },
{ id: 15, name: 'Magneta', img: 'http://vignette4.wikia.nocookie.net/marveldatabase/images/f/fd/MC2_Magneta.png/revision/latest?cb=20120409045056' },
{ id: 16, name: 'RubberMan', img: 'http://images.buycostumes.com/mgen/merchandiser/802980.jpg?zm=1600,1600,1,0,0' },
{ id: 17, name: 'Dynama', img: 'http://orig10.deviantart.net/ad33/f/2008/275/7/6/dynamo_via_hero_creator_by_gwhitmore.png' },
{ id: 18, name: 'Dr IQ', img: 'http://dresdencodak.com/images/costume/drstrange' },
{ id: 19, name: 'Magma', img: 'http://1.bp.blogspot.com/-3kcOLFqmjfg/VHavDweTvqI/AAAAAAAABJ0/TWUql7iB-1o/s1600/279.png' },
{ id: 20, name: 'Tornado', img: 'http://i283.photobucket.com/albums/kk286/skarth7/Renders/GreatTornado.png' }
];

const BADHEROES: Hero[] = [
{ id: 211, name: 'Ultron', img: 'https://i.annihil.us/u/prod/marvel/i/mg/2/03/537bc76411307/standard_xlarge.jpg' },
{ id: 212, name: 'Loki', img: 'https://i.annihil.us/u/prod/marvel/i/mg/2/03/537bc76411307/standard_xlarge.jpg' },
{ id: 213, name: 'Red Skull' , img: 'https://i.annihil.us/u/prod/marvel/i/mg/2/03/526036550cd37/standard_xlarge.jpg'},
{ id: 214, name: 'Mystique', img: 'https://i.annihil.us/u/prod/marvel/i/mg/5/03/5390dc2225782/standard_xlarge.jpg' },
{ id: 215, name: 'Thanos', img: 'https://i.annihil.us/u/prod/marvel/i/mg/a/10/537bc68747ccf/standard_xlarge.jpg' },
{ id: 216, name: 'Ronan', img: 'https://i.annihil.us/u/prod/marvel/i/mg/2/f0/5260363fc40f2/standard_xlarge.jpg' },
{ id: 217, name: 'Magneto', img: 'https://i.annihil.us/u/prod/marvel/i/mg/3/b0/5261a7e53f827/standard_xlarge.jpg' },
{ id: 218, name: 'Dr. Doom', img: 'https://i.annihil.us/u/prod/marvel/i/mg/3/60/53176bb096d17/standard_xlarge.jpg' },
{ id: 219, name: 'Green Goblin', img: 'https://i.annihil.us/u/prod/marvel/i/mg/f/40/538cf0c2acdd9/standard_xlarge.jpg' },
{ id: 220, name: 'Black Cat', img: 'https://i.annihil.us/u/prod/marvel/i/mg/e/03/526952357d91c/standard_xlarge.jpg' }
];

export class Hero {
id: number;
name: string;
img: string;
}
RouterModule.forRoot([
{
path: 'heroes',
component: HeroesComponent
},
{
path: 'dashboard',
component: DashboardComponent
},
]);
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';

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

import { HeroService } from './services/hero.service';
import { InMemoryDataService } from './data/in-memory-data.service';

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

export class AppModule { }

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Master Styles */
h1 {
color: #369;
font-family: Arial, Helvetica, sans-serif;
font-size: 250%;
}
h2, h3 {
color: #444;
font-family: Arial, Helvetica, sans-serif;
font-weight: lighter;
}
body {
margin: 2em;
}
body, input[text], button {
color: #888;
font-family: Cambria, Georgia;
}
/* . . . */
/* everywhere else */
* {
font-family: Arial, Helvetica, sans-serif;
}
h1 {
font-size: 1.2em;
color: #999;
margin-bottom: 0;
}
h2 {
font-size: 2em;
margin-top: 0;
padding-top: 0;
}
nav a {
padding: 5px 10px;
text-decoration: none;
margin-top: 10px;
display: inline-block;
background-color: #eee;
border-radius: 4px;
}
nav a:visited, a:link {
color: #607D8B;
}
nav a:hover {
color: #039be5;
background-color: #CFD8DC;
}
nav a.active {
color: #039be5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="mt-4">
<h2>Top Heroes</h2>
<div class="card">
<div class="card-block">
<div class="row ">
<div class="col" *ngFor="let hero of heroes" [routerLink]="['/detail', hero.id]">
<div class="module hero">
<h4 class="card-link">{{hero.name}}</h4>
</div>
</div>
</div>
</div>
</div>
<div class="mt-4">
<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 '../../shared/hero';
import { HeroService } from '../../services/hero.service';

@Component({
//moduleId: module.id,
selector: 'my-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css'],
})
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,66 @@
import 'rxjs/add/operator/switchMap';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';
import { FormControl, FormGroup, FormBuilder, Validators } from '@angular/forms';

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

@Component({
//moduleId: module.id,
selector: 'my-hero-detail',
templateUrl: './hero-detail.my-hero-detail.html',
styleUrls: ['./hero-detail.my-hero-detail.css'],
})
export class HeroDetailComponent implements OnInit {
hero: Hero;
heroForm: FormGroup;

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

}

createForm() {
this.heroForm = this.fb.group({
id: ['', Validators.required],
name: ['', Validators.required],
img: '',
})
}

saveHero(){
this.heroService.update(this.heroForm.value)
.then(() => this.goBack());
}

ngOnInit(): void {

this.createForm();
this.route.params
.switchMap((params: Params) => this.heroService.getHero(+params['id']))
.subscribe(hero => {
this.hero = hero;
this.heroForm = this.fb.group({
id: [this.hero.id, Validators.required],
name: [this.hero.name, Validators.required],
img: this.hero.img,
})
});
}

save(): void {
console.log(this.hero);
this.heroService.update(this.hero)
.then(() => this.goBack());
}

goBack(): void {
this.location.back();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div *ngIf="hero">
<h2 >{{hero.name}} details!</h2>
<div><label>id: </label><input [(ngModel)]="hero.id" placeholder="id" class="form-control"></div>
<div><label>name: </label><input [(ngModel)]="hero.name" placeholder="name" class="form-control"></div>
<div><label>image: </label><input [(ngModel)]="hero.img" placeholder="img" class="form-control"></div>
<button (click)="goBack()" class="btn btn-primary mt-2">Back</button>
<button (click)="save()" class="btn btn-primary mt-2">Save</button>
</div>

<h2>Update using reactive Forms</h2>
<form (ngSubmit)="saveHero()" [formGroup]="heroForm" class="mt-2">
<div class="form-group">
<label>id: </label><input placeholder="id" class="form-control" formControlName="id">
</div>
<div class="form-group">
<label>name: </label><input placeholder="name" class="form-control" formControlName="name" required>
</div>
<div class="form-group">
<label>image url: </label><input placeholder="image-url" class="form-control" formControlName="img">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary mt-2">Save</button>
</div>
</form>
Loading