-
Notifications
You must be signed in to change notification settings - Fork 4
/
my-component.tsx
86 lines (73 loc) · 1.87 KB
/
my-component.tsx
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
75
76
77
78
79
80
81
82
83
84
85
86
/// <reference types="firebase" />
declare var firebase: firebase.app.App;
import { Component, State } from '@stencil/core';
import { authState } from 'rxfire/auth';
import { collectionData } from 'rxfire/firestore';
import { switchMap } from 'rxjs/operators';
@Component({
tag: 'my-component',
styleUrl: 'my-component.css',
shadow: true
})
export class MyComponent {
@State()
todos;
@State()
user;
ref = firebase.firestore().collection('todos');
componentWillLoad() {
authState(firebase.auth()).subscribe(u => (this.user = u));
// Get associated user todos
authState(firebase.auth())
.pipe(
switchMap(user => {
// Define the query
if (user) {
const query = this.ref.where('user', '==', user.uid);
return collectionData(query, 'taskId');
} else {
return [];
}
})
)
.subscribe(docs => (this.todos = docs));
}
login() {
var provider = new (firebase.auth as any).GoogleAuthProvider();
firebase.auth().signInWithPopup(provider);
}
logout() {
firebase.auth().signOut();
}
addTask(user) {
this.ref.add({ user: user.uid, task: 'blank task' });
}
removeTask(id) {
this.ref.doc(id).delete();
}
render() {
if (this.user) {
return (
<div>
You're logged in as {this.user.displayName}
<button onClick={this.logout}>Logout</button>
<hr />
<ul>
{this.todos.map(todo => (
<li onClick={() => this.removeTask(todo.taskId)}>
Task ID: {todo.taskId}
</li>
))}
</ul>
<button onClick={() => this.addTask(this.user)}>Add Task</button>
</div>
);
} else {
return (
<div>
<button onClick={this.login}>Login with Google</button>
</div>
);
}
}
}