mirror of
https://github.com/Floriansylvain/SpaceMessenger.git
synced 2026-08-19 19:53:22 +02:00
Added messages processing
This commit is contained in:
Vendored
-3
@@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"recommendations": ["Vue.volar", "Vue.vscode-typescript-vue-plugin"]
|
|
||||||
}
|
|
||||||
+20
-5
@@ -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"
|
||||||
|
|||||||
Reference in New Issue
Block a user