From c4fd5e5ec510bd6224db4260f84836359eef3a8c Mon Sep 17 00:00:00 2001 From: Nero Ignis Date: Sat, 19 Sep 2020 18:20:10 +0200 Subject: [PATCH] Include alertify cdn on add-page; Refactor variables; Fix reactions from db; --- addReactions.html | 24 ++++++++++++ addReponse.js | 16 +++++++- app.js | 95 +++++++++++++++++------------------------------ 3 files changed, 73 insertions(+), 62 deletions(-) diff --git a/addReactions.html b/addReactions.html index 509a448..f299267 100644 --- a/addReactions.html +++ b/addReactions.html @@ -65,5 +65,29 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/addReponse.js b/addReponse.js index 60412c8..e924122 100644 --- a/addReponse.js +++ b/addReponse.js @@ -5,12 +5,22 @@ let responses = new Vue({ activeTheme: 'slate', addModal: { includeAll: false, - keywords: '', + keywords: 'keyword', + responses: [ + 'Answer #1' + ] + }, + defaultAddModal: { + includeAll: false, + keywords: 'keyword', responses: [ 'Answer #1' ] } }, + mounted() { + this.addModal = this.defaultAddModal; + }, methods: { addMessage(body, bot, me = false) { this.messages.push({ @@ -38,9 +48,11 @@ let responses = new Vue({ axios.post('/reactions/create', { reaction: newReaction }).then((response) => { - console.log(response); + alertify.notify('Reaction saved!', 'success'); + this.addModal = this.defaultAddModal; }).catch((error) => { console.log(error); + alertify.notify(error, 'danger'); }); }, cleanupMessage(message) { diff --git a/app.js b/app.js index d485796..ce1fe25 100644 --- a/app.js +++ b/app.js @@ -31,30 +31,7 @@ let kara = new Vue({ location: '' }, notes: [], - reactions: [ - { - includeAll: false, - keywords: [ - 'hi', 'hallo', 'servas', 'servus', 'hello' - ], - responses: [ - 'Hey! :)', - 'Hello!', - 'How are you?' - ] - }, - { - includeAll: true, - keywords: [ - 'how', 'are', 'you' - ], - responses: [ - 'I\'m good! Thanks for asking! :) How about you?', - 'A bit tired from trying to do that think called "thinking".. i\'ll figure it out one day.', - 'Wooo! I\'m hyped for party! You\'re in?' - ] - } - ] + reactions: [] }, mounted() { this.getSavedData(); @@ -207,25 +184,25 @@ let kara = new Vue({ getReaction(message) { message = this.cleanupMessage(message); - let phrases = message.split(' '); + let keywords = message.split(' '); let answer = undefined; - if (this.lastMessageData.joke && this.includesOneOf(phrases, ['another', 'more'])) { + if (this.lastMessageData.joke && this.includesOneOf(keywords, ['another', 'more'])) { this.tellJoke(this.lastMessageData.category); return false; } this.lastMessageData = {}; - if (this.includesAllOf(phrases, ['change', 'my', 'name'])) { + if (this.includesAllOf(keywords, ['change', 'my', 'name'])) { this.askedForName = true; return "Please tell me how i should call you."; } - if (this.includesAllOf(phrases, ['new', 'note']) || - this.includesAllOf(phrases, ['new', 'task']) || - this.includesAllOf(phrases, ['take', 'note']) || - this.includesAllOf(phrases, ['save', 'to', 'clipboard']) + if (this.includesAllOf(keywords, ['new', 'note']) || + this.includesAllOf(keywords, ['new', 'task']) || + this.includesAllOf(keywords, ['take', 'note']) || + this.includesAllOf(keywords, ['save', 'to', 'clipboard']) ) { this.takeNote = true; return this.oneOf([ @@ -235,56 +212,56 @@ let kara = new Vue({ ]); } - if (this.includesAllOf(phrases, ['clear', 'chat'])) { + if (this.includesAllOf(keywords, ['clear', 'chat'])) { this.clearChat(); return false; } - if (this.includesAllOf(phrases, ['weather']) && - this.includesOneOf(phrases, ['how', 'whats']) + if (this.includesAllOf(keywords, ['weather']) && + this.includesOneOf(keywords, ['how', 'whats']) ) { this.checkWeather(); return false; } - if (this.includesAllOf(phrases, ['knock', 'joke'])) { + if (this.includesAllOf(keywords, ['knock', 'joke'])) { this.tellJoke('knock-knock'); return false; } - if (this.includesAllOf(phrases, ['joke']) && this.includesOneOf(phrases, ['coding', 'programming', 'code', 'it'])) { + if (this.includesAllOf(keywords, ['joke']) && this.includesOneOf(keywords, ['coding', 'programming', 'code', 'it'])) { this.tellJoke('programming'); return false; } - if (this.includesAllOf(phrases, ['tell', 'joke']) || - this.includesAllOf(phrases, ['something', 'funny']) || - this.includesAllOf(phrases, ['cheer', 'me', 'up']) + if (this.includesAllOf(keywords, ['tell', 'joke']) || + this.includesAllOf(keywords, ['something', 'funny']) || + this.includesAllOf(keywords, ['cheer', 'me', 'up']) ) { this.tellJoke('general'); return false; } - if (this.includesAllOf(phrases, ['whats', 'the', 'time']) || this.includesAllOf(phrases, ['how', 'late'])) { + if (this.includesAllOf(keywords, ['whats', 'the', 'time']) || this.includesAllOf(keywords, ['how', 'late'])) { return "It's " + moment().format('LT'); } - if (this.includesAllOf(phrases, ['what', 'day', 'it'])) { + if (this.includesAllOf(keywords, ['what', 'day', 'it'])) { return "It's " + moment().format('dddd') + "."; } - if (this.includesAllOf(phrases, ['what', 'date', 'it']) || this.includesAllOf(phrases, ['whats', 'the', 'date'])) { + if (this.includesAllOf(keywords, ['what', 'date', 'it']) || this.includesAllOf(keywords, ['whats', 'the', 'date'])) { return "It's " + moment().format('dddd') + ", " + moment().format('MMMM Do YYYY') + "."; } - this.reactions.forEach((answerOption) => { - if (answerOption.includeAll === true) { - if (this.includesAllOf(phrases, answerOption.keywords)) { - answer = this.oneOf(answerOption.responses); + this.reactions.forEach((reactionOption) => { + if (reactionOption.includeAll === true) { + if (this.includesAllOf(keywords, reactionOption.keywords)) { + answer = this.oneOf(reactionOption.responses); } } else { - if (this.includesOneOf(phrases, answerOption.keywords)) { - answer = this.oneOf(answerOption.responses); + if (this.includesOneOf(keywords, reactionOption.keywords)) { + answer = this.oneOf(reactionOption.responses); } } }); @@ -389,7 +366,6 @@ let kara = new Vue({ axios.get(url) .then(function (response) { - console.log(response); let joke = response.data[0]; vue.botMessage(joke.setup); @@ -463,9 +439,6 @@ let kara = new Vue({ let savedMessages = JSON.parse(localStorage.getItem('messages')); this.messages = savedMessages ? savedMessages : []; - let savedAnswers = JSON.parse(localStorage.getItem('answers')); - this.reactions = savedAnswers !== [] ? savedAnswers : this.reactions; - let savedNotes = JSON.parse(localStorage.getItem('notes')); this.notes = savedNotes ? savedNotes : []; @@ -549,27 +522,27 @@ let kara = new Vue({ this.updateStorage(); }, - includesOneOf(phrases, wordsToSearch) { + includesOneOf(keywords, wordsToSearch) { let includes = false; wordsToSearch.forEach((searchWord) => { - if (phrases.includes(searchWord)) { + if (keywords.includes(searchWord)) { includes = true; } }) return includes; }, - includesAllOf(phrases, wordsToSearch) { - let includesAllPhrases = true; + includesAllOf(keywords, wordsToSearch) { + let includesAllkeywords = true; wordsToSearch.forEach((searchWord) => { - if (!phrases.includes(searchWord)) { - includesAllPhrases = false; + if (!keywords.includes(searchWord)) { + includesAllkeywords = false; } }) - return includesAllPhrases; + return includesAllkeywords; }, // Ajax calls for saving/receiving reactions & themes @@ -588,7 +561,9 @@ let kara = new Vue({ let vue = this; axios.get('/reactions/get').then((response) => { - this.reactions = response.data; + response.data.forEach((reaction) => { + vue.reactions.push(JSON.parse(reaction.reaction)); + }); }).catch((error) => { console.log(error); });