Skip to content

Commit

Permalink
add chat application
Browse files Browse the repository at this point in the history
  • Loading branch information
okadamari committed Mar 13, 2021
1 parent c447389 commit 888cb11
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"serve": "vue-cli-service serve --mode local",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
Expand Down
66 changes: 60 additions & 6 deletions src/App.vue
@@ -1,15 +1,69 @@
<template>
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
<div id="app">
<label>名前</label>
<input type="text" v-model="userName" />

<label>メッセージ</label>
<input type="text" v-model="message" />
<button @click="sendMessage">送信</button>
<ul>
<li v-for="(value, key, index) in messageList" v-bind:key="index">
{{ value.user_name }}
<span style="margin-left:100px;">{{ value.message}}</span>
</li>
</ul>
</div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
import firebase from 'firebase'
let messageRef
export default {
name: 'App',
components: {
HelloWorld
name: 'app',
data() {
return {
messageList: [],
userName: 'ユーザーA',
message: 'テストメッセージです'
};
},
created: function() {
var firebaseConfig = {
apiKey: process.env.VUE_APP_API_KEY,
authDomain: process.env.VUE_APP_AUTH_DOMAIN,
projectId: process.env.VUE_APP_PROJECT_ID,
storageBucket: process.env.VUE_APP_STORAGE_BUCKET,
messagingSenderId: process.env.VUE_APP_MESSAGING_SENDER_ID,
appId: process.env.VUE_APP_APP_ID,
measurementId: process.env.VUE_APP_MEASUREMENT_ID,
};
console.log(firebaseConfig);
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore()
messageRef = db.collection("chat_messages")
let messageList = this.messageList
messageRef.orderBy('created', 'desc').onSnapshot(function(qs) {
messageList.length = 0;
qs.forEach(result => {
messageList.push(result.data())
})
});
},
methods: {
sendMessage: function() {
messageRef.add(
{
user_name: this.userName,
message: this.message,
created: new Date().getTime()
}
)
}
}
}
</script>
Expand Down

0 comments on commit 888cb11

Please sign in to comment.