/
profile.ts
74 lines (64 loc) · 1.75 KB
/
profile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Component, OnInit } from '@angular/core';
import { AlertController, NavController } from 'ionic-angular';
import { MeteorObservable } from 'meteor-rxjs';
import { Pictures } from '../../../../imports/collections';
import { Profile } from '../../../../imports/models';
import { PictureService } from '../../services/picture';
import { ChatsPage } from '../chats/chats';
import template from './profile.html';
@Component({
template
})
export class ProfilePage implements OnInit {
picture: string;
profile: Profile;
constructor(
private alertCtrl: AlertController,
private navCtrl: NavController,
private pictureService: PictureService
) {}
ngOnInit(): void {
this.profile = Meteor.user().profile || {
name: ''
};
MeteorObservable.subscribe('user').subscribe(() => {
this.picture = Pictures.getPictureUrl(this.profile.pictureId);
});
}
selectProfilePicture(): void {
this.pictureService.select().then((blob) => {
this.uploadProfilePicture(blob);
})
.catch((e) => {
this.handleError(e);
});
}
uploadProfilePicture(blob: Blob): void {
this.pictureService.upload(blob).then((picture) => {
this.profile.pictureId = picture._id;
this.picture = picture.url;
})
.catch((e) => {
this.handleError(e);
});
}
updateProfile(): void {
MeteorObservable.call('updateProfile', this.profile).subscribe({
next: () => {
this.navCtrl.push(ChatsPage);
},
error: (e: Error) => {
this.handleError(e);
}
});
}
handleError(e: Error): void {
console.error(e);
const alert = this.alertCtrl.create({
title: 'Oops!',
message: e.message,
buttons: ['OK']
});
alert.present();
}
}