Added messages processing

This commit is contained in:
Florian Sylvain
2023-02-22 09:28:12 +01:00
parent a1b8716bc2
commit c782a3b447
2 changed files with 20 additions and 8 deletions
-3
View File
@@ -1,3 +0,0 @@
{
"recommendations": ["Vue.volar", "Vue.vscode-typescript-vue-plugin"]
}
+20 -5
View File
@@ -5,8 +5,10 @@ import { db } from "@/utils/firestore"
import { import {
addDoc, addDoc,
collection, collection,
FieldValue,
onSnapshot, onSnapshot,
QuerySnapshot, QuerySnapshot,
serverTimestamp,
type DocumentData, type DocumentData,
} from "@firebase/firestore" } from "@firebase/firestore"
import { onMounted, ref, type Ref } from "vue" import { onMounted, ref, type Ref } from "vue"
@@ -27,12 +29,14 @@ const messagesPath = collection(
interface Message { interface Message {
author: string author: string
message: string message: string
timestamp: FieldValue
} }
async function postMessage(): Promise<void> { async function postMessage(): Promise<void> {
const message: Message = { const message: Message = {
author: sessionStore.nickname as string, author: sessionStore.nickname as string,
message: currentMessage.value, message: currentMessage.value,
timestamp: serverTimestamp(),
} }
const messageReference = await addDoc(messagesPath, message) const messageReference = await addDoc(messagesPath, message)
@@ -40,7 +44,18 @@ async function postMessage(): Promise<void> {
} }
function updateMessages(querSnap: QuerySnapshot<DocumentData>): void { function updateMessages(querSnap: QuerySnapshot<DocumentData>): void {
console.log(querSnap.docs) // TODO messages.value = querSnap.docs.map((x) =>
x.data({ serverTimestamps: "estimate" })
)
messages.value = messages.value.sort((a, b) => {
return a.timestamp.toDate() - b.timestamp.toDate()
})
}
async function onFormSubmit() {
await postMessage()
currentMessage.value = ""
} }
onMounted(() => { onMounted(() => {
@@ -55,13 +70,13 @@ onMounted(() => {
<template v-else> <template v-else>
<div class="messages"> <div class="messages">
<div> <div v-for="message of messages">
<h3>Author</h3> <h3>{{ message.author }}</h3>
<p>Author's message</p> <p>{{ message.message }}</p>
</div> </div>
</div> </div>
<form @submit.prevent="postMessage"> <form @submit.prevent="onFormSubmit">
<label for="message">Message</label> <label for="message">Message</label>
<input <input
type="text" type="text"