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

Feature/sample backend communication #12

Merged
merged 3 commits into from
Nov 14, 2019
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
4 changes: 2 additions & 2 deletions {{cookiecutter.slug}}/frontend.angular/package.overwrite.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"build:{{code}}": "ng build --configuration=production-{{code}}",
{%- endfor %}
"i18n": "ng xi18n --output-path locale",
"serve": "ng serve --liveReload=false --proxy-config ../proxy.conf.json",
"serve": "ng serve --proxy-config ../proxy.conf.json",
"start": "yarn serve",
{% set localizations = cookiecutter.localizations.split(',') %}
{%- for loc in localizations %}
{%- set code, name = loc.split(':') %}
"serve:{{code}}": "ng serve --liveReload=false --proxy-config ../proxy.conf.json --configuration={{code}}",
"serve:{{code}}": "ng serve --proxy-config ../proxy.conf.json --configuration={{code}}",
{%- endfor %}
"test": "ng test --watch=true",
"test-once": "ng test --watch=false",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<h1 class="title">Home</h1>
<p>NOICE! It works!</p>
<figure class="image" style="width: 480px;height: 401px;">
<img src="https://media.giphy.com/media/yoJC2GnSClbPOkV0eA/source.gif" />
{% raw -%}
<img src="{{hooray}}" alt="Although? If you are seeing this (and not a happy GIF), something is wrong... :("/>
{%- endraw %}
</figure>
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { Component, OnInit } from '@angular/core';
import { BackendService } from './../services/backend.service';

@Component({
selector: '{{cookiecutter.app_prefix}}-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
hooray: string;

constructor() { }
constructor(private backend: BackendService) { }

ngOnInit() {
}
ngOnInit() {
// This is just an example call to /api/whatever
// Note that the backend service doesn't call the backend yet
// and simply returns some mock data
this.backend.get('whatever').then(hoorays => {
if (hoorays.length) {
this.hooray = hoorays[0].message;
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';

import { BackendService } from './backend.service';

describe('BackendService', () => {
beforeEach(() => TestBed.configureTestingModule({}));

it('should be created', () => {
const service: BackendService = TestBed.get(BackendService);
expect(service).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ConfigService } from './config.service';


@Injectable({
providedIn: 'root'
})
export class BackendService {
private apiUrl: Promise<string> | null = null;

constructor(private config: ConfigService, private http: HttpClient) { }

/**
* Collect JSON from an specific url.
* @param objectUrl The part of the URL after the backendUrl from config.json.
* (i.e. whatever comes after, for example, '/api/')
*/
get(objectUrl: string): Promise<any> {
return this.getApiUrl().then(baseUrl => {
if (!objectUrl.endsWith('/')) { objectUrl = `${objectUrl}/`; }
const url: string = encodeURI(baseUrl + objectUrl);

// TODO: remove this part and enable below to actually contact the backend
return Promise.resolve([{
message: 'https://media.giphy.com/media/yoJC2GnSClbPOkV0eA/source.gif'
}]);

// return this.http.get(url)
// .toPromise()
// .then(response => {
// return response;
// })
// .catch(this.handleError);
});
}

getApiUrl(): Promise<string> {
if (!this.apiUrl) {
return this.config.get().then(config => config.backendUrl);
} else {
return Promise.resolve(this.apiUrl);
}
}

private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';

import { ConfigService } from './config.service';

describe('ConfigService', () => {
beforeEach(() => TestBed.configureTestingModule({}));

it('should be created', () => {
const service: ConfigService = TestBed.get(ConfigService);
expect(service).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

export interface Config {
backendUrl: string;
}

@Injectable({
providedIn: 'root'
})
export class ConfigService {
private config: Promise<any>;

constructor(private http: HttpClient) { }

public get(): Promise<Config> {
if (!this.config) {
this.config = new Promise<Config>((resolve, reject) =>
this.http.get('/assets/config.json').subscribe(response => {
resolve(response as Config);
}));
}

return this.config;
}
}
3 changes: 3 additions & 0 deletions {{cookiecutter.slug}}/frontend.angular/src/assets/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"backendUrl": "/api/"
}