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

Graphql integration #7

Open
wants to merge 2 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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"@angular/platform-browser-dynamic": "2.2.1",
"@angular/platform-server": "2.2.1",
"@ionic/storage": "1.1.8",
"apollo-angular": "^0.11.0",
"apollo-client": "^0.8.7",
"apollo-client-rxjs": "^0.5.1",
"graphql-tag": "^1.2.4",
"ionic-angular": "2.0.1",
"ionic-native": "2.5.1",
"ionicons": "3.0.0",
Expand Down
22 changes: 18 additions & 4 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import { AppConfig } from './app.config';
import { ApolloClient, createNetworkInterface } from 'apollo-client';
import { ApolloModule } from 'apollo-angular';
import { BookingPage } from '../pages/booking/booking';
import { BookingService } from '../services/booking.service';
import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';


// by default, this client will send queries to `/graphql` (relative to the URL of your app)
const client = new ApolloClient({
networkInterface: createNetworkInterface({
uri: AppConfig.apiEndpoint
}),
});

export function provideClient(): ApolloClient {
return client;
}

@NgModule({
declarations: [
MyApp,
HomePage,
BookingPage
],
imports: [
IonicModule.forRoot(MyApp)
IonicModule.forRoot(MyApp),
ApolloModule.forRoot(provideClient)
],
bootstrap: [IonicApp],
entryComponents: [
Expand All @@ -23,9 +38,8 @@ import { HomePage } from '../pages/home/home';
providers: [
{
provide: ErrorHandler,
useClass: IonicErrorHandler
useClass: IonicErrorHandler,
},
BookingService
]
})
export class AppModule { }
4 changes: 2 additions & 2 deletions src/pages/home/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

<!--Refresher (slide up to refresh)-->
<ion-refresher (ionRefresh)="doRefresh($event)">
<ion-refresher-content
<ion-refresher-content
pullingIcon="arrow-dropdown"
pullingText="Pull to refresh rooms list"
refreshingSpinner="crescent"
refreshingText="">
refreshingText="Update in progress">
</ion-refresher-content>
</ion-refresher>

Expand Down
132 changes: 96 additions & 36 deletions src/pages/home/home.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import { Subject, Subscription } from 'rxjs/Rx';
import { Apollo, ApolloQueryObservable } from 'apollo-angular';
import { Room } from '../../models/room';
import { BookingService } from '../../services/booking.service';
import { Component, ViewChild } from '@angular/core';
import { Content, NavController, AlertController } from 'ionic-angular';
import { BookingPage } from '../booking/booking';
import { Vibration, BarcodeScanner } from 'ionic-native';

import gql from 'graphql-tag';

const floorMasterRoomNumberQuery = gql`
query AvailableRoomsQuery($roomNumber: Int!)
{ roomsOnFloor: rooms(floorMasterRoomNumber: $roomNumber) {
name
number
capacity
availability {
busy
availableFor
availableFrom
}
}
}`
;

interface QueryResponse {
roomsOnFloor: Room[];
}

@Component({
selector: 'page-home',
templateUrl: 'home.html'
Expand All @@ -13,43 +35,57 @@ import { Vibration, BarcodeScanner } from 'ionic-native';
export class HomePage {

@ViewChild(Content) content: Content;
roomsObs: ApolloQueryObservable<any>;
rooms: Room[] = [];
filteredRooms: Room[] = [];
availabilityFilter: string;
floorMasterRoomNumber: Subject<number> = new Subject<number>();
updateInProgress: boolean;
roomSelected: number;
roomsSubscription: Subscription;

private rooms: Room[];
public filteredRooms: Room[];
private availabilityFilter: string;
private roomSelected: number;
private updateInProgress: boolean;
constructor(
public navCtrl: NavController,
private apollo: Apollo,
private alertCtrl: AlertController
){}

constructor(public navCtrl: NavController, private bookingService: BookingService, private alertCtrl: AlertController) {
this.roomSelected = 412; // "magic" room number
this.updateRooms();
}

updateRooms() {
//Ionic's alternative to ngOnInit()
ionViewWillEnter() {
this.roomSelected = 412; //Magic room number - to be substituted with config
this.updateInProgress = true;
this.filteredRooms = null; //toggles "loading" spinner on UI
this.bookingService.getMeetingRooms(this.roomSelected).subscribe(
result => this.processData(result),
err => {
this.showError(err);
}

this.roomsObs = this.apollo.watchQuery<any>({
query: floorMasterRoomNumberQuery,
variables: {roomNumber: this.roomSelected}
});

this.roomsSubscription = this.roomsObs.subscribe(
res => {
this.processData(res)
this.updateInProgress = false;
},
err => this.rooms = []
);
}

//Ionic's alternative to ngOnInit()
ionViewDidLeave() {
this.roomsSubscription.unsubscribe();
}

processData(result: Room[]) {
this.rooms = result;
//Update rooms list, apply default filter, drop "sync in progress"
processData(res, refresher?) {
this.rooms = res.data.roomsOnFloor || [];
this.availabilityFilter = 'available';
this.updateInProgress = false;
this.showAvailableRooms();
//Vibration added to check integration with native functionality
try {
Vibration.vibrate(1000)
if (refresher !== undefined) {
refresher.complete();
}
catch(e) {
console.log('Vibration failed: '+ (<Error>e).message);
};
this.updateInProgress = false;
}


//Alert modal window
showError(err) {
let alert = this.alertCtrl.create({
title: 'Error',
Expand All @@ -70,13 +106,23 @@ export class HomePage {
this.content.scrollToTop();
}


//Refresh spinner
doRefresh(refresher) {
this.updateRooms();
refresher.complete();
this.rooms = [];
this.filteredRooms = [];
this.roomsObs.refetch()
.then(res => {
this.processData(res, refresher);
})
.catch(err => {
this.showError(err);
refresher.complete()
});
}

openBookingPage(room: Room) {
this.navCtrl.push(BookingPage, {room});
this.navCtrl.push(BookingPage, { room });
}

// Barcode scanning using native cordova plugin
Expand All @@ -88,12 +134,26 @@ export class HomePage {
let roomNumber = Number(url.substr(28, 3));
if (roomNumber > 0) {
this.roomSelected = roomNumber;
this.updateRooms();
this.roomsObs.refetch({roomNumber: roomNumber});
} else {
this.showError('Could not identify room number');
}
}
},
err => this.showError('Unknown error')
)}
},
err => {
this.showError('Unknown error');
}
)
}

//Vibration added to check integration with native functionality
vibrate() {
try {
Vibration.vibrate(1000)
}
catch (e) {
console.log('Vibration failed: ' + (<Error>e).message);
};
}

}
49 changes: 0 additions & 49 deletions src/services/booking.service.ts

This file was deleted.

Loading