Skip to content

Instantly share code, notes, and snippets.

View nerdic-coder's full-sized avatar
💭
Block Photos 2.0 will be out soon and is going to be great!

Johan Axelsson nerdic-coder

💭
Block Photos 2.0 will be out soon and is going to be great!
View GitHub Profile
@nerdic-coder
nerdic-coder / deleteHeroMethod.ts
Created May 20, 2018 20:27
deleteHeroMethod.ts
delete(hero: Hero): void {
this.heroes = this.heroes.filter(h => h !== hero);
this.heroService.deleteHero(hero).subscribe();
}
@nerdic-coder
nerdic-coder / displayhero.html
Created May 16, 2018 20:58
displayhero.html
<div>
{this.hero}
</div>
@nerdic-coder
nerdic-coder / herostyle.html
Created May 16, 2018 20:56
herostyle.html
<style>
/* Application-wide Styles */
h1 {
font-family: Arial, Helvetica, sans-serif;
font-size: 250%;
}
h2, h3 {
color: #444;
font-family: Arial, Helvetica, sans-serif;
@nerdic-coder
nerdic-coder / deleteHeroService.ts
Last active May 13, 2018 15:41
deleteHeroService.ts
/** DELETE: delete the hero from the server */
deleteHero (hero: Hero | number): Observable<Hero> {
return Observable.create((observer) => {
const id = typeof hero === 'number' ? hero : hero.id;
const xhr = new XMLHttpRequest();
xhr.open('DELETE', CONFIG.SERVER_URL + `heroes/${id}`, true);
xhr.onload = () => {
if (xhr.status === 200) {
this.messageService.add(`HeroService: deleted hero with id:${id}`);
observer.next(JSON.parse(xhr.responseText));
@nerdic-coder
nerdic-coder / addHeroForm.html
Created May 12, 2018 19:37
addHeroForm.html
<div>
<label>Hero name:
<input type="text" value={this.hero.name} onInput={(event) => this.handleChangeName(event)} placeholder="name" />
</label>
<button onClick={() => this.add()}>
add
</button>
</div>
handleChangeName(event) {
this.hero = {
id: this.hero.id,
name: event.target.value
};
}
add(): void {
this.hero.name = this.hero.name.trim();
if (!this.hero.name) { return; }
@nerdic-coder
nerdic-coder / addHeroServce.ts
Created May 12, 2018 10:42
addHeroServce.ts
/** POST: add a new hero to the server */
addHero (hero: Hero): Observable<Hero> {
return Observable.create((observer) => {
const xhr = new XMLHttpRequest();
xhr.open('POST', CONFIG.SERVER_URL + `heroes`, true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
xhr.onload = () => {
if (xhr.status === 201) {
this.messageService.add(`HeroService: added new hero`);
observer.next(JSON.parse(xhr.responseText));
save(): void {
this.heroService.updateHero(this.hero)
.subscribe(() => this.goBack());
}
@nerdic-coder
nerdic-coder / updateHero.ts
Created May 12, 2018 09:40
updateHero.ts
/** PUT: update the hero on the server */
updateHero(hero: Hero): Observable<any> {
return Observable.create((observer) => {
const xhr = new XMLHttpRequest();
xhr.open('PUT', CONFIG.SERVER_URL + `heroes/${hero.id}`, true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
xhr.onload = () => {
if (xhr.status === 200) {
this.messageService.add(`HeroService: updated hero with id:${hero.id}`);
observer.next(JSON.parse(xhr.responseText));
@nerdic-coder
nerdic-coder / getHeroHttp.ts
Created May 12, 2018 08:32
getHeroHttp.ts
getHero(id: number): Observable<Hero> {
return Observable.create((observer) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', CONFIG.SERVER_URL + `heroes/${id}`);
xhr.onload = () => {
if (xhr.status === 200) {
this.messageService.add(`HeroService: fetched hero with id:${id}`);
observer.next(JSON.parse(xhr.responseText));
}
else {