Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/6/getting-started/vuejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"serve-without-vuex": "cd without-vuex && vue-cli-service serve",
"build-without-vuex": "cd without-vuex && vue-cli-service build",
"lint-without-vuex": "cd without-vuex && vue-cli-service lint",
"serve-with-vuex": "cd with-vuex && vue-cli-service serve",
"build-with-vuex": "cd with-vuex && vue-cli-service build",
"lint-with-vuex": "cd with-vuex && vue-cli-service lint",
"test": "./node_modules/.bin/cypress run --record"
},
"dependencies": {
Expand Down
5 changes: 5 additions & 0 deletions doc/6/getting-started/vuejs/with-vuex/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
presets: [
'@vue/app'
]
}
Binary file not shown.
17 changes: 17 additions & 0 deletions doc/6/getting-started/vuejs/with-vuex/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title>kuzzle-vuejs-gs</title>
</head>
<body>
<noscript>
<strong>We're sorry but kuzzle-vuejs-gs doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
121 changes: 121 additions & 0 deletions doc/6/getting-started/vuejs/with-vuex/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<template>
<div id="app">
<div v-if="!enteredNickname">
<input
autofocus
v-on:keyup.enter="valid"
type="text"
v-model="nicknameInput"
placeholder="Enter your nickname"
/>
<button @click="valid">Valid</button>
</div>
<div class="wrapper" v-else>
<input
autofocus
type="text"
placeholder="Enter your message"
v-model="message"
v-on:keyup.enter="sendMessage"
/>
<button @click="sendMessage">Send</button>
</div>
<div
v-for="message in messages"
:key="message._id"
:class="`messages ${message.username === nicknameInput? 'fromMe': 'fromOthers'}`"
>
<span class="nickname">{{ message.username }}</span>
<span>({{ getDate(message.createdAt) }})</span>
<p>{{ message.value }}</p>
</div>
</div>
</template>

<script>
import kuzzle from "./services/kuzzle";

export default {
name: "app",
data() {
return {
enteredNickname: false,
nicknameInput: "",
message: ""
};
},
computed: {
nickname() {
return this.$store.state.nickname;
},
messages() {
return this.$store.state.messages;
}
},
methods: {
getDate(timestamp) {
const date = new Date(timestamp);
return date.toString().split("GMT")[0];
},
async sendMessage() {
if (this.message === "") return;
await this.$store.dispatch("SEND_MESSAGE", {
kuzzle,
message: this.message
});
this.message = "";
},
async valid() {
this.$store.commit("SET_NICKNAME", this.nicknameInput);
this.enteredNickname = true;
await this.$store.dispatch("INIT", { kuzzle });
}
},
async destroyed () {
await this.$store.dispatch("UNSUBSCRIBE_MESSAGES", { kuzzle });
}
};
</script>

<style>
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}

.nickname {
font-weight: bold;
}

.wrapper {
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 10px;
}

.messages {
padding: 10px;
margin: 1px;
width: 45vw;
border-radius: 10px;
}

.fromMe {
text-align: right;
float: right;
margin-left: 49vw;
background-color: #9ca4f0;
}

.fromOthers {
text-align: left;
margin-right: 49vw;
float: left;
background-color: rgb(206, 246, 147);
}
</style>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions doc/6/getting-started/vuejs/with-vuex/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Vue from 'vue'
import App from './App.vue'
import store from './store.js'
Vue.config.productionTip = false

new Vue({
store,
render: h => h(App),
}).$mount('#app')
3 changes: 3 additions & 0 deletions doc/6/getting-started/vuejs/with-vuex/src/services/kuzzle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Kuzzle, WebSocket } from 'kuzzle-sdk';

export default new Kuzzle(new WebSocket('localhost'));
71 changes: 71 additions & 0 deletions doc/6/getting-started/vuejs/with-vuex/src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
state: {
messages: [],
roomID: '',
nickname: ''
},
actions: {
INIT: async ({ dispatch }, { kuzzle }) => {
await kuzzle.connect();
if (!(await kuzzle.index.exists("chat"))) {
await kuzzle.index.create("chat");
await kuzzle.collection.create("chat", "messages");
}
dispatch('FETCH_MESSAGES', {kuzzle});
},
SEND_MESSAGE: async ({ state }, { kuzzle, message }) => {
await kuzzle.document.create("chat", "messages", {
value: message,
username: state.nickname
});
},
FETCH_MESSAGES: async ({ commit, dispatch }, { kuzzle }) => {
const results = await kuzzle.document.search(
"chat",
"messages",
{ sort: ["_kuzzle_info.createdAt"] },
{ size: 100 }
);
results.hits.map(hit => {
commit('ADD_MESSAGE', hit);
});
dispatch('SUBSCRIBE_MESSAGES', {kuzzle});
},
SUBSCRIBE_MESSAGES: async ({ commit }, { kuzzle }) => {
const roomID = await kuzzle.realtime.subscribe("chat", "messages", {}, notification => {
if (
notification.type === "document" &&
notification.action === "create"
) {
commit('ADD_MESSAGE', notification.result);
}
});
commit('SET_ROOMID', roomID);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You set the roomId in the store but you never use it. I think you should unsubscribe this roomId at some point (when the route change for example)

Copy link
Contributor Author

@berthieresteban berthieresteban Jul 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Effectively, I did this in the other vuejs gettings-started at line 103, and i explained why I set the roomId in comments because in this little app I don't think its better to weighing the router.
Should I change that in both vuejs getting-started ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just worried people would not understand why we do this in the getting started so I think it would be better, yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I'll do the changes :)

},
UNSUBSCRIBE_MESSAGES: async ({ state }, { kuzzle }) => {
await kuzzle.realtime.unsubscribe(state.roomID);
}
},
mutations: {
ADD_MESSAGE(state, hit) {
const message = {
_id: hit._id,
value: hit._source.value,
createdAt: hit._source._kuzzle_info.createdAt,
username: hit._source.username
};
state.messages = [message, ...state.messages]
},
SET_ROOMID(state, roomID) {
state.roomID = roomID;
},
SET_NICKNAME(state, nickname) {
state.nicnkame = nickname;
}
}
})
2 changes: 1 addition & 1 deletion doc/6/getting-started/vuejs/without-vuex/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ vue create kuzzle-playground
Install the kuzzle-sdk:
```bash
cd kuzzle-playground
yarn add kuzzle-sdk
npm install kuzzle-sdk
```

In the _App.vue_ file, you should remove the tag, the import and the component registration of the `HelloWorld` component, we won't use it.
Expand Down