diff --git a/app/models/question.js b/app/models/question.js index 1d4b5ff..d01ba7a 100644 --- a/app/models/question.js +++ b/app/models/question.js @@ -5,5 +5,6 @@ export default DS.Model.extend({ description: DS.attr(), difficulty: DS.attr(), user: DS.belongsTo('user'), - choices: DS.hasMany('choice') + choices: DS.hasMany('choice'), + tags: DS.hasMany('tag') }) \ No newline at end of file diff --git a/app/models/tag.js b/app/models/tag.js new file mode 100644 index 0000000..86d6c72 --- /dev/null +++ b/app/models/tag.js @@ -0,0 +1,7 @@ +import DS from "ember-data"; + +export default DS.Model.extend({ + title: DS.attr(), + user: DS.belongsTo('user'), + questions: DS.hasMany('question') +}) \ No newline at end of file diff --git a/app/pods/components/question-editor/component.js b/app/pods/components/question-editor/component.js index 40e2ea5..7eb5505 100644 --- a/app/pods/components/question-editor/component.js +++ b/app/pods/components/question-editor/component.js @@ -1,11 +1,24 @@ import Component from '@ember/component'; import { inject as service } from '@ember/service'; +import { task, timeout } from 'ember-concurrency'; export default Component.extend({ + currentUser: service(), store: service(), api: service(), notify: service(), isEditing: false, + tagsFilterTask: task(function * (str) { + yield timeout(250) + const tags = yield this.get('store').query('tag', { + filter: { + title: { + $iLike: `%${str}%` + } + } + }) + return tags.toArray() + }), actions: { toggleEditing () { this.toggleProperty('isEditing') @@ -48,8 +61,18 @@ export default Component.extend({ }).then(() => { this.set('correctChoices', [...correctChoices]) }) - + }, + createNewTag() { + const onSuccess = () => this.get('notify').success('New Tag Created') + + let newTag = this.store.createRecord('tag', { + title: this.get('tagTitle'), + }) + newTag.set('user', this.get('currentUser.user')) + console.log(newTag.user) + newTag.save() + .then(onSuccess) } } }); diff --git a/app/pods/components/question-editor/template.hbs b/app/pods/components/question-editor/template.hbs index 965917a..48a05dc 100644 --- a/app/pods/components/question-editor/template.hbs +++ b/app/pods/components/question-editor/template.hbs @@ -50,6 +50,35 @@ {{/each}} +
+
+ +
+
+ {{#power-select-multiple + search=(perform tagsFilterTask) + selected=question.tags + placeholder="Add tags for this question" + onchange=(action (mut question.tags )) + as |tag|}} + {{tag.title}} + {{/power-select-multiple}} +
+
+
+
+
+ +
+
+ {{input type="text" class="input-text" placeholder="Enter Tag's Title" value=tagTitle}} +
+
+ + Save + +
+
Created By: {{question.user.firstname}} {{/unless}} diff --git a/app/pods/questions/id/route.js b/app/pods/questions/id/route.js index d802acd..50ccba0 100644 --- a/app/pods/questions/id/route.js +++ b/app/pods/questions/id/route.js @@ -7,7 +7,7 @@ export default Route.extend({ model (params) { return RSVP.hash({ question: this.store.findRecord('question', params.id, { - include: 'user,choices' + include: 'user,choices,tags' }), answers: this.api.request(`/questions/${params.id}/answers`) }) diff --git a/app/pods/questions/index/controller.js b/app/pods/questions/index/controller.js index fea1aee..da078ed 100644 --- a/app/pods/questions/index/controller.js +++ b/app/pods/questions/index/controller.js @@ -1,7 +1,9 @@ import Controller from '@ember/controller'; +import { inject as service } from '@ember/service'; import { task, timeout } from 'ember-concurrency'; export default Controller.extend({ + store: service(), queryParams: ['page', 'limit'], page: 1, limit: 10, @@ -10,18 +12,36 @@ export default Controller.extend({ yield timeout(250) let searchStr = this.get('searchString').trim() + let selectedTags = [] + + if(this.get('filterTags')) { + selectedTags = this.get('filterTags') + selectedTags = selectedTags.map(t => +t.id) + } const questions = yield this.get('store').query('question', { include: 'user', filter: { title: { $iLike: `%${this.get('searchString')}%` - } + }, + tags: selectedTags } }) this.set('page', 1) this.set('questions', questions) }).restartable(), + tagsFilterTask: task(function * (str) { + yield timeout(250) + const tags = yield this.get('store').query('tag', { + filter: { + title: { + $iLike: `%${str}%` + } + } + }) + return tags.toArray() + }), actions : { deleteQuestion(question) { question.destroyRecord() diff --git a/app/pods/questions/index/route.js b/app/pods/questions/index/route.js index 3166aa2..284a9d7 100644 --- a/app/pods/questions/index/route.js +++ b/app/pods/questions/index/route.js @@ -21,5 +21,6 @@ export default Route.extend({ }, setupController (controller, model) { controller.set("questions", model) + controller.set("tags", model.filterTags) } }); diff --git a/app/pods/questions/index/template.hbs b/app/pods/questions/index/template.hbs index beb1681..954b3ba 100644 --- a/app/pods/questions/index/template.hbs +++ b/app/pods/questions/index/template.hbs @@ -11,6 +11,18 @@ search +
+
+ {{#power-select-multiple + search=(perform tagsFilterTask) + selected=filterTags + onchange=(action (pipe (action (mut filterTags)) (perform searchTask))) + placeholder="Filter by tags" + as |tag|}} + {{tag.title}} + {{/power-select-multiple}} +
+
diff --git a/ember-cli-build.js b/ember-cli-build.js index 883317f..d4e0b19 100644 --- a/ember-cli-build.js +++ b/ember-cli-build.js @@ -4,6 +4,9 @@ const EmberApp = require('ember-cli/lib/broccoli/ember-app'); module.exports = function(defaults) { let app = new EmberApp(defaults, { + 'ember-composable-helpers': { + // Add conditions + } // Add options here }); diff --git a/package.json b/package.json index d8f9467..ee024c5 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "ember-cli-showdown": "^4.4.4", "ember-cli-sri": "^2.1.0", "ember-cli-uglify": "^2.0.0", + "ember-composable-helpers": "^2.3.1", "ember-data": "~3.3.0", "ember-datetime-picker": "^0.2.1", "ember-decorators": "^2.2.0",