17 | {firebase.app.length > 0 ?
: null}
18 | {firebase.app.length > 0 && LoginState.user ?
19 |
20 | -
21 | {LoginState.user ? : null}
22 |
23 | -
24 | {LoginState.user ? : null}
25 |
26 |
27 | : null}
28 |
29 | );
30 | }
31 | }
32 |
33 |
34 |
--------------------------------------------------------------------------------
/src/components/database/DatabaseState.ts:
--------------------------------------------------------------------------------
1 | import { observable } from "mobx";
2 |
3 | /**
4 | * Sample Database state
5 | */
6 | export class DatabaseStore {
7 |
8 | @observable public rows: any;
9 | public key: string;
10 | public val: string;
11 |
12 | }
13 |
14 | export const DatabaseState = new DatabaseStore();
--------------------------------------------------------------------------------
/src/components/database/DatabaseView.tsx:
--------------------------------------------------------------------------------
1 | import * as React from "react";
2 | import { observer } from "mobx-react";
3 | import { Messages } from "../../services/Messages";
4 | import { DatabaseState } from "./DatabaseState";
5 | import * as firebase from "firebase/app";
6 | import "firebase/firestore";
7 | import { LoginState } from "../login/LoginState";
8 |
9 | /**
10 | * Sample Database view
11 | */
12 |
13 | const colectionName = "shakeout-tests";
14 | const docName = "rows";
15 |
16 | @observer
17 | export default class DatabaseView extends React.Component<{}, {}> {
18 |
19 | private key: string;
20 | private val: string;
21 |
22 | constructor(props: any) {
23 | super(props);
24 | this.getValues();
25 | }
26 |
27 | /**
28 | * Gets the "shakeout-tests" collection and "rows" object from firestore
29 | */
30 | private async deleteValues() {
31 |
32 | firebase.firestore()
33 | .collection(colectionName)
34 | .doc(docName).delete();
35 |
36 | }
37 |
38 | /**
39 | * Gets the "shakeout-tests" collection and "rows" object from firestore
40 | */
41 | private async getValues() {
42 |
43 | const unsubscribe = firebase.firestore()
44 | .collection(colectionName)
45 | .doc(docName)
46 | .onSnapshot(snapShot => DatabaseState.rows = snapShot.data());
47 |
48 | //save the unsub function and execute it before logging out
49 | LoginState.subscriptions.push(unsubscribe);
50 |
51 | }
52 |
53 |
54 | /**
55 | * Updates the "rows" object in the collection "shakeout-tests"
56 | */
57 | private async setValue() {
58 | const update = {};
59 | update[this.key] = this.val;
60 | firebase.firestore()
61 | .collection(colectionName).doc(docName)
62 | .set(update, { merge: true });
63 | }
64 |
65 | private onAddButtonClicked() {
66 |
67 | if (this.key && this.val) {
68 | this.setValue();
69 | }
70 |
71 | }
72 |
73 | private onKeyChange(evt: any) {
74 |
75 | this.key = evt.target.value;
76 |
77 | }
78 |
79 | private onValChange(evt: any) {
80 |
81 | this.val = evt.target.value;
82 |
83 | }
84 |
85 | render() {
86 |
87 | const rows = [];
88 | for (let key in DatabaseState.rows) {
89 | rows.push(