Basic user read

This commit is contained in:
Fergal Moran
2016-08-02 23:04:21 +01:00
parent aa38691023
commit 5d0838315e
2 changed files with 27 additions and 127 deletions

View File

@@ -12,30 +12,14 @@
</ion-navbar>
</ion-header>
<ion-content class="home">
<ion-item>
<ion-row>
<ion-col width-100>
<button (click)="addNewItemClicked()">Add Item</button>
</ion-col>
</ion-row>
<ion-list>
<ion-list-header>Nearby people</ion-list-header>
<ion-item *ngFor="let neighbour of neighbours | async">
<ion-avatar item-left>
<img [src]="neighbour.avatarImage" style="width:32px;height=32px;" alt="">
</ion-avatar>
<h2>{{neighbour.displayName}}</h2>
<p>{{neighbour.tagLine}}</p>
</ion-item>
<ion-card *ngFor="let item of textItems | async">
<ion-card-header>
{{item.title}}
</ion-card-header>
<ion-card-content>
{{item.description}} - {{ item.timestamp | MomentDate:"LLL"}}
</ion-card-content>
<ion-col offset-80>
<button (click)="deleteItemClicked(item)">Delete</button>
</ion-col>
</ion-card>
<ion-card *ngFor="let user of usersWithMessages | async">
<ion-card-header>
{{user.displayName}}
</ion-card-header>
<ion-card-content>
{{ (user.messages | async) | json}}
</ion-card-content>
</ion-card>
</ion-list>
</ion-content>

View File

@@ -1,127 +1,43 @@
import {Modal, NavController, Page} from 'ionic-angular';
import {Component, OnInit, Inject} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {LoginPage} from '../login/login'
import {NewItemModal} from '../item/newItem';
import {MomentDate} from '../../lib/MomentDate'
import 'rxjs';
import {Observable} from 'rxjs/Observable';
import { AngularFire } from 'angularfire2';
import {LoginPage} from '../login/login'
import {NeighboursService} from '../../services/neighbours.service';
import { AngularFire, AuthProviders, AuthMethods } from 'angularfire2';
@Page({
templateUrl: 'build/pages/home/home.html',
pipes: [MomentDate]
providers: [NeighboursService]
})
export class HomePage implements OnInit {
textItems: Observable<any[]>;
usersWithMessages: Observable<any[]>;
authInfo: any
displayName: any
buttonTitle = "LOGIN"
constructor(
public af: AngularFire,
public navCtrl: NavController) {
// dont do anything heavy here... do it in ngOnInit
neighbours: Observable<any[]>;
constructor(public navCtrl: NavController, private _af: AngularFire,
private _neighboursService: NeighboursService) {
}
ngOnInit() {
// subscribe to the auth object to check for the login status
// of the user, if logged in, save some user information and
// execute the firebase query...
// .. otherwise
// show the login modal page
this.af.auth.subscribe((data) => {
console.log("in auth subscribe", data)
this._af.auth.subscribe((data) => {
if (data) {
this.af.auth.unsubscribe()
this.buttonTitle = "LOGOUT"
// if no user, then add it
this.addOrUpdateUser(data)
if (data.auth.providerData[0].providerId === "twitter.com") {
this.authInfo = data.auth.providerData[0]
this.displayName = data.auth.providerData[0].displayName
} else if (data.github) {
this.authInfo = data.github
//this.authInfo.displayName = data.github.displayName
this._af.auth.unsubscribe();
} else {
this.authInfo = data.auth || {}
this.displayName = data.auth.providerData[0].email
this._displayLoginModal();
}
this.textItems = this.af.database.list('/textItems')
//this.getMoreData()
} else {
this.buttonTitle = "LOGIN"
this.authInfo = null
this.displayLoginModal()
}
})
this.neighbours = this._neighboursService.getNeighbours();
});
}
addOrUpdateUser(_authData) {
const itemObservable = this.af.database.object('/users/' + _authData.uid);
itemObservable.set({
"provider": _authData.auth.providerData[0].providerId,
"avatar": _authData.auth.photoURL || "MISSING",
"displayName": _authData.auth.providerData[0].displayName || _authData.auth.email,
})
}
getMoreData() {
this.usersWithMessages = this.af.list('/users').map((_users) => {
return _users.map((_user) => {
_user.messages = this.af.object("/userObjects/public-messages/" + _user.$key)
return _user
})
})
registerMe() {
this._neighboursService.registerMe();
}
/**
* displays the login window
*/
displayLoginModal() {
_displayLoginModal() {
let loginPage = Modal.create(LoginPage);
this.navCtrl.present(loginPage);
}
/**
* adds a new item to firebase /textItems
*
* pass in the auth information to the modal to associate the user with the newly
* created entry
*/
addNewItemClicked(_data) {
let newItemPage = Modal.create(NewItemModal, { "user": this.authInfo });
this.navCtrl.present(newItemPage);
}
deleteItemClicked(_data) {
this.af.database.object("/textItems/" + _data.$key).remove()
.then(() => { alert("success") })
.catch((_error) => { alert("Error") })
}
/**
* logs out the current user
*/
logoutClicked() {
if (this.authInfo && (this.authInfo.email || this.authInfo.providerId)) {
this.af.auth.logout();
this.authInfo = null
this.displayLoginModal()
} else {
this.displayLoginModal()
}
}
}