Starts adding data updates

This commit is contained in:
Julien Lengrand-Lambert
2022-05-24 23:13:51 +02:00
parent a5551cbc99
commit f164642488
3 changed files with 26 additions and 15 deletions

View File

@@ -1,18 +1,17 @@
import { Firestore } from 'firebase/firestore';
import {ReactiveController, ReactiveControllerHost} from 'lit';
import { FirebaseApp, initializeApp } from "firebase/app";
import {FIREBASE_CONFIG} from "./constants.js";
export class FirebaseController implements ReactiveController {
export class FirestoreController implements ReactiveController {
host: ReactiveControllerHost;
value = [];
private firebaseApp: FirebaseApp;
constructor(host: ReactiveControllerHost) {
(this.host = host).addController(this);
this.firebaseApp = initializeApp(FIREBASE_CONFIG);
firestore: Firestore;
constructor(host: ReactiveControllerHost, firestore : Firestore) {
this.host = host;
host.addController(this);
this.firestore = firestore;
}
hostConnected() {

View File

@@ -31,7 +31,7 @@ export class WebBrain extends LitElement {
@property({ type: Object }) user: User | null = null;
@property({type: Array}) persons : Array<IPerson> | null = null;
@property({type: Object}) persons : Map<string, IPerson> = new Map();
public firebaseApp: FirebaseApp;
@@ -43,7 +43,6 @@ export class WebBrain extends LitElement {
private peopleCollection;
constructor() {
super();
this.firebaseApp = initializeApp(FIREBASE_CONFIG);
@@ -61,8 +60,10 @@ export class WebBrain extends LitElement {
const unsub = onSnapshot(this.peopleCollection, (querySnapshot) => {
// const cities = [];
querySnapshot.forEach((doc) => {
console.log(doc.data());
this.persons.set(doc.id, doc.data() as IPerson);
});
this.requestUpdate();
console.log('plop');
});
}
@@ -96,6 +97,7 @@ export class WebBrain extends LitElement {
try {
const docRef = await addDoc(this.peopleCollection, this.newPerson);
this.newPerson = null;
console.log("Document written with ID: ", docRef.id);
} catch (e) {
console.error("Error adding document: ", e);
@@ -139,7 +141,7 @@ export class WebBrain extends LitElement {
<input
type="text"
@input="${(e: any) => {
const p : IPerson = { name: e.target.value }
const p : IPerson = { name: e.target.value, owner: this.user!.uid }
this.newPerson = p;
}}"
/>
@@ -149,7 +151,16 @@ export class WebBrain extends LitElement {
</div>
<!-- Content -->
<main>The content will come here</main>
<main>
<h2>The content will come here</h2>
<ul>
${this.persons.forEach((person: IPerson, id: string) =>
html`<li>${id} aaa</li>`
)}
</ul>
</main>
<footer>Web brain, because you forget everything</footer>
`;

View File

@@ -2,4 +2,5 @@ export interface IPerson {
name: string,
createdAt?: Date | null,
updatedAt?: Date | null,
owner: string,
}