Skip to content

Commit

Permalink
feat: introduce suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
louistiti committed Jun 19, 2022
1 parent 4b5a883 commit dcddacc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
32 changes: 27 additions & 5 deletions app/src/js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { io } from 'socket.io-client'
import Chatbot from './chatbot'

export default class Client {
constructor (client, serverUrl, input, suggestionsContainer, res) {
constructor (client, serverUrl, input, res) {
this.client = client
this._input = input
this._suggestionContainer = suggestionsContainer
this._suggestionContainer = document.querySelector('#suggestions-container')
this.serverUrl = serverUrl
this.socket = io(this.serverUrl)
this.history = localStorage.getItem('history')
this.parsedHistory = []
this.info = res
this.chatbot = new Chatbot()
this._recorder = { }
this._suggestions = []
}

set input (newInput) {
Expand Down Expand Up @@ -45,9 +46,8 @@ export default class Client {
})

this.socket.on('suggest', (data) => {
data.forEach((suggestion) => {
this._suggestionContainer.innerHTML
+= `<button class="suggestion">${suggestion}</button>`
data.forEach((suggestionText) => {
this.addSuggestion(suggestionText)
})
})

Expand Down Expand Up @@ -121,6 +121,12 @@ export default class Client {
this.socket.emit(keyword, { client: this.client, value: this._input.value.trim() })
this.chatbot.sendTo('leon', this._input.value)

this._suggestions.forEach((suggestion) => {
// Remove all event listeners of the suggestion
suggestion.replaceWith(suggestion.cloneNode(true))
this._suggestionContainer.replaceChildren()
})

this.save()

return true
Expand Down Expand Up @@ -150,4 +156,20 @@ export default class Client {

this._input.value = ''
}

addSuggestion (text) {
const newSuggestion = document.createElement('button')
newSuggestion.classList.add('suggestion')
newSuggestion.textContent = text

this._suggestionContainer.appendChild(newSuggestion)

newSuggestion.addEventListener('click', (e) => {
e.preventDefault()
this.input = e.target.textContent
this.send('utterance')
})

this._suggestions.push(newSuggestion)
}
}
3 changes: 1 addition & 2 deletions app/src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ document.addEventListener('DOMContentLoaded', () => {
const mic = document.querySelector('#mic-button')
const v = document.querySelector('#version small')
const logger = document.querySelector('#logger small')
const suggestionsContainer = document.querySelector('#suggestions-container')
const client = new Client(config.app, serverUrl, input, suggestionsContainer, res.body)
const client = new Client(config.app, serverUrl, input, res.body)
let rec = { }
let chunks = []
let sLogger = ' enabled, thank you.'
Expand Down

0 comments on commit dcddacc

Please sign in to comment.