Add functionality to load a single event; Modify share api to redirect to correct url based on environment

Transactions
= 2017-09-14 18:43:02 -06:00
parent 8a4cf9816e
commit cdc9836450
6 changed files with 64 additions and 25 deletions

View File

@ -94,6 +94,10 @@ const Routes =
{ {
path: 'events', path: 'events',
component: EventsPageComponent component: EventsPageComponent
},
{
path: 'events/:id',
component: EventsPageComponent
} }
] ]

View File

@ -1,4 +1,5 @@
import { MdDialog, MdDialogConfig, MdSnackBar } from '@angular/material'; import { MdDialog, MdDialogConfig, MdSnackBar } from '@angular/material';
import { ActivatedRoute, Params } from '@angular/router';
import { EventService } from './../../services/event.service'; import { EventService } from './../../services/event.service';
import { AddEventPopupComponent } from './../popups/add-event-popup/add-event-popup.component'; import { AddEventPopupComponent } from './../popups/add-event-popup/add-event-popup.component';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
@ -22,14 +23,24 @@ export class EventsPageComponent implements OnInit {
constructor(private loginService: LoginService, constructor(private loginService: LoginService,
private eventService: EventService, private eventService: EventService,
private dialog: MdDialog, private dialog: MdDialog,
private snackbar: MdSnackBar) { private snackbar: MdSnackBar,
private activatedRoute: ActivatedRoute) {
this.loginService.isLoggedIn(true).subscribe(is => { this.loggedIn = is }); this.loginService.isLoggedIn(true).subscribe(is => { this.loggedIn = is });
this.loginService.onLogin().subscribe(is => { this.loggedIn = is.isLoggedIn; }); this.loginService.onLogin().subscribe(is => { this.loggedIn = is.isLoggedIn; });
} }
ngOnInit() { ngOnInit() {
this.activatedRoute.params
.subscribe((params: Params) =>{
let id = +params['id'];
id = Number.isNaN(id) ? -1 : id;
if (id > -1){
this.getSingleEvent(id);
} else {
this.getEvents(true); this.getEvents(true);
} }
});
}
getEvents(clearExisting: boolean): void{ getEvents(clearExisting: boolean): void{
this.loading = true; this.loading = true;
@ -47,6 +58,15 @@ export class EventsPageComponent implements OnInit {
); );
} }
getSingleEvent(id: Number): void{
this.events = [];
this.loading = true;
this.eventService.getSingleEvent(id).subscribe(event => {
this.events.push(event);
this.loading = false;
});
}
addEvents(events: Event[]): void{ addEvents(events: Event[]): void{
for(let i = 0; i < events.length; i++){ for(let i = 0; i < events.length; i++){
this.events.push(events[i]); this.events.push(events[i]);

View File

@ -19,10 +19,10 @@ export class UpcomingEventsComponent implements OnInit {
} }
ngOnInit(): void { ngOnInit(): void {
this.getSermons(); this.getEvents();
} }
getSermons(): void{ getEvents(): void{
this.events = []; this.events = [];
this.loading = true; this.loading = true;
this.eventService.getEvents(1).subscribe(events => { this.eventService.getEvents(1).subscribe(events => {
@ -31,4 +31,6 @@ export class UpcomingEventsComponent implements OnInit {
console.log(this.events) console.log(this.events)
}); });
} }
} }

View File

@ -1,4 +1,5 @@
export const EVENTS_ADD_URL = "/api2/events/a/"; export const EVENTS_ADD_URL = "/api2/events/a/";
export const EVENT_BY_ID = "/api2/events/";
export const EVENTS_BY_PAGE_URL = "/api2/events/page/"; export const EVENTS_BY_PAGE_URL = "/api2/events/page/";
export const SERMONS_BY_ID = '/api2/sermons/'; export const SERMONS_BY_ID = '/api2/sermons/';
export const SERMONS_BY_PAGE_URL = '/api2/sermons/page/'; export const SERMONS_BY_PAGE_URL = '/api2/sermons/page/';

View File

@ -3,7 +3,9 @@ import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { Event } from '../interfaces/event'; import { Event } from '../interfaces/event';
import { EVENTS_BY_PAGE_URL, EVENTS_ADD_URL } from '../constants/urls'; import { EVENTS_BY_PAGE_URL,
EVENT_BY_ID,
EVENTS_ADD_URL } from '../constants/urls';
@Injectable() @Injectable()
export class EventService { export class EventService {
@ -14,7 +16,12 @@ export class EventService {
getEvents(page: number): Observable<Event[]> { getEvents(page: number): Observable<Event[]> {
let url = EVENTS_BY_PAGE_URL + page + "?pageSize=" + 10; let url = EVENTS_BY_PAGE_URL + page + "?pageSize=" + 10;
return this.http.get(url).map(this.gotData).catch(this.dataError) return this.http.get(url).map(this.gotData,this).catch(this.dataError)
};
getSingleEvent(id: Number): Observable<Event>{
let url = EVENT_BY_ID + id;
return this.http.get(url).map(this.gotDataSingle,this).catch(this.dataError);
}; };
addEvent(event: Event){ addEvent(event: Event){
@ -29,19 +36,29 @@ export class EventService {
.catch(this.dataError); .catch(this.dataError);
} }
gotData(res: Response){ gotDataSingle(res: Response){
//if (1 == 1) return MOCK;
let body = res.json(); let body = res.json();
body = body.events.map(e => { return { return this.toEvent(body.event);
id: e.id, }
title: e.title,
startDate: new Date(e.startDate), gotData(res: Response){
endDate: new Date(e.endDate), let body = res.json();
description: e.description body = body.events.map(e => {
}}); return this.toEvent(e);
});
return body || { }; return body || { };
} }
toEvent(json: any): Event{
return {
id: json.id,
title: json.title,
startDate: new Date(json.startDate),
endDate: new Date(json.endDate),
description: json.description
}
}
dataError(error: Response | any){ dataError(error: Response | any){
let errMsg: string; let errMsg: string;
if (error instanceof Response) { if (error instanceof Response) {
@ -56,10 +73,3 @@ export class EventService {
} }
} }
const MOCK: Event[] = [
{ id:0, title: 'Missionary', startDate: new Date('2017-04-30T18:00:00'), endDate: null, description: 'Missionary from Africa will be with us' }
,{ id:1, title: 'Door 2 Door', startDate: new Date('2017-04-22T10:00:00'), endDate: null, description: 'Handing out Tracts' }
,{ id:2, title: 'Banquet', startDate: new Date('2017-06-20T18:00:00'), endDate: null, description: 'Food and Fun at the Banquet' }
,{ id:3, title: 'Bible Study', startDate: new Date('2017-08-15T18:00:00'), endDate: null, description: 'Special Bible Study' }
,{ id:4, title: 'Camp', startDate: new Date('2017-07-20T18:00:00'), endDate: new Date('2017-07-25T08:00:00'), description: 'Summer Camp for grades 4 - 9' }
];

View File

@ -39,7 +39,8 @@ function getSermon(req, res, id){
referrer.startsWith('http://opengraphcheck.com')) { referrer.startsWith('http://opengraphcheck.com')) {
getSermonMeta(req, res, id); getSermonMeta(req, res, id);
} else { } else {
res.redirect("https://ofbbutte.com/sermons/" + id); var url = req.protocol + "://" + req.hostname + "/sermons/" + id;
res.redirect(url);
} }
} }
@ -89,7 +90,8 @@ function getEvent(req, res, id){
referrer.startsWith('http://opengraphcheck.com'))) { referrer.startsWith('http://opengraphcheck.com'))) {
getEventMeta(req, res, id); getEventMeta(req, res, id);
} else { } else {
res.redirect("https://ofbbutte.com/events/" + id); var url = req.protocol + "://" + req.hostname + "/events/" + id;
res.redirect(url);
} }
} }