Saves and receives data

This commit is contained in:
Julien Lengrand-Lambert
2022-05-24 14:08:41 +02:00
parent 34330d76bb
commit a5551cbc99
2 changed files with 51 additions and 3 deletions

View File

@@ -15,7 +15,11 @@ import {
onAuthStateChanged onAuthStateChanged
} from 'firebase/auth'; } from 'firebase/auth';
import { getFirestore, collection, Firestore, onSnapshot, addDoc } from "firebase/firestore";
import { FIREBASE_CONFIG } from './constants.js'; import { FIREBASE_CONFIG } from './constants.js';
import { IPerson } from './models/people.js';
@customElement('web-brain') @customElement('web-brain')
export class WebBrain extends LitElement { export class WebBrain extends LitElement {
@@ -23,24 +27,43 @@ export class WebBrain extends LitElement {
@property({ type: String }) search = ''; @property({ type: String }) search = '';
@property({ type: Object }) newPerson: IPerson | null = null;
@property({ type: Object }) user: User | null = null; @property({ type: Object }) user: User | null = null;
@property({type: Array}) persons : Array<IPerson> | null = null;
public firebaseApp: FirebaseApp; public firebaseApp: FirebaseApp;
private auth: Auth; private auth: Auth;
private firestore : Firestore;
private provider: AuthProvider = new GoogleAuthProvider(); private provider: AuthProvider = new GoogleAuthProvider();
private peopleCollection;
constructor() { constructor() {
super(); super();
this.firebaseApp = initializeApp(FIREBASE_CONFIG); this.firebaseApp = initializeApp(FIREBASE_CONFIG);
this.firestore = getFirestore();
this.auth = getAuth(); this.auth = getAuth();
this.peopleCollection = collection(this.firestore, "people");
onAuthStateChanged(this.auth, (user) => { onAuthStateChanged(this.auth, (user) => {
console.log(`onAuthStateChanged${user}`); console.log(`onAuthStateChanged${user}`);
if (user) { this.user = user;} if (user) { this.user = user;}
}); });
const unsub = onSnapshot(this.peopleCollection, (querySnapshot) => {
// const cities = [];
querySnapshot.forEach((doc) => {
console.log(doc.data());
});
});
} }
static styles = css``; static styles = css``;
@@ -68,6 +91,19 @@ export class WebBrain extends LitElement {
} }
} }
async savePerson() {
console.log(`saving ${JSON.stringify(this.newPerson)}`);
try {
const docRef = await addDoc(this.peopleCollection, this.newPerson);
console.log("Document written with ID: ", docRef.id);
} catch (e) {
console.error("Error adding document: ", e);
}
this.newPerson = null;
}
render() { render() {
return html` return html`
<h1>${this.title}</h1> <h1>${this.title}</h1>
@@ -78,12 +114,10 @@ export class WebBrain extends LitElement {
? html` ? html`
<p>Logged in as ${this.user.email}</p> <p>Logged in as ${this.user.email}</p>
<button <button
label="Logout"
@click="${this.logout}" @click="${this.logout}"
>Logout</button> >Logout</button>
` `
: html`<button : html`<button
label="LogIn with Google"
@click="${this.logIn}" @click="${this.logIn}"
>LogIn with Google!</button>`} >LogIn with Google!</button>`}
</div> </div>
@@ -99,6 +133,21 @@ export class WebBrain extends LitElement {
/> />
</div> </div>
<!-- Adding people -->
<div>
Adding a new person
<input
type="text"
@input="${(e: any) => {
const p : IPerson = { name: e.target.value }
this.newPerson = p;
}}"
/>
<button
@click="${this.savePerson}"
>Save new person!</button>
</div>
<!-- Content --> <!-- Content -->
<main>The content will come here</main> <main>The content will come here</main>

View File

@@ -1,5 +1,4 @@
export interface IPerson { export interface IPerson {
id?: string | null,
name: string, name: string,
createdAt?: Date | null, createdAt?: Date | null,
updatedAt?: Date | null, updatedAt?: Date | null,